Remove urlIsUnqiue method, update tests
This commit is contained in:
1 parent
5817698105
commit
a4a01b0b83
3 files changed
+17
-50
No files matched your search
+1
-6
@@ -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,
|
||||
|
||||
@@ -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 };
|
||||
@@ -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;
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user