Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoE105 committed Jul 24, 2023
1 parent 4dfccb6 commit f9f5207
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/cli/test/unit/services/jwt.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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);
});
});

0 comments on commit f9f5207

Please sign in to comment.