1
1
import {
2
- USER_ID , ACCESS_TOKEN , addHeaders , addAuthHeaders ,
3
- mockUserFindById , mockJwtValidate , mockJwtDecode , mockKeystoreFindForKey
2
+ ACCESS_TOKEN , addHeaders , addAuthHeaders ,
3
+ mockUserFindById , mockJwtValidate , mockKeystoreFindForKey ,
4
+ getAccessTokenSpy
4
5
} from './mock' ;
5
6
6
7
import app from '../../../src/app' ;
@@ -12,18 +13,17 @@ describe('authentication validation', () => {
12
13
const request = supertest ( app ) ;
13
14
14
15
beforeEach ( ( ) => {
15
- mockUserFindById . mockClear ( ) ;
16
+ getAccessTokenSpy . mockClear ( ) ;
16
17
mockJwtValidate . mockClear ( ) ;
17
- mockJwtDecode . mockClear ( ) ;
18
+ mockUserFindById . mockClear ( ) ;
18
19
mockKeystoreFindForKey . mockClear ( ) ;
19
20
} ) ;
20
21
21
22
it ( 'Should response with 400 if Authorization header is not passed' , async ( ) => {
22
23
const response = await addHeaders ( request . get ( endpoint ) ) ;
23
24
expect ( response . status ) . toBe ( 400 ) ;
24
25
expect ( response . body . message ) . toMatch ( / a u t h o r i z a t i o n / ) ;
25
- expect ( mockJwtDecode ) . not . toBeCalled ( ) ;
26
- expect ( mockUserFindById ) . not . toBeCalled ( ) ;
26
+ expect ( getAccessTokenSpy ) . not . toBeCalled ( ) ;
27
27
} ) ;
28
28
29
29
@@ -32,17 +32,19 @@ describe('authentication validation', () => {
32
32
. set ( 'Authorization' , '123' ) ;
33
33
expect ( response . status ) . toBe ( 400 ) ;
34
34
expect ( response . body . message ) . toMatch ( / a u t h o r i z a t i o n / ) ;
35
- expect ( mockJwtDecode ) . not . toBeCalled ( ) ;
36
- expect ( mockUserFindById ) . not . toBeCalled ( ) ;
35
+ expect ( getAccessTokenSpy ) . not . toBeCalled ( ) ;
37
36
} ) ;
38
37
39
38
it ( 'Should response with 401 if wrong Authorization header is provided' , async ( ) => {
40
39
const response = await addHeaders ( request . get ( endpoint ) )
41
40
. set ( 'Authorization' , 'Bearer 123' ) ;
42
41
expect ( response . status ) . toBe ( 401 ) ;
43
42
expect ( response . body . message ) . toMatch ( / t o k e n / i) ;
44
- expect ( mockJwtDecode ) . toBeCalledTimes ( 1 ) ;
45
- expect ( mockJwtDecode ) . toBeCalledWith ( '123' ) ;
43
+ expect ( getAccessTokenSpy ) . toBeCalledTimes ( 1 ) ;
44
+ expect ( getAccessTokenSpy ) . toBeCalledWith ( 'Bearer 123' ) ;
45
+ expect ( getAccessTokenSpy ) . toReturnWith ( '123' ) ;
46
+ expect ( mockJwtValidate ) . toBeCalledTimes ( 1 ) ;
47
+ expect ( mockJwtValidate ) . toBeCalledWith ( '123' ) ;
46
48
expect ( mockUserFindById ) . not . toBeCalled ( ) ;
47
49
} ) ;
48
50
@@ -51,9 +53,12 @@ describe('authentication validation', () => {
51
53
expect ( response . body . message ) . not . toMatch ( / n o t r e g i s t e r e d / ) ;
52
54
expect ( response . body . message ) . not . toMatch ( / t o k e n / i) ;
53
55
expect ( response . status ) . toBe ( 404 ) ;
54
- expect ( mockJwtDecode ) . toBeCalledTimes ( 1 ) ;
55
- expect ( mockJwtDecode ) . toBeCalledWith ( ACCESS_TOKEN ) ;
56
- expect ( mockUserFindById ) . toBeCalledTimes ( 1 ) ;
56
+ expect ( getAccessTokenSpy ) . toBeCalledTimes ( 1 ) ;
57
+ expect ( getAccessTokenSpy ) . toBeCalledWith ( `Bearer ${ ACCESS_TOKEN } ` ) ;
58
+ expect ( getAccessTokenSpy ) . toReturnWith ( ACCESS_TOKEN ) ;
57
59
expect ( mockJwtValidate ) . toBeCalledTimes ( 1 ) ;
60
+ expect ( mockJwtValidate ) . toBeCalledWith ( ACCESS_TOKEN ) ;
61
+ expect ( mockUserFindById ) . toBeCalledTimes ( 1 ) ;
62
+ expect ( mockKeystoreFindForKey ) . toBeCalledTimes ( 1 ) ;
58
63
} ) ;
59
64
} ) ;
0 commit comments