Skip to content

Commit 690ac93

Browse files
committed
improve the mock function
1 parent ec46db5 commit 690ac93

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

src/core/JWT.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default class JWT {
4444
if (e && e.name === 'TokenExpiredError') throw new TokenExpiredError();
4545
throw new BadTokenError();
4646
}
47-
};
47+
}
4848

4949
/**
5050
* This method checks the token and returns the decoded data even when the token is expired

tests/auth/apikey.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import app from '../../src/app';
22
import supertest from 'supertest';
3-
import ApiKeyRepo from '../../src/database/repository/ApiKeyRepo';
43
import { IApiKey } from '../../src/database/model/ApiKey';
54

65
export const API_KEY = 'abc';
@@ -10,13 +9,15 @@ export const mockFindApiKey = jest.fn(async (key: string) => {
109
else return null;
1110
});
1211

12+
jest.mock('../../src/database/repository/ApiKeyRepo', () => ({
13+
get findByKey() { return mockFindApiKey; }
14+
}));
15+
1316
describe('apikey validation', () => {
1417

1518
const endpoint = '/v1/dummy/test';
1619
const request = supertest(app);
1720

18-
ApiKeyRepo.findByKey = mockFindApiKey;
19-
2021
beforeEach(() => {
2122
mockFindApiKey.mockClear();
2223
});

tests/auth/authentication.test.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import app from '../../src/app';
22
import supertest, { SuperTest } from 'supertest';
3-
import ApiKeyRepo from '../../src/database/repository/ApiKeyRepo';
4-
import UserRepo from '../../src/database/repository/UserRepo';
53
import { IUser } from '../../src/database/model/User';
64
import { Types } from 'mongoose';
75
import { mockFindApiKey, API_KEY } from './apikey.test';
86
import JWT, { ValidationParams, JwtPayload } from '../../src/core/JWT';
97
import { BadTokenError } from '../../src/core/ApiError';
108
import { IKeystore } from '../../src/database/model/Keystore';
11-
import KeystoreRepo from '../../src/database/repository/KeystoreRepo';
129

1310
export const ACCESS_TOKEN = 'xyz';
1411

@@ -28,24 +25,37 @@ const mockJwtValidate = jest.fn(
2825
const mockKeystoreFindForKey = jest.fn(
2926
async (client: IUser, key: string): Promise<IKeystore> => (<IKeystore>{ client: client, primaryKey: key }));
3027

28+
const mockValidateTokenData =
29+
jest.fn(async (payload: JwtPayload, userId: Types.ObjectId): Promise<JwtPayload> => payload);
30+
3131
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; }
3545
}));
3646

47+
JWT.validate = mockJwtValidate;
48+
3749
describe('authentication validation', () => {
3850

3951
const endpoint = '/v1/profile/my/test';
4052
const request = supertest(app);
4153

42-
UserRepo.findById = mockUserFindById;
43-
ApiKeyRepo.findByKey = mockFindApiKey;
44-
JWT.validate = mockJwtValidate;
45-
KeystoreRepo.findforKey = mockKeystoreFindForKey;
4654

4755
beforeEach(() => {
56+
mockValidateTokenData.mockClear();
4857
mockUserFindById.mockClear();
58+
mockFindApiKey.mockClear();
4959
mockJwtValidate.mockClear();
5060
mockKeystoreFindForKey.mockClear();
5161
});
@@ -91,6 +101,7 @@ describe('authentication validation', () => {
91101
expect(response.status).toBe(401);
92102
expect(response.body.message).toMatch(/token/i);
93103
expect(mockUserFindById).toBeCalledTimes(1);
104+
expect(mockJwtValidate).toBeCalledTimes(1);
94105
});
95106

96107
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', () => {
101112
expect(response.body.message).not.toMatch(/token/i);
102113
expect(response.status).toBe(404);
103114
expect(mockUserFindById).toBeCalledTimes(1);
115+
expect(mockValidateTokenData).toBeCalledTimes(1);
116+
expect(mockJwtValidate).toBeCalledTimes(1);
104117
});
105118
});
106119

0 commit comments

Comments
 (0)