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 },
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("ERROR", error);
|
||||
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 {
|
||||
await getCertificateParamValidation.validateAsync(req.params);
|
||||
} catch (error) {
|
||||
|
||||
@@ -14,12 +14,15 @@ import {
|
||||
addDemoMonitors,
|
||||
} from "../controllers/monitorController.js";
|
||||
import { isAllowed } from "../middleware/isAllowed.js";
|
||||
import { fetchMonitorCertificate } from "../controllers/controllerUtils.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get("/", getAllMonitors);
|
||||
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("/team/summary/:teamId", getMonitorsAndSummaryByTeamId);
|
||||
router.get("/team/:teamId", getMonitorsByTeamId);
|
||||
@@ -32,11 +35,7 @@ router.put("/:monitorId", isAllowed(["admin", "superadmin"]), editMonitor);
|
||||
|
||||
router.delete("/", isAllowed(["superadmin"]), deleteAllMonitors);
|
||||
|
||||
router.post(
|
||||
"/pause/:monitorId",
|
||||
isAllowed(["admin", "superadmin"]),
|
||||
pauseMonitor
|
||||
);
|
||||
router.post("/pause/:monitorId", isAllowed(["admin", "superadmin"]), pauseMonitor);
|
||||
|
||||
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 () => {
|
||||
req.db.checkSuperadmin.rejects(new Error("checkSuperadmin error"));
|
||||
req.db.checkSuperadmin.throws(new Error("checkSuperadmin error"));
|
||||
await registerUser(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.an("error");
|
||||
expect(next.firstCall.args[0].message).to.equal("checkSuperadmin error");
|
||||
});
|
||||
|
||||
it("should reject with an error if getInviteTokenAndDelete fails", async () => {
|
||||
req.db.checkSuperadmin.resolves(true);
|
||||
req.db.getInviteTokenAndDelete.rejects(
|
||||
new Error("getInviteTokenAndDelete error")
|
||||
);
|
||||
req.db.checkSuperadmin.returns(true);
|
||||
req.db.getInviteTokenAndDelete.throws(new Error("getInviteTokenAndDelete error"));
|
||||
await registerUser(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.an("error");
|
||||
expect(next.firstCall.args[0].message).to.equal(
|
||||
"getInviteTokenAndDelete error"
|
||||
);
|
||||
expect(next.firstCall.args[0].message).to.equal("getInviteTokenAndDelete error");
|
||||
});
|
||||
|
||||
it("should reject with an error if updateAppSettings fails", async () => {
|
||||
req.db.checkSuperadmin.resolves(false);
|
||||
req.db.updateAppSettings.rejects(new Error("updateAppSettings error"));
|
||||
req.db.checkSuperadmin.returns(false);
|
||||
req.db.updateAppSettings.throws(new Error("updateAppSettings error"));
|
||||
await registerUser(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.an("error");
|
||||
expect(next.firstCall.args[0].message).to.equal("updateAppSettings error");
|
||||
@@ -132,9 +128,7 @@ describe("Auth Controller - registerUser", () => {
|
||||
);
|
||||
await registerUser(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.an("error");
|
||||
expect(next.firstCall.args[0].message).to.equal(
|
||||
"settingsService.getSettings error"
|
||||
);
|
||||
expect(next.firstCall.args[0].message).to.equal("settingsService.getSettings error");
|
||||
});
|
||||
|
||||
it("should log an error if emailService.buildAndSendEmail fails", async () => {
|
||||
@@ -165,6 +159,24 @@ describe("Auth Controller - registerUser", () => {
|
||||
).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", () => {
|
||||
@@ -534,14 +546,10 @@ describe("Auth Controller - validateRecovery", () => {
|
||||
});
|
||||
|
||||
it("should reject with an error if validateRecoveryToken fails", async () => {
|
||||
req.db.validateRecoveryToken.rejects(
|
||||
new Error("validateRecoveryToken error")
|
||||
);
|
||||
req.db.validateRecoveryToken.rejects(new Error("validateRecoveryToken error"));
|
||||
await validateRecovery(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.an("error");
|
||||
expect(next.firstCall.args[0].message).to.equal(
|
||||
"validateRecoveryToken error"
|
||||
);
|
||||
expect(next.firstCall.args[0].message).to.equal("validateRecoveryToken error");
|
||||
});
|
||||
|
||||
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(next.calledOnce).to.be.true;
|
||||
expect(next.firstCall.args[0].message).to.equal(
|
||||
errorMessages.DB_USER_NOT_FOUND
|
||||
);
|
||||
expect(next.firstCall.args[0].message).to.equal(errorMessages.DB_USER_NOT_FOUND);
|
||||
expect(res.status.notCalled).to.be.true;
|
||||
expect(res.json.notCalled).to.be.true;
|
||||
});
|
||||
@@ -700,10 +706,9 @@ describe("Auth Controller - deleteUser", () => {
|
||||
).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.deletePageSpeedChecksByMonitorId.calledOnceWith("monitor_id"))
|
||||
.to.be.true;
|
||||
expect(req.db.deleteNotificationsByMonitorId.calledOnceWith("monitor_id"))
|
||||
.to.be.true;
|
||||
expect(req.db.deletePageSpeedChecksByMonitorId.calledOnceWith("monitor_id")).to.be
|
||||
.true;
|
||||
expect(req.db.deleteNotificationsByMonitorId.calledOnceWith("monitor_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.deleteMonitorsByUserId.calledOnceWith("user_id")).to.be.true;
|
||||
|
||||
@@ -204,7 +204,7 @@ describe("controllerUtils - fetchMonitorCertificate", () => {
|
||||
try {
|
||||
await fetchMonitorCertificate(tls, monitor);
|
||||
} 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 { successMessages } from "../../utils/messages.js";
|
||||
import logger from "../../utils/logger.js";
|
||||
import * as monitorController from "../../controllers/monitorController.js";
|
||||
import { fetchMonitorCertificate } from "../../controllers/controllerUtils.js";
|
||||
|
||||
const SERVICE_NAME = "monitorController";
|
||||
|
||||
describe("Monitor Controller - getAllMonitors", () => {
|
||||
@@ -116,96 +119,78 @@ describe("Monitor Controller - getMonitorStatsById", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// 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();
|
||||
// });
|
||||
describe("Monitor Controller - getMonitorCertificate", () => {
|
||||
let req, res, next, fetchMonitorCertificate;
|
||||
beforeEach(() => {
|
||||
req = {
|
||||
params: {
|
||||
monitorId: "123",
|
||||
},
|
||||
query: {},
|
||||
body: {},
|
||||
db: {
|
||||
getMonitorById: sinon.stub(),
|
||||
},
|
||||
};
|
||||
res = {
|
||||
status: sinon.stub().returnsThis(),
|
||||
json: sinon.stub(),
|
||||
};
|
||||
next = sinon.stub();
|
||||
fetchMonitorCertificate = sinon.stub();
|
||||
});
|
||||
|
||||
// afterEach(() => {
|
||||
// sinon.restore();
|
||||
// });
|
||||
afterEach(() => {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
// 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;
|
||||
// });
|
||||
// });
|
||||
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 getMonitorById operation fails", async () => {
|
||||
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 () => {
|
||||
req.db.getMonitorById.returns({ url: "https://www.google.com" });
|
||||
const data = { certificate: "cert", validTo: "2024/08/08" };
|
||||
fetchMonitorCertificate.returns(data);
|
||||
await getMonitorCertificate(req, res, next, fetchMonitorCertificate);
|
||||
expect(res.status.firstCall.args[0]).to.equal(200);
|
||||
expect(
|
||||
res.json.calledOnceWith({
|
||||
success: true,
|
||||
msg: successMessages.MONITOR_CERTIFICATE,
|
||||
data: { certificateDate: new Date(data.validTo) },
|
||||
})
|
||||
).to.be.true;
|
||||
});
|
||||
it("should return success message and data if all operations succeed with an invalid cert", async () => {
|
||||
req.db.getMonitorById.returns({ url: "https://www.google.com" });
|
||||
fetchMonitorCertificate.returns({});
|
||||
await getMonitorCertificate(req, res, next, fetchMonitorCertificate);
|
||||
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 an error if fetchMonitorCertificate fails", async () => {
|
||||
req.db.getMonitorById.returns({ url: "https://www.google.com" });
|
||||
fetchMonitorCertificate.throws(new Error("Certificate error"));
|
||||
await getMonitorCertificate(req, res, next, fetchMonitorCertificate);
|
||||
expect(next.firstCall.args[0]).to.be.an("error");
|
||||
expect(next.firstCall.args[0].message).to.equal("Certificate error");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Monitor Controller - getMonitorById", () => {
|
||||
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}`
|
||||
);
|
||||
});
|
||||
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", () => {
|
||||
|
||||
Reference in new issue
Block a user