fix(test): Use 'npm run lint-fix' to fix 'mocha/no-mocha-arrows' like lint warnings

- ref: https://mochajs.org/#arrow-functions
This commit is contained in:
Mert Şişmanoğlu committed 2024-12-29 14:25:59 +03:00
1 parent 319e6d6017
commit fcda60fe5b
31 files changed
+1268 -907

No files matched your search

+201 -112
View File
@@ -21,9 +21,10 @@ import logger from "../../utils/logger.js";
import axios from "axios";
const SERVICE_NAME = "monitorController";
describe("Monitor Controller - getAllMonitors", () => {
describe("Monitor Controller - getAllMonitors", function() {
let req, res, next;
beforeEach(() => {
beforeEach(function() {
req = {
params: {},
query: {},
@@ -38,17 +39,19 @@ describe("Monitor Controller - getAllMonitors", () => {
};
next = sinon.stub();
});
afterEach(() => {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if DB operations fail", async () => {
it("should reject with an error if DB operations fail", async function() {
req.db.getAllMonitors.throws(new Error("DB error"));
await getAllMonitors(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should return success message and data if all operations succeed", async () => {
it("should return success message and data if all operations succeed", async function() {
const data = [{ monitor: "data" }];
req.db.getAllMonitors.returns(data);
await getAllMonitors(req, res, next);
@@ -62,9 +65,10 @@ describe("Monitor Controller - getAllMonitors", () => {
).to.be.true;
});
});
describe("Monitor Controller - getAllMonitorsWithUptimeStats", () => {
describe("Monitor Controller - getAllMonitorsWithUptimeStats", function() {
let req, res, next;
beforeEach(() => {
beforeEach(function() {
req = {
params: {},
query: {},
@@ -79,17 +83,19 @@ describe("Monitor Controller - getAllMonitorsWithUptimeStats", () => {
};
next = sinon.stub();
});
afterEach(() => {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if DB operations fail", async () => {
it("should reject with an error if DB operations fail", async function() {
req.db.getAllMonitorsWithUptimeStats.throws(new Error("DB error"));
await getAllMonitorsWithUptimeStats(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should return success message and data if all operations succeed", async () => {
it("should return success message and data if all operations succeed", async function() {
const data = [{ monitor: "data" }];
req.db.getAllMonitorsWithUptimeStats.returns(data);
await getAllMonitorsWithUptimeStats(req, res, next);
@@ -104,9 +110,10 @@ describe("Monitor Controller - getAllMonitorsWithUptimeStats", () => {
});
});
describe("Monitor Controller - getMonitorStatsById", () => {
describe("Monitor Controller - getMonitorStatsById", function() {
let req, res, next;
beforeEach(() => {
beforeEach(function() {
req = {
params: {
monitorId: "123",
@@ -123,29 +130,33 @@ describe("Monitor Controller - getMonitorStatsById", () => {
};
next = sinon.stub();
});
afterEach(() => {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if param validation fails", async () => {
it("should reject with an error if param validation fails", async function() {
req.params = {};
await getMonitorStatsById(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject with an error if query validation fails", async () => {
it("should reject with an error if query validation fails", async function() {
req.query = { invalid: 1 };
await getMonitorStatsById(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject with an error if DB operations fail", async () => {
it("should reject with an error if DB operations fail", async function() {
req.db.getMonitorStatsById.throws(new Error("DB error"));
await getMonitorStatsById(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should return success message and data if all operations succeed", async () => {
it("should return success message and data if all operations succeed", async function() {
const data = [{ monitorStats: "data" }];
req.db.getMonitorStatsById.returns(data);
await getMonitorStatsById(req, res, next);
@@ -160,9 +171,10 @@ describe("Monitor Controller - getMonitorStatsById", () => {
});
});
describe("Monitor Controller - getMonitorCertificate", () => {
describe("Monitor Controller - getMonitorCertificate", function() {
let req, res, next, fetchMonitorCertificate;
beforeEach(() => {
beforeEach(function() {
req = {
params: {
monitorId: "123",
@@ -181,23 +193,25 @@ describe("Monitor Controller - getMonitorCertificate", () => {
fetchMonitorCertificate = sinon.stub();
});
afterEach(() => {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if param validation fails", async () => {
it("should reject with an error if param validation fails", async function() {
req.params = {};
await getMonitorCertificate(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject with an error if getMonitorById operation fails", async () => {
it("should reject with an error if getMonitorById operation fails", async function() {
req.db.getMonitorById.throws(new Error("DB error"));
await getMonitorCertificate(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should return success message and data if all operations succeed with a valid cert", async () => {
it("should return success message and data if all operations succeed with a valid cert", async function() {
req.db.getMonitorById.returns({ url: "https://www.google.com" });
const data = { certificate: "cert", validTo: "2024/08/08" };
fetchMonitorCertificate.returns(data);
@@ -211,7 +225,8 @@ describe("Monitor Controller - getMonitorCertificate", () => {
})
).to.be.true;
});
it("should return an error if fetchMonitorCertificate fails", async () => {
it("should return an error if fetchMonitorCertificate fails", async function() {
req.db.getMonitorById.returns({ url: "https://www.google.com" });
fetchMonitorCertificate.throws(new Error("Certificate error"));
await getMonitorCertificate(req, res, next, fetchMonitorCertificate);
@@ -220,9 +235,10 @@ describe("Monitor Controller - getMonitorCertificate", () => {
});
});
describe("Monitor Controller - getMonitorById", () => {
describe("Monitor Controller - getMonitorById", function() {
let req, res, next;
beforeEach(() => {
beforeEach(function() {
req = {
params: {
monitorId: "123",
@@ -239,28 +255,33 @@ describe("Monitor Controller - getMonitorById", () => {
};
next = sinon.stub();
});
afterEach(() => {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if param validation fails", async () => {
it("should reject with an error if param validation fails", async function() {
req.params = {};
await getMonitorById(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject with an error if query param validation fails", async () => {
it("should reject with an error if query param validation fails", async function() {
req.query = { invalid: 1 };
await getMonitorById(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject with an error if DB operations fail", async () => {
it("should reject with an error if DB operations fail", async function() {
req.db.getMonitorById.throws(new Error("DB error"));
await getMonitorById(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should return 404 if a monitor is not found", async () => {
it("should return 404 if a monitor is not found", async function() {
const error = new Error("Monitor not found");
error.status = 404;
req.db.getMonitorById.throws(error);
@@ -268,7 +289,8 @@ describe("Monitor Controller - getMonitorById", () => {
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(404);
});
it("should return success message and data if all operations succeed", async () => {
it("should return success message and data if all operations succeed", async function() {
const data = { monitor: "data" };
req.db.getMonitorById.returns(data);
await getMonitorById(req, res, next);
@@ -283,9 +305,10 @@ describe("Monitor Controller - getMonitorById", () => {
});
});
describe("Monitor Controller - getMonitorsAndSummaryByTeamId", () => {
describe("Monitor Controller - getMonitorsAndSummaryByTeamId", function() {
let req, res, next;
beforeEach(() => {
beforeEach(function() {
req = {
params: {
teamId: "123",
@@ -302,29 +325,33 @@ describe("Monitor Controller - getMonitorsAndSummaryByTeamId", () => {
};
next = sinon.stub();
});
afterEach(() => {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if param validation fails", async () => {
it("should reject with an error if param validation fails", async function() {
req.params = {};
await getMonitorsAndSummaryByTeamId(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject with an error if query validation fails", async () => {
it("should reject with an error if query validation fails", async function() {
req.query = { invalid: 1 };
await getMonitorsAndSummaryByTeamId(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject with an error if DB operations fail", async () => {
it("should reject with an error if DB operations fail", async function() {
req.db.getMonitorsAndSummaryByTeamId.throws(new Error("DB error"));
await getMonitorsAndSummaryByTeamId(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should return success message and data if all operations succeed", async () => {
it("should return success message and data if all operations succeed", async function() {
const data = { monitors: "data", summary: "data" };
req.db.getMonitorsAndSummaryByTeamId.returns(data);
await getMonitorsAndSummaryByTeamId(req, res, next);
@@ -339,9 +366,10 @@ describe("Monitor Controller - getMonitorsAndSummaryByTeamId", () => {
});
});
describe("Monitor Controller - getMonitorsByTeamId", () => {
describe("Monitor Controller - getMonitorsByTeamId", function() {
let req, res, next;
beforeEach(() => {
beforeEach(function() {
req = {
params: {
teamId: "123",
@@ -358,28 +386,33 @@ describe("Monitor Controller - getMonitorsByTeamId", () => {
};
next = sinon.stub();
});
afterEach(() => {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if param validation fails", async () => {
it("should reject with an error if param validation fails", async function() {
req.params = {};
await getMonitorsByTeamId(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject with an error if query validation fails", async () => {
it("should reject with an error if query validation fails", async function() {
req.query = { invalid: 1 };
await getMonitorsByTeamId(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject with an error if DB operations fail", async () => {
it("should reject with an error if DB operations fail", async function() {
req.db.getMonitorsByTeamId.throws(new Error("DB error"));
await getMonitorsByTeamId(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should return success message and data if all operations succeed", async () => {
it("should return success message and data if all operations succeed", async function() {
const data = { monitors: "data" };
req.db.getMonitorsByTeamId.returns(data);
await getMonitorsByTeamId(req, res, next);
@@ -394,9 +427,10 @@ describe("Monitor Controller - getMonitorsByTeamId", () => {
});
});
describe("Monitor Controller - createMonitor", () => {
describe("Monitor Controller - createMonitor", function() {
let req, res, next;
beforeEach(() => {
beforeEach(function() {
req = {
params: {},
query: {},
@@ -423,29 +457,34 @@ describe("Monitor Controller - createMonitor", () => {
};
next = sinon.stub();
});
afterEach(() => {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if body validation fails", async () => {
it("should reject with an error if body validation fails", async function() {
req.body = {};
await createMonitor(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject with an error if DB createMonitor operation fail", async () => {
it("should reject with an error if DB createMonitor operation fail", async function() {
req.db.createMonitor.throws(new Error("DB error"));
await createMonitor(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should reject with an error if DB createNotification operation fail", async () => {
it("should reject with an error if DB createNotification operation fail", async function() {
req.db.createNotification.throws(new Error("DB error"));
req.db.createMonitor.returns({ _id: "123" });
await createMonitor(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should reject with an error if monitor.save operation fail", async () => {
it("should reject with an error if monitor.save operation fail", async function() {
req.db.createMonitor.returns({
_id: "123",
save: sinon.stub().throws(new Error("Monitor save error")),
@@ -454,14 +493,16 @@ describe("Monitor Controller - createMonitor", () => {
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("Monitor save error");
});
it("should throw an error if addJob operation fails", async () => {
it("should throw an error if addJob operation fails", async function() {
req.db.createMonitor.returns({ _id: "123", save: sinon.stub() });
req.jobQueue.addJob.throws(new Error("Job error"));
await createMonitor(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("Job error");
});
it("should return success message and data if all operations succeed", async () => {
it("should return success message and data if all operations succeed", async function() {
const monitor = { _id: "123", save: sinon.stub() };
req.db.createMonitor.returns(monitor);
await createMonitor(req, res, next);
@@ -476,18 +517,21 @@ describe("Monitor Controller - createMonitor", () => {
});
});
describe("Monitor Controller - checkEndpointResolution", () => {
describe("Monitor Controller - checkEndpointResolution", function() {
let req, res, next, axiosGetStub;
beforeEach(() => {
beforeEach(function() {
req = { query: { monitorURL: "https://example.com" } };
res = { status: sinon.stub().returnsThis(), json: sinon.stub() };
next = sinon.stub();
axiosGetStub = sinon.stub(axios, "get");
});
afterEach(() => {
afterEach(function() {
sinon.restore();
});
it("should resolve the URL successfully", async () => {
it("should resolve the URL successfully", async function() {
axiosGetStub.resolves({ status: 200, statusText: "OK" });
await checkEndpointResolution(req, res, next);
expect(res.status.calledWith(200)).to.be.true;
@@ -501,7 +545,8 @@ describe("Monitor Controller - checkEndpointResolution", () => {
).to.be.true;
expect(next.called).to.be.false;
});
it("should return an error if endpoint resolution fails", async () => {
it("should return an error if endpoint resolution fails", async function() {
const axiosError = new Error("resolution failed");
axiosError.code = "ENOTFOUND";
axiosGetStub.rejects(axiosError);
@@ -513,7 +558,8 @@ describe("Monitor Controller - checkEndpointResolution", () => {
expect(errorPassedToNext.code).to.equal("ENOTFOUND");
expect(errorPassedToNext.status).to.equal(500);
});
it("should reject with an error if query validation fails", async () => {
it("should reject with an error if query validation fails", async function() {
req.query.monitorURL = "invalid-url";
await checkEndpointResolution(req, res, next);
expect(next.calledOnce).to.be.true;
@@ -524,9 +570,10 @@ describe("Monitor Controller - checkEndpointResolution", () => {
});
});
describe("Monitor Controller - deleteMonitor", () => {
describe("Monitor Controller - deleteMonitor", function() {
let req, res, next;
beforeEach(() => {
beforeEach(function() {
req = {
params: {
monitorId: "123",
@@ -550,22 +597,26 @@ describe("Monitor Controller - deleteMonitor", () => {
next = sinon.stub();
sinon.stub(logger, "error");
});
afterEach(() => {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if param validation fails", async () => {
it("should reject with an error if param validation fails", async function() {
req.params = {};
await deleteMonitor(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject with an error if DB deleteMonitor operation fail", async () => {
it("should reject with an error if DB deleteMonitor operation fail", async function() {
req.db.deleteMonitor.throws(new Error("DB error"));
await deleteMonitor(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should log an error if deleteJob throws an error", async () => {
it("should log an error if deleteJob throws an error", async function() {
const error = new Error("Job error");
const monitor = { name: "test_monitor", _id: "123" };
req.db.deleteMonitor.returns(monitor);
@@ -576,7 +627,8 @@ describe("Monitor Controller - deleteMonitor", () => {
`Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`
);
});
it("should log an error if deleteChecks throws an error", async () => {
it("should log an error if deleteChecks throws an error", async function() {
const error = new Error("Checks error");
const monitor = { name: "test_monitor", _id: "123" };
req.db.deleteMonitor.returns(monitor);
@@ -587,7 +639,8 @@ describe("Monitor Controller - deleteMonitor", () => {
`Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`
);
});
it("should log an error if deletePageSpeedChecksByMonitorId throws an error", async () => {
it("should log an error if deletePageSpeedChecksByMonitorId throws an error", async function() {
const error = new Error("PageSpeed error");
const monitor = { name: "test_monitor", _id: "123" };
req.db.deleteMonitor.returns(monitor);
@@ -598,7 +651,8 @@ describe("Monitor Controller - deleteMonitor", () => {
`Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`
);
});
it("should log an error if deleteNotificationsByMonitorId throws an error", async () => {
it("should log an error if deleteNotificationsByMonitorId throws an error", async function() {
const error = new Error("Notifications error");
const monitor = { name: "test_monitor", _id: "123" };
req.db.deleteMonitor.returns(monitor);
@@ -609,7 +663,8 @@ describe("Monitor Controller - deleteMonitor", () => {
`Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`
);
});
it("should return success message if all operations succeed", async () => {
it("should return success message if all operations succeed", async function() {
const monitor = { name: "test_monitor", _id: "123" };
req.db.deleteMonitor.returns(monitor);
await deleteMonitor(req, res, next);
@@ -623,9 +678,10 @@ describe("Monitor Controller - deleteMonitor", () => {
});
});
describe("Monitor Controller - deleteAllMonitors", () => {
describe("Monitor Controller - deleteAllMonitors", function() {
let req, res, next, stub;
beforeEach(() => {
beforeEach(function() {
stub = sinon.stub(jwt, "verify").callsFake(() => {
return { teamId: "123" };
});
@@ -658,24 +714,28 @@ describe("Monitor Controller - deleteAllMonitors", () => {
next = sinon.stub();
sinon.stub(logger, "error");
});
afterEach(() => {
afterEach(function() {
sinon.restore();
stub.restore();
});
it("should reject with an error if getTokenFromHeaders throws an error", async () => {
it("should reject with an error if getTokenFromHeaders throws an error", async function() {
req.headers = {};
await deleteAllMonitors(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("No auth headers");
expect(next.firstCall.args[0].status).to.equal(500);
});
it("should reject with an error if token validation fails", async () => {
it("should reject with an error if token validation fails", async function() {
stub.restore();
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
await deleteAllMonitors(req, res, next);
expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError);
});
it("should reject with an error if DB deleteAllMonitors operation fail", async () => {
it("should reject with an error if DB deleteAllMonitors operation fail", async function() {
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
req.db.deleteAllMonitors.throws(new Error("DB error"));
await deleteAllMonitors(req, res, next);
@@ -683,7 +743,7 @@ describe("Monitor Controller - deleteAllMonitors", () => {
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should log an error if deleteChecks throws an error", async () => {
it("should log an error if deleteChecks throws an error", async function() {
const monitors = [{ name: "test_monitor", _id: "123" }];
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
req.db.deleteAllMonitors.returns({ monitors, deletedCount: 1 });
@@ -695,7 +755,8 @@ describe("Monitor Controller - deleteAllMonitors", () => {
`Error deleting associated records for monitor ${monitors[0]._id} with name ${monitors[0].name}`
);
});
it("should log an error if deletePageSpeedChecksByMonitorId throws an error", async () => {
it("should log an error if deletePageSpeedChecksByMonitorId throws an error", async function() {
const monitors = [{ name: "test_monitor", _id: "123" }];
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
req.db.deleteAllMonitors.returns({ monitors, deletedCount: 1 });
@@ -707,7 +768,8 @@ describe("Monitor Controller - deleteAllMonitors", () => {
`Error deleting associated records for monitor ${monitors[0]._id} with name ${monitors[0].name}`
);
});
it("should log an error if deleteNotificationsByMonitorId throws an error", async () => {
it("should log an error if deleteNotificationsByMonitorId throws an error", async function() {
const monitors = [{ name: "test_monitor", _id: "123" }];
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
req.db.deleteAllMonitors.returns({ monitors, deletedCount: 1 });
@@ -719,7 +781,8 @@ describe("Monitor Controller - deleteAllMonitors", () => {
`Error deleting associated records for monitor ${monitors[0]._id} with name ${monitors[0].name}`
);
});
it("should return success message if all operations succeed", async () => {
it("should return success message if all operations succeed", async function() {
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
req.db.deleteAllMonitors.returns({
monitors: [{ name: "test_monitor", _id: "123" }],
@@ -736,9 +799,10 @@ describe("Monitor Controller - deleteAllMonitors", () => {
});
});
describe("Monitor Controller - editMonitor", () => {
describe("Monitor Controller - editMonitor", function() {
let req, res, next;
beforeEach(() => {
beforeEach(function() {
req = {
headers: {},
params: {
@@ -768,35 +832,41 @@ describe("Monitor Controller - editMonitor", () => {
};
next = sinon.stub();
});
afterEach(() => {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if param validation fails", async () => {
it("should reject with an error if param validation fails", async function() {
req.params = {};
await editMonitor(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject with an error if body validation fails", async () => {
it("should reject with an error if body validation fails", async function() {
req.body = { invalid: 1 };
await editMonitor(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject with an error if getMonitorById operation fails", async () => {
it("should reject with an error if getMonitorById operation fails", async function() {
req.db.getMonitorById.throws(new Error("DB error"));
await editMonitor(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should reject with an error if editMonitor operation fails", async () => {
it("should reject with an error if editMonitor operation fails", async function() {
req.db.getMonitorById.returns({ teamId: "123" });
req.db.editMonitor.throws(new Error("DB error"));
await editMonitor(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should reject with an error if deleteNotificationsByMonitorId operation fails", async () => {
it("should reject with an error if deleteNotificationsByMonitorId operation fails", async function() {
req.db.getMonitorById.returns({ teamId: "123" });
req.db.editMonitor.returns({ _id: "123" });
req.db.deleteNotificationsByMonitorId.throws(new Error("DB error"));
@@ -804,7 +874,8 @@ describe("Monitor Controller - editMonitor", () => {
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should reject with an error if createNotification operation fails", async () => {
it("should reject with an error if createNotification operation fails", async function() {
req.db.getMonitorById.returns({ teamId: "123" });
req.db.editMonitor.returns({ _id: "123" });
req.db.createNotification.throws(new Error("DB error"));
@@ -812,7 +883,8 @@ describe("Monitor Controller - editMonitor", () => {
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should reject with an error if deleteJob operation fails", async () => {
it("should reject with an error if deleteJob operation fails", async function() {
req.db.getMonitorById.returns({ teamId: "123" });
req.db.editMonitor.returns({ _id: "123" });
req.jobQueue.deleteJob.throws(new Error("Job error"));
@@ -820,7 +892,8 @@ describe("Monitor Controller - editMonitor", () => {
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("Job error");
});
it("should reject with an error if addJob operation fails", async () => {
it("should reject with an error if addJob operation fails", async function() {
req.db.getMonitorById.returns({ teamId: "123" });
req.db.editMonitor.returns({ _id: "123" });
req.jobQueue.addJob.throws(new Error("Add Job error"));
@@ -828,7 +901,8 @@ describe("Monitor Controller - editMonitor", () => {
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("Add Job error");
});
it("should return success message with data if all operations succeed", async () => {
it("should return success message with data if all operations succeed", async function() {
const monitor = { _id: "123" };
req.db.getMonitorById.returns({ teamId: "123" });
req.db.editMonitor.returns(monitor);
@@ -844,9 +918,10 @@ describe("Monitor Controller - editMonitor", () => {
});
});
describe("Monitor Controller - pauseMonitor", () => {
describe("Monitor Controller - pauseMonitor", function() {
let req, res, next;
beforeEach(() => {
beforeEach(function() {
req = {
headers: {},
params: {
@@ -871,23 +946,26 @@ describe("Monitor Controller - pauseMonitor", () => {
};
next = sinon.stub();
});
afterEach(() => {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if param validation fails", async () => {
it("should reject with an error if param validation fails", async function() {
req.params = {};
await pauseMonitor(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject with an error if getMonitorById operation fails", async () => {
it("should reject with an error if getMonitorById operation fails", async function() {
req.db.getMonitorById.throws(new Error("DB error"));
await pauseMonitor(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should reject with an error if deleteJob operation fails", async () => {
it("should reject with an error if deleteJob operation fails", async function() {
const monitor = { _id: req.params.monitorId, isActive: true };
req.db.getMonitorById.returns(monitor);
req.jobQueue.deleteJob.throws(new Error("Delete Job error"));
@@ -896,7 +974,7 @@ describe("Monitor Controller - pauseMonitor", () => {
expect(next.firstCall.args[0].message).to.equal("Delete Job error");
});
it("should reject with an error if addJob operation fails", async () => {
it("should reject with an error if addJob operation fails", async function() {
const monitor = { _id: req.params.monitorId, isActive: false };
req.db.getMonitorById.returns(monitor);
req.jobQueue.addJob.throws(new Error("Add Job error"));
@@ -904,7 +982,8 @@ describe("Monitor Controller - pauseMonitor", () => {
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("Add Job error");
});
it("should reject with an error if monitor.save operation fails", async () => {
it("should reject with an error if monitor.save operation fails", async function() {
const monitor = {
_id: req.params.monitorId,
active: false,
@@ -915,7 +994,8 @@ describe("Monitor Controller - pauseMonitor", () => {
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("Save error");
});
it("should return success pause message with data if all operations succeed with inactive monitor", async () => {
it("should return success pause message with data if all operations succeed with inactive monitor", async function() {
const monitor = {
_id: req.params.monitorId,
isActive: false,
@@ -932,7 +1012,8 @@ describe("Monitor Controller - pauseMonitor", () => {
})
).to.be.true;
});
it("should return success resume message with data if all operations succeed with active monitor", async () => {
it("should return success resume message with data if all operations succeed with active monitor", async function() {
const monitor = {
_id: req.params.monitorId,
isActive: true,
@@ -951,9 +1032,10 @@ describe("Monitor Controller - pauseMonitor", () => {
});
});
describe("Monitor Controller - addDemoMonitors", () => {
describe("Monitor Controller - addDemoMonitors", function() {
let req, res, next, stub;
beforeEach(() => {
beforeEach(function() {
stub = sinon.stub(jwt, "verify").callsFake(() => {
return { _id: "123", teamId: "123" };
});
@@ -980,37 +1062,43 @@ describe("Monitor Controller - addDemoMonitors", () => {
};
next = sinon.stub();
});
afterEach(() => {
afterEach(function() {
sinon.restore();
stub.restore();
});
it("should reject with an error if getTokenFromHeaders fails", async () => {
it("should reject with an error if getTokenFromHeaders fails", async function() {
req.headers = {};
await addDemoMonitors(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("No auth headers");
expect(next.firstCall.args[0].status).to.equal(500);
});
it("should reject with an error if getting settings fails", async () => {
it("should reject with an error if getting settings fails", async function() {
req.settingsService.getSettings.throws(new Error("Settings error"));
await addDemoMonitors(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("Settings error");
});
it("should reject with an error if JWT validation fails", async () => {
it("should reject with an error if JWT validation fails", async function() {
stub.restore();
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
await addDemoMonitors(req, res, next);
expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError);
});
it("should reject with an error if addDemoMonitors operation fails", async () => {
it("should reject with an error if addDemoMonitors operation fails", async function() {
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
req.db.addDemoMonitors.throws(new Error("DB error"));
await addDemoMonitors(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should reject with an error if addJob operation fails", async () => {
it("should reject with an error if addJob operation fails", async function() {
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
req.db.addDemoMonitors.returns([{ _id: "123" }]);
req.jobQueue.addJob.throws(new Error("Add Job error"));
@@ -1018,7 +1106,8 @@ describe("Monitor Controller - addDemoMonitors", () => {
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("Add Job error");
});
it("should return success message with data if all operations succeed", async () => {
it("should return success message with data if all operations succeed", async function() {
const monitors = [{ _id: "123" }];
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
req.db.addDemoMonitors.returns(monitors);