/
portal.spec.ts
107 lines (97 loc) · 4.16 KB
/
portal.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { UsersSchema, ERoles } from '@api/users';
import { Body } from '@lib/mongoose';
import { superAgent } from '@test/index';
import { Constants, NetworkStatus, tokenService } from '@core/helpers';
import { getUri, generateExpiredToken, UserUtilityFixture } from '@test/fixture';
import { AppUtils } from '@core/utils';
import { IRefreshTokenBody } from './portal.routes';
import { PortalHelper } from './portal.helper';
const ENDPOINT = (suffix: string) => getUri(`${Constants.Endpoints.PORTAL}/${suffix}`);
const LOGIN_ENDPOINT = ENDPOINT(Constants.Endpoints.LOGIN);
const RESET_ENDPOINT = ENDPOINT(Constants.Endpoints.RESET_PASSWORD);
const FORGET_ENDPOINT = ENDPOINT(Constants.Endpoints.FORGET_PASSWORD);
const REFRESH_TOKEN = ENDPOINT(Constants.Endpoints.REFRESH_TOKEN);
const mockUser = {
password: '123456789',
username: 'portalLogin',
email: 'portal@login.com',
mobile: '0792807794',
profile: null,
role: ERoles.SUPERADMIN
} as Body<UsersSchema>;
describe('Login should fail if..', () => {
it('Request with non existing user', async () => {
const req = (await superAgent).post(LOGIN_ENDPOINT);
const res = await req.send(AppUtils.assignObject({}, mockUser, {
username: 'fakeUsername',
password: 'fakePassword'
}));
expect(res.status).toBe(NetworkStatus.BAD_REQUEST);
});
it('The Username was wrong', async () => {
const req = (await superAgent).post(LOGIN_ENDPOINT);
const res = await req.send(AppUtils.assignObject({}, mockUser, { username: 'fakeUsername' }));
expect(res.status).toBe(NetworkStatus.BAD_REQUEST);
});
it('The password was wrong', async () => {
const req = (await superAgent).post(LOGIN_ENDPOINT);
const res = await req.send(AppUtils.assignObject({}, mockUser, { password: 'fakePassword' }));
expect(res.status).toBe(NetworkStatus.BAD_REQUEST);
});
it.todo('User has more than three session');
});
describe('Forgot password ', () => {
it.todo('refactor');
});
describe('Reset password ', () => {
it.todo('refactor');
});
describe('Refresh Token', () => {
async function getResponse(invalidToken, token = null) {
const req = (await superAgent).post(REFRESH_TOKEN);
return await req.send({ refreshToken: invalidToken, token } as IRefreshTokenBody);
}
const INVALID_TOKEN = 'invalid.token.refuesed';
it('should throw if the refresh token is expired', async () => {
// REVIEW find a way to generate invalid tokens
const res = await getResponse(generateExpiredToken());
expect(res.status).toBe(NetworkStatus.BAD_REQUEST);
});
it('should throw if the refresh token is invalid', async () => {
const res = await getResponse(INVALID_TOKEN);
expect(res.status).toBe(NetworkStatus.BAD_REQUEST);
});
it('should throw if the refresh token is invalid', async () => {
const res = await getResponse(
PortalHelper.generateRefreshToken(null),
INVALID_TOKEN
);
expect(res.status).toBe(NetworkStatus.BAD_REQUEST);
});
it('should throw if the token is not expired', async () => {
const res = await getResponse(
PortalHelper.generateRefreshToken(null),
PortalHelper.generateToken(null, null)
);
expect(res.status).toBe(NetworkStatus.NOT_ACCEPTABLE);
});
it.todo('should retrun with valid token and refresh token', async () => {
const userUtility = new UserUtilityFixture();
const user = await userUtility.createUser();
// TODO: Replace them with fixture function to check if it has a value
expect(user).toHaveProperty('token');
expect(user).toHaveProperty('refreshToken');
const res = await getResponse(
PortalHelper.generateRefreshToken(null),
PortalHelper.generateToken(null, null)
);
expect(res.status).toBe(NetworkStatus.BAD_REQUEST);
});
});
describe('Login should success when', () => {
it.todo('Token is valid');
it.todo('Token has the appropriate schema');
});
// TODO: Move it to fixture folder
export function expectValueToHaveProperty<T>(value: T, property: string) {
}