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