11import {
2- USER_ID , ACCESS_TOKEN , addHeaders , addAuthHeaders ,
3- mockUserFindById , mockJwtValidate , mockJwtDecode , mockKeystoreFindForKey
2+ ACCESS_TOKEN , addHeaders , addAuthHeaders ,
3+ mockUserFindById , mockJwtValidate , mockKeystoreFindForKey ,
4+ getAccessTokenSpy
45} from './mock' ;
56
67import app from '../../../src/app' ;
@@ -12,18 +13,17 @@ describe('authentication validation', () => {
1213 const request = supertest ( app ) ;
1314
1415 beforeEach ( ( ) => {
15- mockUserFindById . mockClear ( ) ;
16+ getAccessTokenSpy . mockClear ( ) ;
1617 mockJwtValidate . mockClear ( ) ;
17- mockJwtDecode . mockClear ( ) ;
18+ mockUserFindById . mockClear ( ) ;
1819 mockKeystoreFindForKey . mockClear ( ) ;
1920 } ) ;
2021
2122 it ( 'Should response with 400 if Authorization header is not passed' , async ( ) => {
2223 const response = await addHeaders ( request . get ( endpoint ) ) ;
2324 expect ( response . status ) . toBe ( 400 ) ;
2425 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 ( ) ;
2727 } ) ;
2828
2929
@@ -32,17 +32,19 @@ describe('authentication validation', () => {
3232 . set ( 'Authorization' , '123' ) ;
3333 expect ( response . status ) . toBe ( 400 ) ;
3434 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 ( ) ;
3736 } ) ;
3837
3938 it ( 'Should response with 401 if wrong Authorization header is provided' , async ( ) => {
4039 const response = await addHeaders ( request . get ( endpoint ) )
4140 . set ( 'Authorization' , 'Bearer 123' ) ;
4241 expect ( response . status ) . toBe ( 401 ) ;
4342 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' ) ;
4648 expect ( mockUserFindById ) . not . toBeCalled ( ) ;
4749 } ) ;
4850
@@ -51,9 +53,12 @@ describe('authentication validation', () => {
5153 expect ( response . body . message ) . not . toMatch ( / n o t r e g i s t e r e d / ) ;
5254 expect ( response . body . message ) . not . toMatch ( / t o k e n / i) ;
5355 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 ) ;
5759 expect ( mockJwtValidate ) . toBeCalledTimes ( 1 ) ;
60+ expect ( mockJwtValidate ) . toBeCalledWith ( ACCESS_TOKEN ) ;
61+ expect ( mockUserFindById ) . toBeCalledTimes ( 1 ) ;
62+ expect ( mockKeystoreFindForKey ) . toBeCalledTimes ( 1 ) ;
5863 } ) ;
5964} ) ;
0 commit comments