-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.js
365 lines (314 loc) · 11 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
var path = require('path');
var Promise = require('bluebird');
var utils = require(path.join(__dirname, '..', 'utils'));
var config = require(path.join(__dirname, '..', 'config'));
var chakram = require(path.join(__dirname, '..', 'chakram')), expect = chakram.expect;
var _ = require('lodash');
// Chai expectations
var chai = require('chai');
var cheerio = require('cheerio');
var chaiCheerio = require('chai-cheerio');
var chaiUrl = require('chai-url');
chai.use(chaiCheerio);
chai.use(chaiUrl);
var chaiExpect = chai.expect;
var methods = require(path.join(__dirname, '..', 'methods'));
var auth = methods.auth;
var users = methods.users;
var email = require(path.join(__dirname, '..', 'email'));
var usersData = require(path.join(__dirname, 'data', 'users.json'));
describe("User Invite", function() {
var userInfo = {
username: 'user',
email: '[email protected]',
password: 'password',
confirmation: 'password'
};
before("start email server", function() {
return email.serverListen();
});
it("Sends an email invitation", function() {
return utils.sudo().then(function(response) {
var adminToken = response.body.token;
return Promise.join(email.listen(), users.invite(userInfo.email, adminToken));
})
.spread(function(email, response) {
// check response
expect(response).to.have.status(200);
expect(response).to.have.property('body');
var body = response.body;
expect(response.body).to.have.all.keys([
'message',
'confirm_token'
]);
var message = body.message;
expect(message).to.equal('Successfully Sent Invitation');
var confirm_token = body.confirm_token;
expect(confirm_token).to.be.a('string');
// check email
expect(email).to.contain.all.keys([ 'subject', 'from', 'to', 'html' ]);
expect(email.subject).to.contain('You\'ve been sent an invitation');
expect(email.to).to.have.someProperties({ address: userInfo.email });;
var $ = cheerio.load(email.html);
chaiExpect($('body a'))
.to.have.text('Confirm Account')
.and.attr('href').which.contains.path('/join');
});
});
it("Does not send an email invitation again", function() {
return utils.sudo().then(function(response) {
var adminToken = response.body.token;
return users.invite(userInfo.email, adminToken);
})
.then(function(response) {
// check response
expect(response).to.have.status(422);
});
});
after("Remove invitation", function() {
return utils.sudo().then(function(response) {
var adminToken = response.body.token;
return users.removeInvite(userInfo.email, adminToken);
});
});
});
describe("User Invitations List (No invitations)", function() {
var userInfo = {
username: 'user',
email: '[email protected]',
password: 'password',
confirmation: 'password'
};
it("Returns an empty list of invitations", function() {
return utils.sudo().then(function(response) {
var adminToken = response.body.token;
return users.invitations({}, adminToken);
})
.then(function(response) {
expect(response).to.have.status(200);
var body = response.body;
expect(body).to.have.all.keys([ 'page', 'limit', 'invitations', 'has_more' ]);
var invitations = body.invitations;
expect(invitations).to.be.an('array').with.length(0);
var page = body.page;
expect(page).to.equal(1);
var limit = body.limit;
expect(limit).to.equal(25);
var hasMore = body.has_more;
expect(hasMore).to.equal(false);
});
});
});
describe("User Invitations List (Single invitation)", function() {
var userInfo = {
username: 'user',
email: '[email protected]',
password: 'password',
confirmation: 'password'
};
before("Create the invitation", function() {
return utils.sudo().then(function(response) {
var adminToken = response.body.token;
return users.invite(userInfo.email, adminToken);
});
});
it("Returns the list of invitations with defaults", function() {
return utils.sudo().then(function(response) {
var adminToken = response.body.token;
return users.invitations({}, adminToken);
})
.then(function(response) {
expect(response).to.have.status(200);
var body = response.body;
expect(body).to.have.all.keys([ 'page', 'limit', 'invitations', 'has_more' ]);
var invitations = body.invitations;
expect(invitations).to.be.an('array').with.length(1);
var page = body.page;
expect(page).to.equal(1);
var limit = body.limit;
expect(limit).to.equal(25);
var hasMore = body.has_more;
expect(hasMore).to.equal(false);
});
});
after("Remove invitation", function() {
return utils.sudo().then(function(response) {
var adminToken = response.body.token;
return users.removeInvite(userInfo.email, adminToken);
});
});
});
describe("User Invitations List (Multiple invitations)", function() {
// load 50 users from the sample data set
var userInfos = _.slice(usersData, 0, 50);
before("Create the invitations", function() {
return utils.sudo().then(function(response) {
var adminToken = response.body.token;
return Promise.map(userInfos, userInfo => { return users.invite(userInfo.email, adminToken); });
});
});
it("Returns the first page of invitations with defaults", function() {
return utils.sudo().then(function(response) {
var adminToken = response.body.token;
return users.invitations({}, adminToken);
})
.then(function(response) {
expect(response).to.have.status(200);
var body = response.body;
expect(body).to.have.all.keys([ 'page', 'limit', 'invitations', 'has_more' ]);
var invitations = body.invitations;
expect(invitations).to.be.an('array').with.length(25);
var page = body.page;
expect(page).to.equal(1);
var limit = body.limit;
expect(limit).to.equal(25);
var hasMore = body.has_more;
expect(hasMore).to.equal(true);
});
});
it("Returns the second page of invitations", function() {
return utils.sudo().then(function(response) {
var adminToken = response.body.token;
return users.invitations({ page: 2 }, adminToken);
})
.then(function(response) {
expect(response).to.have.status(200);
var body = response.body;
expect(body).to.have.all.keys([ 'page', 'limit', 'invitations', 'has_more' ]);
var invitations = body.invitations;
expect(invitations).to.be.an('array').with.length(25);
var page = body.page;
expect(page).to.equal(2);
var limit = body.limit;
expect(limit).to.equal(25);
var hasMore = body.has_more;
expect(hasMore).to.equal(false);
});
});
after("Remove invitations", function() {
return utils.sudo().then(function(response) {
var adminToken = response.body.token;
return Promise.map(userInfos, userInfo => { return users.removeInvite(userInfo.email, adminToken); });
});
});
});
describe("User Find", function() {
var userInfo = {
username: 'user',
email: '[email protected]',
password: 'password',
confirmation: 'password'
};
before("create the user to find", function() {
return auth.register(userInfo.username, userInfo.email, userInfo.password, userInfo.confirmation)
.then(function(response) {
// save the user id for cleanup
userInfo.id = response.body.id;
});
});
it("finds a user", function () {
return users.find(userInfo.username)
.then(function(response) {
expect(response).to.have.status(200);
expect(response).to.have.property('body');
var body = response.body;
expect(response.body).to.have.all.keys([
'activity',
'avatar',
'created_at',
'id',
'priority',
'role_name',
'role_highlight_color',
'roles',
'updated_at',
'username',
'malicious_score'
]);
var userId = body.id;
expect(userId).to.be.a.slugid;
});
});
it("doesn't find a nonexistant user", function () {
return users.find('invalidusername')
.then(function(response) {
expect(response).to.have.status(404);
expect(response).to.have.property('body');
var body = response.body;
expect(response.body).to.have.all.keys(['error', 'message', 'statusCode']);
expect(body.statusCode).to.equal(404);
expect(body.error).to.equal('Not Found');
});
});
it("doesn't find a crazy user", function () {
return users.find('j!@#$%^&*()_=[][}{`~')
.then(function(response) {
expect(response).to.have.status(404);
expect(response).to.have.property('body');
var body = response.body;
expect(response.body).to.have.all.keys(['error', 'message', 'statusCode']);
expect(body.statusCode).to.equal(404);
expect(body.error).to.equal('Not Found');
});
});
after("delete the created user", function() {
return utils.sudo().then(function(response) {
var adminToken = response.body.token;
return users.delete(adminToken, userInfo.id);
})
});
});
describe("User Delete", function() {
var userInfo = {
username: 'user',
email: '[email protected]',
password: 'password',
confirmation: 'password'
};
before("create the user to delete", function() {
return auth.register(userInfo.username, userInfo.email, userInfo.password, userInfo.confirmation);
});
it("deletes a user", function () {
// log in with admin account and get created user id
var todo = [
auth.login(utils.admin.username, utils.admin.password),
users.find(userInfo.username)
];
return chakram.all(todo)
.spread(function(adminResponse, userResponse) {
expect(adminResponse).to.have.status(200);
expect(adminResponse).to.have.property('body');
expect(adminResponse.body).to.have.property('token');
var adminToken = adminResponse.body.token;
expect(userResponse).to.have.status(200);
expect(userResponse).to.have.property('body');
expect(userResponse.body).to.have.property('id');
var userId = userResponse.body.id;
return users.delete(adminToken, userId);
})
.then(function(response) {
expect(response).to.have.status(200);
expect(response).to.have.property('body');
var body = response.body;
expect(response.body).to.have.all.keys(['username', 'email']);
var username = body.username;
expect(username).to.equal(userInfo.username);
var email = body.email;
expect(email).to.equal(userInfo.email);
});
});
it("doesn't find a deleted user", function () {
return users.find(userInfo.username)
.then(function(response) {
expect(response).to.have.status(404);
expect(response).to.have.property('body');
var body = response.body;
expect(response.body).to.have.all.keys(['error', 'message', 'statusCode']);
expect(body.statusCode).to.equal(404);
expect(body.error).to.equal('Not Found');
});
});
after("stop email server", function() {
return email.serverClose();
});
});