Completed
Push — master ( 78d2f9...949122 )
by greg
42s
created

test/users.js   A

Complexity

Total Complexity 34
Complexity/F 1

Size

Lines of Code 501
Function Count 34

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 5 Features 0
Metric Value
cc 0
wmc 34
nc 1
mnd 0
bc 34
fnc 34
dl 0
loc 501
rs 9.2
bpm 1
cpm 1
noi 19
c 8
b 5
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B users.js ➔ describe(ꞌusersꞌ) 0 482 1
1
var chai = require('chai');
2
var sinonChai = require('sinon-chai')
3
var expect = chai.expect
4
chai.use(sinonChai)
5
var sinon = require('sinon');
6
var fs = require('fs-extra');
7
var mkdirp = require('mkdirp');
8
var path = require('path');
9
var bcrypt = require('bcrypt-nodejs');
10
var Cookies = require('cookies');
11
var jwt = require('jwt-simple');
12
var Handlebars =require('../src/cli').Handlebars
13
14
var coreUtils = require('../src/cli').coreUtils
15
var config = require('../src/cli').config
16
config.set({root: path.join(__dirname,'fixtures')})
17
18
var User = require('../src/cli').User;
19
20
describe('users', function() {
21
  before( function() {
22
    config.users.enable = true
23
    this.fixture = {
24
      htmlIsAuthorized: fs.readFileSync(path.join(__dirname, 'fixtures', 'templates', 'isAuthorized.html'), 'utf8'),
25
      htmlIsAuthorizedTrue: fs.readFileSync(path.join(__dirname, 'fixtures', 'templates', 'isAuthorizedTrue.html'), 'utf8'),
26
      users: JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures', 'users', 'users.json'), 'utf8'))
27
    }
28
  });
29
30
  it('Handlebars.helpers.isAuthorized', function() {
31
    var template = Handlebars.compile(this.fixture.htmlIsAuthorized)
32
    var resHtml = template({})
33
    chai.expect(resHtml).to.be.equal("");
34
35
    template = Handlebars.compile(this.fixture.htmlIsAuthorizedTrue)
36
    resHtml = template({})
37
    chai.expect(resHtml).to.not.be.equal("");
38
  });
39
40
  it('User.utils.getUserRoutes', function(){
41
    var sinonInstance = sinon.sandbox.create();
0 ignored issues
show
Unused Code introduced by
The variable sinonInstance seems to be never used. Consider removing it.
Loading history...
42
    var role = User.utils.getUserRoutes("review")
43
    chai.expect(role).to.not.be.undefined
0 ignored issues
show
introduced by
The result of the property access to chai.expect(role).to.not.be.undefined is not used.
Loading history...
44
    chai.expect(role.length).to.above(0)
45
  })
46
47
  it('User.utils.findByUsername', function(done){
48
    // stub
49
    var sinonInstance = sinon.sandbox.create();
50
    var stub = sinonInstance.stub(User.manager.instance, 'get');
51
    stub.returns(JSON.parse(JSON.stringify(this.fixture.users)))
52
53
    // test
54
    User.utils.findByUsername("test", function (err, user) {
55
      chai.expect(err).to.be.null
0 ignored issues
show
introduced by
The result of the property access to chai.expect(err).to.be.null is not used.
Loading history...
56
      chai.expect(user).to.not.be.undefined
0 ignored issues
show
introduced by
The result of the property access to chai.expect(user).to.not.be.undefined is not used.
Loading history...
57
      chai.expect(user.username).to.equal('test')
58
59
      // unstub
60
      sinon.assert.calledOnce(User.manager.instance.get)
61
      User.manager.instance.get.restore()
62
      done()
63
    })
64
  })
65
66
  it('User.utils.findByEmail', function(done){
67
    // stub
68
    var sinonInstance = sinon.sandbox.create();
69
    var stub = sinonInstance.stub(User.manager.instance, 'get');
70
    stub.returns(JSON.parse(JSON.stringify(this.fixture.users)))
71
72
    // test
73
    User.utils.findByEmail("[email protected]", function (err, user) {
74
      chai.expect(err).to.be.null
0 ignored issues
show
introduced by
The result of the property access to chai.expect(err).to.be.null is not used.
Loading history...
75
      chai.expect(user).to.not.be.undefined
0 ignored issues
show
introduced by
The result of the property access to chai.expect(user).to.not.be.undefined is not used.
Loading history...
76
      chai.expect(user.username).to.equal('test')
77
78
      // unstub
79
      sinon.assert.calledOnce(User.manager.instance.get)
80
      User.manager.instance.get.restore()
81
      done()
82
    })
83
  })
84
85
  it('User.utils.findByResetPasswordToken', function(done){
86
    // stub
87
    var sinonInstance = sinon.sandbox.create();
88
    var stub = sinonInstance.stub(User.manager.instance, 'get');
89
    stub.returns(JSON.parse(JSON.stringify(this.fixture.users)))
90
91
    // test
92
    User.utils.findByResetPasswordToken("token", function (err, user) {
93
      chai.expect(err).to.be.null
0 ignored issues
show
introduced by
The result of the property access to chai.expect(err).to.be.null is not used.
Loading history...
94
      chai.expect(user).to.not.be.undefined
0 ignored issues
show
introduced by
The result of the property access to chai.expect(user).to.not.be.undefined is not used.
Loading history...
95
      chai.expect(user.username).to.equal('test')
96
97
      //unstub
98
      sinon.assert.calledOnce(User.manager.instance.get)
99
      User.manager.instance.get.restore()
100
      done()
101
    })
102
  })
103
104
  it('User.operations.deactivate', function(){
105
    // stub
106
    var sinonInstance = sinon.sandbox.create();
107
    var stub = sinonInstance.stub(User.manager.instance, 'get');
108
    stub.returns(JSON.parse(JSON.stringify(this.fixture.users)))
109
    var stubSave = sinonInstance.stub(User.manager.instance, 'save');
110
    stubSave.returns(null)
111
112
    // test
113
    var bdd = User.operations.deactivate(1)
114
    chai.expect(bdd[0].actif).to.equal(0)
115
116
    // unstub
117
    sinon.assert.calledOnce(User.manager.instance.get)
118
    User.manager.instance.get.restore()
119
    sinon.assert.calledOnce(User.manager.instance.save)
120
    User.manager.instance.save.restore()
121
  })
122
123
  it('User.operations.activate', function(){
124
    // stub
125
    var sinonInstance = sinon.sandbox.create();
126
    var stub = sinonInstance.stub(User.manager.instance, 'get');
127
    stub.returns(JSON.parse(JSON.stringify(this.fixture.users)))
128
    var stubSave = sinonInstance.stub(User.manager.instance, 'save');
129
    stubSave.returns(null)
130
131
    // test
132
    var bdd = User.operations.activate(1)
133
    chai.expect(bdd[0].actif).to.equal(1)
134
135
    // unstub
136
    sinon.assert.calledOnce(User.manager.instance.get)
137
    User.manager.instance.get.restore()
138
    sinon.assert.calledOnce(User.manager.instance.save)
139
    User.manager.instance.save.restore()
140
  })
141
142
  it('User.operations.remove', function(){
143
    // stub
144
    var sinonInstance = sinon.sandbox.create();
145
    var stub = sinonInstance.stub(User.manager.instance, 'get');
146
    stub.returns(JSON.parse(JSON.stringify(this.fixture.users)))
147
    var stubSave = sinonInstance.stub(User.manager.instance, 'save');
148
    stubSave.returns(null)
149
150
    // test
151
    var bdd = User.operations.remove(1)
152
    chai.expect(bdd.length).to.equal(0)
153
154
    // unstub
155
    sinon.assert.calledOnce(User.manager.instance.get)
156
    User.manager.instance.get.restore()
157
    sinon.assert.calledOnce(User.manager.instance.save)
158
    User.manager.instance.save.restore()
159
  })
160
161
  it('User.utils.decodeUser', function(){
162
    // sub
163
    var sinonInstance = sinon.sandbox.create();
164
    var stubGetTokenFromCookies = sinonInstance.stub(User.utils, 'getTokenFromCookies');
165
    stubGetTokenFromCookies.returns("test")
166
    var stubJwt = sinonInstance.stub(jwt, 'decode');
167
    stubJwt.returns(JSON.parse(JSON.stringify(this.fixture.users))[0])
168
169
    var user = User.utils.decodeUser(1)
170
    chai.expect(user.id).to.not.be.null
0 ignored issues
show
introduced by
The result of the property access to chai.expect(user.id).to.not.be.null is not used.
Loading history...
171
172
    sinon.assert.calledOnce(User.utils.getTokenFromCookies)
173
    User.utils.getTokenFromCookies.restore()
174
    sinon.assert.calledOnce(jwt.decode)
175
    jwt.decode.restore()
176
  })
177
178
  it('User.operations.update', function(){
179
    // stub
180
    var sinonInstance = sinon.sandbox.create();
181
    var stubTextXss = sinonInstance.stub(coreUtils.text, 'checkXss');
182
    stubTextXss.returns({ success:1 });
183
    var stubCheckSameEmail = sinonInstance.stub(User.utils, 'checkSameEmail');
184
    stubCheckSameEmail.returns({ success:1 });
185
    var stubGetRole = sinonInstance.stub(User.utils, 'getRole');
186
    stubGetRole.returns(JSON.parse(JSON.stringify(this.fixture.users))[0].role);
187
    var stubGet = sinonInstance.stub(User.manager.instance, 'get');
188
    stubGet.returns(JSON.parse(JSON.stringify(this.fixture.users)))
189
    var stubSave = sinonInstance.stub(User.manager.instance, 'save');
190
    stubSave.returns(null)
191
192
    // test
193
    var bdd = User.operations.update({id: 2})
194
    chai.expect(bdd.user.id).to.be.equal(2)
195
196
    // unstub
197
    sinon.assert.calledOnce(coreUtils.text.checkXss)
198
    coreUtils.text.checkXss.restore()
199
    sinon.assert.calledOnce(User.utils.checkSameEmail)
200
    User.utils.checkSameEmail.restore()
201
    sinon.assert.calledOnce(User.utils.getRole)
202
    User.utils.getRole.restore()
203
    sinon.assert.calledOnce(User.manager.instance.get)
204
    User.manager.instance.get.restore()
205
    sinon.assert.calledOnce(User.manager.instance.save)
206
    User.manager.instance.save.restore()
207
  })
208
209
  it('User.operations.updatePassword', function(){
210
    // stub
211
    var sinonInstance = sinon.sandbox.create();
212
    var stubCommonPassword = sinonInstance.stub(User.utils, 'commonPassword');
213
    stubCommonPassword.returns({ success:1 });
214
    var stubEncryptPassword = sinonInstance.stub(User.utils, 'encryptPassword');
215
    stubEncryptPassword.returns("newPassword2");
216
    var stubGet = sinonInstance.stub(User.manager.instance, 'get');
217
    stubGet.returns(JSON.parse(JSON.stringify(this.fixture.users)))
218
    var stubSave = sinonInstance.stub(User.manager.instance, 'save');
219
    stubSave.returns(null)
220
221
    // test
222
    var oldPassword = JSON.parse(JSON.stringify(this.fixture.users)).password
223
    var bdd = User.operations.updatePassword(JSON.parse(JSON.stringify(this.fixture.users))[0], "newPassword")
224
    chai.expect(bdd.user.password).to.not.be.equal(oldPassword)
225
226
    // unstub
227
    sinon.assert.calledOnce(User.utils.commonPassword)
228
    User.utils.commonPassword.restore()
229
    sinon.assert.calledOnce(User.utils.encryptPassword)
230
    User.utils.encryptPassword.restore()
231
    sinon.assert.calledOnce(User.manager.instance.get)
232
    User.manager.instance.get.restore()
233
    sinon.assert.calledOnce(User.manager.instance.save)
234
    User.manager.instance.save.restore()
235
  })
236
237
  it('User.operations.add', function(){
238
    // stub
239
    var sinonInstance = sinon.sandbox.create();
240
    var stubTextXss = sinonInstance.stub(coreUtils.text, 'checkXss');
241
    stubTextXss.returns({ success:1 });
242
    var stubCheckSameEmail = sinonInstance.stub(User.utils, 'checkSameEmail');
243
    stubCheckSameEmail.returns({ success:1 });
244
    var stubCommonPassword = sinonInstance.stub(User.utils, 'commonPassword');
245
    stubCommonPassword.returns({ success:1 });
246
    var stubEncryptPassword = sinonInstance.stub(User.utils, 'encryptPassword');
247
    stubEncryptPassword.returns("newPassword2");
248
    var stubGetRole = sinonInstance.stub(User.utils, 'getRole');
249
    stubGetRole.returns(JSON.parse(JSON.stringify(this.fixture.users))[0].role);
250
    var stubGet = sinonInstance.stub(User.manager.instance, 'get');
251
    stubGet.returns(JSON.parse(JSON.stringify(this.fixture.users)))
252
    var stubSave = sinonInstance.stub(User.manager.instance, 'save');
253
    stubSave.returns(null)
254
    
255
    // test
256
    var res = User.operations.add(JSON.parse(JSON.stringify(this.fixture.users))[0])
257
    chai.expect(res.success).to.be.equal(1)
258
259
    // unstub
260
    sinon.assert.calledOnce(coreUtils.text.checkXss)
261
    coreUtils.text.checkXss.restore()
262
    sinon.assert.calledOnce(User.utils.checkSameEmail)
263
    User.utils.checkSameEmail.restore()
264
    sinon.assert.calledOnce(User.utils.commonPassword)
265
    User.utils.commonPassword.restore()
266
    sinon.assert.calledOnce(User.utils.encryptPassword)
267
    User.utils.encryptPassword.restore()
268
    sinon.assert.calledOnce(User.utils.getRole)
269
    User.utils.getRole.restore()
270
    sinon.assert.calledOnce(User.manager.instance.get)
271
    User.manager.instance.get.restore()
272
    sinon.assert.calledOnce(User.manager.instance.save)
273
    User.manager.instance.save.restore()
274
  })
275
276
  it('User.utils.getAll', function(){
277
    var sinonInstance = sinon.sandbox.create();
278
    var stubGet = sinonInstance.stub(User.manager.instance, 'get');
279
    stubGet.returns(JSON.parse(JSON.stringify(this.fixture.users)))
280
281
    var bdd = User.utils.getAll()
282
    chai.expect(bdd).to.not.be.undefined
0 ignored issues
show
introduced by
The result of the property access to chai.expect(bdd).to.not.be.undefined is not used.
Loading history...
283
    chai.expect(bdd[0].username).to.equal('test')
284
285
    sinon.assert.calledOnce(User.manager.instance.get)
286
    User.manager.instance.get.restore()
287
  })
288
289
  it('User.utils.isValid', function(){
290
    // stub
291
    var sinonInstance = sinon.sandbox.create();
292
    var stubHashSync = sinonInstance.stub(bcrypt, 'compareSync');
293
    stubHashSync.returns(true);
294
295
    // test
296
    var res = User.utils.isValid(JSON.parse(JSON.stringify(this.fixture.users))[0])
297
    chai.expect(res).to.be.equal(true)
298
299
    // unstub
300
    sinon.assert.calledOnce(bcrypt.compareSync)
301
    bcrypt.compareSync.restore()
302
  })
303
304
  it('User.utils.findSync', function(){
305
    // stub
306
    var sinonInstance = sinon.sandbox.create();
307
    var stubGet = sinonInstance.stub(User.manager.instance, 'get');
308
    stubGet.returns(JSON.parse(JSON.stringify(this.fixture.users)))
309
310
    // test
311
    var user = User.utils.findSync(1)
312
    chai.expect(user).to.not.be.undefined
0 ignored issues
show
introduced by
The result of the property access to chai.expect(user).to.not.be.undefined is not used.
Loading history...
313
    chai.expect(user.username).to.equal('test')
314
315
    // unstub
316
    sinon.assert.calledOnce(User.manager.instance.get)
317
    User.manager.instance.get.restore()
318
  })
319
320
  it('User.utils.find', function(done){
321
    // stub
322
    var sinonInstance = sinon.sandbox.create();
323
    var stubGet = sinonInstance.stub(User.manager.instance, 'get');
324
    stubGet.returns(JSON.parse(JSON.stringify(this.fixture.users)))
325
326
    // test
327
    var user = User.utils.find(1, function (err, user) {
0 ignored issues
show
Unused Code introduced by
The variable user seems to be never used. Consider removing it.
Loading history...
328
      chai.expect(user).to.not.be.undefined
0 ignored issues
show
introduced by
The result of the property access to chai.expect(user).to.not.be.undefined is not used.
Loading history...
329
      chai.expect(user.username).to.equal('test')
330
331
      // unstub
332
      sinon.assert.calledOnce(User.manager.instance.get)
333
      User.manager.instance.get.restore()
334
      done()
335
    })
336
  })
337
338
  it('User.utils.checkSameEmail', function(){
339
    // stub
340
    var sinonInstance = sinon.sandbox.create();
341
    var stubGet = sinonInstance.stub(User.manager.instance, 'get');
342
    stubGet.returns(JSON.parse(JSON.stringify(this.fixture.users)))
343
344
    // test
345
    var user = JSON.parse(JSON.stringify(this.fixture.users))[0]
346
    user.id = 2
347
    var res = User.utils.checkSameEmail(user)
348
    chai.expect(res.success).to.equal(0)
349
350
    // unstub
351
    sinon.assert.calledOnce(User.manager.instance.get)
352
    User.manager.instance.get.restore()
353
  })
354
355
  it('User.utils.getRole', function(){
356
    // stub
357
358
    // test
359
    var user = JSON.parse(JSON.stringify(this.fixture.users))[0]
360
    user.role = "admin"
361
    var res = User.utils.getRole(JSON.parse(JSON.stringify(this.fixture.users))[0])
362
    chai.expect(res.role).to.not.be.equal("admin")
363
364
    // unstub
365
  })
366
367
  it('User.utils.commonPassword', function(){
368
    // stub
369
370
    // test
371
    var user = JSON.parse(JSON.stringify(this.fixture.users))[0]
372
    user.password = "password"
373
    var res = User.utils.commonPassword(user)
374
    chai.expect(res.success).to.be.equal(0)
375
376
    // unstub
377
  })
378
379
  it('User.utils.encryptPassword', function(){
380
    // stub
381
    var sinonInstance = sinon.sandbox.create();
382
    var stubGenSaltSync = sinonInstance.stub(bcrypt, 'genSaltSync');
383
    stubGenSaltSync.returns(10)
384
    var stubHashSync = sinonInstance.stub(bcrypt, 'hashSync');
385
    stubHashSync.returns("test2")
386
387
    // test
388
    var user = JSON.parse(JSON.stringify(this.fixture.users))[0]
389
    user.password = "password"
390
    var res = User.utils.encryptPassword(10, "test")
391
    chai.expect(res).to.not.be.equal("test")
392
393
    // unstub
394
    sinon.assert.calledOnce(bcrypt.genSaltSync)
395
    bcrypt.genSaltSync.restore()
396
    sinon.assert.calledOnce(bcrypt.hashSync)
397
    bcrypt.hashSync.restore()
398
  })
399
400
  it('User.utils.isUserAllowedOnRoute', function(){
401
    // stub
402
403
    // test
404
    var user = JSON.parse(JSON.stringify(this.fixture.users))[0]
0 ignored issues
show
Unused Code introduced by
The variable user seems to be never used. Consider removing it.
Loading history...
405
    var res = User.utils.isUserAllowedOnRoute("admin", "/abe/test")
406
    chai.expect(res).to.be.equal(true)
407
408
    var res = User.utils.isUserAllowedOnRoute("review", "/abe/truc")
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable res already seems to be declared on line 405. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
409
    chai.expect(res).to.be.equal(false)
410
411
    // unstub
412
  })
413
414
  it('User.manager.instance.get', function(){
415
    // stub
416
    var sinonInstance = sinon.sandbox.create();
417
    var stubRead = sinonInstance.stub(User.manager.instance, 'read');
418
    stubRead.returns(JSON.parse(JSON.stringify(this.fixture.users)))
419
420
    // test
421
    var res = User.manager.instance.get()
422
    chai.expect(res.length).to.be.above(0)
423
424
    // unstub
425
    User.manager.instance.read.restore()
426
  })
427
428
  it('User.manager.instance.save', function(){
429
    // stub
430
    var sinonInstance = sinon.sandbox.create();
431
    var stubFs = sinonInstance.stub(fs, 'writeJsonSync');
432
    stubFs.returns(null)
433
434
    // test
435
    var res = User.manager.instance.save()
0 ignored issues
show
Unused Code introduced by
The variable res seems to be never used. Consider removing it.
Loading history...
436
437
    // unstub
438
    sinon.assert.calledOnce(fs.writeJsonSync)
439
    fs.writeJsonSync.restore()
440
  })
441
442
  it('User.manager.instance.read', function(){
443
    // stub
444
    var sinonInstance = sinon.sandbox.create();
445
    var stubFs = sinonInstance.stub(fs, 'readFileSync');
446
    stubFs.returns(JSON.stringify(this.fixture.users))
447
    var stubExist = sinonInstance.stub(coreUtils.file, 'exist');
448
    stubExist.returns(true)
449
450
    // test
451
    var res = User.manager.instance.read()
452
    chai.expect(res).to.not.be.null
0 ignored issues
show
introduced by
The result of the property access to chai.expect(res).to.not.be.null is not used.
Loading history...
453
454
    // unstub
455
    sinon.assert.calledOnce(fs.readFileSync)
456
    fs.readFileSync.restore()
457
    sinon.assert.calledOnce(coreUtils.file.exist)
458
    coreUtils.file.exist.restore()
459
  })
460
461
  it('User.manager.instance.update', function(){
462
    // stub
463
    var sinonInstance = sinon.sandbox.create();
464
    var stubSave = sinonInstance.stub(User.manager.instance, 'save');
465
    stubSave.returns(JSON.parse(JSON.stringify(this.fixture.users)))
466
467
    // test
468
    var res = User.manager.instance.update()
469
    chai.expect(res).to.be.equal(true)
470
471
    // unstub
472
    sinon.assert.calledOnce(User.manager.instance.save)
473
    User.manager.instance.save.restore()
474
  })
475
476
  it('User.utils.getUserWorkflow', function(){
477
    // stub
478
479
    // test
480
    config.users.enable = false
481
    var res = User.utils.getUserWorkflow()
482
    chai.expect(res.length).to.be.equal(2)
483
484
    config.users.enable = true
485
    var res = User.utils.getUserWorkflow("draft", "admin")
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable res already seems to be declared on line 481. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
486
    chai.expect(res.length).to.be.equal(2)
487
    // unstub
488
  })
489
490
  it('User.utils.loginLimitTry', function(done){
491
    // stub
492
493
    // test
494
    var res = User.utils.loginLimitTry()
0 ignored issues
show
Unused Code introduced by
The variable res seems to be never used. Consider removing it.
Loading history...
495
      .then(function () {
496
        done()
497
      })
498
499
    // unstub
500
  })
501
});