-
Notifications
You must be signed in to change notification settings - Fork 137
/
user-change-locale.server.routes.tests.js
151 lines (114 loc) · 3.81 KB
/
user-change-locale.server.routes.tests.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
const mongoose = require('mongoose'),
path = require('path'),
request = require('supertest'),
should = require('should'),
utils = require(path.resolve('./testutils/data.server.testutils')),
express = require(path.resolve('./config/lib/express')),
User = mongoose.model('User');
const app = express.init(mongoose.connection);
const agent = request.agent(app);
describe('User account: change locale', () => {
const publicUsers = utils.generateUsers(1, { public: true });
const nonpublicUsers = utils.generateUsers(1, { public: false, username: 'nonpublic', email: 'nonpublic@example.com' });
let users;
// save users
beforeEach(async () => {
users = await utils.saveUsers([...publicUsers, ...nonpublicUsers]);
});
afterEach(utils.clearDatabase);
context('logged in', () => {
beforeEach(async () => {
await utils.signIn(publicUsers[0], agent);
});
afterEach(async () => {
await utils.signOut(agent);
});
context('valid value', () => {
it('200 and save the new locale to user model', async () => {
const { username } = users[0];
const userBefore = await User.findOne({ username }).lean();
should(userBefore).have.property('locale', '');
const { body } = await agent
.put('/api/users')
.send({ locale: 'cze' })
.expect(200);
should(body).have.property('locale', 'cze');
const userAfter = await User.findOne({ username }).lean();
should(userAfter).have.property('locale', 'cze');
});
});
context('invalid value', () => {
it('[number] 400', async () => {
await agent
.put('/api/users')
.send({ locale: 1111 })
.expect(400);
});
it('[invalid string] 400', async () => {
await agent
.put('/api/users')
.send({ locale: 'invalid string' })
.expect(400);
});
});
});
context('logged in as nonpublic user', () => {
beforeEach(async () => {
await utils.signIn(nonpublicUsers[0], agent);
});
afterEach(async () => {
await utils.signOut(agent);
});
it('can update, too', async () => {
const { username } = nonpublicUsers[0];
const userBefore = await User.findOne({ username }).lean();
should(userBefore).have.property('locale', '');
await agent
.put('/api/users')
.send({ locale: 'eng' })
.expect(200);
const userAfter = await User.findOne({ username }).lean();
should(userAfter).have.property('locale', 'eng');
});
});
context('not logged in', () => {
it('403', async () => {
await agent
.put('/api/users')
.send({ locale: 'eng' })
.expect(403);
});
});
});
describe('User account: read locale', () => {
const publicUsers = utils.generateUsers(2, { public: true, locale: 'eng' });
// save users
beforeEach(async () => {
await utils.saveUsers(publicUsers);
});
afterEach(utils.clearDatabase);
context('logged in', () => {
beforeEach(async () => {
await utils.signIn(publicUsers[0], agent);
});
afterEach(async () => {
await utils.signOut(agent);
});
it('[self] response body of /api/auth/signin should include locale', async () => {
const { username, password } = publicUsers[0];
const { body } = await agent
.post('/api/auth/signin')
.send({ username, password })
.expect(200);
should(body).have.property('locale', 'eng');
});
it('response body of /api/users/:username shouldn\'t include locale', async () => {
const otherUser = publicUsers[1];
const { body } = await agent
.get(`/api/users/${otherUser.username}`)
.expect(200);
should(body).not.have.property('locale');
});
});
});