fix: revert tests

This commit is contained in:
cihatata authored and cihat committed 2025-02-11 07:43:39 +03:00
1 parent dea8c4fdd8
commit 08933743f1
12 files changed
+617 -645

No files matched your search

+99 -102
View File
@@ -18,16 +18,14 @@ import { getTokenFromHeaders, tokenType } from "../../utils/utils.js";
import logger from "../../utils/logger.js";
import e from "cors";
const mockLanguage = 'en';
describe("Auth Controller - issueToken", function () {
describe("Auth Controller - issueToken", function() {
let stub;
afterEach(function () {
afterEach(function() {
sinon.restore(); // Restore stubs after each test
});
it("should reject with an error if jwt.sign fails", function () {
it("should reject with an error if jwt.sign fails", function() {
const error = new Error("jwt.sign error");
stub = sinon.stub(jwt, "sign").throws(error);
const payload = { id: "123" };
@@ -37,7 +35,7 @@ describe("Auth Controller - issueToken", function () {
);
});
it("should return a token if jwt.sign is successful and appSettings.jwtTTL is not defined", function () {
it("should return a token if jwt.sign is successful and appSettings.jwtTTL is not defined", function() {
const payload = { id: "123" };
const appSettings = { jwtSecret: "my_secret" };
const expectedToken = "mockToken";
@@ -47,7 +45,7 @@ describe("Auth Controller - issueToken", function () {
expect(token).to.equal(expectedToken);
});
it("should return a token if jwt.sign is successful and appSettings.jwtTTL is defined", function () {
it("should return a token if jwt.sign is successful and appSettings.jwtTTL is defined", function() {
const payload = { id: "123" };
const appSettings = { jwtSecret: "my_secret", jwtTTL: "1s" };
const expectedToken = "mockToken";
@@ -57,7 +55,7 @@ describe("Auth Controller - issueToken", function () {
expect(token).to.equal(expectedToken);
});
it("should return a refresh token if jwt.sign is successful and appSettings.refreshTokenTTL is not defined", function () {
it("should return a refresh token if jwt.sign is successful and appSettings.refreshTokenTTL is not defined", function() {
const payload = {};
const appSettings = { refreshTokenSecret: "my_refresh_secret" };
const expectedToken = "mockRefreshToken";
@@ -67,7 +65,7 @@ describe("Auth Controller - issueToken", function () {
expect(token).to.equal(expectedToken);
});
it("should return a refresh token if jwt.sign is successful and appSettings.refreshTokenTTL is defined", function () {
it("should return a refresh token if jwt.sign is successful and appSettings.refreshTokenTTL is defined", function() {
const payload = {};
const appSettings = {
refreshTokenSecret: "my_refresh_secret",
@@ -81,10 +79,10 @@ describe("Auth Controller - issueToken", function () {
});
});
describe("Auth Controller - registerUser", function () {
describe("Auth Controller - registerUser", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
body: {
firstName: "firstname",
@@ -120,25 +118,25 @@ describe("Auth Controller - registerUser", function () {
sinon.stub(logger, "error");
});
afterEach(function () {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if body validation fails", async function () {
it("should reject with an error if body validation fails", async function() {
req.body = {};
await registerUser(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 checkSuperadmin fails", async function () {
it("should reject with an error if checkSuperadmin fails", async function() {
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 function () {
it("should reject with an error if getInviteTokenAndDelete fails", async function() {
req.db.checkSuperadmin.returns(true);
req.db.getInviteTokenAndDelete.throws(new Error("getInviteTokenAndDelete error"));
await registerUser(req, res, next);
@@ -146,7 +144,7 @@ describe("Auth Controller - registerUser", function () {
expect(next.firstCall.args[0].message).to.equal("getInviteTokenAndDelete error");
});
it("should reject with an error if updateAppSettings fails", async function () {
it("should reject with an error if updateAppSettings fails", async function() {
req.db.checkSuperadmin.returns(false);
req.db.updateAppSettings.throws(new Error("updateAppSettings error"));
await registerUser(req, res, next);
@@ -154,7 +152,7 @@ describe("Auth Controller - registerUser", function () {
expect(next.firstCall.args[0].message).to.equal("updateAppSettings error");
});
it("should reject with an error if insertUser fails", async function () {
it("should reject with an error if insertUser fails", async function() {
req.db.checkSuperadmin.resolves(false);
req.db.updateAppSettings.resolves();
req.db.insertUser.rejects(new Error("insertUser error"));
@@ -163,7 +161,7 @@ describe("Auth Controller - registerUser", function () {
expect(next.firstCall.args[0].message).to.equal("insertUser error");
});
it("should reject with an error if settingsService.getSettings fails", async function () {
it("should reject with an error if settingsService.getSettings fails", async function() {
req.db.checkSuperadmin.resolves(false);
req.db.updateAppSettings.resolves();
req.db.insertUser.resolves({ _id: "123" });
@@ -175,7 +173,7 @@ describe("Auth Controller - registerUser", function () {
expect(next.firstCall.args[0].message).to.equal("settingsService.getSettings error");
});
it("should log an error if emailService.buildAndSendEmail fails", async function () {
it("should log an error if emailService.buildAndSendEmail fails", async function() {
req.db.checkSuperadmin.resolves(false);
req.db.updateAppSettings.resolves();
req.db.insertUser.returns({ _id: "123" });
@@ -189,7 +187,7 @@ describe("Auth Controller - registerUser", function () {
expect(logger.error.firstCall.args[0].message).to.equal("emailService error");
});
it("should return a success message and data if all operations are successful", async function () {
it("should return a success message and data if all operations are successful", async function() {
const user = { _id: "123" };
req.db.checkSuperadmin.resolves(false);
req.db.updateAppSettings.resolves();
@@ -204,14 +202,14 @@ describe("Auth Controller - registerUser", function () {
expect(
res.json.calledWith({
success: true,
msg: successMessages.AUTH_CREATE_USER(mockLanguage),
msg: successMessages.AUTH_CREATE_USER,
data: { user, token: sinon.match.string, refreshToken: sinon.match.string },
})
).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 function () {
it("should return a success message and data if all operations are successful and superAdmin true", async function() {
const user = { _id: "123" };
req.db.checkSuperadmin.resolves(true);
req.db.updateAppSettings.resolves();
@@ -226,7 +224,7 @@ describe("Auth Controller - registerUser", function () {
expect(
res.json.calledWith({
success: true,
msg: successMessages.AUTH_CREATE_USER(mockLanguage),
msg: successMessages.AUTH_CREATE_USER,
data: { user, token: sinon.match.string, refreshToken: sinon.match.string },
})
).to.be.true;
@@ -234,16 +232,15 @@ describe("Auth Controller - registerUser", function () {
});
});
describe("Auth Controller - loginUser", function () {
describe("Auth Controller - loginUser", function() {
let req, res, next, user;
beforeEach(function () {
beforeEach(function() {
req = {
body: { email: "test@example.com", password: "Password123!" },
db: {
getUserByEmail: sinon.stub(),
},
language: 'en',
settingsService: {
getSettings: sinon.stub().resolves({
jwtSecret: "my_secret",
@@ -264,21 +261,21 @@ describe("Auth Controller - loginUser", function () {
};
});
it("should reject with an error if validation fails", async function () {
it("should reject with an error if validation fails", async function() {
req.body = {};
await loginUser(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 getUserByEmail fails", async function () {
it("should reject with an error if getUserByEmail fails", async function() {
req.db.getUserByEmail.rejects(new Error("getUserByEmail error"));
await loginUser(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("getUserByEmail error");
});
it("should login user successfully", async function () {
it("should login user successfully", async function() {
req.db.getUserByEmail.resolves(user);
user.comparePassword.resolves(true);
await loginUser(req, res, next);
@@ -286,7 +283,7 @@ describe("Auth Controller - loginUser", function () {
expect(
res.json.calledWith({
success: true,
msg: successMessages.AUTH_LOGIN_USER(mockLanguage),
msg: successMessages.AUTH_LOGIN_USER,
data: {
user: {
email: "test@example.com",
@@ -300,7 +297,7 @@ describe("Auth Controller - loginUser", function () {
expect(next.notCalled).to.be.true;
});
it("should reject a user with an incorrect password", async function () {
it("should reject a user with an incorrect password", async function() {
req.body = {
email: "test@test.com",
password: "Password123!",
@@ -310,15 +307,15 @@ describe("Auth Controller - loginUser", function () {
await loginUser(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal(
errorMessages.AUTH_INCORRECT_PASSWORD(mockLanguage)
errorMessages.AUTH_INCORRECT_PASSWORD
);
});
});
describe("Auth Controller - refreshAuthToken", function () {
describe("Auth Controller - refreshAuthToken", function() {
let req, res, next, issueTokenStub;
beforeEach(function () {
beforeEach(function() {
req = {
headers: {
"x-refresh-token": "valid_refresh_token",
@@ -342,39 +339,39 @@ describe("Auth Controller - refreshAuthToken", function () {
sinon.replace({ issueToken }, "issueToken", issueTokenStub);
});
afterEach(function () {
afterEach(function() {
sinon.restore();
});
it("should reject if no refresh token is provided", async function () {
it("should reject if no refresh token is provided", async function() {
delete req.headers["x-refresh-token"];
await refreshAuthToken(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal(errorMessages.NO_REFRESH_TOKEN(req.language));
expect(next.firstCall.args[0].message).to.equal(errorMessages.NO_REFRESH_TOKEN);
expect(next.firstCall.args[0].status).to.equal(401);
});
it("should reject if the refresh token is invalid", async function () {
it("should reject if the refresh token is invalid", async function() {
jwt.verify.yields(new Error("invalid token"));
await refreshAuthToken(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal(errorMessages.INVALID_REFRESH_TOKEN(req.language));
expect(next.firstCall.args[0].message).to.equal(errorMessages.INVALID_REFRESH_TOKEN);
expect(next.firstCall.args[0].status).to.equal(401);
});
it("should reject if the refresh token is expired", async function () {
it("should reject if the refresh token is expired", async function() {
const error = new Error("Token expired");
error.name = "TokenExpiredError";
jwt.verify.yields(error);
await refreshAuthToken(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal(errorMessages.EXPIRED_REFRESH_TOKEN(req.language));
expect(next.firstCall.args[0].message).to.equal(errorMessages.EXPIRED_REFRESH_TOKEN);
expect(next.firstCall.args[0].status).to.equal(401);
});
it("should reject if settingsService.getSettings fails", async function () {
it("should reject if settingsService.getSettings fails", async function() {
req.settingsService.getSettings.rejects(
new Error("settingsService.getSettings error")
);
@@ -384,7 +381,7 @@ describe("Auth Controller - refreshAuthToken", function () {
expect(next.firstCall.args[0].message).to.equal("settingsService.getSettings error");
});
it("should generate a new auth token if the refresh token is valid", async function () {
it("should generate a new auth token if the refresh token is valid", async function() {
const decodedPayload = { expiresIn: "60" };
jwt.verify.callsFake(() => {
return decodedPayload;
@@ -395,7 +392,7 @@ describe("Auth Controller - refreshAuthToken", function () {
expect(
res.json.calledWith({
success: true,
msg: successMessages.AUTH_TOKEN_REFRESHED(mockLanguage),
msg: successMessages.AUTH_TOKEN_REFRESHED,
data: {
user: decodedPayload,
token: sinon.match.string,
@@ -406,10 +403,10 @@ describe("Auth Controller - refreshAuthToken", function () {
});
});
describe("Auth Controller - editUser", function () {
describe("Auth Controller - editUser", function() {
let req, res, next, stub, user;
beforeEach(function () {
beforeEach(function() {
req = {
params: { userId: "123" },
body: { password: "Password1!", newPassword: "Password2!" },
@@ -431,40 +428,40 @@ describe("Auth Controller - editUser", function () {
stub = sinon.stub(jwt, "verify").returns({ email: "test@example.com" });
});
afterEach(function () {
afterEach(function() {
sinon.restore();
stub.restore();
});
it("should reject with an error if param validation fails", async function () {
it("should reject with an error if param validation fails", async function() {
req.params = {};
await editUser(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 function () {
it("should reject with an error if body validation fails", async function() {
req.body = { invalid: 1 };
await editUser(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 param.userId !== req.user._id", async function () {
it("should reject with an error if param.userId !== req.user._id", async function() {
req.params = { userId: "456" };
await editUser(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(401);
});
it("should reject with an error if !req.body.password and getUserByEmail fails", async function () {
it("should reject with an error if !req.body.password and getUserByEmail fails", async function() {
req.db.getUserByEmail.rejects(new Error("getUserByEmail error"));
await editUser(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("getUserByEmail error");
});
it("should reject with an error if user.comparePassword fails", async function () {
it("should reject with an error if user.comparePassword fails", async function() {
req.db.getUserByEmail.returns({
comparePassword: sinon.stub().rejects(new Error("Bad Password Match")),
});
@@ -473,7 +470,7 @@ describe("Auth Controller - editUser", function () {
expect(next.firstCall.args[0].message).to.equal("Bad Password Match");
});
it("should reject with an error if user.comparePassword returns false", async function () {
it("should reject with an error if user.comparePassword returns false", async function() {
req.db.getUserByEmail.returns({
comparePassword: sinon.stub().returns(false),
});
@@ -481,11 +478,11 @@ describe("Auth Controller - editUser", function () {
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(401);
expect(next.firstCall.args[0].message).to.equal(
errorMessages.AUTH_INCORRECT_PASSWORD(mockLanguage)
errorMessages.AUTH_INCORRECT_PASSWORD
);
});
it("should edit a user if it receives a proper request", async function () {
it("should edit a user if it receives a proper request", async function() {
const user = {
comparePassword: sinon.stub().resolves(true),
};
@@ -500,14 +497,14 @@ describe("Auth Controller - editUser", function () {
expect(
res.json.calledWith({
success: true,
msg: successMessages.AUTH_UPDATE_USER(mockLanguage),
msg: successMessages.AUTH_UPDATE_USER,
data: { email: "test@example.com" },
})
).to.be.true;
expect(next.notCalled).to.be.true;
});
it("should edit a user if it receives a proper request and both password fields are undefined", async function () {
it("should edit a user if it receives a proper request and both password fields are undefined", async function() {
req.body.password = undefined;
req.body.newPassword = undefined;
req.db.getUserByEmail.resolves(user);
@@ -518,14 +515,14 @@ describe("Auth Controller - editUser", function () {
expect(
res.json.calledWith({
success: true,
msg: successMessages.AUTH_UPDATE_USER(mockLanguage),
msg: successMessages.AUTH_UPDATE_USER,
data: { email: "test@example.com" },
})
).to.be.true;
expect(next.notCalled).to.be.true;
});
it("should reject an edit request if password format is incorrect", async function () {
it("should reject an edit request if password format is incorrect", async function() {
req.body = { password: "bad_password", newPassword: "bad_password" };
const user = {
comparePassword: sinon.stub().resolves(true),
@@ -539,10 +536,10 @@ describe("Auth Controller - editUser", function () {
});
});
describe("Auth Controller - checkSuperadminExists", function () {
describe("Auth Controller - checkSuperadminExists", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
db: {
checkSuperadmin: sinon.stub(),
@@ -555,35 +552,35 @@ describe("Auth Controller - checkSuperadminExists", function () {
next = sinon.stub();
});
it("should reject with an error if checkSuperadmin fails", async function () {
it("should reject with an error if checkSuperadmin fails", async function() {
req.db.checkSuperadmin.rejects(new Error("checkSuperadmin error"));
await checkSuperadminExists(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("checkSuperadmin error");
});
it("should return true if a superadmin exists", async function () {
it("should return true if a superadmin exists", async function() {
req.db.checkSuperadmin.resolves(true);
await checkSuperadminExists(req, res, next);
expect(res.status.calledWith(200)).to.be.true;
expect(
res.json.calledWith({
success: true,
msg: successMessages.AUTH_SUPERADMIN_EXISTS(mockLanguage),
msg: successMessages.AUTH_SUPERADMIN_EXISTS,
data: true,
})
).to.be.true;
expect(next.notCalled).to.be.true;
});
it("should return false if a superadmin does not exist", async function () {
it("should return false if a superadmin does not exist", async function() {
req.db.checkSuperadmin.resolves(false);
await checkSuperadminExists(req, res, next);
expect(res.status.calledWith(200)).to.be.true;
expect(
res.json.calledWith({
success: true,
msg: successMessages.AUTH_SUPERADMIN_EXISTS(mockLanguage),
msg: successMessages.AUTH_SUPERADMIN_EXISTS,
data: false,
})
).to.be.true;
@@ -591,10 +588,10 @@ describe("Auth Controller - checkSuperadminExists", function () {
});
});
describe("Auth Controller - requestRecovery", function () {
describe("Auth Controller - requestRecovery", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
body: { email: "test@test.com" },
db: {
@@ -615,21 +612,21 @@ describe("Auth Controller - requestRecovery", function () {
next = sinon.stub();
});
it("should reject with an error if validation fails", async function () {
it("should reject with an error if validation fails", async function() {
req.body = {};
await requestRecovery(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 getUserByEmail fails", async function () {
it("should reject with an error if getUserByEmail fails", async function() {
req.db.getUserByEmail.rejects(new Error("getUserByEmail error"));
await requestRecovery(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("getUserByEmail error");
});
it("should throw an error if the user is not found", async function () {
it("should throw an error if the user is not found", async function() {
req.db.getUserByEmail.resolves(null);
await requestRecovery(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
@@ -638,14 +635,14 @@ describe("Auth Controller - requestRecovery", function () {
// );
});
it("should throw an error if the email is not provided", async function () {
it("should throw an error if the email is not provided", async function() {
req.body = {};
await requestRecovery(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 email is provided", async function () {
it("should return a success message if the email is provided", async function() {
const user = { firstName: "John" };
const recoveryToken = { token: "recovery-token" };
const msgId = "message-id";
@@ -671,7 +668,7 @@ describe("Auth Controller - requestRecovery", function () {
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.AUTH_CREATE_RECOVERY_TOKEN(mockLanguage),
msg: successMessages.AUTH_CREATE_RECOVERY_TOKEN,
data: msgId,
})
).to.be.true;
@@ -679,10 +676,10 @@ describe("Auth Controller - requestRecovery", function () {
});
});
describe("Auth Controller - validateRecovery", function () {
describe("Auth Controller - validateRecovery", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
body: { recoveryToken: "recovery-token" },
db: {
@@ -696,38 +693,38 @@ describe("Auth Controller - validateRecovery", function () {
next = sinon.stub();
});
it("should reject with an error if validation fails", async function () {
it("should reject with an error if validation fails", async function() {
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 reject with an error if validateRecoveryToken fails", async function () {
it("should reject with an error if validateRecoveryToken fails", async function() {
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");
});
it("should return a success message if the token is valid", async function () {
it("should return a success message if the token is valid", async function() {
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(mockLanguage),
msg: successMessages.AUTH_VERIFY_RECOVERY_TOKEN,
})
).to.be.true;
expect(next.notCalled).to.be.true;
});
});
describe("Auth Controller - resetPassword", function () {
describe("Auth Controller - resetPassword", function() {
let req, res, next, newPasswordValidation, handleValidationError, handleError;
beforeEach(function () {
beforeEach(function() {
req = {
body: {
recoveryToken: "recovery-token",
@@ -752,14 +749,14 @@ describe("Auth Controller - resetPassword", function () {
handleError = sinon.stub();
});
it("should reject with an error if validation fails", async function () {
it("should reject with an error if validation fails", async function() {
req.body = { password: "bad_password" };
await resetPassword(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 resetPassword fails", async function () {
it("should reject with an error if resetPassword fails", async function() {
const error = new Error("resetPassword error");
newPasswordValidation.validateAsync.resolves();
req.db.resetPassword.rejects(error);
@@ -768,7 +765,7 @@ describe("Auth Controller - resetPassword", function () {
expect(next.firstCall.args[0].message).to.equal("resetPassword error");
});
it("should reset password successfully", async function () {
it("should reset password successfully", async function() {
const user = { _doc: {} };
const appSettings = { jwtSecret: "my_secret" };
const token = "token";
@@ -785,7 +782,7 @@ describe("Auth Controller - resetPassword", function () {
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.AUTH_RESET_PASSWORD(mockLanguage),
msg: successMessages.AUTH_RESET_PASSWORD,
data: { user: sinon.match.object, token: sinon.match.string },
})
).to.be.true;
@@ -793,10 +790,10 @@ describe("Auth Controller - resetPassword", function () {
});
});
describe("Auth Controller - deleteUser", function () {
describe("Auth Controller - deleteUser", function() {
let req, res, next, handleError;
beforeEach(function () {
beforeEach(function() {
req = {
headers: {
authorization: "Bearer token",
@@ -828,24 +825,24 @@ describe("Auth Controller - deleteUser", function () {
handleError = sinon.stub();
});
afterEach(function () {
afterEach(function() {
sinon.restore();
});
it("should throw an error if user is not found", async function () {
it("should throw an error if user is not found", async function() {
jwt.decode.returns({ email: "test@example.com" });
req.db.getUserByEmail.throws(new Error(errorMessages.DB_USER_NOT_FOUND(req.language)));
req.db.getUserByEmail.throws(new Error(errorMessages.DB_USER_NOT_FOUND));
await deleteUser(req, res, next);
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(req.language));
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;
});
it("should delete user and associated data if user is superadmin", async function () {
it("should delete user and associated data if user is superadmin", async function() {
const user = {
_id: "user_id",
email: "test@example.com",
@@ -879,13 +876,13 @@ describe("Auth Controller - deleteUser", function () {
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.AUTH_DELETE_USER(mockLanguage),
msg: successMessages.AUTH_DELETE_USER,
})
).to.be.true;
expect(next.notCalled).to.be.true;
});
it("should delete user if user is not superadmin", async function () {
it("should delete user if user is not superadmin", async function() {
const user = {
_id: "user_id",
email: "test@example.com",
@@ -909,13 +906,13 @@ describe("Auth Controller - deleteUser", function () {
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.AUTH_DELETE_USER(mockLanguage),
msg: successMessages.AUTH_DELETE_USER,
})
).to.be.true;
expect(next.notCalled).to.be.true;
});
it("should handle errors", async function () {
it("should handle errors", async function() {
const error = new Error("Something went wrong");
const SERVICE_NAME = "AuthController";
jwt.decode.returns({ email: "test@example.com" });
@@ -928,10 +925,10 @@ describe("Auth Controller - deleteUser", function () {
});
});
describe("Auth Controller - getAllUsers", function () {
describe("Auth Controller - getAllUsers", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
db: {
getAllUsers: sinon.stub(),
@@ -944,11 +941,11 @@ describe("Auth Controller - getAllUsers", function () {
next = sinon.stub();
});
afterEach(function () {
afterEach(function() {
sinon.restore(); // Restore the original methods after each test
});
it("should return 200 and all users", async function () {
it("should return 200 and all users", async function() {
const allUsers = [{ id: 1, name: "John Doe" }];
req.db.getAllUsers.resolves(allUsers);
@@ -966,7 +963,7 @@ describe("Auth Controller - getAllUsers", function () {
expect(next.notCalled).to.be.true;
});
it("should call next with error when an exception occurs", async function () {
it("should call next with error when an exception occurs", async function() {
const error = new Error("Something went wrong");
req.db.getAllUsers.rejects(error);
await getAllUsers(req, res, next);
@@ -9,12 +9,11 @@ import {
import jwt from "jsonwebtoken";
import { errorMessages, successMessages } from "../../utils/messages.js";
import sinon from "sinon";
describe("Check Controller - createCheck", function () {
describe("Check Controller - createCheck", function() {
let req, res, next, handleError;
beforeEach(function () {
beforeEach(function() {
req = {
language: 'en',
params: {},
body: {},
db: {
@@ -29,17 +28,17 @@ describe("Check Controller - createCheck", function () {
handleError = sinon.stub();
});
afterEach(function () {
afterEach(function() {
sinon.restore(); // Restore the original methods after each test
});
it("should reject with a validation if params are invalid", async function () {
it("should reject with a validation if params are invalid", async function() {
await createCheck(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 a validation error if body is invalid", async function () {
it("should reject with a validation error if body is invalid", async function() {
req.params = {
monitorId: "monitorId",
};
@@ -48,7 +47,7 @@ describe("Check Controller - createCheck", function () {
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should call next with error if data retrieval fails", async function () {
it("should call next with error if data retrieval fails", async function() {
req.params = {
monitorId: "monitorId",
};
@@ -64,7 +63,7 @@ describe("Check Controller - createCheck", function () {
expect(next.firstCall.args[0]).to.be.an("error");
});
it("should return a success message if check is created", async function () {
it("should return a success message if check is created", async function() {
req.params = {
monitorId: "monitorId",
};
@@ -81,7 +80,7 @@ describe("Check Controller - createCheck", function () {
expect(
res.json.calledWith({
success: true,
msg: successMessages.CHECK_CREATE(req.language),
msg: successMessages.CHECK_CREATE,
data: { id: "123" },
})
).to.be.true;
@@ -89,10 +88,10 @@ describe("Check Controller - createCheck", function () {
});
});
describe("Check Controller - getChecks", function () {
describe("Check Controller - getChecks", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
params: {},
query: {},
@@ -108,17 +107,17 @@ describe("Check Controller - getChecks", function () {
next = sinon.stub();
});
afterEach(function () {
afterEach(function() {
sinon.restore();
});
it("should reject with a validation error if params are invalid", async function () {
it("should reject with a validation error if params are invalid", async function() {
await getChecks(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 checks are found", async function () {
it("should return a success message if checks are found", async function() {
req.params = {
monitorId: "monitorId",
};
@@ -129,14 +128,14 @@ describe("Check Controller - getChecks", function () {
expect(
res.json.calledWith({
success: true,
msg: successMessages.CHECK_GET(req.language),
msg: successMessages.CHECK_GET,
data: { checksCount: 1, checks: [{ id: "123" }] },
})
).to.be.true;
expect(next.notCalled).to.be.true;
});
it("should call next with error if data retrieval fails", async function () {
it("should call next with error if data retrieval fails", async function() {
req.params = {
monitorId: "monitorId",
};
@@ -146,10 +145,10 @@ describe("Check Controller - getChecks", function () {
});
});
describe("Check Controller - getTeamChecks", function () {
describe("Check Controller - getTeamChecks", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
params: {},
query: {},
@@ -164,17 +163,17 @@ describe("Check Controller - getTeamChecks", function () {
next = sinon.stub();
});
afterEach(function () {
afterEach(function() {
sinon.restore();
});
it("should reject with a validation error if params are invalid", async function () {
it("should reject with a validation error if params are invalid", async function() {
await getTeamChecks(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should return 200 and check data on successful validation and data retrieval", async function () {
it("should return 200 and check data on successful validation and data retrieval", async function() {
req.params = { teamId: "1" };
const checkData = [{ id: 1, name: "Check 1" }];
req.db.getTeamChecks.resolves(checkData);
@@ -185,13 +184,13 @@ describe("Check Controller - getTeamChecks", function () {
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.CHECK_GET(req.language),
msg: successMessages.CHECK_GET,
data: checkData,
})
).to.be.true;
});
it("should call next with error if data retrieval fails", async function () {
it("should call next with error if data retrieval fails", async function() {
req.params = { teamId: "1" };
req.db.getTeamChecks.rejects(new Error("Retrieval Error"));
await getTeamChecks(req, res, next);
@@ -202,10 +201,10 @@ describe("Check Controller - getTeamChecks", function () {
});
});
describe("Check Controller - deleteChecks", function () {
describe("Check Controller - deleteChecks", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
params: {},
db: {
@@ -219,17 +218,17 @@ describe("Check Controller - deleteChecks", function () {
next = sinon.stub();
});
afterEach(function () {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if param validation fails", async function () {
it("should reject with an error if param validation fails", async function() {
await deleteChecks(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should call next with error if data retrieval fails", async function () {
it("should call next with error if data retrieval fails", async function() {
req.params = { monitorId: "1" };
req.db.deleteChecks.rejects(new Error("Deletion Error"));
await deleteChecks(req, res, next);
@@ -239,7 +238,7 @@ describe("Check Controller - deleteChecks", function () {
expect(res.json.notCalled).to.be.true;
});
it("should delete checks successfully", async function () {
it("should delete checks successfully", async function() {
req.params = { monitorId: "123" };
req.db.deleteChecks.resolves(1);
await deleteChecks(req, res, next);
@@ -248,17 +247,17 @@ describe("Check Controller - deleteChecks", function () {
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.CHECK_DELETE(req.language),
msg: successMessages.CHECK_DELETE,
data: { deletedCount: 1 },
})
).to.be.true;
});
});
describe("Check Controller - deleteChecksByTeamId", function () {
describe("Check Controller - deleteChecksByTeamId", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
params: {},
db: {
@@ -272,17 +271,17 @@ describe("Check Controller - deleteChecksByTeamId", function () {
next = sinon.stub();
});
afterEach(function () {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if param validation fails", async function () {
it("should reject with an error if param validation fails", async function() {
await deleteChecksByTeamId(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should call next with error if data retrieval fails", async function () {
it("should call next with error if data retrieval fails", async function() {
req.params = { teamId: "1" };
req.db.deleteChecksByTeamId.rejects(new Error("Deletion Error"));
await deleteChecksByTeamId(req, res, next);
@@ -292,7 +291,7 @@ describe("Check Controller - deleteChecksByTeamId", function () {
expect(res.json.notCalled).to.be.true;
});
it("should delete checks successfully", async function () {
it("should delete checks successfully", async function() {
req.params = { teamId: "123" };
req.db.deleteChecksByTeamId.resolves(1);
await deleteChecksByTeamId(req, res, next);
@@ -301,17 +300,17 @@ describe("Check Controller - deleteChecksByTeamId", function () {
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.CHECK_DELETE(req.language),
msg: successMessages.CHECK_DELETE,
data: { deletedCount: 1 },
})
).to.be.true;
});
});
describe("Check Controller - updateCheckTTL", function () {
describe("Check Controller - updateCheckTTL", function() {
let stub, req, res, next;
beforeEach(function () {
beforeEach(function() {
stub = sinon.stub(jwt, "verify").callsFake(() => {
return { teamId: "123" };
});
@@ -333,18 +332,18 @@ describe("Check Controller - updateCheckTTL", function () {
next = sinon.stub();
});
afterEach(function () {
afterEach(function() {
sinon.restore();
stub.restore();
});
it("should reject if body validation fails", async function () {
it("should reject if body validation fails", async function() {
await updateChecksTTL(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should throw a JwtError if verification fails", async function () {
it("should throw a JwtError if verification fails", async function() {
stub.restore();
req.body = {
ttl: 1,
@@ -353,7 +352,7 @@ describe("Check Controller - updateCheckTTL", function () {
expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError);
});
it("should call next with error if data retrieval fails", async function () {
it("should call next with error if data retrieval fails", async function() {
req.body = {
ttl: 1,
};
@@ -362,7 +361,7 @@ describe("Check Controller - updateCheckTTL", function () {
expect(next.firstCall.args[0]).to.be.an("error");
});
it("should update TTL successfully", async function () {
it("should update TTL successfully", async function() {
req.body = {
ttl: 1,
};
@@ -373,7 +372,7 @@ describe("Check Controller - updateCheckTTL", function () {
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.CHECK_UPDATE_TTL(req.language),
msg: successMessages.CHECK_UPDATE_TTL,
})
).to.be.true;
});
@@ -11,12 +11,11 @@ import jwt from "jsonwebtoken";
import { successMessages } from "../../utils/messages.js";
import sinon from "sinon";
describe("maintenanceWindowController - createMaintenanceWindows", function () {
describe("maintenanceWindowController - createMaintenanceWindows", function() {
let req, res, next, stub;
beforeEach(function () {
beforeEach(function() {
req = {
language: 'en',
body: {
monitors: ["66ff52e7c5911c61698ac724"],
name: "window",
@@ -42,11 +41,11 @@ describe("maintenanceWindowController - createMaintenanceWindows", function () {
next = sinon.stub();
});
afterEach(function () {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if body validation fails", async function () {
it("should reject with an error if body validation fails", async function() {
stub = sinon.stub(jwt, "verify").callsFake(() => {
return { teamId: "123" };
});
@@ -57,14 +56,14 @@ describe("maintenanceWindowController - createMaintenanceWindows", function () {
stub.restore();
});
it("should reject with an error if jwt.verify fails", async function () {
it("should reject with an error if jwt.verify fails", async function() {
stub = sinon.stub(jwt, "verify").throws(new jwt.JsonWebTokenError());
await createMaintenanceWindows(req, res, next);
expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError);
stub.restore();
});
it("should reject with an error DB operations fail", async function () {
it("should reject with an error DB operations fail", async function() {
stub = sinon.stub(jwt, "verify").callsFake(() => {
return { teamId: "123" };
});
@@ -75,7 +74,7 @@ describe("maintenanceWindowController - createMaintenanceWindows", function () {
stub.restore();
});
it("should return success message if all operations are successful", async function () {
it("should return success message if all operations are successful", async function() {
stub = sinon.stub(jwt, "verify").callsFake(() => {
return { teamId: "123" };
});
@@ -84,13 +83,13 @@ describe("maintenanceWindowController - createMaintenanceWindows", function () {
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.MAINTENANCE_WINDOW_CREATE(req.language),
msg: successMessages.MAINTENANCE_WINDOW_CREATE,
})
).to.be.true;
stub.restore();
});
it("should return success message if all operations are successful with active set to undefined", async function () {
it("should return success message if all operations are successful with active set to undefined", async function() {
req.body.active = undefined;
stub = sinon.stub(jwt, "verify").callsFake(() => {
return { teamId: "123" };
@@ -100,17 +99,17 @@ describe("maintenanceWindowController - createMaintenanceWindows", function () {
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.MAINTENANCE_WINDOW_CREATE(req.language),
msg: successMessages.MAINTENANCE_WINDOW_CREATE,
})
).to.be.true;
stub.restore();
});
});
describe("maintenanceWindowController - getMaintenanceWindowById", function () {
describe("maintenanceWindowController - getMaintenanceWindowById", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
body: {},
params: {
@@ -122,7 +121,6 @@ describe("maintenanceWindowController - getMaintenanceWindowById", function () {
settingsService: {
getSettings: sinon.stub().returns({ jwtSecret: "jwtSecret" }),
},
language: 'en',
db: {
getMaintenanceWindowById: sinon.stub(),
},
@@ -134,38 +132,38 @@ describe("maintenanceWindowController - getMaintenanceWindowById", function () {
next = sinon.stub();
});
it("should reject if param validation fails", async function () {
it("should reject if param validation fails", async function() {
req.params = {};
await getMaintenanceWindowById(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject if DB operations fail", async function () {
it("should reject if DB operations fail", async function() {
req.db.getMaintenanceWindowById.throws(new Error("DB error"));
await getMaintenanceWindowById(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 with data if all operations are successful", async function () {
it("should return success message with data if all operations are successful", async function() {
req.db.getMaintenanceWindowById.returns({ id: "123" });
await getMaintenanceWindowById(req, res, next);
expect(res.status.firstCall.args[0]).to.equal(200);
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID(req.language),
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID,
data: { id: "123" },
})
).to.be.true;
});
});
describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function () {
describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function() {
let req, res, next, stub;
beforeEach(function () {
beforeEach(function() {
req = {
body: {},
params: {},
@@ -179,7 +177,6 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
db: {
getMaintenanceWindowsByTeamId: sinon.stub(),
},
language: 'en',
};
res = {
status: sinon.stub().returnsThis(),
@@ -188,7 +185,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
next = sinon.stub();
});
it("should reject if query validation fails", async function () {
it("should reject if query validation fails", async function() {
req.query = {
invalid: 1,
};
@@ -197,14 +194,14 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject if jwt.verify fails", async function () {
it("should reject if jwt.verify fails", async function() {
stub = sinon.stub(jwt, "verify").throws(new jwt.JsonWebTokenError());
await getMaintenanceWindowsByTeamId(req, res, next);
expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError);
stub.restore();
});
it("should reject with an error if DB operations fail", async function () {
it("should reject with an error if DB operations fail", async function() {
stub = sinon.stub(jwt, "verify").callsFake(() => {
return { teamId: "123" };
});
@@ -215,7 +212,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
stub.restore();
});
it("should return success message with data if all operations are successful", async function () {
it("should return success message with data if all operations are successful", async function() {
stub = sinon.stub(jwt, "verify").callsFake(() => {
return { teamId: "123" };
});
@@ -225,7 +222,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM(req.language),
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM,
data: [{ id: jwt.verify().teamId }],
})
).to.be.true;
@@ -233,10 +230,10 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
});
});
describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", function () {
describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
body: {},
params: {
@@ -260,25 +257,25 @@ describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", funct
next = sinon.stub();
});
afterEach(function () {
afterEach(function() {
sinon.restore();
});
it("should reject if param validation fails", async function () {
it("should reject if param validation fails", async function() {
req.params = {};
await getMaintenanceWindowsByMonitorId(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 function () {
it("should reject with an error if DB operations fail", async function() {
req.db.getMaintenanceWindowsByMonitorId.throws(new Error("DB error"));
await getMaintenanceWindowsByMonitorId(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 with data if all operations are successful", async function () {
it("should return success message with data if all operations are successful", async function() {
const data = [{ monitorId: "123" }];
req.db.getMaintenanceWindowsByMonitorId.returns(data);
await getMaintenanceWindowsByMonitorId(req, res, next);
@@ -287,17 +284,17 @@ describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", funct
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_MONITOR(req.language),
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_MONITOR,
data: data,
})
).to.be.true;
});
});
describe("maintenanceWindowController - deleteMaintenanceWindow", function () {
describe("maintenanceWindowController - deleteMaintenanceWindow", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
body: {},
params: {
@@ -321,47 +318,46 @@ describe("maintenanceWindowController - deleteMaintenanceWindow", function () {
next = sinon.stub();
});
afterEach(function () {
afterEach(function() {
sinon.restore();
});
it("should reject if param validation fails", async function () {
it("should reject if param validation fails", async function() {
req.params = {};
await deleteMaintenanceWindow(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 function () {
it("should reject with an error if DB operations fail", async function() {
req.db.deleteMaintenanceWindowById.throws(new Error("DB error"));
await deleteMaintenanceWindow(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 if all operations are successful", async function () {
it("should return success message if all operations are successful", async function() {
await deleteMaintenanceWindow(req, res, next);
expect(req.db.deleteMaintenanceWindowById.calledOnceWith(req.params.id));
expect(res.status.firstCall.args[0]).to.equal(200);
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.MAINTENANCE_WINDOW_DELETE(req.language),
msg: successMessages.MAINTENANCE_WINDOW_DELETE,
})
).to.be.true;
});
});
describe("maintenanceWindowController - editMaintenanceWindow", function () {
describe("maintenanceWindowController - editMaintenanceWindow", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
body: {
active: true,
name: "test",
},
language: 'en',
params: {
id: "123",
},
@@ -383,32 +379,32 @@ describe("maintenanceWindowController - editMaintenanceWindow", function () {
next = sinon.stub();
});
afterEach(function () {
afterEach(function() {
sinon.restore();
});
it("should reject if param validation fails", async function () {
it("should reject if param validation fails", async function() {
req.params = {};
await editMaintenanceWindow(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
it("should reject if body validation fails", async function () {
it("should reject if body validation fails", async function() {
req.body = { invalid: 1 };
await editMaintenanceWindow(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 function () {
it("should reject with an error if DB operations fail", async function() {
req.db.editMaintenanceWindowById.throws(new Error("DB error"));
await editMaintenanceWindow(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 with data if all operations are successful", async function () {
it("should return success message with data if all operations are successful", async function() {
const data = { id: "123" };
req.db.editMaintenanceWindowById.returns(data);
@@ -418,7 +414,7 @@ describe("maintenanceWindowController - editMaintenanceWindow", function () {
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.MAINTENANCE_WINDOW_EDIT(req.language),
msg: successMessages.MAINTENANCE_WINDOW_EDIT,
data: data,
})
).to.be.true;
File diff suppressed because it is too large. Load diff
@@ -8,10 +8,10 @@ import {
import { successMessages } from "../../utils/messages.js";
import sinon from "sinon";
describe("Queue Controller - getMetrics", function () {
describe("Queue Controller - getMetrics", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
headers: {},
params: {},
@@ -32,14 +32,14 @@ describe("Queue Controller - getMetrics", function () {
sinon.restore();
});
it("should throw an error if getMetrics throws an error", async function () {
it("should throw an error if getMetrics throws an error", async function() {
req.jobQueue.getMetrics.throws(new Error("getMetrics error"));
await getMetrics(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("getMetrics error");
});
it("should return a success message and data if getMetrics is successful", async function () {
it("should return a success message and data if getMetrics is successful", async function() {
const data = { data: "metrics" };
req.jobQueue.getMetrics.returns(data);
await getMetrics(req, res, next);
@@ -52,10 +52,10 @@ describe("Queue Controller - getMetrics", function () {
});
});
describe("Queue Controller - getJobs", function () {
describe("Queue Controller - getJobs", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
headers: {},
params: {},
@@ -76,14 +76,14 @@ describe("Queue Controller - getJobs", function () {
sinon.restore();
});
it("should reject with an error if getJobs throws an error", async function () {
it("should reject with an error if getJobs throws an error", async function() {
req.jobQueue.getJobStats.throws(new Error("getJobs error"));
await getJobs(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("getJobs error");
});
it("should return a success message and data if getJobs is successful", async function () {
it("should return a success message and data if getJobs is successful", async function() {
const data = { data: "jobs" };
req.jobQueue.getJobStats.returns(data);
await getJobs(req, res, next);
@@ -96,10 +96,10 @@ describe("Queue Controller - getJobs", function () {
});
});
describe("Queue Controller - addJob", function () {
describe("Queue Controller - addJob", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
headers: {},
params: {},
@@ -120,14 +120,14 @@ describe("Queue Controller - addJob", function () {
sinon.restore();
});
it("should reject with an error if addJob throws an error", async function () {
it("should reject with an error if addJob throws an error", async function() {
req.jobQueue.addJob.throws(new Error("addJob error"));
await addJob(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("addJob error");
});
it("should return a success message if addJob is successful", async function () {
it("should return a success message if addJob is successful", async function() {
req.jobQueue.addJob.resolves();
await addJob(req, res, next);
expect(res.status.firstCall.args[0]).to.equal(200);
@@ -138,10 +138,10 @@ describe("Queue Controller - addJob", function () {
});
});
describe("Queue Controller - obliterateQueue", function () {
describe("Queue Controller - obliterateQueue", function() {
let req, res, next;
beforeEach(function () {
beforeEach(function() {
req = {
headers: {},
params: {},
@@ -162,14 +162,14 @@ describe("Queue Controller - obliterateQueue", function () {
sinon.restore();
});
it("should reject with an error if obliterateQueue throws an error", async function () {
it("should reject with an error if obliterateQueue throws an error", async function() {
req.jobQueue.obliterate.throws(new Error("obliterateQueue error"));
await obliterateQueue(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("obliterateQueue error");
});
it("should return a success message if obliterateQueue is successful", async function () {
it("should return a success message if obliterateQueue is successful", async function() {
req.jobQueue.obliterate.resolves();
await obliterateQueue(req, res, next);
expect(res.status.firstCall.args[0]).to.equal(200);