Add validateRecovery test

This commit is contained in:
Alex Holliday
2024-10-10 19:18:25 +08:00
parent a430ccc051
commit 2a15c1589e

View File

@@ -4,11 +4,13 @@ const {
editUser,
checkSuperadminExists,
requestRecovery,
validateRecovery,
} = require("../../controllers/authController");
const jwt = require("jsonwebtoken");
const { errorMessages, successMessages } = require("../../utils/messages");
const sinon = require("sinon");
const { before } = require("node:test");
describe("Auth Controller - registerUser", () => {
// Set up test
beforeEach(() => {
@@ -318,3 +320,40 @@ describe("Auth Controller - requestRecovery", async () => {
).to.be.true;
});
});
describe("Auth Controller - validateRecovery", async () => {
beforeEach(() => {
req = {
body: { recoveryToken: "recovery-token" },
db: {
validateRecoveryToken: sinon.stub(),
},
};
res = {
status: sinon.stub().returnsThis(),
json: sinon.stub(),
};
next = sinon.stub();
});
it("should call next with a validation error if the token is invalid", async () => {
req = {
body: {},
};
await validateRecovery(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should return a success message if the token is valid", async () => {
req.db.validateRecoveryToken.resolves();
await validateRecovery(req, res, next);
expect(res.status.calledOnceWith(200)).to.be.true;
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.AUTH_VERIFY_RECOVERY_TOKEN,
})
).to.be.true;
});
});