n8n/packages/cli/test/unit/services/jwt.service.test.ts
Ricardo Espinoza 89f44021b9
fix(core): Use JWT as reset password token (#6714)
* use jwt to reset password

* increase expiration time to 1d

* drop user id query string

* refactor

* use service instead of package in tests

* sqlite migration

* postgres migration

* mysql migration

* remove unused properties

* remove userId from FE

* fix test for users.api

* move migration to the common folder

* move type assertion to the jwt.service

* Add jwt secret as a readonly property

* use signData instead of sign in user.controller

* remove base class

* remove base class

* add tests
2023-07-24 17:40:17 -04:00

43 lines
1.1 KiB
TypeScript

import config from '@/config';
import { JwtService } from '@/services/jwt.service';
import { randomString } from '../../integration/shared/random';
import * as jwt from 'jsonwebtoken';
describe('JwtService', () => {
config.set('userManagement.jwtSecret', randomString(5, 10));
const jwtService = new JwtService();
beforeEach(() => {
jest.clearAllMocks();
});
test('Should sign input with user management secret', async () => {
const userId = 1;
const token = jwtService.signData({ sub: userId });
expect(typeof token).toBe('string');
const secret = config.get('userManagement.jwtSecret');
const decodedToken = jwt.verify(token, secret);
expect(decodedToken).toHaveProperty('sub');
expect(decodedToken).toHaveProperty('iat');
expect(decodedToken?.sub).toBe(userId);
});
test('Should verify token with user management secret', async () => {
const userId = 1;
const secret = config.get('userManagement.jwtSecret');
const token = jwt.sign({ sub: userId }, secret);
const decodedToken = jwt.verify(token, secret);
expect(decodedToken).toHaveProperty('sub');
expect(decodedToken?.sub).toBe(userId);
});
});