1
+ import { readFileSpy , ACCESS_TOKEN } from './mock' ;
2
+ import JWT , { JwtPayload , ValidationParams } from '../../../src/core/JWT' ;
3
+ import { BadTokenError , TokenExpiredError } from '../../../src/core/ApiError' ;
4
+
5
+ describe ( 'JWT class tests' , ( ) => {
6
+
7
+ const issuer = 'issuer' ;
8
+ const audience = 'audience' ;
9
+ const subject = 'subject' ;
10
+ const param = 'param' ;
11
+ const validity = 1 ;
12
+
13
+ it ( 'Should throw error for invalid token in JWT.decode' , async ( ) => {
14
+
15
+ beforeEach ( ( ) => {
16
+ readFileSpy . mockClear ( ) ;
17
+ } ) ;
18
+
19
+ try {
20
+ await JWT . decode ( 'abc' , new ValidationParams ( issuer , audience , subject ) ) ;
21
+ } catch ( e ) {
22
+ expect ( e ) . toBeInstanceOf ( BadTokenError ) ;
23
+ }
24
+
25
+ expect ( readFileSpy ) . toBeCalledTimes ( 1 ) ;
26
+ } ) ;
27
+
28
+ it ( 'Should generate a token for JWT.encode' , async ( ) => {
29
+
30
+ beforeEach ( ( ) => {
31
+ readFileSpy . mockClear ( ) ;
32
+ } ) ;
33
+
34
+ const payload = new JwtPayload ( issuer , audience , subject , param , validity ) ;
35
+ const token = await JWT . encode ( payload ) ;
36
+
37
+ expect ( typeof token ) . toBe ( 'string' ) ;
38
+ expect ( readFileSpy ) . toBeCalledTimes ( 1 ) ;
39
+ } ) ;
40
+
41
+ it ( 'Should decode a valid token for JWT.decode' , async ( ) => {
42
+
43
+ beforeEach ( ( ) => {
44
+ readFileSpy . mockClear ( ) ;
45
+ } ) ;
46
+
47
+ const payload = new JwtPayload ( issuer , audience , subject , param , validity ) ;
48
+ const token = await JWT . encode ( payload ) ;
49
+ const decoded = await JWT . decode ( token , new ValidationParams ( issuer , audience , subject ) ) ;
50
+
51
+ expect ( decoded ) . toMatchObject ( payload ) ;
52
+ expect ( readFileSpy ) . toBeCalledTimes ( 2 ) ;
53
+ } ) ;
54
+
55
+ it ( 'Should parse an expired token for JWT.decode' , async ( ) => {
56
+
57
+ beforeEach ( ( ) => {
58
+ readFileSpy . mockClear ( ) ;
59
+ } ) ;
60
+
61
+ const time = Math . floor ( Date . now ( ) / 1000 ) ;
62
+
63
+ const payload = < JwtPayload > {
64
+ aud : audience ,
65
+ sub : subject ,
66
+ iss : issuer ,
67
+ iat : time ,
68
+ exp : time ,
69
+ prm : param ,
70
+ } ;
71
+ const token = await JWT . encode ( payload ) ;
72
+ const decoded = await JWT . decode ( token , new ValidationParams ( issuer , audience , subject ) ) ;
73
+
74
+ expect ( decoded ) . toMatchObject ( payload ) ;
75
+ expect ( readFileSpy ) . toBeCalledTimes ( 2 ) ;
76
+ } ) ;
77
+
78
+ it ( 'Should throw error for invalid token in JWT.validate' , async ( ) => {
79
+
80
+ beforeEach ( ( ) => {
81
+ readFileSpy . mockClear ( ) ;
82
+ } ) ;
83
+
84
+ try {
85
+ await JWT . validate ( 'abc' , new ValidationParams ( issuer , audience , subject ) ) ;
86
+ } catch ( e ) {
87
+ expect ( e ) . toBeInstanceOf ( BadTokenError ) ;
88
+ }
89
+
90
+ expect ( readFileSpy ) . toBeCalledTimes ( 1 ) ;
91
+ } ) ;
92
+
93
+ it ( 'Should validate a valid token for JWT.validate' , async ( ) => {
94
+
95
+ beforeEach ( ( ) => {
96
+ readFileSpy . mockClear ( ) ;
97
+ } ) ;
98
+
99
+ const payload = new JwtPayload ( issuer , audience , subject , param , validity ) ;
100
+ const token = await JWT . encode ( payload ) ;
101
+ const decoded = await JWT . validate ( token , new ValidationParams ( issuer , audience , subject ) ) ;
102
+
103
+ expect ( decoded ) . toMatchObject ( payload ) ;
104
+ expect ( readFileSpy ) . toBeCalledTimes ( 2 ) ;
105
+ } ) ;
106
+
107
+ it ( 'Should validate a token expiry for JWT.validate' , async ( ) => {
108
+
109
+ beforeEach ( ( ) => {
110
+ readFileSpy . mockClear ( ) ;
111
+ } ) ;
112
+
113
+ const time = Math . floor ( Date . now ( ) / 1000 ) ;
114
+
115
+ const payload = < JwtPayload > {
116
+ aud : audience ,
117
+ sub : subject ,
118
+ iss : issuer ,
119
+ iat : time ,
120
+ exp : time ,
121
+ prm : param ,
122
+ } ;
123
+ const token = await JWT . encode ( payload ) ;
124
+ try {
125
+ await JWT . validate ( token , new ValidationParams ( issuer , audience , subject ) ) ;
126
+ } catch ( e ) {
127
+ expect ( e ) . toBeInstanceOf ( TokenExpiredError ) ;
128
+ }
129
+ expect ( readFileSpy ) . toBeCalledTimes ( 2 ) ;
130
+ } ) ;
131
+ } ) ;
0 commit comments