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,
|
"all": true,
|
||||||
"include": [
|
"include": ["controllers/*.js", "utils/*.js", "service/*.js", "db/mongo/modules/*.js"],
|
||||||
"controllers/*.js",
|
|
||||||
"utils/*.js",
|
|
||||||
"service/*.js",
|
|
||||||
"db/mongo/modules/monitorModule.js"
|
|
||||||
],
|
|
||||||
"exclude": ["**/*.test.js"],
|
"exclude": ["**/*.test.js"],
|
||||||
"reporter": ["html", "text", "lcov"],
|
"reporter": ["html", "text", "lcov"],
|
||||||
"sourceMap": false,
|
"sourceMap": false,
|
||||||
|
|||||||
@@ -5,16 +5,15 @@ const SERVICE_NAME = "statusPageModule";
|
|||||||
|
|
||||||
const createStatusPage = async (statusPageData) => {
|
const createStatusPage = async (statusPageData) => {
|
||||||
try {
|
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 });
|
const statusPage = new StatusPage({ ...statusPageData });
|
||||||
await statusPage.save();
|
await statusPage.save();
|
||||||
return statusPage;
|
return statusPage;
|
||||||
} catch (error) {
|
} 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.service = SERVICE_NAME;
|
||||||
error.method = "createStatusPage";
|
error.method = "createStatusPage";
|
||||||
throw error;
|
throw error;
|
||||||
@@ -38,16 +37,4 @@ const getStatusPageByUrl = async (url) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const urlIsUnique = async (url) => {
|
export { createStatusPage, getStatusPageByUrl };
|
||||||
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 };
|
|
||||||
@@ -2,7 +2,6 @@ import sinon from "sinon";
|
|||||||
import {
|
import {
|
||||||
createStatusPage,
|
createStatusPage,
|
||||||
getStatusPageByUrl,
|
getStatusPageByUrl,
|
||||||
urlIsUnique,
|
|
||||||
} from "../../db/mongo/modules/statusPageModule.js";
|
} from "../../db/mongo/modules/statusPageModule.js";
|
||||||
import StatusPage from "../../db/models/StatusPage.js";
|
import StatusPage from "../../db/models/StatusPage.js";
|
||||||
import { errorMessages } from "../../utils/messages.js";
|
import { errorMessages } from "../../utils/messages.js";
|
||||||
@@ -20,7 +19,6 @@ describe("statusPageModule", () => {
|
|||||||
describe("createStatusPage", () => {
|
describe("createStatusPage", () => {
|
||||||
it("should throw an error if a non-unique url is provided", async () => {
|
it("should throw an error if a non-unique url is provided", async () => {
|
||||||
statusPageFindOneStub.resolves(true);
|
statusPageFindOneStub.resolves(true);
|
||||||
statusPageFindStub.resolves([{}]);
|
|
||||||
try {
|
try {
|
||||||
await createStatusPage({ url: "test" });
|
await createStatusPage({ url: "test" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -28,6 +26,16 @@ describe("statusPageModule", () => {
|
|||||||
expect(error.message).to.equal(errorMessages.STATUS_PAGE_URL_NOT_UNIQUE);
|
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 () => {
|
it("should return a status page if a unique url is provided", async () => {
|
||||||
statusPageFindOneStub.resolves(null);
|
statusPageFindOneStub.resolves(null);
|
||||||
statusPageFindStub.resolves([]);
|
statusPageFindStub.resolves([]);
|
||||||
@@ -56,27 +64,4 @@ describe("statusPageModule", () => {
|
|||||||
expect(statusPage).to.deep.equal(mockStatusPage);
|
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