Migrate tests to ESM

This commit is contained in:
Alex Holliday
2024-10-15 14:00:24 +08:00
parent f057807b56
commit 896b3437e5
22 changed files with 1153 additions and 238 deletions
+8
View File
@@ -0,0 +1,8 @@
module.exports = {
require: ["esm", "chai/register-expect.js"], // Include Chai's "expect" interface globally
spec: "tests/**/*.test.js", // Specify test files
timeout: 5000, // Set test-case timeout in milliseconds
recursive: true, // Include subdirectories
reporter: "spec", // Use the "spec" reporter
exit: true, // Force Mocha to quit after tests complete
};
+2 -2
View File
@@ -14,7 +14,7 @@ import jwt from "jsonwebtoken";
import { getTokenFromHeaders } from "../utils/utils.js";
import crypto from "crypto";
import { handleValidationError, handleError } from "./controllerUtils.js";
const SERVICE_NAME = "authController";
dotenv.config();
/**
@@ -430,7 +430,7 @@ const deleteUser = async (req, res, next) => {
}
};
const getAllUsers = async (req, res) => {
const getAllUsers = async (req, res, next) => {
try {
const allUsers = await req.db.getAllUsers(req, res);
res
+1 -1
View File
@@ -144,7 +144,7 @@ const updateChecksTTL = async (req, res, next) => {
}
};
export default {
export {
createCheck,
getChecks,
getTeamChecks,
@@ -161,7 +161,7 @@ const editMaintenanceWindow = async (req, res, next) => {
}
};
export default {
export {
createMaintenanceWindows,
getMaintenanceWindowById,
getMaintenanceWindowsByTeamId,
+1 -1
View File
@@ -477,7 +477,7 @@ const addDemoMonitors = async (req, res, next) => {
}
};
export default {
export {
getAllMonitors,
getMonitorStatsById,
getMonitorCertificate,
+1 -6
View File
@@ -56,9 +56,4 @@ const obliterateQueue = async (req, res, next) => {
}
};
export default {
getMetrics,
getJobs,
addJob,
obliterateQueue,
};
export { getMetrics, getJobs, addJob, obliterateQueue };
+1 -4
View File
@@ -39,7 +39,4 @@ const updateAppSettings = async (req, res, next) => {
}
};
export default {
getAppSettings,
updateAppSettings,
};
export { getAppSettings, updateAppSettings };
+887
View File
File diff suppressed because it is too large Load Diff
+2
View File
@@ -37,9 +37,11 @@
"winston": "^3.13.0"
},
"devDependencies": {
"esm": "3.2.25",
"nodemon": "3.1.0",
"nyc": "17.1.0",
"proxyquire": "2.1.3",
"rewire": "7.0.0",
"sinon": "19.0.2"
}
}
+14 -15
View File
@@ -1,35 +1,34 @@
import { Router } from "express";
import checkController from "../controllers/checkController.js";
import {
createCheck,
getChecks,
deleteChecks,
getTeamChecks,
deleteChecksByTeamId,
updateChecksTTL,
} from "../controllers/checkController.js";
import { verifyOwnership } from "../middleware/verifyOwnership.js";
import { isAllowed } from "../middleware/isAllowed.js";
import Monitor from "../db/models/Monitor.js";
const router = Router();
router.get("/:monitorId", checkController.getChecks);
router.post(
"/:monitorId",
verifyOwnership(Monitor, "monitorId"),
checkController.createCheck
);
router.get("/:monitorId", getChecks);
router.post("/:monitorId", verifyOwnership(Monitor, "monitorId"), createCheck);
router.delete(
"/:monitorId",
verifyOwnership(Monitor, "monitorId"),
checkController.deleteChecks
deleteChecks
);
router.get("/team/:teamId", checkController.getTeamChecks);
router.get("/team/:teamId", getTeamChecks);
router.delete(
"/team/:teamId",
isAllowed(["admin", "superadmin"]),
checkController.deleteChecksByTeamId
deleteChecksByTeamId
);
router.put(
"/team/ttl",
isAllowed(["admin", "superadmin"]),
checkController.updateChecksTTL
);
router.put("/team/ttl", isAllowed(["admin", "superadmin"]), updateChecksTTL);
export default router;
+14 -7
View File
@@ -1,24 +1,31 @@
import { Router } from "express";
import maintenanceWindowController from "../controllers/maintenanceWindowController.js";
import {
createMaintenanceWindows,
getMaintenanceWindowById,
getMaintenanceWindowsByTeamId,
getMaintenanceWindowsByMonitorId,
deleteMaintenanceWindow,
editMaintenanceWindow,
} from "../controllers/maintenanceWindowController.js";
import { verifyOwnership } from "../middleware/verifyOwnership.js";
import Monitor from "../db/models/Monitor.js";
const router = Router();
router.post("/", maintenanceWindowController.createMaintenanceWindows);
router.post("/", createMaintenanceWindows);
router.get(
"/monitor/:monitorId",
verifyOwnership(Monitor, "monitorId"),
maintenanceWindowController.getMaintenanceWindowsByMonitorId
getMaintenanceWindowsByMonitorId
);
router.get("/team/", maintenanceWindowController.getMaintenanceWindowsByTeamId);
router.get("/team/", getMaintenanceWindowsByTeamId);
router.get("/:id", maintenanceWindowController.getMaintenanceWindowById);
router.get("/:id", getMaintenanceWindowById);
router.put("/:id", maintenanceWindowController.editMaintenanceWindow);
router.put("/:id", editMaintenanceWindow);
router.delete("/:id", maintenanceWindowController.deleteMaintenanceWindow);
router.delete("/:id", deleteMaintenanceWindow);
export default router;
+26 -36
View File
@@ -1,53 +1,43 @@
import { Router } from "express";
import monitorController from "../controllers/monitorController.js";
import {
getAllMonitors,
getMonitorStatsById,
getMonitorCertificate,
getMonitorById,
getMonitorsAndSummaryByTeamId,
getMonitorsByTeamId,
createMonitor,
deleteMonitor,
deleteAllMonitors,
editMonitor,
pauseMonitor,
addDemoMonitors,
} from "../controllers/monitorController.js";
import { isAllowed } from "../middleware/isAllowed.js";
const router = Router();
router.get("/", monitorController.getAllMonitors);
router.get("/stats/:monitorId", monitorController.getMonitorStatsById);
router.get("/certificate/:monitorId", monitorController.getMonitorCertificate);
router.get("/:monitorId", monitorController.getMonitorById);
router.get(
"/team/summary/:teamId",
monitorController.getMonitorsAndSummaryByTeamId
);
router.get("/team/:teamId", monitorController.getMonitorsByTeamId);
router.get("/", getAllMonitors);
router.get("/stats/:monitorId", getMonitorStatsById);
router.get("/certificate/:monitorId", getMonitorCertificate);
router.get("/:monitorId", getMonitorById);
router.get("/team/summary/:teamId", getMonitorsAndSummaryByTeamId);
router.get("/team/:teamId", getMonitorsByTeamId);
router.post(
"/",
isAllowed(["admin", "superadmin"]),
monitorController.createMonitor
);
router.post("/", isAllowed(["admin", "superadmin"]), createMonitor);
router.delete(
"/:monitorId",
isAllowed(["admin", "superadmin"]),
monitorController.deleteMonitor
);
router.delete("/:monitorId", isAllowed(["admin", "superadmin"]), deleteMonitor);
router.put(
"/:monitorId",
isAllowed(["admin", "superadmin"]),
monitorController.editMonitor
);
router.put("/:monitorId", isAllowed(["admin", "superadmin"]), editMonitor);
router.delete(
"/",
isAllowed(["superadmin"]),
monitorController.deleteAllMonitors
);
router.delete("/", isAllowed(["superadmin"]), deleteAllMonitors);
router.post(
"/pause/:monitorId",
isAllowed(["admin", "superadmin"]),
monitorController.pauseMonitor
pauseMonitor
);
router.post(
"/demo",
isAllowed(["admin", "superadmin"]),
monitorController.addDemoMonitors
);
router.post("/demo", isAllowed(["admin", "superadmin"]), addDemoMonitors);
export default router;
+10 -5
View File
@@ -1,16 +1,21 @@
import { Router } from "express";
import queueController from "../controllers/queueController.js";
import {
getMetrics,
getJobs,
addJob,
obliterateQueue,
} from "../controllers/queueController.js";
const router = Router();
router.get("/metrics", queueController.getMetrics);
router.get("/metrics", getMetrics);
// Get Jobs
router.get("/jobs", queueController.getJobs);
router.get("/jobs", getJobs);
// Add Job
router.post("/jobs", queueController.addJob);
router.post("/jobs", addJob);
// Obliterate Queue
router.post("/obliterate", queueController.obliterateQueue);
router.post("/obliterate", obliterateQueue);
export default router;
+6 -7
View File
@@ -1,14 +1,13 @@
import { Router } from "express";
import settingsController from "../controllers/settingsController.js";
import {
getAppSettings,
updateAppSettings,
} from "../controllers/settingsController.js";
import { isAllowed } from "../middleware/isAllowed.js";
const router = Router();
router.get("/", settingsController.getAppSettings);
router.put(
"/",
isAllowed(["superadmin"]),
settingsController.updateAppSettings
);
router.get("/", getAppSettings);
router.put("/", isAllowed(["superadmin"]), updateAppSettings);
export default router;
+22 -13
View File
@@ -1,4 +1,4 @@
const {
import {
issueToken,
registerUser,
loginUser,
@@ -9,16 +9,16 @@ const {
resetPassword,
deleteUser,
getAllUsers,
} = require("../../controllers/authController");
const jwt = require("jsonwebtoken");
const { errorMessages, successMessages } = require("../../utils/messages");
const sinon = require("sinon");
const logger = require("../../utils/logger");
} from "../../controllers/authController.js";
import jwt from "jsonwebtoken";
import { errorMessages, successMessages } from "../../utils/messages.js";
import sinon from "sinon";
import logger from "../../utils/logger.js";
describe("Auth Controller - issueToken", () => {
it("should reject with an error if jwt.sign fails", () => {
const error = new Error("jwt.sign error");
stub = sinon.stub(jwt, "sign").throws(error);
const stub = sinon.stub(jwt, "sign").throws(error);
const payload = { id: "123" };
const appSettings = { jwtSecret: "my_secret" };
expect(() => issueToken(payload, appSettings)).to.throw(error);
@@ -41,6 +41,7 @@ describe("Auth Controller - issueToken", () => {
});
describe("Auth Controller - registerUser", () => {
let req, res, next;
beforeEach(() => {
req = {
body: {
@@ -167,6 +168,7 @@ describe("Auth Controller - registerUser", () => {
});
describe("Auth Controller - loginUser", () => {
let req, res, next, user;
beforeEach(() => {
req = {
body: { email: "test@example.com", password: "Password123!" },
@@ -241,6 +243,7 @@ describe("Auth Controller - loginUser", () => {
});
describe("Auth Controller - editUser", async () => {
let req, res, next, stub, user;
beforeEach(() => {
req = {
params: { userId: "123" },
@@ -371,6 +374,7 @@ describe("Auth Controller - editUser", async () => {
});
describe("Auth Controller - checkSuperadminExists", async () => {
let req, res, next;
beforeEach(() => {
req = {
db: {
@@ -420,7 +424,8 @@ describe("Auth Controller - checkSuperadminExists", async () => {
});
});
describe("Auth Controller - requestRecovery", async () => {
describe("Auth Controller - requestRecovery", () => {
let req, res, next;
beforeEach(() => {
req = {
body: { email: "test@test.com" },
@@ -505,7 +510,8 @@ describe("Auth Controller - requestRecovery", async () => {
});
});
describe("Auth Controller - validateRecovery", async () => {
describe("Auth Controller - validateRecovery", () => {
let req, res, next;
beforeEach(() => {
req = {
body: { recoveryToken: "recovery-token" },
@@ -552,7 +558,8 @@ describe("Auth Controller - validateRecovery", async () => {
});
});
describe("Auth Controller - resetPassword", async () => {
describe("Auth Controller - resetPassword", () => {
let req, res, next, newPasswordValidation, handleValidationError, handleError;
beforeEach(() => {
req = {
body: {
@@ -618,7 +625,8 @@ describe("Auth Controller - resetPassword", async () => {
});
});
describe("Auth Controller - deleteUser", async () => {
describe("Auth Controller - deleteUser", () => {
let req, res, next, handleError;
beforeEach(() => {
req = {
headers: {
@@ -753,7 +761,9 @@ describe("Auth Controller - deleteUser", async () => {
});
});
describe("Auth Controller - getAllUsers", async () => {
describe("Auth Controller - getAllUsers", () => {
let req, res, next;
beforeEach(() => {
req = {
db: {
@@ -765,7 +775,6 @@ describe("Auth Controller - getAllUsers", async () => {
json: sinon.stub(),
};
next = sinon.stub();
handleError = sinon.stub();
});
afterEach(() => {
@@ -1,16 +1,16 @@
const {
import {
createCheck,
getChecks,
getTeamChecks,
deleteChecks,
deleteChecksByTeamId,
updateChecksTTL,
} = require("../../controllers/checkController");
const jwt = require("jsonwebtoken");
const { errorMessages, successMessages } = require("../../utils/messages");
const sinon = require("sinon");
} from "../../controllers/checkController.js";
import jwt from "jsonwebtoken";
import { errorMessages, successMessages } from "../../utils/messages.js";
import sinon from "sinon";
describe("Check Controller - createCheck", () => {
let req, res, next, handleError;
beforeEach(() => {
req = {
params: {},
@@ -88,6 +88,7 @@ describe("Check Controller - createCheck", () => {
});
describe("Check Controller - getChecks", () => {
let req, res, next;
beforeEach(() => {
req = {
params: {},
@@ -143,6 +144,7 @@ describe("Check Controller - getChecks", () => {
});
describe("Check Controller - getTeamChecks", () => {
let req, res, next;
beforeEach(() => {
req = {
params: {},
@@ -197,6 +199,7 @@ describe("Check Controller - getTeamChecks", () => {
});
describe("Check Controller - deleteChecks", () => {
let req, res, next;
beforeEach(() => {
req = {
params: {},
@@ -248,6 +251,7 @@ describe("Check Controller - deleteChecks", () => {
});
describe("Check Controller - deleteChecksByTeamId", () => {
let req, res, next;
beforeEach(() => {
req = {
params: {},
@@ -301,6 +305,7 @@ describe("Check Controller - deleteChecksByTeamId", () => {
});
describe("Check Controller - updateCheckTTL", () => {
let stub, req, res, next;
beforeEach(() => {
stub = sinon.stub(jwt, "verify").callsFake(() => {
return { teamId: "123" };
@@ -1,7 +1,7 @@
const {
import {
handleValidationError,
handleError,
} = require("../../controllers/controllerUtils");
} from "../../controllers/controllerUtils.js";
describe("controllerUtils - handleValidationError", () => {
it("should set status to 422", () => {
@@ -1,12 +1,12 @@
const {
import {
issueInvitation,
inviteVerifyController,
} = require("../../controllers/inviteController");
const jwt = require("jsonwebtoken");
const { errorMessages, successMessages } = require("../../utils/messages");
const sinon = require("sinon");
const joi = require("joi");
} from "../../controllers/inviteController.js";
import jwt from "jsonwebtoken";
import sinon from "sinon";
import joi from "joi";
describe("inviteController - issueInvitation", () => {
let req, res, next, stub;
beforeEach(() => {
req = {
headers: { authorization: "Bearer token" },
@@ -154,6 +154,7 @@ describe("inviteController - issueInvitation", () => {
});
describe("inviteController - inviteVerifyController", () => {
let req, res, next;
beforeEach(() => {
req = {
body: { token: "token" },
@@ -1,17 +1,18 @@
const {
import {
createMaintenanceWindows,
getMaintenanceWindowById,
getMaintenanceWindowsByTeamId,
getMaintenanceWindowsByMonitorId,
deleteMaintenanceWindow,
editMaintenanceWindow,
} = require("../../controllers/maintenanceWindowController");
} from "../../controllers/maintenanceWindowController.js";
const jwt = require("jsonwebtoken");
const { errorMessages, successMessages } = require("../../utils/messages");
const sinon = require("sinon");
import jwt from "jsonwebtoken";
import { successMessages } from "../../utils/messages.js";
import sinon from "sinon";
describe("maintenanceWindowController - createMaintenanceWindows", () => {
let req, res, next, stub;
beforeEach(() => {
req = {
body: {
@@ -103,6 +104,7 @@ describe("maintenanceWindowController - createMaintenanceWindows", () => {
});
describe("maintenanceWindowController - getMaintenanceWindowById", () => {
let req, res, next;
beforeEach(() => {
req = {
body: {},
@@ -154,6 +156,7 @@ describe("maintenanceWindowController - getMaintenanceWindowById", () => {
});
describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", () => {
let req, res, next, stub;
beforeEach(() => {
req = {
body: {},
@@ -220,6 +223,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", () => {
});
describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", () => {
let req, res, next;
beforeEach(() => {
req = {
body: {},
@@ -283,6 +287,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", () =>
});
describe("maintenanceWindowController - deleteMaintenanceWindow", () => {
let req, res, next;
beforeEach(() => {
req = {
body: {},
@@ -338,6 +343,7 @@ describe("maintenanceWindowController - deleteMaintenanceWindow", () => {
});
describe("maintenanceWindowController - editMaintenanceWindow", () => {
let req, res, next;
beforeEach(() => {
req = {
body: {
+110 -103
View File
@@ -1,4 +1,4 @@
const {
import {
getAllMonitors,
getMonitorStatsById,
getMonitorCertificate,
@@ -11,19 +11,15 @@ const {
editMonitor,
pauseMonitor,
addDemoMonitors,
} = require("../../controllers/monitorController");
const jwt = require("jsonwebtoken");
const proxyquire = require("proxyquire");
const sinon = require("sinon");
const { errorMessages, successMessages } = require("../../utils/messages");
const sslCheckerStub = sinon.stub();
const monitorController = proxyquire("../../controllers/monitorController", {
"ssl-checker": sslCheckerStub,
});
const logger = require("../../utils/logger");
} from "../../controllers/monitorController.js";
import jwt from "jsonwebtoken";
import sinon from "sinon";
import { successMessages } from "../../utils/messages.js";
import logger from "../../utils/logger.js";
const SERVICE_NAME = "monitorController";
describe("Monitor Controller - getAllMonitors", () => {
let req, res, next;
beforeEach(() => {
req = {
params: {},
@@ -65,6 +61,7 @@ describe("Monitor Controller - getAllMonitors", () => {
});
describe("Monitor Controller - getMonitorStatsById", () => {
let req, res, next;
beforeEach(() => {
req = {
params: {
@@ -119,97 +116,99 @@ describe("Monitor Controller - getMonitorStatsById", () => {
});
});
describe("Monitor Controller - getMonitorCertificate", () => {
beforeEach(() => {
req = {
params: {
monitorId: "123",
},
query: {},
body: {},
db: {
getMonitorById: sinon.stub(),
},
};
res = {
status: sinon.stub().returnsThis(),
json: sinon.stub(),
};
next = sinon.stub();
});
afterEach(() => {
sinon.restore();
});
// describe("Monitor Controller - getMonitorCertificate", () => {
// let req, res, next, sslCheckerStub;
// beforeEach(() => {
// req = {
// params: {
// monitorId: "123",
// },
// query: {},
// body: {},
// db: {
// getMonitorById: sinon.stub(),
// },
// };
// res = {
// status: sinon.stub().returnsThis(),
// json: sinon.stub(),
// };
// next = sinon.stub();
// });
it("should reject with an error if param validation fails", async () => {
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 creating a URL fails", async () => {
req.db.getMonitorById.returns(null);
await getMonitorCertificate(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0]).to.be.instanceOf(TypeError);
});
it("should reject with an error if SSL checker fails", async () => {
req.db.getMonitorById.returns({ url: "https://example.com" });
sslCheckerStub.throws(new Error("SSL checker error"));
await monitorController.getMonitorCertificate(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("SSL checker error");
});
it("should return success message and data if all operations succeed", async () => {
req.db.getMonitorById.returns({ url: "https://example.com" });
const certificate = { validTo: 1 };
sslCheckerStub.returns(certificate);
await monitorController.getMonitorCertificate(req, res, next);
expect(res.status.firstCall.args[0]).to.equal(200);
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.MONITOR_CERTIFICATE,
data: {
certificateDate: new Date(certificate.validTo).toLocaleDateString(),
},
})
).to.be.true;
});
it("should return success message and N/A if certificate is not found", async () => {
req.db.getMonitorById.returns({ url: "https://example.com" });
sslCheckerStub.returns(null);
await monitorController.getMonitorCertificate(req, res, next);
expect(res.status.firstCall.args[0]).to.equal(200);
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.MONITOR_CERTIFICATE,
data: {
certificateDate: "N/A",
},
})
).to.be.true;
});
// afterEach(() => {
// sinon.restore();
// });
it("should return success message and N/A if certificate doesn't have validTo", async () => {
req.db.getMonitorById.returns({ url: "https://example.com" });
sslCheckerStub.returns({ valid: 1 });
await monitorController.getMonitorCertificate(req, res, next);
expect(res.status.firstCall.args[0]).to.equal(200);
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.MONITOR_CERTIFICATE,
data: {
certificateDate: "N/A",
},
})
).to.be.true;
});
});
// it("should reject with an error if param validation fails", async () => {
// 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 creating a URL fails", async () => {
// req.db.getMonitorById.returns(null);
// await getMonitorCertificate(req, res, next);
// expect(next.firstCall.args[0]).to.be.an("error");
// expect(next.firstCall.args[0]).to.be.instanceOf(TypeError);
// });
// it("should reject with an error if SSL checker fails", async () => {
// req.db.getMonitorById.returns({ url: "https://example.com" });
// await monitorController.getMonitorCertificate(req, res, next);
// expect(next.firstCall.args[0]).to.be.an("error");
// expect(next.firstCall.args[0].message).to.equal("SSL checker error");
// });
// it("should return success message and data if all operations succeed", async () => {
// req.db.getMonitorById.returns({ url: "https://example.com" });
// const certificate = { validTo: 1 };
// sslCheckerStub.returns(certificate);
// await getMonitorCertificate(req, res, next);
// expect(res.status.firstCall.args[0]).to.equal(200);
// expect(
// res.json.calledOnceWith({
// success: true,
// msg: successMessages.MONITOR_CERTIFICATE,
// data: {
// certificateDate: new Date(certificate.validTo).toLocaleDateString(),
// },
// })
// ).to.be.true;
// });
// it("should return success message and N/A if certificate is not found", async () => {
// req.db.getMonitorById.returns({ url: "https://example.com" });
// // sslCheckerStub.returns(null);
// await getMonitorCertificate(req, res, next);
// expect(res.status.firstCall.args[0]).to.equal(200);
// expect(
// res.json.calledOnceWith({
// success: true,
// msg: successMessages.MONITOR_CERTIFICATE,
// data: {
// certificateDate: "N/A",
// },
// })
// ).to.be.true;
// });
// it("should return success message and N/A if certificate doesn't have validTo", async () => {
// req.db.getMonitorById.returns({ url: "https://example.com" });
// // sslCheckerStub.returns({ valid: 1 });
// await getMonitorCertificate(req, res, next);
// expect(res.status.firstCall.args[0]).to.equal(200);
// expect(
// res.json.calledOnceWith({
// success: true,
// msg: successMessages.MONITOR_CERTIFICATE,
// data: {
// certificateDate: "N/A",
// },
// })
// ).to.be.true;
// });
// });
describe("Monitor Controller - getMonitorById", () => {
let req, res, next;
beforeEach(() => {
req = {
params: {
@@ -270,6 +269,7 @@ describe("Monitor Controller - getMonitorById", () => {
});
describe("Monitor Controller - getMonitorsAndSummaryByTeamId", () => {
let req, res, next;
beforeEach(() => {
req = {
params: {
@@ -325,6 +325,7 @@ describe("Monitor Controller - getMonitorsAndSummaryByTeamId", () => {
});
describe("Monitor Controller - getMonitorsByTeamId", () => {
let req, res, next;
beforeEach(() => {
req = {
params: {
@@ -379,6 +380,7 @@ describe("Monitor Controller - getMonitorsByTeamId", () => {
});
describe("Monitor Controller - createMonitor", () => {
let req, res, next;
beforeEach(() => {
req = {
params: {},
@@ -474,6 +476,7 @@ describe("Monitor Controller - createMonitor", () => {
});
describe("Monitor Controller - deleteMonitor", () => {
let req, res, next;
beforeEach(() => {
req = {
params: {
@@ -518,7 +521,7 @@ describe("Monitor Controller - deleteMonitor", () => {
const monitor = { name: "test_monitor", _id: "123" };
req.db.deleteMonitor.returns(monitor);
req.jobQueue.deleteJob.rejects(error);
await monitorController.deleteMonitor(req, res, next);
await deleteMonitor(req, res, next);
expect(logger.error.calledOnce).to.be.true;
expect(logger.error.firstCall.args[0]).to.equal(
`Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`
@@ -534,7 +537,7 @@ describe("Monitor Controller - deleteMonitor", () => {
const monitor = { name: "test_monitor", _id: "123" };
req.db.deleteMonitor.returns(monitor);
req.db.deleteChecks.rejects(error);
await monitorController.deleteMonitor(req, res, next);
await deleteMonitor(req, res, next);
expect(logger.error.calledOnce).to.be.true;
expect(logger.error.firstCall.args[0]).to.equal(
`Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`
@@ -550,7 +553,7 @@ describe("Monitor Controller - deleteMonitor", () => {
const monitor = { name: "test_monitor", _id: "123" };
req.db.deleteMonitor.returns(monitor);
req.db.deletePageSpeedChecksByMonitorId.rejects(error);
await monitorController.deleteMonitor(req, res, next);
await deleteMonitor(req, res, next);
expect(logger.error.calledOnce).to.be.true;
expect(logger.error.firstCall.args[0]).to.equal(
`Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`
@@ -566,7 +569,7 @@ describe("Monitor Controller - deleteMonitor", () => {
const monitor = { name: "test_monitor", _id: "123" };
req.db.deleteMonitor.returns(monitor);
req.db.deleteNotificationsByMonitorId.rejects(error);
await monitorController.deleteMonitor(req, res, next);
await deleteMonitor(req, res, next);
expect(logger.error.calledOnce).to.be.true;
expect(logger.error.firstCall.args[0]).to.equal(
`Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`
@@ -580,7 +583,7 @@ describe("Monitor Controller - deleteMonitor", () => {
it("should return success message if all operations succeed", async () => {
const monitor = { name: "test_monitor", _id: "123" };
req.db.deleteMonitor.returns(monitor);
await monitorController.deleteMonitor(req, res, next);
await deleteMonitor(req, res, next);
expect(res.status.firstCall.args[0]).to.equal(200);
expect(
res.json.calledOnceWith({
@@ -592,6 +595,7 @@ describe("Monitor Controller - deleteMonitor", () => {
});
describe("Monitor Controller - deleteAllMonitors", () => {
let req, res, next, stub;
beforeEach(() => {
stub = sinon.stub(jwt, "verify").callsFake(() => {
return { teamId: "123" };
@@ -700,6 +704,7 @@ describe("Monitor Controller - deleteAllMonitors", () => {
});
describe("Monitor Controller - editMonitor", () => {
let req, res, next;
beforeEach(() => {
req = {
headers: {},
@@ -822,6 +827,7 @@ describe("Monitor Controller - editMonitor", () => {
});
describe("Monitor Controller - pauseMonitor", () => {
let req, res, next;
beforeEach(() => {
req = {
headers: {},
@@ -928,6 +934,7 @@ describe("Monitor Controller - pauseMonitor", () => {
});
describe("Monitor Controller - addDemoMonitors", () => {
let req, res, next, stub;
beforeEach(() => {
stub = sinon.stub(jwt, "verify").callsFake(() => {
return { _id: "123", teamId: "123" };
@@ -1,16 +1,15 @@
const { afterEach } = require("node:test");
const {
import { afterEach } from "node:test";
import {
getMetrics,
getJobs,
addJob,
obliterateQueue,
} = require("../../controllers/queueController");
const SERVICE_NAME = "JobQueueController";
const { errorMessages, successMessages } = require("../../utils/messages");
const sinon = require("sinon");
} from "../../controllers/queueController.js";
import { successMessages } from "../../utils/messages.js";
import sinon from "sinon";
describe("Queue Controller - getMetrics", () => {
let req, res, next;
beforeEach(() => {
req = {
headers: {},
@@ -26,7 +25,6 @@ describe("Queue Controller - getMetrics", () => {
json: sinon.stub(),
};
next = sinon.stub();
handleError = sinon.stub();
});
afterEach(() => {
sinon.restore();
@@ -52,6 +50,7 @@ describe("Queue Controller - getMetrics", () => {
});
describe("Queue Controller - getJobs", () => {
let req, res, next;
beforeEach(() => {
req = {
headers: {},
@@ -67,7 +66,6 @@ describe("Queue Controller - getJobs", () => {
json: sinon.stub(),
};
next = sinon.stub();
handleError = sinon.stub();
});
afterEach(() => {
sinon.restore();
@@ -92,6 +90,7 @@ describe("Queue Controller - getJobs", () => {
});
describe("Queue Controller - addJob", () => {
let req, res, next;
beforeEach(() => {
req = {
headers: {},
@@ -107,7 +106,6 @@ describe("Queue Controller - addJob", () => {
json: sinon.stub(),
};
next = sinon.stub();
handleError = sinon.stub();
});
afterEach(() => {
sinon.restore();
@@ -130,6 +128,7 @@ describe("Queue Controller - addJob", () => {
});
describe("Queue Controller - obliterateQueue", () => {
let req, res, next;
beforeEach(() => {
req = {
headers: {},
@@ -145,7 +144,6 @@ describe("Queue Controller - obliterateQueue", () => {
json: sinon.stub(),
};
next = sinon.stub();
handleError = sinon.stub();
});
afterEach(() => {
sinon.restore();
@@ -1,13 +1,14 @@
const { afterEach } = require("node:test");
const {
import { afterEach } from "node:test";
import {
getAppSettings,
updateAppSettings,
} = require("../../controllers/settingsController");
} from "../../controllers/settingsController.js";
const { errorMessages, successMessages } = require("../../utils/messages");
const sinon = require("sinon");
import { successMessages } from "../../utils/messages.js";
import sinon from "sinon";
describe("Settings Controller - getAppSettings", () => {
let req, res, next;
beforeEach(() => {
req = {
headers: {},
@@ -23,7 +24,6 @@ describe("Settings Controller - getAppSettings", () => {
json: sinon.stub(),
};
next = sinon.stub();
handleError = sinon.stub();
});
afterEach(() => {
sinon.restore();
@@ -49,6 +49,7 @@ describe("Settings Controller - getAppSettings", () => {
});
describe("Settings Controller - updateAppSettings", () => {
let req, res, next;
beforeEach(() => {
req = {
headers: {},
@@ -66,7 +67,6 @@ describe("Settings Controller - updateAppSettings", () => {
json: sinon.stub(),
};
next = sinon.stub();
handleError = sinon.stub();
});
afterEach(() => {
sinon.restore();