1
1
import app from '../../src/app' ;
2
2
import supertest , { SuperTest } from 'supertest' ;
3
- import ApiKeyRepo from '../../src/database/repository/ApiKeyRepo' ;
4
- import UserRepo from '../../src/database/repository/UserRepo' ;
5
3
import { IUser } from '../../src/database/model/User' ;
6
4
import { Types } from 'mongoose' ;
7
5
import { mockFindApiKey , API_KEY } from './apikey.test' ;
8
6
import JWT , { ValidationParams , JwtPayload } from '../../src/core/JWT' ;
9
7
import { BadTokenError } from '../../src/core/ApiError' ;
10
8
import { IKeystore } from '../../src/database/model/Keystore' ;
11
- import KeystoreRepo from '../../src/database/repository/KeystoreRepo' ;
12
9
13
10
export const ACCESS_TOKEN = 'xyz' ;
14
11
@@ -28,24 +25,37 @@ const mockJwtValidate = jest.fn(
28
25
const mockKeystoreFindForKey = jest . fn (
29
26
async ( client : IUser , key : string ) : Promise < IKeystore > => ( < IKeystore > { client : client , primaryKey : key } ) ) ;
30
27
28
+ const mockValidateTokenData =
29
+ jest . fn ( async ( payload : JwtPayload , userId : Types . ObjectId ) : Promise < JwtPayload > => payload ) ;
30
+
31
31
jest . mock ( '../../src/auth/authUtils' , ( ) => ( {
32
- get validateTokenData ( ) {
33
- return jest . fn ( async ( payload : JwtPayload , userId : Types . ObjectId ) : Promise < JwtPayload > => payload ) ;
34
- }
32
+ get validateTokenData ( ) { return mockValidateTokenData ; }
33
+ } ) ) ;
34
+
35
+ jest . mock ( '../../src/database/repository/UserRepo' , ( ) => ( {
36
+ get findById ( ) { return mockUserFindById ; }
37
+ } ) ) ;
38
+
39
+ jest . mock ( '../../src/database/repository/ApiKeyRepo' , ( ) => ( {
40
+ get findByKey ( ) { return mockFindApiKey ; }
41
+ } ) ) ;
42
+
43
+ jest . mock ( '../../src/database/repository/KeystoreRepo' , ( ) => ( {
44
+ get findforKey ( ) { return mockKeystoreFindForKey ; }
35
45
} ) ) ;
36
46
47
+ JWT . validate = mockJwtValidate ;
48
+
37
49
describe ( 'authentication validation' , ( ) => {
38
50
39
51
const endpoint = '/v1/profile/my/test' ;
40
52
const request = supertest ( app ) ;
41
53
42
- UserRepo . findById = mockUserFindById ;
43
- ApiKeyRepo . findByKey = mockFindApiKey ;
44
- JWT . validate = mockJwtValidate ;
45
- KeystoreRepo . findforKey = mockKeystoreFindForKey ;
46
54
47
55
beforeEach ( ( ) => {
56
+ mockValidateTokenData . mockClear ( ) ;
48
57
mockUserFindById . mockClear ( ) ;
58
+ mockFindApiKey . mockClear ( ) ;
49
59
mockJwtValidate . mockClear ( ) ;
50
60
mockKeystoreFindForKey . mockClear ( ) ;
51
61
} ) ;
@@ -91,6 +101,7 @@ describe('authentication validation', () => {
91
101
expect ( response . status ) . toBe ( 401 ) ;
92
102
expect ( response . body . message ) . toMatch ( / t o k e n / i) ;
93
103
expect ( mockUserFindById ) . toBeCalledTimes ( 1 ) ;
104
+ expect ( mockJwtValidate ) . toBeCalledTimes ( 1 ) ;
94
105
} ) ;
95
106
96
107
it ( 'Should response with 404 if correct x-access-token and x-user-id header are provided' , async ( ) => {
@@ -101,6 +112,8 @@ describe('authentication validation', () => {
101
112
expect ( response . body . message ) . not . toMatch ( / t o k e n / i) ;
102
113
expect ( response . status ) . toBe ( 404 ) ;
103
114
expect ( mockUserFindById ) . toBeCalledTimes ( 1 ) ;
115
+ expect ( mockValidateTokenData ) . toBeCalledTimes ( 1 ) ;
116
+ expect ( mockJwtValidate ) . toBeCalledTimes ( 1 ) ;
104
117
} ) ;
105
118
} ) ;
106
119
0 commit comments