11import {
22 USER_ID , ACCESS_TOKEN , addHeaders , addAuthHeaders ,
3- mockValidateTokenData , mockUserFindById , mockJwtValidate , mockKeystoreFindForKey
3+ mockUserFindById , mockJwtValidate , mockJwtDecode , mockKeystoreFindForKey
44} from './mock' ;
55
66import app from '../../../src/app' ;
@@ -12,63 +12,48 @@ describe('authentication validation', () => {
1212 const request = supertest ( app ) ;
1313
1414 beforeEach ( ( ) => {
15- mockValidateTokenData . mockClear ( ) ;
1615 mockUserFindById . mockClear ( ) ;
1716 mockJwtValidate . mockClear ( ) ;
17+ mockJwtDecode . mockClear ( ) ;
1818 mockKeystoreFindForKey . mockClear ( ) ;
1919 } ) ;
2020
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 ) ) ;
2423 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 ( ) ;
2626 expect ( mockUserFindById ) . not . toBeCalled ( ) ;
2727 } ) ;
2828
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- } ) ;
3629
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 ( ) => {
3831 const response = await addHeaders ( request . get ( endpoint ) )
39- . set ( 'x-access-token' , ACCESS_TOKEN )
40- . set ( 'x-user-id' , '123' ) ;
32+ . set ( 'Authorization' , '123' ) ;
4133 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 ( ) ;
4336 expect ( mockUserFindById ) . not . toBeCalled ( ) ;
4437 } ) ;
4538
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 ( ) => {
4740 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' ) ;
5942 expect ( response . status ) . toBe ( 401 ) ;
6043 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 ( ) ;
6347 } ) ;
6448
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 ( ) => {
6650 const response = await addAuthHeaders ( request . get ( endpoint ) ) ;
6751 expect ( response . body . message ) . not . toMatch ( / n o t r e g i s t e r e d / ) ;
6852 expect ( response . body . message ) . not . toMatch ( / t o k e n / i) ;
6953 expect ( response . status ) . toBe ( 404 ) ;
54+ expect ( mockJwtDecode ) . toBeCalledTimes ( 1 ) ;
55+ expect ( mockJwtDecode ) . toBeCalledWith ( ACCESS_TOKEN ) ;
7056 expect ( mockUserFindById ) . toBeCalledTimes ( 1 ) ;
71- expect ( mockValidateTokenData ) . toBeCalledTimes ( 1 ) ;
7257 expect ( mockJwtValidate ) . toBeCalledTimes ( 1 ) ;
7358 } ) ;
7459} ) ;
0 commit comments