nest-phylax
Version:
Security library for NestJS
53 lines • 2.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@nestjs/common");
const jwt_auth_service_1 = require("../../src/jwt-auth/services/jwt.auth.service");
const user_mock_1 = require("../__mocks__/user-mock");
const jsonwebtoken_1 = require("jsonwebtoken");
const jwt_auth_constants_1 = require("../../src/jwt-auth/jwt.auth.constants");
describe('JwtAuthService', () => {
let service = new jwt_auth_service_1.JwtAuthService({
accessTokenConfig: {
secretKey: 'secret',
expiresIn: '1h',
},
refreshTokenConfig: {
secretKey: 'secret',
expiresIn: '1d',
},
}, user_mock_1.USER_REPOSITORY_MOCK, {
encode: () => Promise.resolve('encoded'),
compare: (password, encodedPassword) => Promise.resolve(password === encodedPassword),
});
describe('passwordLogin', () => {
it('should return access token and refresh token', async () => {
const result = await service.passwordLogin({
email: 'mortysmith@gmail.com',
password: 'morty_password',
});
expect(result).toBeDefined();
expect(result.accessToken).toBeDefined();
expect(result.refreshToken).toBeDefined();
});
});
describe('refreshToken', () => {
it('should throw an unauthorized exception if the token is expired', async () => {
const request = service.refreshToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJmY2Y1YmI1Ny03NjQxLTRhNzAtYTEyMS05MTk0ZTlhYmM1MzAiLCJpYXQiOjE3NDY5MTk5NDAsImV4cCI6MTc0NjkyMzU0MH0.l5HXx1DEXnlDevgeYg-XPOunzOOGjziDlzv6oV9BEcE');
expect(request).rejects.toThrow(common_1.UnauthorizedException);
await expect(request).rejects.toMatchObject({
message: jwt_auth_constants_1.TOKEN_EXPIRED_ERROR_MESSAGE,
});
});
it('should throw an unauthorized exception if the token is invalid', async () => {
const invalidToken = (0, jsonwebtoken_1.sign)({}, 'secresdfttg', {
expiresIn: '1h',
});
const request = service.refreshToken(invalidToken);
expect(request).rejects.toThrow(common_1.UnauthorizedException);
await expect(request).rejects.toMatchObject({
message: jwt_auth_constants_1.INVALID_TOKEN_SIGNATURE_ERROR_MESSAGE,
});
});
});
});
//# sourceMappingURL=jwt.auth.service.spec.js.map