Merge branch 'develop' into feat/be/esm
This commit is contained in:
commit
81f1fe4694
16 files changed
+305
-186
No files matched your search
@@ -13,30 +13,67 @@ import {
|
||||
import jwt from "jsonwebtoken";
|
||||
import { errorMessages, successMessages } from "../../utils/messages.js";
|
||||
import sinon from "sinon";
|
||||
import { tokenType } from "../../utils/utils.js";
|
||||
import logger from "../../utils/logger.js";
|
||||
|
||||
describe("Auth Controller - issueToken", () => {
|
||||
it("should reject with an error if jwt.sign fails", () => {
|
||||
const error = new Error("jwt.sign error");
|
||||
const stub = sinon.stub(jwt, "sign").throws(error);
|
||||
const payload = { id: "123" };
|
||||
const appSettings = { jwtSecret: "my_secret" };
|
||||
expect(() => issueToken(payload, appSettings)).to.throw(error);
|
||||
stub.restore();
|
||||
let stub;
|
||||
|
||||
afterEach(() => {
|
||||
sinon.restore(); // Restore stubs after each test
|
||||
});
|
||||
|
||||
it("should return a token if jwt.sign is successful and appSettings.jtwTTL is not defined", () => {
|
||||
it("should reject with an error if jwt.sign fails", () => {
|
||||
const error = new Error("jwt.sign error");
|
||||
stub = sinon.stub(jwt, "sign").throws(error);
|
||||
const payload = { id: "123" };
|
||||
const appSettings = { jwtSecret: "my_secret" };
|
||||
const token = issueToken(payload, appSettings);
|
||||
expect(token).to.be.a("string");
|
||||
expect(() => issueToken(payload, tokenType.ACCESS_TOKEN, appSettings)).to.throw(
|
||||
error
|
||||
);
|
||||
});
|
||||
|
||||
it("should return a token if jwt.sign is successful and appSettings.jwtTTL is not defined", () => {
|
||||
const payload = { id: "123" };
|
||||
const appSettings = { jwtSecret: "my_secret" };
|
||||
const expectedToken = "mockToken";
|
||||
|
||||
stub = sinon.stub(jwt, "sign").returns(expectedToken);
|
||||
const token = issueToken(payload, tokenType.ACCESS_TOKEN, appSettings);
|
||||
expect(token).to.equal(expectedToken);
|
||||
});
|
||||
|
||||
it("should return a token if jwt.sign is successful and appSettings.jwtTTL is defined", () => {
|
||||
const payload = { id: "123" };
|
||||
const appSettings = { jwtSecret: "my_secret", jwtTTL: "1s" };
|
||||
const token = issueToken(payload, appSettings);
|
||||
expect(token).to.be.a("string");
|
||||
const expectedToken = "mockToken";
|
||||
|
||||
stub = sinon.stub(jwt, "sign").returns(expectedToken);
|
||||
const token = issueToken(payload, tokenType.ACCESS_TOKEN, appSettings);
|
||||
expect(token).to.equal(expectedToken);
|
||||
});
|
||||
|
||||
it("should return a refresh token if jwt.sign is successful and appSettings.refreshTokenTTL is not defined", () => {
|
||||
const payload = {};
|
||||
const appSettings = { refreshTokenSecret: "my_refresh_secret" };
|
||||
const expectedToken = "mockRefreshToken";
|
||||
|
||||
stub = sinon.stub(jwt, "sign").returns(expectedToken);
|
||||
const token = issueToken(payload, tokenType.REFRESH_TOKEN, appSettings);
|
||||
expect(token).to.equal(expectedToken);
|
||||
});
|
||||
|
||||
it("should return a refresh token if jwt.sign is successful and appSettings.refreshTokenTTL is defined", () => {
|
||||
const payload = {};
|
||||
const appSettings = {
|
||||
refreshTokenSecret: "my_refresh_secret",
|
||||
refreshTokenTTL: "7d",
|
||||
};
|
||||
const expectedToken = "mockRefreshToken";
|
||||
|
||||
stub = sinon.stub(jwt, "sign").returns(expectedToken);
|
||||
const token = issueToken(payload, tokenType.REFRESH_TOKEN, appSettings);
|
||||
expect(token).to.equal(expectedToken);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -62,6 +99,7 @@ describe("Auth Controller - registerUser", () => {
|
||||
settingsService: {
|
||||
getSettings: sinon.stub().resolves({
|
||||
jwtSecret: "my_secret",
|
||||
refreshTokenSecret: "my_refresh_secret",
|
||||
}),
|
||||
},
|
||||
emailService: {
|
||||
@@ -172,7 +210,7 @@ describe("Auth Controller - registerUser", () => {
|
||||
res.json.calledWith({
|
||||
success: true,
|
||||
msg: successMessages.AUTH_CREATE_USER,
|
||||
data: { user, token: sinon.match.string },
|
||||
data: { user, token: sinon.match.string, refreshToken: sinon.match.string },
|
||||
})
|
||||
).to.be.true;
|
||||
expect(next.notCalled).to.be.true;
|
||||
@@ -190,6 +228,7 @@ describe("Auth Controller - loginUser", () => {
|
||||
settingsService: {
|
||||
getSettings: sinon.stub().resolves({
|
||||
jwtSecret: "my_secret",
|
||||
refreshTokenSecret: "my_refresh_token",
|
||||
}),
|
||||
},
|
||||
};
|
||||
@@ -234,6 +273,7 @@ describe("Auth Controller - loginUser", () => {
|
||||
avatarImage: undefined,
|
||||
},
|
||||
token: sinon.match.string,
|
||||
refreshToken: sinon.match.string,
|
||||
},
|
||||
})
|
||||
).to.be.true;
|
||||
|
||||
Reference in new issue
Block a user