-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathservice.test.js
More file actions
84 lines (65 loc) · 2.72 KB
/
service.test.js
File metadata and controls
84 lines (65 loc) · 2.72 KB
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
const request = require('supertest');
const app = require('./service');
function getRandomName(prefix) {
return `${prefix}_${Math.random().toString(36).substring(2, 15)}`;
}
async function registerUser() {
const email = getRandomName('email');
const password = 'toomanysecrets';
const response = await request(app).post('/api/auth').send({ email, password });
return [response, email, password];
}
function validateAuth(response) {
expect(response).toBeDefined();
expect(response.status).toBe(200);
const cookie = response.headers['set-cookie'];
expect(cookie).toBeDefined();
const uuidRegex = /^token=[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}.*$/i;
const token = cookie.find((c) => c.match(uuidRegex));
expect(token).toBeDefined();
}
test('register', async () => {
const [register, email] = await registerUser();
validateAuth(register);
expect(register.headers['content-type']).toMatch('application/json; charset=utf-8');
expect(register.body).toMatchObject({ email });
});
test('register existing', async () => {
const [, email, password] = await registerUser();
const response = await request(app).post('/api/auth').send({ email, password });
expect(response.status).toBe(409);
});
test('login', async () => {
const [, email, password] = await registerUser();
const login = await request(app).put('/api/auth').send({ email, password });
validateAuth(login);
expect(login.headers['content-type']).toMatch('application/json; charset=utf-8');
expect(login.body).toMatchObject({ email });
});
test('login bad password', async () => {
const [, email] = await registerUser();
const login = await request(app).put('/api/auth').send({ email, password: 'wrong' });
expect(login.status).toBe(401);
});
test('logout', async () => {
const [register] = await registerUser();
const cookie = register.headers['set-cookie'];
const logout = await request(app).delete('/api/auth').set('Cookie', cookie);
expect(logout.status).toBe(200);
const getMe = await request(app).get('/api/user/me').set('Cookie', cookie);
expect(getMe.status).toBe(401);
});
test('get me', async () => {
const [register, email] = await registerUser();
const cookie = register.headers['set-cookie'];
const getMe = await request(app).get('/api/user/me').set('Cookie', cookie);
expect(getMe.status).toBe(200);
expect(getMe.headers['content-type']).toMatch('application/json; charset=utf-8');
expect(getMe.body).toMatchObject({ email });
});
test('get me no auth', async () => {
const getMe = await request(app).get('/api/user/me');
expect(getMe.status).toBe(401);
expect(getMe.headers['content-type']).toMatch('application/json; charset=utf-8');
expect(getMe.body).toMatchObject({ msg: 'Unauthorized' });
});