/
users.spec.ts
126 lines (111 loc) · 5 KB
/
users.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import '@test/index';
import { superAgent } from '@test/supertest';
import { createUser, deleteUser, UserFixture } from '@test/fixture';
import { Constants, NetworkStatus } from '@core/helpers';
import { Body } from '@lib/mongoose';
import { UsersSchema } from './users.model';
const ENDPOINT = `/api/${Constants.Endpoints.USERS}`;
let user: UserFixture = null;
beforeAll(async () => {
user = await createUser();
});
afterAll(async () => {
await deleteUser();
});
// NOTE test the fail, don't test the success
describe('CREATE USER', () => {
test('Fail if the user exist before', async () => {
const body = {
email: 'test@create1.com',
mobile: '0792807794',
password: '123456789',
username: 'testCreate1'
} as Body<UsersSchema>;
const req1 = (await superAgent).post(ENDPOINT);
const res1 = await req1.send(body);
const req2 = (await superAgent).post(ENDPOINT);
const res2 = await req2.send(body);
const deleteReq = await (await superAgent).delete(`${ENDPOINT}/${res1.body.id}`);
expect(res2.status).toBe(NetworkStatus.BAD_REQUEST);
});
test('Special Char is not allowed', async () => {
const body = {
email: 'test@create2.com',
mobile: '0792807794',
password: '123456789',
username: 'testCreate2#$'
} as Body<UsersSchema>;
const req = (await superAgent).post(ENDPOINT);
const res = await req.send(body);
const deleteReq = await (await superAgent).delete(`${ENDPOINT}/${res.body.id}`);
expect(res.status).toBe(NetworkStatus.BAD_REQUEST);
});
test('Mobile number shouldn"t be wrong', async () => {
const body = {
email: 'test@create3.com',
mobile: '079280779',
password: '123456789',
username: 'testCreate3'
} as Body<UsersSchema>;
const req = (await superAgent).post(ENDPOINT);
const res = await req.send(body);
const deleteReq = await (await superAgent).delete(`${ENDPOINT}/${res.body.id}`);
expect(res.status).toBe(NetworkStatus.BAD_REQUEST);
});
});
// STUB CREATE: user password should be hashed
// STUB CREATE: user should have an account
// STUB DELETE: remove associated account
// describe('GET BY ${id}/', () => {
// test('Reject request without token', async () => {
// const res = await (await superAgent).get(`${ENDPOINT}/${user.id}`);
// expect(res.status).toBe(NetworkStatus.UNAUTHORIZED);
// });
// test('should fail if requested with id not of type ObjectId', async () => {
// // this will rise cast error
// const req = (await superAgent).get(`${ENDPOINT}/${undefined}`);
// const res = await req.set('Authorization', user.token);
// expect(res.status).toBe(NetworkStatus.BAD_REQUEST);
// });
// test('should fail if requested to non exist entity', async () => {
// const req = (await superAgent).get(`${ENDPOINT}/${new Types.ObjectId()}`);
// const res = await req.set('Authorization', user.token);
// expect(res.status).toBe(NetworkStatus.NOT_ACCEPTABLE);
// });
// test('resposne body should equal to', async () => {
// const req = (await superAgent).get(`${ENDPOINT}/${user.id}`);
// const res = await req.set('Authorization', user.token);
// const { data } = res.body;
// expect(data).toHaveProperty('username');
// expect(data).toHaveProperty('email');
// expect(data).toHaveProperty('mobile');
// expect(data).toHaveProperty('createdAt');
// expect(data).toHaveProperty('updatedAt');
// expect(data).toHaveProperty('_id');
// // password return, even it returned without being hashing
// expect(data).not.toHaveProperty('password');
// });
// });
// describe('DELETE BY ${id}/', () => {
// test('Reject request without token', async () => {
// const res = await (await superAgent).delete(`${ENDPOINT}/${user.id}`);
// expect(res.status).toBe(NetworkStatus.UNAUTHORIZED);
// });
// test('should fail if requested with id not of type ObjectId', async () => {
// const req = (await superAgent).delete(`${ENDPOINT}/${undefined}`);
// const res = await req.set('Authorization', user.token);
// expect(res.status).toBe(NetworkStatus.BAD_REQUEST);
// });
// test('should fail if requested to non exist entity', async () => {
// const req = (await superAgent).delete(`${ENDPOINT}/${new Types.ObjectId()}`);
// const res = await req.set('Authorization', user.token);
// expect(res.status).toBe(NetworkStatus.NOT_ACCEPTABLE);
// });
// test('resposne body should equal to', async () => {
// const req = (await superAgent).delete(`${ENDPOINT}/${user.id}`);
// const res = await req.set('Authorization', user.token);
// const { data } = res.body;
// expect(data).toBeNull();
// });
// });
// tslint:disable-next-line: max-line-length