fix(test): Use 'npm run lint-fix' to fix 'mocha/no-mocha-arrows' like lint warnings

- ref: https://mochajs.org/#arrow-functions
This commit is contained in:
Mert Şişmanoğlu committed 2024-12-29 14:25:59 +03:00
1 parent 319e6d6017
commit fcda60fe5b
31 files changed
+1268 -907

No files matched your search

+96 -79
View File
@@ -18,14 +18,14 @@ import { getTokenFromHeaders, tokenType } from "../../utils/utils.js";
import logger from "../../utils/logger.js";
import e from "cors";
describe("Auth Controller - issueToken", () => {
describe("Auth Controller - issueToken", function() {
let stub;
afterEach(() => {
afterEach(function() {
sinon.restore(); // Restore stubs after each test
});
it("should reject with an error if jwt.sign fails", () => {
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" };
@@ -35,7 +35,7 @@ describe("Auth Controller - issueToken", () => {
);
});
it("should return a token if jwt.sign is successful and appSettings.jwtTTL is not defined", () => {
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";
@@ -45,7 +45,7 @@ describe("Auth Controller - issueToken", () => {
expect(token).to.equal(expectedToken);
});
it("should return a token if jwt.sign is successful and appSettings.jwtTTL is defined", () => {
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";
@@ -55,7 +55,7 @@ describe("Auth Controller - issueToken", () => {
expect(token).to.equal(expectedToken);
});
it("should return a refresh token if jwt.sign is successful and appSettings.refreshTokenTTL is not defined", () => {
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";
@@ -65,7 +65,7 @@ describe("Auth Controller - issueToken", () => {
expect(token).to.equal(expectedToken);
});
it("should return a refresh token if jwt.sign is successful and appSettings.refreshTokenTTL is defined", () => {
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",
@@ -79,9 +79,10 @@ describe("Auth Controller - issueToken", () => {
});
});
describe("Auth Controller - registerUser", () => {
describe("Auth Controller - registerUser", function() {
let req, res, next;
beforeEach(() => {
beforeEach(function() {
req = {
body: {
firstName: "firstname",
@@ -116,25 +117,26 @@ describe("Auth Controller - registerUser", () => {
next = sinon.stub();
sinon.stub(logger, "error");
});
afterEach(() => {
afterEach(function() {
sinon.restore();
});
it("should reject with an error if body validation fails", async () => {
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 () => {
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 () => {
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);
@@ -142,7 +144,7 @@ describe("Auth Controller - registerUser", () => {
expect(next.firstCall.args[0].message).to.equal("getInviteTokenAndDelete error");
});
it("should reject with an error if updateAppSettings fails", async () => {
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);
@@ -150,7 +152,7 @@ describe("Auth Controller - registerUser", () => {
expect(next.firstCall.args[0].message).to.equal("updateAppSettings error");
});
it("should reject with an error if insertUser fails", async () => {
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"));
@@ -159,7 +161,7 @@ describe("Auth Controller - registerUser", () => {
expect(next.firstCall.args[0].message).to.equal("insertUser error");
});
it("should reject with an error if settingsService.getSettings fails", async () => {
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" });
@@ -171,7 +173,7 @@ describe("Auth Controller - registerUser", () => {
expect(next.firstCall.args[0].message).to.equal("settingsService.getSettings error");
});
it("should log an error if emailService.buildAndSendEmail fails", async () => {
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" });
@@ -184,7 +186,8 @@ describe("Auth Controller - registerUser", () => {
expect(logger.error.calledOnce).to.be.true;
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 () => {
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();
@@ -205,7 +208,8 @@ 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 () => {
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();
@@ -228,9 +232,10 @@ describe("Auth Controller - registerUser", () => {
});
});
describe("Auth Controller - loginUser", () => {
describe("Auth Controller - loginUser", function() {
let req, res, next, user;
beforeEach(() => {
beforeEach(function() {
req = {
body: { email: "test@example.com", password: "Password123!" },
db: {
@@ -255,21 +260,22 @@ describe("Auth Controller - loginUser", () => {
comparePassword: sinon.stub(),
};
});
it("should reject with an error if validation fails", async () => {
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 () => {
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 () => {
it("should login user successfully", async function() {
req.db.getUserByEmail.resolves(user);
user.comparePassword.resolves(true);
await loginUser(req, res, next);
@@ -290,7 +296,8 @@ describe("Auth Controller - loginUser", () => {
).to.be.true;
expect(next.notCalled).to.be.true;
});
it("should reject a user with an incorrect password", async () => {
it("should reject a user with an incorrect password", async function() {
req.body = {
email: "test@test.com",
password: "Password123!",
@@ -305,10 +312,10 @@ describe("Auth Controller - loginUser", () => {
});
});
describe("Auth Controller - refreshAuthToken", () => {
describe("Auth Controller - refreshAuthToken", function() {
let req, res, next, issueTokenStub;
beforeEach(() => {
beforeEach(function() {
req = {
headers: {
"x-refresh-token": "valid_refresh_token",
@@ -332,11 +339,11 @@ describe("Auth Controller - refreshAuthToken", () => {
sinon.replace({ issueToken }, "issueToken", issueTokenStub);
});
afterEach(() => {
afterEach(function() {
sinon.restore();
});
it("should reject if no refresh token is provided", async () => {
it("should reject if no refresh token is provided", async function() {
delete req.headers["x-refresh-token"];
await refreshAuthToken(req, res, next);
@@ -345,7 +352,7 @@ describe("Auth Controller - refreshAuthToken", () => {
expect(next.firstCall.args[0].status).to.equal(401);
});
it("should reject if the refresh token is invalid", async () => {
it("should reject if the refresh token is invalid", async function() {
jwt.verify.yields(new Error("invalid token"));
await refreshAuthToken(req, res, next);
@@ -354,7 +361,7 @@ describe("Auth Controller - refreshAuthToken", () => {
expect(next.firstCall.args[0].status).to.equal(401);
});
it("should reject if the refresh token is expired", async () => {
it("should reject if the refresh token is expired", async function() {
const error = new Error("Token expired");
error.name = "TokenExpiredError";
jwt.verify.yields(error);
@@ -364,7 +371,7 @@ describe("Auth Controller - refreshAuthToken", () => {
expect(next.firstCall.args[0].status).to.equal(401);
});
it("should reject if settingsService.getSettings fails", async () => {
it("should reject if settingsService.getSettings fails", async function() {
req.settingsService.getSettings.rejects(
new Error("settingsService.getSettings error")
);
@@ -374,7 +381,7 @@ describe("Auth Controller - refreshAuthToken", () => {
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 () => {
it("should generate a new auth token if the refresh token is valid", async function() {
const decodedPayload = { expiresIn: "60" };
jwt.verify.callsFake(() => {
return decodedPayload;
@@ -396,9 +403,10 @@ describe("Auth Controller - refreshAuthToken", () => {
});
});
describe("Auth Controller - editUser", async () => {
describe("Auth Controller - editUser", function() {
let req, res, next, stub, user;
beforeEach(() => {
beforeEach(function() {
req = {
params: { userId: "123" },
body: { password: "Password1!", newPassword: "Password2!" },
@@ -419,40 +427,41 @@ describe("Auth Controller - editUser", async () => {
next = sinon.stub();
stub = sinon.stub(jwt, "verify").returns({ email: "test@example.com" });
});
afterEach(() => {
afterEach(function() {
sinon.restore();
stub.restore();
});
it("should reject with an error if param validation fails", async () => {
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 () => {
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 () => {
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 () => {
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 () => {
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")),
});
@@ -461,7 +470,7 @@ describe("Auth Controller - editUser", async () => {
expect(next.firstCall.args[0].message).to.equal("Bad Password Match");
});
it("should reject with an error if user.comparePassword returns false", async () => {
it("should reject with an error if user.comparePassword returns false", async function() {
req.db.getUserByEmail.returns({
comparePassword: sinon.stub().returns(false),
});
@@ -473,7 +482,7 @@ describe("Auth Controller - editUser", async () => {
);
});
it("should edit a user if it receives a proper request", async () => {
it("should edit a user if it receives a proper request", async function() {
const user = {
comparePassword: sinon.stub().resolves(true),
};
@@ -495,7 +504,7 @@ describe("Auth Controller - editUser", async () => {
expect(next.notCalled).to.be.true;
});
it("should edit a user if it receives a proper request and both password fields are undefined", async () => {
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);
@@ -513,7 +522,7 @@ describe("Auth Controller - editUser", async () => {
expect(next.notCalled).to.be.true;
});
it("should reject an edit request if password format is incorrect", async () => {
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),
@@ -527,9 +536,10 @@ describe("Auth Controller - editUser", async () => {
});
});
describe("Auth Controller - checkSuperadminExists", async () => {
describe("Auth Controller - checkSuperadminExists", function() {
let req, res, next;
beforeEach(() => {
beforeEach(function() {
req = {
db: {
checkSuperadmin: sinon.stub(),
@@ -542,14 +552,14 @@ describe("Auth Controller - checkSuperadminExists", async () => {
next = sinon.stub();
});
it("should reject with an error if checkSuperadmin fails", async () => {
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 () => {
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;
@@ -563,7 +573,7 @@ describe("Auth Controller - checkSuperadminExists", async () => {
expect(next.notCalled).to.be.true;
});
it("should return false if a superadmin does not exist", async () => {
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;
@@ -578,9 +588,10 @@ describe("Auth Controller - checkSuperadminExists", async () => {
});
});
describe("Auth Controller - requestRecovery", () => {
describe("Auth Controller - requestRecovery", function() {
let req, res, next;
beforeEach(() => {
beforeEach(function() {
req = {
body: { email: "test@test.com" },
db: {
@@ -601,21 +612,21 @@ describe("Auth Controller - requestRecovery", () => {
next = sinon.stub();
});
it("should reject with an error if validation fails", async () => {
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 () => {
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 () => {
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");
@@ -624,13 +635,14 @@ describe("Auth Controller - requestRecovery", () => {
// );
});
it("should throw an error if the email is not provided", async () => {
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 () => {
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";
@@ -664,9 +676,10 @@ describe("Auth Controller - requestRecovery", () => {
});
});
describe("Auth Controller - validateRecovery", () => {
describe("Auth Controller - validateRecovery", function() {
let req, res, next;
beforeEach(() => {
beforeEach(function() {
req = {
body: { recoveryToken: "recovery-token" },
db: {
@@ -680,21 +693,21 @@ describe("Auth Controller - validateRecovery", () => {
next = sinon.stub();
});
it("should reject with an error if validation fails", async () => {
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 () => {
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 () => {
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;
@@ -708,9 +721,10 @@ describe("Auth Controller - validateRecovery", () => {
});
});
describe("Auth Controller - resetPassword", () => {
describe("Auth Controller - resetPassword", function() {
let req, res, next, newPasswordValidation, handleValidationError, handleError;
beforeEach(() => {
beforeEach(function() {
req = {
body: {
recoveryToken: "recovery-token",
@@ -734,14 +748,15 @@ describe("Auth Controller - resetPassword", () => {
handleValidationError = sinon.stub();
handleError = sinon.stub();
});
it("should reject with an error if validation fails", async () => {
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 () => {
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);
@@ -750,7 +765,7 @@ describe("Auth Controller - resetPassword", () => {
expect(next.firstCall.args[0].message).to.equal("resetPassword error");
});
it("should reset password successfully", async () => {
it("should reset password successfully", async function() {
const user = { _doc: {} };
const appSettings = { jwtSecret: "my_secret" };
const token = "token";
@@ -775,9 +790,10 @@ describe("Auth Controller - resetPassword", () => {
});
});
describe("Auth Controller - deleteUser", () => {
describe("Auth Controller - deleteUser", function() {
let req, res, next, handleError;
beforeEach(() => {
beforeEach(function() {
req = {
headers: {
authorization: "Bearer token",
@@ -809,10 +825,11 @@ describe("Auth Controller - deleteUser", () => {
handleError = sinon.stub();
});
afterEach(() => {
afterEach(function() {
sinon.restore();
});
it("should throw an error if user is not found", async () => {
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));
@@ -825,7 +842,7 @@ describe("Auth Controller - deleteUser", () => {
expect(res.json.notCalled).to.be.true;
});
it("should delete user and associated data if user is superadmin", async () => {
it("should delete user and associated data if user is superadmin", async function() {
const user = {
_id: "user_id",
email: "test@example.com",
@@ -865,7 +882,7 @@ describe("Auth Controller - deleteUser", () => {
expect(next.notCalled).to.be.true;
});
it("should delete user if user is not superadmin", async () => {
it("should delete user if user is not superadmin", async function() {
const user = {
_id: "user_id",
email: "test@example.com",
@@ -895,7 +912,7 @@ describe("Auth Controller - deleteUser", () => {
expect(next.notCalled).to.be.true;
});
it("should handle errors", async () => {
it("should handle errors", async function() {
const error = new Error("Something went wrong");
const SERVICE_NAME = "AuthController";
jwt.decode.returns({ email: "test@example.com" });
@@ -908,10 +925,10 @@ describe("Auth Controller - deleteUser", () => {
});
});
describe("Auth Controller - getAllUsers", () => {
describe("Auth Controller - getAllUsers", function() {
let req, res, next;
beforeEach(() => {
beforeEach(function() {
req = {
db: {
getAllUsers: sinon.stub(),
@@ -924,11 +941,11 @@ describe("Auth Controller - getAllUsers", () => {
next = sinon.stub();
});
afterEach(() => {
afterEach(function() {
sinon.restore(); // Restore the original methods after each test
});
it("should return 200 and all users", async () => {
it("should return 200 and all users", async function() {
const allUsers = [{ id: 1, name: "John Doe" }];
req.db.getAllUsers.resolves(allUsers);
@@ -946,7 +963,7 @@ describe("Auth Controller - getAllUsers", () => {
expect(next.notCalled).to.be.true;
});
it("should call next with error when an exception occurs", async () => {
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);