From a4a01b0b8305c57887d75ec1fc7d9cf9d1217859 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 11 Nov 2024 14:39:30 +0800 Subject: [PATCH] Remove urlIsUnqiue method, update tests --- Server/.nycrc | 7 +---- Server/db/mongo/modules/statusPageModule.js | 25 ++++----------- Server/tests/db/statusPageModule.test.js | 35 ++++++--------------- 3 files changed, 17 insertions(+), 50 deletions(-) diff --git a/Server/.nycrc b/Server/.nycrc index 159e02a0d..96f20d795 100644 --- a/Server/.nycrc +++ b/Server/.nycrc @@ -1,11 +1,6 @@ { "all": true, - "include": [ - "controllers/*.js", - "utils/*.js", - "service/*.js", - "db/mongo/modules/monitorModule.js" - ], + "include": ["controllers/*.js", "utils/*.js", "service/*.js", "db/mongo/modules/*.js"], "exclude": ["**/*.test.js"], "reporter": ["html", "text", "lcov"], "sourceMap": false, diff --git a/Server/db/mongo/modules/statusPageModule.js b/Server/db/mongo/modules/statusPageModule.js index 2f30bcbeb..194287448 100644 --- a/Server/db/mongo/modules/statusPageModule.js +++ b/Server/db/mongo/modules/statusPageModule.js @@ -5,16 +5,15 @@ const SERVICE_NAME = "statusPageModule"; const createStatusPage = async (statusPageData) => { try { - const isUnique = await urlIsUnique(statusPageData.url); - if (!isUnique) { - const error = new Error(errorMessages.STATUS_PAGE_URL_NOT_UNIQUE); - error.status = 400; - throw error; - } const statusPage = new StatusPage({ ...statusPageData }); await statusPage.save(); return statusPage; } catch (error) { + if (error?.code === 11000) { + // Handle duplicate URL errors + error.status = 400; + error.message = errorMessages.STATUS_PAGE_URL_NOT_UNIQUE; + } error.service = SERVICE_NAME; error.method = "createStatusPage"; throw error; @@ -38,16 +37,4 @@ const getStatusPageByUrl = async (url) => { } }; -const urlIsUnique = async (url) => { - try { - const statusPage = await StatusPage.find({ url }); - if (statusPage.length > 0) return false; - return true; - } catch (error) { - error.service = SERVICE_NAME; - error.method = "urlIsUnique"; - throw error; - } -}; - -export { createStatusPage, getStatusPageByUrl, urlIsUnique }; +export { createStatusPage, getStatusPageByUrl }; diff --git a/Server/tests/db/statusPageModule.test.js b/Server/tests/db/statusPageModule.test.js index bf35e2e3e..8450ac5fc 100644 --- a/Server/tests/db/statusPageModule.test.js +++ b/Server/tests/db/statusPageModule.test.js @@ -2,7 +2,6 @@ import sinon from "sinon"; import { createStatusPage, getStatusPageByUrl, - urlIsUnique, } from "../../db/mongo/modules/statusPageModule.js"; import StatusPage from "../../db/models/StatusPage.js"; import { errorMessages } from "../../utils/messages.js"; @@ -20,7 +19,6 @@ describe("statusPageModule", () => { describe("createStatusPage", () => { it("should throw an error if a non-unique url is provided", async () => { statusPageFindOneStub.resolves(true); - statusPageFindStub.resolves([{}]); try { await createStatusPage({ url: "test" }); } catch (error) { @@ -28,6 +26,16 @@ describe("statusPageModule", () => { expect(error.message).to.equal(errorMessages.STATUS_PAGE_URL_NOT_UNIQUE); } }); + it("should handle duplicate URL errors", async () => { + const err = new Error("test"); + err.code = 11000; + statusPageSaveStub.rejects(err); + try { + await createStatusPage({ url: "test" }); + } catch (error) { + expect(error).to.deep.equal(err); + } + }); it("should return a status page if a unique url is provided", async () => { statusPageFindOneStub.resolves(null); statusPageFindStub.resolves([]); @@ -56,27 +64,4 @@ describe("statusPageModule", () => { expect(statusPage).to.deep.equal(mockStatusPage); }); }); - describe("urlIsUnique", () => { - it("should throw an error if an error occurs", async () => { - const err = new Error("test"); - statusPageFindStub.rejects(err); - try { - await urlIsUnique("test"); - } catch (error) { - expect(error).to.deep.equal(err); - } - }); - - it("should return true if a url is unique", async () => { - statusPageFindStub.resolves([]); - const isUnique = await urlIsUnique("test"); - expect(isUnique).to.be.true; - }); - - it("should return false if a url is not unique", async () => { - statusPageFindStub.resolves([{ url: "test" }]); - const isUnique = await urlIsUnique("test"); - expect(isUnique).to.be.false; - }); - }); });