forked from heusalagroup/fi.hg.backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJwtEncodeServiceImpl.test.ts
117 lines (94 loc) · 4.25 KB
/
JwtEncodeServiceImpl.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2023. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import { JwtEngine } from "../core/jwt/JwtEngine";
import { JwtEncodeServiceImpl } from './JwtEncodeServiceImpl';
import { sign as mockJwsSign, verify as mockJwsVerify } from 'jws';
jest.mock('jws', () => ({
sign: jest.fn(),
verify: jest.fn()
}));
describe('JwtEncodeService', () => {
let service: JwtEncodeServiceImpl;
const secret = 'secret';
const defaultAlgorithm = 'HS256';
beforeEach(() => {
service = JwtEncodeServiceImpl.create();
});
describe('getDefaultAlgorithm', () => {
it('should return the default algorithm', () => {
expect(service.getDefaultAlgorithm()).toEqual(defaultAlgorithm);
});
});
describe('setDefaultAlgorithm', () => {
it('should set the default algorithm', () => {
const newAlgorithm = 'RS256';
service.setDefaultAlgorithm(newAlgorithm);
expect(service.getDefaultAlgorithm()).toEqual(newAlgorithm);
});
});
describe('createJwtEngine', () => {
let engine : JwtEngine;
beforeEach(() => {
engine = service.createJwtEngine(secret);
});
it('should create a JwtEngine', () => {
expect(engine).toBeTruthy();
});
describe('JwtEngine', () => {
const payload = {
aud: 'testAud',
exp: 123456,
};
describe('getDefaultAlgorithm', () => {
it('should return the default algorithm', () => {
expect(engine.getDefaultAlgorithm()).toEqual(defaultAlgorithm);
});
});
describe('setDefaultAlgorithm', () => {
it('should set the default algorithm', () => {
const newAlgorithm = 'RS256';
engine.setDefaultAlgorithm(newAlgorithm);
expect(engine.getDefaultAlgorithm()).toEqual(newAlgorithm);
});
});
describe('sign', () => {
it('should sign the payload with the secret and the default algorithm', () => {
(mockJwsSign as any).mockReturnValue('token');
const token = engine.sign(payload);
expect(mockJwsSign).toHaveBeenCalledWith({
header: { alg: defaultAlgorithm },
payload: payload,
secret: secret,
});
expect(token).toBeTruthy(); // replace with actual expected result
});
it('should sign the payload with the secret and a specified algorithm', () => {
(mockJwsSign as any).mockReturnValue('token');
const newAlgorithm = 'RS256';
const token = engine.sign(payload, newAlgorithm);
expect(mockJwsSign).toHaveBeenCalledWith({
header: { alg: newAlgorithm },
payload: payload,
secret: secret,
});
expect(token).toBeTruthy(); // replace with actual expected result
});
});
describe('verify', () => {
const token = 'mockToken'; // replace with actual mock token
it('should verify the token with the secret and the default algorithm', () => {
(mockJwsVerify as any).mockReturnValue(true);
const result = engine.verify(token);
expect(mockJwsVerify).toHaveBeenCalledWith(token, defaultAlgorithm, secret);
expect(result).toBeTruthy(); // replace with actual expected result
});
it('should verify the token with the secret and a specified algorithm', () => {
(mockJwsVerify as any).mockReturnValue(true);
const newAlgorithm = 'RS256';
const result = engine.verify(token, newAlgorithm);
expect(mockJwsVerify).toHaveBeenCalledWith(token, newAlgorithm, secret);
expect(result).toBeTruthy(); // replace with actual expected result
});
});
});
});
});