-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.js
55 lines (43 loc) · 1.51 KB
/
users.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
'use strict';
const middleware = require('../../src/middleware/users.js');
const expect = require('chai').expect;
const sinon = require('sinon');
describe('Users middleware', () => {
const defaultUserId = 'user-id-1';
let request, response;
beforeEach(() => {
request = { cookies: {} };
response = { cookie: () => {} };
});
it('if the user already signed in, reads their ID from a cookie and exposes the user on the request', () => {
// Given
request.cookies.userId = defaultUserId;
// When
middleware(request, response, () => {});
// Then
expect(request.user).to.exist;
expect(request.user.id).to.equal(defaultUserId);
});
it('calls the next middleware in the chain', () => {
// Given
const next = sinon.spy();
// When
middleware(request, response, next);
// Then
expect(next.called).to.be.true;
});
it('if the user is not already signed in, ' +
'creates a new user id and stores it in a cookie', () => {
// Given
request.cookies.userId = undefined;
response = { cookie: sinon.spy() };
// When
middleware(request, response, () => {});
// Then
expect(request.user).to.exist;
const newUserId = request.user.id;
expect(newUserId).to.exist;
expect(response.cookie.calledWith(
'userId', newUserId)).to.be.true;
});
});