Issues (791)

test/users/utils.js (17 issues)

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(process.cwd(), 'test', 'fixtures')})
17
18
var User = require('../../src/cli').User;
19
20
describe('User.utils', function() {
21
  before( function() {
22
    config.users.enable = true
23
    this.fixture = {
24
      htmlIsAuthorized: fs.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'isAuthorized.html'), 'utf8'),
25
      htmlIsAuthorizedTrue: fs.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'isAuthorizedTrue.html'), 'utf8'),
26
      users: JSON.parse(fs.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'users', 'users.json'), 'utf8'))
27
    }
28
  });
29
30
  it('User.utils.getUserRoutes', function(){
31
    var sinonInstance = sinon.sandbox.create();
0 ignored issues
show
The variable sinonInstance seems to be never used. Consider removing it.
Loading history...
32
    var role = User.utils.getUserRoutes("review")
33
    chai.expect(role).to.not.be.undefined
0 ignored issues
show
The result of the property access to chai.expect(role).to.not.be.undefined is not used.
Loading history...
34
    chai.expect(role.length).to.above(0)
35
  })
36
37
  it('User.utils.findByUsername', function(done){
38
    // stub
39
    var sinonInstance = sinon.sandbox.create();
40
    var stub = sinonInstance.stub(User.manager.instance, 'get');
41
    stub.returns(JSON.parse(JSON.stringify(this.fixture.users)))
42
43
    // test
44
    User.utils.findByUsername("test", function (err, user) {
45
      chai.expect(err).to.be.null
0 ignored issues
show
The result of the property access to chai.expect(err).to.be.null is not used.
Loading history...
46
      chai.expect(user).to.not.be.undefined
0 ignored issues
show
The result of the property access to chai.expect(user).to.not.be.undefined is not used.
Loading history...
47
      chai.expect(user.username).to.equal('test')
48
49
      // unstub
50
      sinon.assert.calledOnce(User.manager.instance.get)
51
      User.manager.instance.get.restore()
52
      done()
53
    })
54
  })
55
56
  it('User.utils.findByEmail', function(done){
57
    // stub
58
    var sinonInstance = sinon.sandbox.create();
59
    var stub = sinonInstance.stub(User.manager.instance, 'get');
60
    stub.returns(JSON.parse(JSON.stringify(this.fixture.users)))
61
62
    // test
63
    User.utils.findByEmail("[email protected]", function (err, user) {
64
      chai.expect(err).to.be.null
0 ignored issues
show
The result of the property access to chai.expect(err).to.be.null is not used.
Loading history...
65
      chai.expect(user).to.not.be.undefined
0 ignored issues
show
The result of the property access to chai.expect(user).to.not.be.undefined is not used.
Loading history...
66
      chai.expect(user.username).to.equal('test')
67
68
      // unstub
69
      sinon.assert.calledOnce(User.manager.instance.get)
70
      User.manager.instance.get.restore()
71
      done()
72
    })
73
  })
74
75
  it('User.utils.findByResetPasswordToken', function(done){
76
    // stub
77
    var sinonInstance = sinon.sandbox.create();
78
    var stub = sinonInstance.stub(User.manager.instance, 'get');
79
    stub.returns(JSON.parse(JSON.stringify(this.fixture.users)))
80
81
    // test
82
    User.utils.findByResetPasswordToken("token", function (err, user) {
83
      chai.expect(err).to.be.null
0 ignored issues
show
The result of the property access to chai.expect(err).to.be.null is not used.
Loading history...
84
      chai.expect(user).to.not.be.undefined
0 ignored issues
show
The result of the property access to chai.expect(user).to.not.be.undefined is not used.
Loading history...
85
      chai.expect(user.username).to.equal('test')
86
87
      //unstub
88
      sinon.assert.calledOnce(User.manager.instance.get)
89
      User.manager.instance.get.restore()
90
      done()
91
    })
92
  })
93
94
  it('User.utils.decodeUser', function(){
95
    // sub
96
    var sinonInstance = sinon.sandbox.create();
97
    var stubGetTokenFromCookies = sinonInstance.stub(User.utils, 'getTokenFromCookies');
98
    stubGetTokenFromCookies.returns("test")
99
    var stubJwt = sinonInstance.stub(jwt, 'decode');
100
    stubJwt.returns(JSON.parse(JSON.stringify(this.fixture.users))[0])
101
102
    var user = User.utils.decodeUser(1)
103
    chai.expect(user.id).to.not.be.null
0 ignored issues
show
The result of the property access to chai.expect(user.id).to.not.be.null is not used.
Loading history...
104
105
    sinon.assert.calledOnce(User.utils.getTokenFromCookies)
106
    User.utils.getTokenFromCookies.restore()
107
    sinon.assert.calledOnce(jwt.decode)
108
    jwt.decode.restore()
109
  })
110
111
  it('User.utils.getAll', function(){
112
    var sinonInstance = sinon.sandbox.create();
113
    var stubGet = sinonInstance.stub(User.manager.instance, 'get');
114
    stubGet.returns(JSON.parse(JSON.stringify(this.fixture.users)))
115
116
    var bdd = User.utils.getAll()
117
    chai.expect(bdd).to.not.be.undefined
0 ignored issues
show
The result of the property access to chai.expect(bdd).to.not.be.undefined is not used.
Loading history...
118
    chai.expect(bdd[0].username).to.equal('test')
119
120
    sinon.assert.calledOnce(User.manager.instance.get)
121
    User.manager.instance.get.restore()
122
  })
123
124
  it('User.utils.isValid', function(){
125
    // stub
126
    var sinonInstance = sinon.sandbox.create();
127
    var stubHashSync = sinonInstance.stub(bcrypt, 'compareSync');
128
    stubHashSync.returns(true);
129
130
    // test
131
    var res = User.utils.isValid(JSON.parse(JSON.stringify(this.fixture.users))[0])
132
    chai.expect(res).to.be.equal(true)
133
134
    // unstub
135
    sinon.assert.calledOnce(bcrypt.compareSync)
136
    bcrypt.compareSync.restore()
137
  })
138
139
  it('User.utils.findSync', function(){
140
    // stub
141
    var sinonInstance = sinon.sandbox.create();
142
    var stubGet = sinonInstance.stub(User.manager.instance, 'get');
143
    stubGet.returns(JSON.parse(JSON.stringify(this.fixture.users)))
144
145
    // test
146
    var user = User.utils.findSync(1)
147
    chai.expect(user).to.not.be.undefined
0 ignored issues
show
The result of the property access to chai.expect(user).to.not.be.undefined is not used.
Loading history...
148
    chai.expect(user.username).to.equal('test')
149
150
    // unstub
151
    sinon.assert.calledOnce(User.manager.instance.get)
152
    User.manager.instance.get.restore()
153
  })
154
155
  it('User.utils.find', function(done){
156
    // stub
157
    var sinonInstance = sinon.sandbox.create();
158
    var stubGet = sinonInstance.stub(User.manager.instance, 'get');
159
    stubGet.returns(JSON.parse(JSON.stringify(this.fixture.users)))
160
161
    // test
162
    var user = User.utils.find(1, function (err, user) {
0 ignored issues
show
The variable user seems to be never used. Consider removing it.
Loading history...
163
      chai.expect(user).to.not.be.undefined
0 ignored issues
show
The result of the property access to chai.expect(user).to.not.be.undefined is not used.
Loading history...
164
      chai.expect(user.username).to.equal('test')
165
166
      // unstub
167
      sinon.assert.calledOnce(User.manager.instance.get)
168
      User.manager.instance.get.restore()
169
      done()
170
    })
171
  })
172
173
  it('User.utils.checkSameEmail', function(){
174
    // stub
175
    var sinonInstance = sinon.sandbox.create();
176
    var stubGet = sinonInstance.stub(User.manager.instance, 'get');
177
    stubGet.returns(JSON.parse(JSON.stringify(this.fixture.users)))
178
179
    // test
180
    var user = JSON.parse(JSON.stringify(this.fixture.users))[0]
181
    user.id = 2
182
    var res = User.utils.checkSameEmail(user)
183
    chai.expect(res.success).to.equal(0)
184
185
    // unstub
186
    sinon.assert.calledOnce(User.manager.instance.get)
187
    User.manager.instance.get.restore()
188
  })
189
190
  it('User.utils.getRole', function(){
191
    // stub
192
193
    // test
194
    var user = JSON.parse(JSON.stringify(this.fixture.users))[0]
195
    user.role = "admin"
196
    var res = User.utils.getRole(JSON.parse(JSON.stringify(this.fixture.users))[0])
197
    chai.expect(res.role).to.not.be.equal("admin")
198
199
    // unstub
200
  })
201
202
  it('User.utils.commonPassword', function(){
203
    // stub
204
205
    // test
206
    var user = JSON.parse(JSON.stringify(this.fixture.users))[0]
207
    user.password = "password"
208
    var res = User.utils.commonPassword(user)
209
    chai.expect(res.success).to.be.equal(0)
210
211
    // unstub
212
  })
213
214
  it('User.utils.encryptPassword', function(){
215
    // stub
216
    var sinonInstance = sinon.sandbox.create();
217
    var stubGenSaltSync = sinonInstance.stub(bcrypt, 'genSaltSync');
218
    stubGenSaltSync.returns(10)
219
    var stubHashSync = sinonInstance.stub(bcrypt, 'hashSync');
220
    stubHashSync.returns("test2")
221
222
    // test
223
    var user = JSON.parse(JSON.stringify(this.fixture.users))[0]
224
    user.password = "password"
225
    var res = User.utils.encryptPassword(10, "test")
226
    chai.expect(res).to.not.be.equal("test")
227
228
    // unstub
229
    sinon.assert.calledOnce(bcrypt.genSaltSync)
230
    bcrypt.genSaltSync.restore()
231
    sinon.assert.calledOnce(bcrypt.hashSync)
232
    bcrypt.hashSync.restore()
233
  })
234
235
  it('User.utils.isUserAllowedOnRoute', function(){
236
    // stub
237
238
    // test
239
    var user = JSON.parse(JSON.stringify(this.fixture.users))[0]
0 ignored issues
show
The variable user seems to be never used. Consider removing it.
Loading history...
240
    var res = User.utils.isUserAllowedOnRoute("admin", "/abe/test")
241
    chai.expect(res).to.be.equal(true)
242
243
    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 240. 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...
244
    chai.expect(res).to.be.equal(false)
245
246
    // unstub
247
  })
248
249
  it('User.utils.getUserWorkflow', function(){
250
    // stub
251
252
    // test
253
    config.users.enable = false
254
    var res = User.utils.getUserWorkflow()
255
    chai.expect(res.length).to.be.equal(2)
256
257
    config.users.enable = true
258
    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 254. 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...
259
    chai.expect(res.length).to.be.equal(2)
260
    // unstub
261
  })
262
263
  it('User.utils.loginLimitTry', function(done){
264
    // stub
265
266
    // test
267
    var res = User.utils.loginLimitTry()
0 ignored issues
show
The variable res seems to be never used. Consider removing it.
Loading history...
268
      .then(function () {
269
        done()
270
      })
271
272
    // unstub
273
  })
274
});