This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
express-handlers.sinon-test.js
117 lines (109 loc) · 2.99 KB
/
express-handlers.sinon-test.js
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
const test = require('ava');
const sinon = require('sinon');
const mockRequest = (sessionData, body) => ({
session: { data: sessionData },
body,
});
const mockResponse = () => {
const res = {};
res.status = sinon.stub().returns(res);
res.json = sinon.stub().returns(res);
return res;
};
const { login, logout, checkAuth } = require('./express-handlers');
test('login > should 400 if username is missing from body', async (t) => {
const req = mockRequest(
{},
{ password: 'boss' }
);
const res = mockResponse();
await login(req, res);
t.true(res.status.calledWith(400));
t.true(res.json.calledWith({
message: 'username and password are required'
}));
});
test('should 400 if password is missing from body', async (t) => {
const req = mockRequest(
{},
{ username: 'hugo' }
);
const res = mockResponse();
await login(req, res);
t.true(res.status.calledWith(400));
t.true(res.json.calledWith({
message: 'username and password are required'
}));
});
test('should 401 with message if user with passed username does not exist', async (t) => {
const req = mockRequest(
{},
{
username: 'hugo-boss',
password: 'boss'
}
);
const res = mockResponse();
await login(req, res);
t.true(res.status.calledWith(401));
t.true(res.json.calledWith({
message: 'No user with matching username'
}));
});
test('should 401 with message if passed password does not match stored password', async (t) => {
const req = mockRequest(
{},
{
username: 'guest',
password: 'not-good-password'
}
);
const res = mockResponse();
await login(req, res);
t.true(res.status.calledWith(401));
t.true(res.json.calledWith({
message: 'Wrong password'
}));
});
test('should 201 and set session.data with username if user exists and right password provided', async (t) => {
const req = mockRequest(
{},
{
username: 'guest',
password: 'guest-boss'
}
);
const res = mockResponse();
await login(req, res);
t.true(res.status.calledWith(201));
t.true(res.json.called);
t.deepEqual(
req.session.data,
{ username: 'guest' }
);
});
test('logout > should set session.data to null', async (t) => {
const req = mockRequest({ username: 'hugo' });
const res = mockResponse();
await logout(req, res);
t.is(req.session.data, null);
});
test('logout > should 200', async (t) => {
const req = mockRequest({ username: 'hugo' });
const res = mockResponse();
await logout(req, res);
t.true(res.status.calledWith(200));
});
test('checkAuth > should 401 if session data is not set', async (t) => {
const req = mockRequest();
const res = mockResponse();
await checkAuth(req, res);
t.true(res.status.calledWith(401));
});
test('checkAuth > should 200 with username from session if data is set', async (t) => {
const req = mockRequest({ username: 'hugo' });
const res = mockResponse();
await checkAuth(req, res);
t.true(res.status.calledWith(200));
t.true(res.json.calledWith({ username: 'hugo' }));
})