-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathuser.server.model.tests.js
407 lines (339 loc) · 10.2 KB
/
user.server.model.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
* Module dependencies.
*/
const should = require('should');
const mongoose = require('mongoose');
const config = require('../../../../config/config');
const utils = require('../../../../testutils/server/data.server.testutil');
const User = mongoose.model('User');
/**
* Globals
*/
let user;
let user2;
let user3;
/**
* Unit tests
*/
describe('User Model Unit Tests:', function () {
before(function () {
user = new User({
firstName: 'Full',
lastName: 'Name',
displayName: 'Full Name',
email: 'test@test.com',
username: 'username123',
password: 'password123',
provider: 'local',
});
user2 = new User({
firstName: 'Full',
lastName: 'Name',
displayName: 'Full Name',
email: 'test@test.com',
username: 'username123',
password: 'password123',
provider: 'local',
});
user3 = {
firstName: 'Different',
lastName: 'User',
displayName: 'Full Different Name',
email: 'test3@test.com',
username: 'different_username',
password: 'different_password',
provider: 'local',
};
});
afterEach(utils.clearDatabase);
describe('Method Save', function () {
it('should begin with no users', function (done) {
User.find({}, function (err, users) {
users.should.have.length(0);
done();
});
});
it('should be able to save without problems', function (done) {
const _user = new User(user);
_user.save(function (err) {
should.not.exist(err);
done();
});
});
it('should fail to save an existing user again', function (done) {
const _user = new User(user);
const _user2 = new User(user2);
_user.save(function () {
_user2.save(function (err) {
should.exist(err);
done();
});
});
});
it('should be able to generate displayName when saving user', function (done) {
const _user = new User(user);
_user.firstName = 'Test';
_user.save(function (err, savedUser) {
if (err) return done(err);
savedUser.firstName.should.equal('Test');
savedUser.lastName.should.equal('Name');
savedUser.displayName.should.equal('Test Name');
done();
});
});
it('should be able to generate displayName when updating user', function (done) {
const _user = new User(user);
// Create user
_user.save(function (err) {
if (err) return done(err);
// Re-save the user we just created, but with new first name
_user.firstName = 'Test';
_user.save(function (err, savedUser2) {
if (err) return done(err);
savedUser2.firstName.should.equal('Test');
savedUser2.lastName.should.equal('Name');
savedUser2.displayName.should.equal('Test Name');
done();
});
});
});
it('should be able to show an error when try to save without first name', function (done) {
const _user = new User(user);
_user.firstName = '';
_user.save(function (err) {
should.exist(err);
done();
});
});
it('should be able to show an error when try to save with too short password', function (done) {
const _user = new User(user);
_user.password = 's1';
_user.save(function (err) {
should.exist(err);
done();
});
});
it('should confirm that saving user model doesnt change the password', function (done) {
const _user = new User(user);
_user.save(function (err) {
should.not.exist(err);
const passwordBefore = _user.password;
_user.firstName = 'test';
_user.save(function (err) {
should.not.exist(err);
const passwordAfter = _user.password;
passwordBefore.should.equal(passwordAfter);
done();
});
});
});
it('should be able to save 2 different users', function (done) {
const _user = new User(user);
const _user3 = new User(user3);
_user.save(function (err) {
should.not.exist(err);
_user3.save(function (err) {
should.not.exist(err);
done();
});
});
});
it('should not be able to save different user with the same email address', function (done) {
const _user = new User(user);
const _user3 = new User(user3);
_user.remove(function (err) {
should.not.exist(err);
_user.save(function (err) {
should.not.exist(err);
const user3_email = _user3.email;
_user3.email = _user.email;
_user3.save(function (err) {
should.exist(err);
// Restoring the original email for test3 so it can be used in later tests
_user3.email = user3_email;
done();
});
});
});
});
it('should not be able to save different user with the same username', function (done) {
const _user = new User(user);
const _user3 = new User(user3);
_user.remove(function (err) {
should.not.exist(err);
_user.save(function (err) {
should.not.exist(err);
const user3_username = _user3.username;
_user3.username = _user.username;
_user3.save(function (err) {
should.exist(err);
// Restoring the original username for test3 so it can be used in later tests
_user3.username = user3_username;
done();
});
});
});
});
});
describe('Username Validation', function () {
it('should show error to save username beginning with .', function (done) {
const _user = new User(user);
_user.username = '.login';
_user.save(function (err) {
should.exist(err);
done();
});
});
it('should be able to show an error when try to save with not allowed username', function (done) {
const _user = new User(user);
_user.username =
config.illegalStrings[
Math.floor(Math.random() * config.illegalStrings.length)
];
_user.save(function (err) {
should.exist(err);
done();
});
});
it('should show error to save username end with .', function (done) {
const _user = new User(user);
_user.username = 'login.';
_user.save(function (err) {
should.exist(err);
done();
});
});
it('should show error to save username with ..', function (done) {
const _user = new User(user);
_user.username = 'log..in';
_user.save(function (err) {
should.exist(err);
done();
});
});
it('should show error to save username shorter than 3 character', function (done) {
const _user = new User(user);
_user.username = 'lo';
_user.save(function (err) {
should.exist(err);
done();
});
});
it('should show error saving a username without at least one alphanumeric character', function (done) {
const _user = new User(user);
_user.username = '-_-';
_user.save(function (err) {
should.exist(err);
done();
});
});
it('should show error saving a username longer than 34 characters', function (done) {
const _user = new User(user);
_user.username = 'l'.repeat(35);
_user.save(function (err) {
should.exist(err);
done();
});
});
it('should save username with dot', function (done) {
const _user = new User(user);
_user.username = 'log.in';
_user.save(function (err) {
should.not.exist(err);
done();
});
});
});
describe('Roles Validation', function () {
it('should show error when trying to save with non-existing role', function (done) {
const _user = new User(user);
_user.roles = ['nope'];
_user.save(function (err) {
should.exist(err);
done();
});
});
it('should save without any roles', function (done) {
const _user = new User(user);
_user.roles = [];
_user.save(function (err) {
should.not.exist(err);
done();
});
});
it('should save with "user" role', function (done) {
const _user = new User(user);
_user.roles = ['user'];
_user.save(function (err) {
should.not.exist(err);
done();
});
});
it('should save with "admin" role', function (done) {
const _user = new User(user);
_user.roles = ['admin'];
_user.save(function (err) {
should.not.exist(err);
done();
});
});
it('should save with "moderator" role', function (done) {
const _user = new User(user);
_user.roles = ['moderator'];
_user.save(function (err) {
should.not.exist(err);
done();
});
});
it('should save with "shadowban" role', function (done) {
const _user = new User(user);
_user.roles = ['shadowban'];
_user.save(function (err) {
should.not.exist(err);
done();
});
});
it('should save with "suspended" role', function (done) {
const _user = new User(user);
_user.roles = ['suspended'];
_user.save(function (err) {
should.not.exist(err);
done();
});
});
it('should save with multiple roles', function (done) {
const _user = new User(user);
_user.roles = ['user', 'admin'];
_user.save(function (err) {
should.not.exist(err);
done();
});
});
});
describe('Language Validation', function () {
it('should show error when trying to save with non-existing language code', function (done) {
const _user = new User(user);
_user.languages = ['nope'];
_user.save(function (err) {
should.exist(err);
done();
});
});
it('should save without any languages', function (done) {
const _user = new User(user);
_user.languages = [];
_user.save(function (err) {
should.not.exist(err);
done();
});
});
it('should save with valid language codes', function (done) {
const _user = new User(user);
_user.languages = ['fin', 'iso_639_3-vsi'];
_user.save(function (err) {
should.not.exist(err);
done();
});
});
});
});