diff --git a/api/src/services/pub-code-service.js b/api/src/services/pub-code-service.js index a7301bea..a0bc17c4 100644 --- a/api/src/services/pub-code-service.js +++ b/api/src/services/pub-code-service.js @@ -40,26 +40,12 @@ async function insertOrUpdate(payload, notInsertedArray) { } } -async function sendEmailForError(error) { - if (EMAIL_RECIPIENTS) { - try { - const email = emailService.generateHtmlEmail("Error During Bulk Load of Pub Codes Saving to Database Failed", EMAIL_RECIPIENTS, "Error During Bulk Load of Pub Codes Saving to Database Failed.", `

${error.message}

`); - await emailService.send(email); - } catch (e) { - logger.error("bulkLoad: ", e); - } - } -} - const bulkLoad = async (req, res) => { try { const notInsertedArray = []; const payload = req.body; if (payload && Array.isArray(payload) && payload.length > 0) { - insertOrUpdate(payload, notInsertedArray).catch(async (error) => { - await sendEmailForError(error); - logger.error("bulkLoad: ", error); - }); + await insertOrUpdate(payload, notInsertedArray); res.status(200).json({}); } else { res.status(400).json({ message: "Invalid request body" }); diff --git a/api/test/bulk-load.test.js b/api/test/bulk-load.test.js new file mode 100644 index 00000000..1fd943bb --- /dev/null +++ b/api/test/bulk-load.test.js @@ -0,0 +1,69 @@ +"use strict"; +const { test } = require("node:test"); +const assert = require("node:assert/strict"); + +process.env.LOG_LEVEL = "error"; +// The email service refuses to load without configuration; nothing is sent while EMAIL_RECIPIENTS is unset. +process.env.CHES_TOKEN_URL = "http://localhost/token"; +process.env.CHES_CLIENT_ID = "test"; +process.env.CHES_CLIENT_SECRET = "test"; +process.env.CHES_API_URL = "http://localhost/ches"; +delete process.env.EMAIL_RECIPIENTS; + +const pubcodeEntity = require("../src/entities/pub-code-entity"); +const { bulkLoad } = require("../src/services/pub-code-service"); + +function fakeResponse() { + const res = { statusCode: undefined, body: undefined }; + res.status = (code) => { + res.statusCode = code; + return res; + }; + res.json = (body) => { + res.body = body; + return res; + }; + return res; +} + +const validPubcode = { + repo_name: "some-repo", + version: 1, + product_information: { product_name: "Some product" }, + data_management_roles: { product_owner: "Owner" }, + product_technology_information: { backend_language: ["JavaScript"] }, +}; + +test("bulk load answers 500 when saving fails", async (t) => { + t.mock.method(pubcodeEntity, "findOneAndReplace", () => ({ + exec: () => Promise.reject(new Error("database unavailable")), + })); + const res = fakeResponse(); + + await bulkLoad({ body: [validPubcode] }, res); + + assert.equal(res.statusCode, 500); + assert.deepEqual(res.body, { message: "Internal server error" }); +}); + +test("bulk load answers 200 only after saving", async (t) => { + let saved = false; + t.mock.method(pubcodeEntity, "findOneAndReplace", () => ({ + exec: async () => { + await new Promise((resolve) => setTimeout(resolve, 20)); + saved = true; + return { _id: "existing" }; + }, + })); + const res = fakeResponse(); + let savedWhenAnswered; + res.json = () => { + savedWhenAnswered = saved; + return res; + }; + + await bulkLoad({ body: [validPubcode] }, res); + + assert.equal(res.statusCode, 200); + assert.equal(savedWhenAnswered, true); +});