Skip to content

Commit 180628d

Browse files
committed
migrate authorization tests
1 parent e7e2d8b commit 180628d

File tree

3 files changed

+42
-35
lines changed

3 files changed

+42
-35
lines changed

tests/auth/authUtils/unit.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('authUtils validateTokenData tests', () => {
1717
tokenInfo.audience,
1818
'abc',
1919
ACCESS_TOKEN_KEY,
20-
tokenInfo.accessTokenValidityDays,
20+
tokenInfo.accessTokenValidity,
2121
);
2222

2323
try {
@@ -33,7 +33,7 @@ describe('authUtils validateTokenData tests', () => {
3333
tokenInfo.audience,
3434
new Types.ObjectId().toHexString(),
3535
'123',
36-
tokenInfo.accessTokenValidityDays,
36+
tokenInfo.accessTokenValidity,
3737
);
3838

3939
try {
@@ -49,7 +49,7 @@ describe('authUtils validateTokenData tests', () => {
4949
tokenInfo.audience,
5050
new Types.ObjectId().toHexString(), // Random Key
5151
ACCESS_TOKEN_KEY,
52-
tokenInfo.accessTokenValidityDays,
52+
tokenInfo.accessTokenValidity,
5353
);
5454

5555
const validatedPayload = validateTokenData(payload);

tests/auth/authorization/mock.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,35 @@ export const mockUserFindById = jest.fn(async (id: Types.ObjectId) => {
4343
else return null;
4444
});
4545

46-
export const mockRoleRepoFindByCode = jest.fn(async (code: string): Promise<Role | null> => {
47-
switch (code) {
48-
case RoleCode.WRITER:
49-
return {
50-
_id: WRITER_ROLE_ID,
51-
code: RoleCode.WRITER,
52-
status: true,
53-
} as Role;
54-
case RoleCode.EDITOR:
55-
return {
56-
_id: EDITOR_ROLE_ID,
57-
code: RoleCode.EDITOR,
58-
status: true,
59-
} as Role;
60-
case RoleCode.LEARNER:
61-
return {
62-
_id: LEARNER_ROLE_ID,
63-
code: RoleCode.LEARNER,
64-
status: true,
65-
} as Role;
46+
export const mockRoleRepoFindByCodes = jest.fn(async (codes: string[]): Promise<Role[]> => {
47+
const results: Role[] = [];
48+
for (const code of codes) {
49+
switch (code) {
50+
case RoleCode.WRITER:
51+
results.push({
52+
_id: WRITER_ROLE_ID,
53+
code: RoleCode.WRITER,
54+
status: true,
55+
} as Role);
56+
break;
57+
case RoleCode.EDITOR:
58+
results.push({
59+
_id: EDITOR_ROLE_ID,
60+
code: RoleCode.EDITOR,
61+
status: true,
62+
} as Role);
63+
break;
64+
case RoleCode.LEARNER:
65+
results.push({
66+
_id: LEARNER_ROLE_ID,
67+
code: RoleCode.LEARNER,
68+
status: true,
69+
} as Role);
70+
break;
71+
}
6672
}
67-
return null;
73+
74+
return results;
6875
});
6976

7077
export const mockJwtValidate = jest.fn(async (token: string): Promise<JwtPayload> => {
@@ -97,7 +104,7 @@ jest.mock('../../../src/database/repository/UserRepo', () => ({
97104
}));
98105

99106
jest.mock('../../../src/database/repository/RoleRepo', () => ({
100-
findByCode: mockRoleRepoFindByCode,
107+
findByCodes: mockRoleRepoFindByCodes,
101108
}));
102109

103110
JWT.validate = mockJwtValidate;

tests/auth/authorization/unit.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
import { addAuthHeaders } from '../authentication/mock';
22

33
// import the mock for the current test after all other mock imports
4-
// this will prevent the different implementations by the other mock
5-
import { mockRoleRepoFindByCode, mockUserFindById, EDITOR_ACCESS_TOKEN } from './mock';
4+
// this will prevent the different implementations for same function by the other mocks
5+
import { mockRoleRepoFindByCodes, mockUserFindById, EDITOR_ACCESS_TOKEN } from './mock';
66

77
import app from '../../../src/app';
88
import supertest from 'supertest';
99
import { RoleCode } from '../../../src/database/model/Role';
1010

1111
describe('authentication validation for editor', () => {
12-
const endpoint = '/v1/editor/blog/test';
12+
const endpoint = '/v1/blog/editor/test';
1313
const request = supertest(app);
1414

1515
beforeEach(() => {
16-
mockRoleRepoFindByCode.mockClear();
16+
mockRoleRepoFindByCodes.mockClear();
1717
mockUserFindById.mockClear();
1818
});
1919

2020
it('Should response with 401 if user do not have editor role', async () => {
2121
const response = await addAuthHeaders(request.get(endpoint));
2222
expect(response.status).toBe(401);
2323
expect(response.body.message).toMatch(/denied/);
24-
expect(mockRoleRepoFindByCode).toBeCalledTimes(1);
24+
expect(mockRoleRepoFindByCodes).toBeCalledTimes(1);
2525
expect(mockUserFindById).toBeCalledTimes(1);
26-
expect(mockRoleRepoFindByCode).toBeCalledWith(RoleCode.EDITOR);
26+
expect(mockRoleRepoFindByCodes).toBeCalledWith([RoleCode.ADMIN, RoleCode.EDITOR]);
2727
});
2828
});
2929

3030
describe('authentication validation for writer', () => {
31-
const endpoint = '/v1/writer/blog/test';
31+
const endpoint = '/v1/blog/writer/test';
3232
const request = supertest(app);
3333

3434
beforeEach(() => {
35-
mockRoleRepoFindByCode.mockClear();
35+
mockRoleRepoFindByCodes.mockClear();
3636
mockUserFindById.mockClear();
3737
});
3838

3939
it('Should response with 404 if user have writer role', async () => {
4040
const response = await addAuthHeaders(request.get(endpoint), EDITOR_ACCESS_TOKEN);
4141
expect(response.status).toBe(404);
42-
expect(mockRoleRepoFindByCode).toBeCalledTimes(1);
42+
expect(mockRoleRepoFindByCodes).toBeCalledTimes(1);
4343
expect(mockUserFindById).toBeCalledTimes(1);
44-
expect(mockRoleRepoFindByCode).toBeCalledWith(RoleCode.WRITER);
44+
expect(mockRoleRepoFindByCodes).toBeCalledWith([RoleCode.WRITER]);
4545
});
4646
});

0 commit comments

Comments
 (0)