Fix coverage for auth
This commit is contained in:
1 parent
c356843799
commit
4a05157eb3
6 files changed
+126
-123
No files matched your search
@@ -103,7 +103,6 @@ const registerUser = async (req, res, next) => {
|
|||||||
data: { user: newUser, token: token },
|
data: { user: newUser, token: token },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("ERROR", error);
|
|
||||||
next(handleError(error, SERVICE_NAME, "registerController"));
|
next(handleError(error, SERVICE_NAME, "registerController"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ const getMonitorStatsById = async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getMonitorCertificate = async (req, res, next) => {
|
const getMonitorCertificate = async (req, res, next, fetchMonitorCertificate) => {
|
||||||
try {
|
try {
|
||||||
await getCertificateParamValidation.validateAsync(req.params);
|
await getCertificateParamValidation.validateAsync(req.params);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -14,12 +14,15 @@ import {
|
|||||||
addDemoMonitors,
|
addDemoMonitors,
|
||||||
} from "../controllers/monitorController.js";
|
} from "../controllers/monitorController.js";
|
||||||
import { isAllowed } from "../middleware/isAllowed.js";
|
import { isAllowed } from "../middleware/isAllowed.js";
|
||||||
|
import { fetchMonitorCertificate } from "../controllers/controllerUtils.js";
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.get("/", getAllMonitors);
|
router.get("/", getAllMonitors);
|
||||||
router.get("/stats/:monitorId", getMonitorStatsById);
|
router.get("/stats/:monitorId", getMonitorStatsById);
|
||||||
router.get("/certificate/:monitorId", getMonitorCertificate);
|
router.get("/certificate/:monitorId", (req, res, next) => {
|
||||||
|
getMonitorCertificate(req, res, next, fetchMonitorCertificate);
|
||||||
|
});
|
||||||
router.get("/:monitorId", getMonitorById);
|
router.get("/:monitorId", getMonitorById);
|
||||||
router.get("/team/summary/:teamId", getMonitorsAndSummaryByTeamId);
|
router.get("/team/summary/:teamId", getMonitorsAndSummaryByTeamId);
|
||||||
router.get("/team/:teamId", getMonitorsByTeamId);
|
router.get("/team/:teamId", getMonitorsByTeamId);
|
||||||
@@ -32,11 +35,7 @@ router.put("/:monitorId", isAllowed(["admin", "superadmin"]), editMonitor);
|
|||||||
|
|
||||||
router.delete("/", isAllowed(["superadmin"]), deleteAllMonitors);
|
router.delete("/", isAllowed(["superadmin"]), deleteAllMonitors);
|
||||||
|
|
||||||
router.post(
|
router.post("/pause/:monitorId", isAllowed(["admin", "superadmin"]), pauseMonitor);
|
||||||
"/pause/:monitorId",
|
|
||||||
isAllowed(["admin", "superadmin"]),
|
|
||||||
pauseMonitor
|
|
||||||
);
|
|
||||||
|
|
||||||
router.post("/demo", isAllowed(["admin", "superadmin"]), addDemoMonitors);
|
router.post("/demo", isAllowed(["admin", "superadmin"]), addDemoMonitors);
|
||||||
|
|
||||||
|
|||||||
@@ -88,27 +88,23 @@ describe("Auth Controller - registerUser", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should reject with an error if checkSuperadmin fails", async () => {
|
it("should reject with an error if checkSuperadmin fails", async () => {
|
||||||
req.db.checkSuperadmin.rejects(new Error("checkSuperadmin error"));
|
req.db.checkSuperadmin.throws(new Error("checkSuperadmin error"));
|
||||||
await registerUser(req, res, next);
|
await registerUser(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal("checkSuperadmin error");
|
expect(next.firstCall.args[0].message).to.equal("checkSuperadmin error");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should reject with an error if getInviteTokenAndDelete fails", async () => {
|
it("should reject with an error if getInviteTokenAndDelete fails", async () => {
|
||||||
req.db.checkSuperadmin.resolves(true);
|
req.db.checkSuperadmin.returns(true);
|
||||||
req.db.getInviteTokenAndDelete.rejects(
|
req.db.getInviteTokenAndDelete.throws(new Error("getInviteTokenAndDelete error"));
|
||||||
new Error("getInviteTokenAndDelete error")
|
|
||||||
);
|
|
||||||
await registerUser(req, res, next);
|
await registerUser(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal(
|
expect(next.firstCall.args[0].message).to.equal("getInviteTokenAndDelete error");
|
||||||
"getInviteTokenAndDelete error"
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should reject with an error if updateAppSettings fails", async () => {
|
it("should reject with an error if updateAppSettings fails", async () => {
|
||||||
req.db.checkSuperadmin.resolves(false);
|
req.db.checkSuperadmin.returns(false);
|
||||||
req.db.updateAppSettings.rejects(new Error("updateAppSettings error"));
|
req.db.updateAppSettings.throws(new Error("updateAppSettings error"));
|
||||||
await registerUser(req, res, next);
|
await registerUser(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal("updateAppSettings error");
|
expect(next.firstCall.args[0].message).to.equal("updateAppSettings error");
|
||||||
@@ -132,9 +128,7 @@ describe("Auth Controller - registerUser", () => {
|
|||||||
);
|
);
|
||||||
await registerUser(req, res, next);
|
await registerUser(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal(
|
expect(next.firstCall.args[0].message).to.equal("settingsService.getSettings error");
|
||||||
"settingsService.getSettings error"
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should log an error if emailService.buildAndSendEmail fails", async () => {
|
it("should log an error if emailService.buildAndSendEmail fails", async () => {
|
||||||
@@ -165,6 +159,24 @@ describe("Auth Controller - registerUser", () => {
|
|||||||
).to.be.true;
|
).to.be.true;
|
||||||
expect(next.notCalled).to.be.true;
|
expect(next.notCalled).to.be.true;
|
||||||
});
|
});
|
||||||
|
it("should return a success message and data if all operations are successful and superAdmin true", async () => {
|
||||||
|
const user = { _id: "123" };
|
||||||
|
req.db.checkSuperadmin.resolves(true);
|
||||||
|
req.db.updateAppSettings.resolves();
|
||||||
|
req.db.insertUser.returns(user);
|
||||||
|
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
|
||||||
|
req.emailService.buildAndSendEmail.resolves("message-id");
|
||||||
|
await registerUser(req, res, next);
|
||||||
|
expect(res.status.calledWith(200)).to.be.true;
|
||||||
|
expect(
|
||||||
|
res.json.calledWith({
|
||||||
|
success: true,
|
||||||
|
msg: successMessages.AUTH_CREATE_USER,
|
||||||
|
data: { user, token: sinon.match.string },
|
||||||
|
})
|
||||||
|
).to.be.true;
|
||||||
|
expect(next.notCalled).to.be.true;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Auth Controller - loginUser", () => {
|
describe("Auth Controller - loginUser", () => {
|
||||||
@@ -534,14 +546,10 @@ describe("Auth Controller - validateRecovery", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should reject with an error if validateRecoveryToken fails", async () => {
|
it("should reject with an error if validateRecoveryToken fails", async () => {
|
||||||
req.db.validateRecoveryToken.rejects(
|
req.db.validateRecoveryToken.rejects(new Error("validateRecoveryToken error"));
|
||||||
new Error("validateRecoveryToken error")
|
|
||||||
);
|
|
||||||
await validateRecovery(req, res, next);
|
await validateRecovery(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal(
|
expect(next.firstCall.args[0].message).to.equal("validateRecoveryToken error");
|
||||||
"validateRecoveryToken error"
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return a success message if the token is valid", async () => {
|
it("should return a success message if the token is valid", async () => {
|
||||||
@@ -670,9 +678,7 @@ describe("Auth Controller - deleteUser", () => {
|
|||||||
|
|
||||||
expect(req.db.getUserByEmail.calledOnceWith("test@example.com")).to.be.true;
|
expect(req.db.getUserByEmail.calledOnceWith("test@example.com")).to.be.true;
|
||||||
expect(next.calledOnce).to.be.true;
|
expect(next.calledOnce).to.be.true;
|
||||||
expect(next.firstCall.args[0].message).to.equal(
|
expect(next.firstCall.args[0].message).to.equal(errorMessages.DB_USER_NOT_FOUND);
|
||||||
errorMessages.DB_USER_NOT_FOUND
|
|
||||||
);
|
|
||||||
expect(res.status.notCalled).to.be.true;
|
expect(res.status.notCalled).to.be.true;
|
||||||
expect(res.json.notCalled).to.be.true;
|
expect(res.json.notCalled).to.be.true;
|
||||||
});
|
});
|
||||||
@@ -700,10 +706,9 @@ describe("Auth Controller - deleteUser", () => {
|
|||||||
).to.be.true;
|
).to.be.true;
|
||||||
expect(req.jobQueue.deleteJob.calledOnceWith(monitors[0])).to.be.true;
|
expect(req.jobQueue.deleteJob.calledOnceWith(monitors[0])).to.be.true;
|
||||||
expect(req.db.deleteChecks.calledOnceWith("monitor_id")).to.be.true;
|
expect(req.db.deleteChecks.calledOnceWith("monitor_id")).to.be.true;
|
||||||
expect(req.db.deletePageSpeedChecksByMonitorId.calledOnceWith("monitor_id"))
|
expect(req.db.deletePageSpeedChecksByMonitorId.calledOnceWith("monitor_id")).to.be
|
||||||
.to.be.true;
|
.true;
|
||||||
expect(req.db.deleteNotificationsByMonitorId.calledOnceWith("monitor_id"))
|
expect(req.db.deleteNotificationsByMonitorId.calledOnceWith("monitor_id")).to.be.true;
|
||||||
.to.be.true;
|
|
||||||
expect(req.db.deleteTeam.calledOnceWith("team_id")).to.be.true;
|
expect(req.db.deleteTeam.calledOnceWith("team_id")).to.be.true;
|
||||||
expect(req.db.deleteAllOtherUsers.calledOnce).to.be.true;
|
expect(req.db.deleteAllOtherUsers.calledOnce).to.be.true;
|
||||||
expect(req.db.deleteMonitorsByUserId.calledOnceWith("user_id")).to.be.true;
|
expect(req.db.deleteMonitorsByUserId.calledOnceWith("user_id")).to.be.true;
|
||||||
|
|||||||
@@ -204,7 +204,7 @@ describe("controllerUtils - fetchMonitorCertificate", () => {
|
|||||||
try {
|
try {
|
||||||
await fetchMonitorCertificate(tls, monitor);
|
await fetchMonitorCertificate(tls, monitor);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
expect(error.message).to.equal("Certificate error");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -16,6 +16,9 @@ import jwt from "jsonwebtoken";
|
|||||||
import sinon from "sinon";
|
import sinon from "sinon";
|
||||||
import { successMessages } from "../../utils/messages.js";
|
import { successMessages } from "../../utils/messages.js";
|
||||||
import logger from "../../utils/logger.js";
|
import logger from "../../utils/logger.js";
|
||||||
|
import * as monitorController from "../../controllers/monitorController.js";
|
||||||
|
import { fetchMonitorCertificate } from "../../controllers/controllerUtils.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "monitorController";
|
const SERVICE_NAME = "monitorController";
|
||||||
|
|
||||||
describe("Monitor Controller - getAllMonitors", () => {
|
describe("Monitor Controller - getAllMonitors", () => {
|
||||||
@@ -116,96 +119,78 @@ describe("Monitor Controller - getMonitorStatsById", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// describe("Monitor Controller - getMonitorCertificate", () => {
|
describe("Monitor Controller - getMonitorCertificate", () => {
|
||||||
// let req, res, next, sslCheckerStub;
|
let req, res, next, fetchMonitorCertificate;
|
||||||
// beforeEach(() => {
|
beforeEach(() => {
|
||||||
// req = {
|
req = {
|
||||||
// params: {
|
params: {
|
||||||
// monitorId: "123",
|
monitorId: "123",
|
||||||
// },
|
},
|
||||||
// query: {},
|
query: {},
|
||||||
// body: {},
|
body: {},
|
||||||
// db: {
|
db: {
|
||||||
// getMonitorById: sinon.stub(),
|
getMonitorById: sinon.stub(),
|
||||||
// },
|
},
|
||||||
// };
|
};
|
||||||
// res = {
|
res = {
|
||||||
// status: sinon.stub().returnsThis(),
|
status: sinon.stub().returnsThis(),
|
||||||
// json: sinon.stub(),
|
json: sinon.stub(),
|
||||||
// };
|
};
|
||||||
// next = sinon.stub();
|
next = sinon.stub();
|
||||||
// });
|
fetchMonitorCertificate = sinon.stub();
|
||||||
|
});
|
||||||
|
|
||||||
// afterEach(() => {
|
afterEach(() => {
|
||||||
// sinon.restore();
|
sinon.restore();
|
||||||
// });
|
});
|
||||||
|
|
||||||
// it("should reject with an error if param validation fails", async () => {
|
it("should reject with an error if param validation fails", async () => {
|
||||||
// req.params = {};
|
req.params = {};
|
||||||
// await getMonitorCertificate(req, res, next);
|
await getMonitorCertificate(req, res, next);
|
||||||
// expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
// expect(next.firstCall.args[0].status).to.equal(422);
|
expect(next.firstCall.args[0].status).to.equal(422);
|
||||||
// });
|
});
|
||||||
// it("should reject with an error if creating a URL fails", async () => {
|
it("should reject with an error if getMonitorById operation fails", async () => {
|
||||||
// req.db.getMonitorById.returns(null);
|
req.db.getMonitorById.throws(new Error("DB error"));
|
||||||
// await getMonitorCertificate(req, res, next);
|
await getMonitorCertificate(req, res, next);
|
||||||
// expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
// expect(next.firstCall.args[0]).to.be.instanceOf(TypeError);
|
expect(next.firstCall.args[0].message).to.equal("DB error");
|
||||||
// });
|
});
|
||||||
// it("should reject with an error if SSL checker fails", async () => {
|
it("should return success message and data if all operations succeed with a valid cert", async () => {
|
||||||
// req.db.getMonitorById.returns({ url: "https://example.com" });
|
req.db.getMonitorById.returns({ url: "https://www.google.com" });
|
||||||
// await monitorController.getMonitorCertificate(req, res, next);
|
const data = { certificate: "cert", validTo: "2024/08/08" };
|
||||||
// expect(next.firstCall.args[0]).to.be.an("error");
|
fetchMonitorCertificate.returns(data);
|
||||||
// expect(next.firstCall.args[0].message).to.equal("SSL checker error");
|
await getMonitorCertificate(req, res, next, fetchMonitorCertificate);
|
||||||
// });
|
expect(res.status.firstCall.args[0]).to.equal(200);
|
||||||
// it("should return success message and data if all operations succeed", async () => {
|
expect(
|
||||||
// req.db.getMonitorById.returns({ url: "https://example.com" });
|
res.json.calledOnceWith({
|
||||||
// const certificate = { validTo: 1 };
|
success: true,
|
||||||
// sslCheckerStub.returns(certificate);
|
msg: successMessages.MONITOR_CERTIFICATE,
|
||||||
// await getMonitorCertificate(req, res, next);
|
data: { certificateDate: new Date(data.validTo) },
|
||||||
// expect(res.status.firstCall.args[0]).to.equal(200);
|
})
|
||||||
// expect(
|
).to.be.true;
|
||||||
// res.json.calledOnceWith({
|
});
|
||||||
// success: true,
|
it("should return success message and data if all operations succeed with an invalid cert", async () => {
|
||||||
// msg: successMessages.MONITOR_CERTIFICATE,
|
req.db.getMonitorById.returns({ url: "https://www.google.com" });
|
||||||
// data: {
|
fetchMonitorCertificate.returns({});
|
||||||
// certificateDate: new Date(certificate.validTo).toLocaleDateString(),
|
await getMonitorCertificate(req, res, next, fetchMonitorCertificate);
|
||||||
// },
|
expect(res.status.firstCall.args[0]).to.equal(200);
|
||||||
// })
|
expect(
|
||||||
// ).to.be.true;
|
res.json.calledOnceWith({
|
||||||
// });
|
success: true,
|
||||||
// it("should return success message and N/A if certificate is not found", async () => {
|
msg: successMessages.MONITOR_CERTIFICATE,
|
||||||
// req.db.getMonitorById.returns({ url: "https://example.com" });
|
data: { certificateDate: "N/A" },
|
||||||
// // sslCheckerStub.returns(null);
|
})
|
||||||
// await getMonitorCertificate(req, res, next);
|
).to.be.true;
|
||||||
// expect(res.status.firstCall.args[0]).to.equal(200);
|
});
|
||||||
// expect(
|
it("should return an error if fetchMonitorCertificate fails", async () => {
|
||||||
// res.json.calledOnceWith({
|
req.db.getMonitorById.returns({ url: "https://www.google.com" });
|
||||||
// success: true,
|
fetchMonitorCertificate.throws(new Error("Certificate error"));
|
||||||
// msg: successMessages.MONITOR_CERTIFICATE,
|
await getMonitorCertificate(req, res, next, fetchMonitorCertificate);
|
||||||
// data: {
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
// certificateDate: "N/A",
|
expect(next.firstCall.args[0].message).to.equal("Certificate error");
|
||||||
// },
|
});
|
||||||
// })
|
});
|
||||||
// ).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", () => {
|
describe("Monitor Controller - getMonitorById", () => {
|
||||||
let req, res, next;
|
let req, res, next;
|
||||||
@@ -701,6 +686,21 @@ describe("Monitor Controller - deleteAllMonitors", () => {
|
|||||||
`Error deleting associated records for monitor ${monitors[0]._id} with name ${monitors[0].name}`
|
`Error deleting associated records for monitor ${monitors[0]._id} with name ${monitors[0].name}`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
it("should return success message if all operations succeed", async () => {
|
||||||
|
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
|
||||||
|
req.db.deleteAllMonitors.returns({
|
||||||
|
monitors: [{ name: "test_monitor", _id: "123" }],
|
||||||
|
deletedCount: 1,
|
||||||
|
});
|
||||||
|
await deleteAllMonitors(req, res, next);
|
||||||
|
expect(res.status.firstCall.args[0]).to.equal(200);
|
||||||
|
expect(
|
||||||
|
res.json.calledOnceWith({
|
||||||
|
success: true,
|
||||||
|
msg: "Deleted 1 monitors",
|
||||||
|
})
|
||||||
|
).to.be.true;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Monitor Controller - editMonitor", () => {
|
describe("Monitor Controller - editMonitor", () => {
|
||||||
|
|||||||
Reference in new issue
Block a user