larvituser 0.18.1 → 0.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -99
- package/dist/dataWriter.d.ts +27 -0
- package/dist/dataWriter.d.ts.map +1 -0
- package/dist/dataWriter.js +205 -0
- package/dist/dataWriter.js.map +1 -0
- package/dist/helpers.d.ts +19 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +59 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.d.ts +156 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +418 -0
- package/dist/index.js.map +1 -0
- package/dist/userBase.d.ts +53 -0
- package/dist/userBase.d.ts.map +1 -0
- package/dist/userBase.js +120 -0
- package/dist/userBase.js.map +1 -0
- package/dist/users.d.ts +43 -0
- package/dist/users.d.ts.map +1 -0
- package/dist/users.js +186 -0
- package/dist/users.js.map +1 -0
- package/package.json +27 -12
- package/.eslintignore +0 -1
- package/.eslintrc +0 -35
- package/.travis.yml +0 -22
- package/config/db_travis.json +0 -9
- package/controllers/adminUsers/edit.js +0 -180
- package/controllers/adminUsers/list.js +0 -47
- package/dataWriter.js +0 -715
- package/dbmigration/1.sql +0 -33
- package/dbmigration/2.js +0 -63
- package/helpers.js +0 -110
- package/index.js +0 -955
- package/public/tmpl/adminUsers/edit.tmpl +0 -71
- package/public/tmpl/adminUsers/list.tmpl +0 -22
- package/renovate.json +0 -9
- package/test/00test.js +0 -850
- package/test/98lint.js +0 -9
- package/users.js +0 -281
package/test/00test.js
DELETED
|
@@ -1,850 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const UserLib = require('../index.js'),
|
|
4
|
-
assert = require('assert'),
|
|
5
|
-
lUtils = new (require('larvitutils'))(),
|
|
6
|
-
async = require('async'),
|
|
7
|
-
log = new lUtils.Log('warning'),
|
|
8
|
-
db = require('larvitdb'),
|
|
9
|
-
fs = require('fs');
|
|
10
|
-
|
|
11
|
-
let userLib;
|
|
12
|
-
|
|
13
|
-
before(function (done) {
|
|
14
|
-
this.timeout(10000);
|
|
15
|
-
const tasks = [];
|
|
16
|
-
|
|
17
|
-
// Run DB Setup
|
|
18
|
-
tasks.push(function (cb) {
|
|
19
|
-
let confFile;
|
|
20
|
-
|
|
21
|
-
if (process.env.DBCONFFILE === undefined) {
|
|
22
|
-
confFile = __dirname + '/../config/db_test.json';
|
|
23
|
-
} else {
|
|
24
|
-
confFile = process.env.DBCONFFILE;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
log.verbose('DB config file: "' + confFile + '"');
|
|
28
|
-
|
|
29
|
-
// First look for absolute path
|
|
30
|
-
fs.stat(confFile, function (err) {
|
|
31
|
-
let conf;
|
|
32
|
-
|
|
33
|
-
if (err) {
|
|
34
|
-
|
|
35
|
-
// Then look for this string in the config folder
|
|
36
|
-
confFile = __dirname + '/../config/' + confFile;
|
|
37
|
-
fs.stat(confFile, function (err) {
|
|
38
|
-
if (err) throw err;
|
|
39
|
-
log.verbose('DB config: ' + JSON.stringify(require(confFile)));
|
|
40
|
-
|
|
41
|
-
conf = require(confFile);
|
|
42
|
-
conf.log = log;
|
|
43
|
-
db.setup(conf, cb);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
log.verbose('DB config: ' + JSON.stringify(require(confFile)));
|
|
50
|
-
conf = require(confFile);
|
|
51
|
-
conf.log = log;
|
|
52
|
-
db.setup(conf, cb);
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
// Check for empty db
|
|
57
|
-
tasks.push(function (cb) {
|
|
58
|
-
db.query('SHOW TABLES', function (err, rows) {
|
|
59
|
-
if (err) throw err;
|
|
60
|
-
|
|
61
|
-
if (rows.length) {
|
|
62
|
-
throw new Error('Database is not empty. To make a test, you must supply an empty database!');
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
cb();
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
tasks.push(function (cb) {
|
|
70
|
-
userLib = new UserLib({
|
|
71
|
-
'log': log,
|
|
72
|
-
'db': db
|
|
73
|
-
}, cb);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
async.series(tasks, done);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
describe('User', function () {
|
|
80
|
-
let createdUuid;
|
|
81
|
-
|
|
82
|
-
it('should check if a username is available', function (done) {
|
|
83
|
-
userLib.usernameAvailable('testuser', function (err, res) {
|
|
84
|
-
if (err) throw err;
|
|
85
|
-
assert.strictEqual(res, true);
|
|
86
|
-
done();
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
describe('fields', function () {
|
|
91
|
-
let fieldUuid;
|
|
92
|
-
|
|
93
|
-
it('should return an UUID for the field we are asking for', function (done) {
|
|
94
|
-
userLib.helpers.getFieldUuid('firstname', function (err, result) {
|
|
95
|
-
if (err) throw err;
|
|
96
|
-
fieldUuid = result;
|
|
97
|
-
assert.notStrictEqual(lUtils.formatUuid(fieldUuid), false);
|
|
98
|
-
done();
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('shold return field name "firstname" for the UUID we created above', function (done) {
|
|
103
|
-
userLib.helpers.getFieldName(fieldUuid, function (err, fieldName) {
|
|
104
|
-
if (err) throw err;
|
|
105
|
-
assert.strictEqual(fieldName, 'firstname');
|
|
106
|
-
done();
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
describe('passwordHash', function () {
|
|
112
|
-
let hashedPassword;
|
|
113
|
-
|
|
114
|
-
it('should create a hashed password', function (done) {
|
|
115
|
-
userLib.hashPassword('foobar', function (err, hash) {
|
|
116
|
-
if (err) throw err;
|
|
117
|
-
hashedPassword = hash;
|
|
118
|
-
done();
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it('should check the hashed password back against the plain text password', function (done) {
|
|
123
|
-
userLib.checkPassword('foobar', hashedPassword, function (err, res) {
|
|
124
|
-
if (err) throw err;
|
|
125
|
-
assert.strictEqual(res, true);
|
|
126
|
-
done();
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('should not crash when undefined is sent in', function (done) {
|
|
131
|
-
userLib.hashPassword(undefined, function (err) {
|
|
132
|
-
if (err) throw err;
|
|
133
|
-
done();
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
describe('create', function () {
|
|
139
|
-
it('should create a new user with random uuid', function (done) {
|
|
140
|
-
userLib.create('lilleman', 'foobar', {'firstname': 'migal', 'lastname': ['Arvidsson', 'Göransson']}, function (err, user) {
|
|
141
|
-
if (err) throw err;
|
|
142
|
-
|
|
143
|
-
createdUuid = user.uuid;
|
|
144
|
-
|
|
145
|
-
assert.notStrictEqual(createdUuid, false);
|
|
146
|
-
assert.strictEqual(user.fields.lastname[1], 'Göransson');
|
|
147
|
-
assert(typeof user.uuid === 'string', 'uuid should be a string');
|
|
148
|
-
assert(user.uuid.length === 36, 'uuid should be exactly 36 characters long');
|
|
149
|
-
done();
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it('should try to create a new user with the same username and fail', function (done) {
|
|
154
|
-
userLib.create('lilleman', 'foobar', {'firstname': 'migal', 'lastname': ['Arvidsson', 'Göransson']}, function (err, user) {
|
|
155
|
-
assert.notEqual(err, null);
|
|
156
|
-
assert.strictEqual(user, undefined);
|
|
157
|
-
done();
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
it('should try to create a user with a field that is undefined', function (done) {
|
|
162
|
-
userLib.create('trams', false, {'firstname': undefined, 'lastname': ['biff', 'baff']}, function (err, user) {
|
|
163
|
-
if (err) throw err;
|
|
164
|
-
assert.notStrictEqual(user.uuid, undefined);
|
|
165
|
-
done();
|
|
166
|
-
});
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
describe('logins', function () {
|
|
172
|
-
it('should log the created user in by username', function (done) {
|
|
173
|
-
userLib.fromUsername('lilleman', function (err, user) {
|
|
174
|
-
if (err) throw err;
|
|
175
|
-
assert(user.uuid !== undefined, 'uuid should be set');
|
|
176
|
-
assert(user.uuid === createdUuid, 'uuid should match the earlier created uuid');
|
|
177
|
-
done();
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
it('should log the created user in by username and password', function (done) {
|
|
182
|
-
userLib.fromUserAndPass('lilleman', 'foobar', function (err, user) {
|
|
183
|
-
if (err) throw err;
|
|
184
|
-
assert(user.uuid === createdUuid, 'uuid should match the earlier created uuid');
|
|
185
|
-
done();
|
|
186
|
-
});
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
it('should fail to log the created user in by username and password', function (done) {
|
|
190
|
-
userLib.fromUserAndPass('lilleman', 'nisse', function (err, user) {
|
|
191
|
-
if (err) throw err;
|
|
192
|
-
assert(user === false, 'user should be false');
|
|
193
|
-
done();
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
it('should fail to log in a non existing user by username and password', function (done) {
|
|
198
|
-
userLib.fromUserAndPass('does_not_exist', 'foobar', function (err, user) {
|
|
199
|
-
if (err) throw err;
|
|
200
|
-
assert(user === false, 'user should be false');
|
|
201
|
-
done();
|
|
202
|
-
});
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
it('should log in user by field', function (done) {
|
|
206
|
-
userLib.fromField('firstname', 'migal', function (err, user) {
|
|
207
|
-
if (err) throw err;
|
|
208
|
-
assert.notStrictEqual(user, false);
|
|
209
|
-
done();
|
|
210
|
-
});
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
it('should fail to log in user by an errorous field', function (done) {
|
|
214
|
-
userLib.fromField('firstname', 'mupp', function (err, user) {
|
|
215
|
-
if (err) throw err;
|
|
216
|
-
assert.strictEqual(user, false);
|
|
217
|
-
done();
|
|
218
|
-
});
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
it('should log in user by multiple fields', function (done) {
|
|
222
|
-
userLib.fromFields({'firstname': 'migal', 'lastname': 'Arvidsson'}, function (err, user) {
|
|
223
|
-
if (err) throw err;
|
|
224
|
-
assert.notStrictEqual(user, false);
|
|
225
|
-
done();
|
|
226
|
-
});
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
it('should fail to log in user by multiple fields when one is wrong', function (done) {
|
|
230
|
-
userLib.fromFields({'firstname': 'migal', 'lastname': 'no its not'}, function (err, user) {
|
|
231
|
-
if (err) throw err;
|
|
232
|
-
assert.strictEqual(user, false);
|
|
233
|
-
done();
|
|
234
|
-
});
|
|
235
|
-
});
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
describe('fields on logged in user', function () {
|
|
239
|
-
it('should remove a field from a logged in user', function (done) {
|
|
240
|
-
userLib.fromUsername('lilleman', function (err, user) {
|
|
241
|
-
if (err) throw err;
|
|
242
|
-
assert.deepEqual(user.fields.firstname, ['migal']);
|
|
243
|
-
user.rmField('firstname', function () {
|
|
244
|
-
assert.strictEqual(user.fields.firstname, undefined);
|
|
245
|
-
assert.strictEqual(user.fields.lastname[0], 'Arvidsson');
|
|
246
|
-
|
|
247
|
-
// Trying to load the user again to be sure
|
|
248
|
-
userLib.fromUsername('lilleman', function (err, user) {
|
|
249
|
-
if (err) throw err;
|
|
250
|
-
assert.strictEqual(user.fields.firstname, undefined);
|
|
251
|
-
|
|
252
|
-
done();
|
|
253
|
-
});
|
|
254
|
-
});
|
|
255
|
-
});
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
it('should set a field on a logged in user', function (done) {
|
|
259
|
-
userLib.fromUsername('lilleman', function (err, user) {
|
|
260
|
-
if (err) throw err;
|
|
261
|
-
user.addField('cell', 46709771337, function (err) {
|
|
262
|
-
if (err) throw err;
|
|
263
|
-
|
|
264
|
-
assert.strictEqual(user.fields.cell[0], 46709771337);
|
|
265
|
-
assert.strictEqual(user.fields.lastname[0], 'Arvidsson');
|
|
266
|
-
done();
|
|
267
|
-
});
|
|
268
|
-
});
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
it('should replace fields with new data', function (done) {
|
|
272
|
-
userLib.fromUsername('lilleman', function (err, user) {
|
|
273
|
-
const newFields = {
|
|
274
|
-
'foo': 'bar',
|
|
275
|
-
'income': [670, 'more than you']
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
if (err) throw err;
|
|
279
|
-
|
|
280
|
-
user.replaceFields(newFields, function (err) {
|
|
281
|
-
if (err) throw err;
|
|
282
|
-
|
|
283
|
-
assert.deepEqual(user.fields.foo, ['bar']);
|
|
284
|
-
assert.strictEqual(user.fields.firstname, undefined);
|
|
285
|
-
assert.strictEqual(user.fields.income[1], 'more than you');
|
|
286
|
-
|
|
287
|
-
// Reload user to make sure the fields are saved in database correctly
|
|
288
|
-
userLib.fromUsername('lilleman', function (err, secondUser) {
|
|
289
|
-
if (err) throw err;
|
|
290
|
-
|
|
291
|
-
assert.deepEqual(secondUser.fields.foo, ['bar']);
|
|
292
|
-
assert.strictEqual(secondUser.fields.firstname, undefined);
|
|
293
|
-
assert.strictEqual(secondUser.fields.income[1], 'more than you');
|
|
294
|
-
|
|
295
|
-
done();
|
|
296
|
-
});
|
|
297
|
-
});
|
|
298
|
-
});
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
it('should get field data from any user', function (done) {
|
|
302
|
-
userLib.fromUsername('lilleman', function (err, user) {
|
|
303
|
-
if (err) throw err;
|
|
304
|
-
|
|
305
|
-
userLib.getFieldData(user.uuid, 'foo', function (err, data) {
|
|
306
|
-
if (err) throw err;
|
|
307
|
-
assert.deepEqual(data, ['bar']);
|
|
308
|
-
done();
|
|
309
|
-
});
|
|
310
|
-
});
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
it('should set a new password for a user', function (done) {
|
|
314
|
-
userLib.fromUsername('lilleman', function (err, user) {
|
|
315
|
-
if (err) throw err;
|
|
316
|
-
|
|
317
|
-
assert(user !== false, 'The user object should not be false');
|
|
318
|
-
|
|
319
|
-
user.setPassword('biffelbaffel', function (err) {
|
|
320
|
-
if (err) throw err;
|
|
321
|
-
|
|
322
|
-
userLib.fromUserAndPass('lilleman', 'biffelbaffel', function (err, user) {
|
|
323
|
-
if (err) throw err;
|
|
324
|
-
|
|
325
|
-
assert(user !== false, 'The user object should not be false');
|
|
326
|
-
|
|
327
|
-
user.setPassword('BOOM', function (err) {
|
|
328
|
-
if (err) throw err;
|
|
329
|
-
|
|
330
|
-
userLib.fromUserAndPass('lilleman', 'biffelbaffel', function (err, user) {
|
|
331
|
-
if (err) throw err;
|
|
332
|
-
|
|
333
|
-
assert(user === false, 'The user object should be false');
|
|
334
|
-
done();
|
|
335
|
-
});
|
|
336
|
-
});
|
|
337
|
-
});
|
|
338
|
-
});
|
|
339
|
-
});
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
it('should add a new field along side existing fields', function (done) {
|
|
343
|
-
userLib.fromUsername('lilleman', function (err, user) {
|
|
344
|
-
if (err) throw err;
|
|
345
|
-
assert.notStrictEqual(user, false);
|
|
346
|
-
assert.strictEqual(user.fields.foo.length, 1);
|
|
347
|
-
assert.strictEqual(user.fields.foo[0], 'bar');
|
|
348
|
-
|
|
349
|
-
user.addFields({'foo': ['yes', 'no', 'bar']}, function (err) {
|
|
350
|
-
if (err) throw err;
|
|
351
|
-
|
|
352
|
-
assert.strictEqual(user.fields.foo.length, 4);
|
|
353
|
-
|
|
354
|
-
done();
|
|
355
|
-
});
|
|
356
|
-
});
|
|
357
|
-
});
|
|
358
|
-
|
|
359
|
-
it('should set no fields on a user', function (done) {
|
|
360
|
-
userLib.fromUsername('trams', function (err, user1) {
|
|
361
|
-
if (err) throw err;
|
|
362
|
-
|
|
363
|
-
userLib.addUserDataFields(user1.uuid, {}, function (err) {
|
|
364
|
-
if (err) throw err;
|
|
365
|
-
|
|
366
|
-
userLib.fromUsername('trams', function (err, user2) {
|
|
367
|
-
if (err) throw err;
|
|
368
|
-
|
|
369
|
-
assert.deepEqual(user1.fields, user2.fields);
|
|
370
|
-
|
|
371
|
-
done();
|
|
372
|
-
});
|
|
373
|
-
});
|
|
374
|
-
});
|
|
375
|
-
});
|
|
376
|
-
});
|
|
377
|
-
|
|
378
|
-
describe('set new username', function () {
|
|
379
|
-
it('should set a new username', function (done) {
|
|
380
|
-
let userUuid;
|
|
381
|
-
|
|
382
|
-
userLib.create('habblabang', false, {}, function (err, user) {
|
|
383
|
-
if (err) throw err;
|
|
384
|
-
|
|
385
|
-
userUuid = user.uuid;
|
|
386
|
-
|
|
387
|
-
userLib.setUsername(userUuid, 'blambadam', function (err) {
|
|
388
|
-
if (err) throw err;
|
|
389
|
-
|
|
390
|
-
userLib.fromUsername('blambadam', function (err, user) {
|
|
391
|
-
if (err) throw err;
|
|
392
|
-
|
|
393
|
-
assert(user.uuid === userUuid, 'User Uuids missmatch! Is "' + user.uuid + '" but should be "' + userUuid + '"');
|
|
394
|
-
done();
|
|
395
|
-
});
|
|
396
|
-
});
|
|
397
|
-
});
|
|
398
|
-
});
|
|
399
|
-
|
|
400
|
-
it('should refresh read the new value back on the user object', function (done) {
|
|
401
|
-
userLib.create('böb', false, {}, function (err, user) {
|
|
402
|
-
if (err) throw err;
|
|
403
|
-
|
|
404
|
-
user.setUsername('untz-lord-69', function (err) {
|
|
405
|
-
if (err) throw err;
|
|
406
|
-
|
|
407
|
-
assert.strictEqual(user.username, 'untz-lord-69');
|
|
408
|
-
done();
|
|
409
|
-
});
|
|
410
|
-
});
|
|
411
|
-
});
|
|
412
|
-
});
|
|
413
|
-
|
|
414
|
-
describe('set new password', function () {
|
|
415
|
-
let user;
|
|
416
|
-
|
|
417
|
-
it('should log the created user in by username', function (done) {
|
|
418
|
-
userLib.fromUsername('lilleman', function (err, result) {
|
|
419
|
-
if (err) throw err;
|
|
420
|
-
assert.equal(result.username, 'lilleman');
|
|
421
|
-
user = result;
|
|
422
|
-
done();
|
|
423
|
-
});
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
it('should set new password', function (done) {
|
|
427
|
-
userLib.setPassword(user.uuid, 'secretpassword', function (err) {
|
|
428
|
-
if (err) throw err;
|
|
429
|
-
done();
|
|
430
|
-
});
|
|
431
|
-
});
|
|
432
|
-
|
|
433
|
-
it('should log the user in by the new password', function (done) {
|
|
434
|
-
userLib.fromUserAndPass(user.username, 'secretpassword', function (err, result) {
|
|
435
|
-
if (err) throw err;
|
|
436
|
-
assert(user.uuid === result.uuid, 'uuid should match the earlier created uuid');
|
|
437
|
-
done();
|
|
438
|
-
});
|
|
439
|
-
});
|
|
440
|
-
});
|
|
441
|
-
|
|
442
|
-
describe('remove user', function () {
|
|
443
|
-
it('should remove a user', function (done) {
|
|
444
|
-
userLib.rmUser(createdUuid, function (err) {
|
|
445
|
-
if (err) throw err;
|
|
446
|
-
|
|
447
|
-
db.query('SELECT * FROM user_users WHERE uuid = ?', [lUtils.uuidToBuffer(createdUuid)], function (err, rows) {
|
|
448
|
-
if (err) throw err;
|
|
449
|
-
|
|
450
|
-
assert.strictEqual(rows.length, 0);
|
|
451
|
-
done();
|
|
452
|
-
});
|
|
453
|
-
});
|
|
454
|
-
});
|
|
455
|
-
});
|
|
456
|
-
|
|
457
|
-
describe('Get list of users', function () {
|
|
458
|
-
const uuids = [];
|
|
459
|
-
|
|
460
|
-
this.timeout(5000);
|
|
461
|
-
|
|
462
|
-
it('Get list of users', function (done) {
|
|
463
|
-
const tasks = [];
|
|
464
|
-
|
|
465
|
-
tasks.push(function (cb) {
|
|
466
|
-
userLib.create('user1', 'somepassword', { 'role' : ['customer', 'user'], 'veryUnique': ['muchUnique']}, function (err, user) {
|
|
467
|
-
uuids.push(user.uuid);
|
|
468
|
-
if (err) throw err;
|
|
469
|
-
cb();
|
|
470
|
-
});
|
|
471
|
-
});
|
|
472
|
-
|
|
473
|
-
tasks.push(function (cb) {
|
|
474
|
-
userLib.create('user2', 'somepassword', { 'role' : ['not customer', 'user'], 'lastname': ['biff', 'bonk']}, function (err, user) {
|
|
475
|
-
uuids.push(user.uuid);
|
|
476
|
-
if (err) throw err;
|
|
477
|
-
cb();
|
|
478
|
-
});
|
|
479
|
-
});
|
|
480
|
-
|
|
481
|
-
tasks.push(function (cb) {
|
|
482
|
-
let users = new UserLib.Users({'db': db, 'log': log});
|
|
483
|
-
|
|
484
|
-
users.get(function (err, userList) {
|
|
485
|
-
let foundUser1 = false,
|
|
486
|
-
foundUser2 = false;
|
|
487
|
-
|
|
488
|
-
if (err) throw err;
|
|
489
|
-
|
|
490
|
-
assert(userList.length >= 2, '2 or more users should exist in database');
|
|
491
|
-
|
|
492
|
-
for (let i = 0; i < userList.length; i ++) {
|
|
493
|
-
if (userList[i].uuid === uuids[0]) {
|
|
494
|
-
foundUser1 = true;
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
if (userList[i].uuid === uuids[1]) {
|
|
498
|
-
foundUser2 = true;
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
assert.strictEqual(foundUser1, true, 'user1 not found');
|
|
503
|
-
assert.strictEqual(foundUser2, true, 'user2 not found');
|
|
504
|
-
|
|
505
|
-
cb();
|
|
506
|
-
});
|
|
507
|
-
});
|
|
508
|
-
|
|
509
|
-
async.series(tasks, done);
|
|
510
|
-
});
|
|
511
|
-
|
|
512
|
-
it('Get list of users with matching fields', function (done) {
|
|
513
|
-
const users = new UserLib.Users({'db': db, 'log': log});
|
|
514
|
-
|
|
515
|
-
users.matchAllFields = { 'role': ['customer']};
|
|
516
|
-
users.returnFields = [];
|
|
517
|
-
|
|
518
|
-
users.get(function (err, userList, totalElements) {
|
|
519
|
-
if (err) throw err;
|
|
520
|
-
assert.strictEqual(totalElements, 1);
|
|
521
|
-
assert.strictEqual(userList.length, 1);
|
|
522
|
-
assert.strictEqual(userList[0].username, 'user1');
|
|
523
|
-
|
|
524
|
-
done();
|
|
525
|
-
});
|
|
526
|
-
});
|
|
527
|
-
|
|
528
|
-
it('Get list of users with query match on a specific field', function (done) {
|
|
529
|
-
const tasks = [];
|
|
530
|
-
|
|
531
|
-
// Create test user that should match
|
|
532
|
-
tasks.push(function (cb) {
|
|
533
|
-
userLib.create('match1', 'somepassword', { 'code' : ['abc999'], 'code2': 'hej' }, function (err, user) {
|
|
534
|
-
uuids.push(user.uuid);
|
|
535
|
-
if (err) throw err;
|
|
536
|
-
cb();
|
|
537
|
-
});
|
|
538
|
-
});
|
|
539
|
-
|
|
540
|
-
// Create another test user that should match
|
|
541
|
-
tasks.push(function (cb) {
|
|
542
|
-
userLib.create('match2', 'somepassword', { 'code' : ['code_abc'], 'code2': 'jeh' }, function (err, user) {
|
|
543
|
-
uuids.push(user.uuid);
|
|
544
|
-
if (err) throw err;
|
|
545
|
-
cb();
|
|
546
|
-
});
|
|
547
|
-
});
|
|
548
|
-
|
|
549
|
-
// Create test user that shouldn't match
|
|
550
|
-
tasks.push(function (cb) {
|
|
551
|
-
userLib.create('noMatch', 'somepassword', { 'code' : ['def123'], 'code2': 'abc' }, function (err, user) {
|
|
552
|
-
uuids.push(user.uuid);
|
|
553
|
-
if (err) throw err;
|
|
554
|
-
cb();
|
|
555
|
-
});
|
|
556
|
-
});
|
|
557
|
-
|
|
558
|
-
// Match on specific field
|
|
559
|
-
tasks.push(function (cb) {
|
|
560
|
-
const users = new UserLib.Users({'db': db, 'log': log});
|
|
561
|
-
|
|
562
|
-
users.matchAllFieldsQ = { 'code': 'abc' };
|
|
563
|
-
users.returnFields = [];
|
|
564
|
-
|
|
565
|
-
users.get(function (err, userList, totalElements) {
|
|
566
|
-
if (err) throw err;
|
|
567
|
-
assert.strictEqual(totalElements, 2);
|
|
568
|
-
assert.strictEqual(userList.length, 2);
|
|
569
|
-
|
|
570
|
-
assert.ok(userList.find((user) => user.username === 'match1'));
|
|
571
|
-
assert.ok(userList.find((user) => user.username === 'match2'));
|
|
572
|
-
cb();
|
|
573
|
-
});
|
|
574
|
-
});
|
|
575
|
-
|
|
576
|
-
// Match on specific fields
|
|
577
|
-
tasks.push(function (cb) {
|
|
578
|
-
const users = new UserLib.Users({'db': db, 'log': log});
|
|
579
|
-
|
|
580
|
-
users.matchAllFieldsQ = { 'code': 'abc', 'code2': 'he' };
|
|
581
|
-
users.returnFields = [];
|
|
582
|
-
|
|
583
|
-
users.get(function (err, userList, totalElements) {
|
|
584
|
-
if (err) throw err;
|
|
585
|
-
assert.strictEqual(totalElements, 1);
|
|
586
|
-
assert.strictEqual(userList.length, 1);
|
|
587
|
-
|
|
588
|
-
assert.ok(userList.find((user) => user.username === 'match1'));
|
|
589
|
-
cb();
|
|
590
|
-
});
|
|
591
|
-
});
|
|
592
|
-
|
|
593
|
-
async.series(tasks, done);
|
|
594
|
-
});
|
|
595
|
-
|
|
596
|
-
it('Get list of data values for field', function (done) {
|
|
597
|
-
const users = new UserLib.Users({'db': db, 'log': log});
|
|
598
|
-
|
|
599
|
-
users.getFieldData('lastname', function (err, result) {
|
|
600
|
-
if (err) throw err;
|
|
601
|
-
assert.strictEqual(result.length, 3);
|
|
602
|
-
|
|
603
|
-
assert.strictEqual(result.indexOf('biff') > - 1, true);
|
|
604
|
-
assert.strictEqual(result.indexOf('baff') > - 1, true);
|
|
605
|
-
assert.strictEqual(result.indexOf('bonk') > - 1, true);
|
|
606
|
-
done();
|
|
607
|
-
});
|
|
608
|
-
});
|
|
609
|
-
|
|
610
|
-
it('Get list of users with requested field data', function (done) {
|
|
611
|
-
const users = new UserLib.Users({'db': db, 'log': log});
|
|
612
|
-
|
|
613
|
-
users.returnFields = ['lastname'];
|
|
614
|
-
|
|
615
|
-
users.get(function (err, result) {
|
|
616
|
-
const expectedFields = ['uuid', 'username', 'lastname'];
|
|
617
|
-
|
|
618
|
-
if (err) throw err;
|
|
619
|
-
assert.notStrictEqual(result, undefined);
|
|
620
|
-
assert.strictEqual(result.length, 8);
|
|
621
|
-
|
|
622
|
-
for (let r of result) {
|
|
623
|
-
for (let key in r) {
|
|
624
|
-
assert.strictEqual(expectedFields.indexOf(key) > - 1, true);
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
done();
|
|
629
|
-
});
|
|
630
|
-
});
|
|
631
|
-
|
|
632
|
-
it('Get list of users where fieldData exists', function (done) {
|
|
633
|
-
const users = new UserLib.Users({'db': db, 'log': log});
|
|
634
|
-
|
|
635
|
-
users.matchExistingFields = ['veryUnique'];
|
|
636
|
-
|
|
637
|
-
users.get(function (err, userList, totalElements) {
|
|
638
|
-
if (err) throw err;
|
|
639
|
-
|
|
640
|
-
assert.strictEqual(totalElements, 1);
|
|
641
|
-
assert.strictEqual(userList.length, 1);
|
|
642
|
-
assert.strictEqual(userList[0].username, 'user1');
|
|
643
|
-
|
|
644
|
-
done();
|
|
645
|
-
});
|
|
646
|
-
});
|
|
647
|
-
|
|
648
|
-
it('Get list of users sorted on username with ascending order', function (done) {
|
|
649
|
-
const users = new UserLib.Users({'db': db, 'log': log});
|
|
650
|
-
|
|
651
|
-
users.order = {
|
|
652
|
-
'by': 'username',
|
|
653
|
-
'direction': 'asc'
|
|
654
|
-
};
|
|
655
|
-
|
|
656
|
-
users.get(function (err, userList, totalElements) {
|
|
657
|
-
if (err) throw err;
|
|
658
|
-
|
|
659
|
-
assert.strictEqual(totalElements, 8);
|
|
660
|
-
assert.strictEqual(userList[0].username, 'blambadam');
|
|
661
|
-
done();
|
|
662
|
-
});
|
|
663
|
-
});
|
|
664
|
-
|
|
665
|
-
it('Get list of users sorted on username with descending order', function (done) {
|
|
666
|
-
const users = new UserLib.Users({'db': db, 'log': log});
|
|
667
|
-
|
|
668
|
-
users.order = {
|
|
669
|
-
'by': 'username',
|
|
670
|
-
'direction': 'desc'
|
|
671
|
-
};
|
|
672
|
-
|
|
673
|
-
users.get(function (err, userList, totalElements) {
|
|
674
|
-
if (err) throw err;
|
|
675
|
-
|
|
676
|
-
assert.strictEqual(totalElements, 8);
|
|
677
|
-
assert.strictEqual(userList[0].username, 'user2');
|
|
678
|
-
|
|
679
|
-
done();
|
|
680
|
-
});
|
|
681
|
-
});
|
|
682
|
-
|
|
683
|
-
it('Get list of users with sorted on a data field', function (done) {
|
|
684
|
-
const tasks = [];
|
|
685
|
-
|
|
686
|
-
// Get users and add sortable data
|
|
687
|
-
tasks.push(function (cb) {
|
|
688
|
-
const users = new UserLib.Users({'db': db, 'log': log}),
|
|
689
|
-
subtasks = [];
|
|
690
|
-
|
|
691
|
-
users.get(function (err, userList) {
|
|
692
|
-
if (err) throw err;
|
|
693
|
-
|
|
694
|
-
for (let i = 0; userList[i] !== undefined; i ++) {
|
|
695
|
-
subtasks.push(function (cb) {
|
|
696
|
-
userLib.addUserDataField(userList[i].uuid, 'sortable', 'sortvalue' + i, cb);
|
|
697
|
-
});
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
async.series(subtasks, cb);
|
|
701
|
-
});
|
|
702
|
-
});
|
|
703
|
-
|
|
704
|
-
// Get the same userlist with users sorted by "sortable" as ascending
|
|
705
|
-
tasks.push(function (cb) {
|
|
706
|
-
const users = new UserLib.Users({'db': db, 'log': log});
|
|
707
|
-
|
|
708
|
-
users.returnFields = ['sortable'];
|
|
709
|
-
|
|
710
|
-
users.order = {
|
|
711
|
-
'by': 'sortable',
|
|
712
|
-
'direction': 'asc'
|
|
713
|
-
};
|
|
714
|
-
|
|
715
|
-
users.get(function (err, userList, totalElements) {
|
|
716
|
-
if (err) throw err;
|
|
717
|
-
|
|
718
|
-
assert.strictEqual(totalElements, 8);
|
|
719
|
-
assert.deepStrictEqual(userList[0].sortable, ['sortvalue0']);
|
|
720
|
-
cb();
|
|
721
|
-
});
|
|
722
|
-
});
|
|
723
|
-
|
|
724
|
-
// Get the same userlist with users sorted by "sortable" as descending
|
|
725
|
-
tasks.push(function (cb) {
|
|
726
|
-
const users = new UserLib.Users({'db': db, 'log': log});
|
|
727
|
-
|
|
728
|
-
users.returnFields = ['sortable'];
|
|
729
|
-
|
|
730
|
-
users.order = {
|
|
731
|
-
'by': 'sortable',
|
|
732
|
-
'direction': 'desc'
|
|
733
|
-
};
|
|
734
|
-
|
|
735
|
-
users.get(function (err, userList, totalElements) {
|
|
736
|
-
if (err) throw err;
|
|
737
|
-
|
|
738
|
-
assert.strictEqual(totalElements, 8);
|
|
739
|
-
assert.deepStrictEqual(userList[0].sortable, ['sortvalue7']);
|
|
740
|
-
cb();
|
|
741
|
-
});
|
|
742
|
-
});
|
|
743
|
-
|
|
744
|
-
async.series(tasks, function (err) {
|
|
745
|
-
if (err) throw err;
|
|
746
|
-
|
|
747
|
-
done();
|
|
748
|
-
});
|
|
749
|
-
});
|
|
750
|
-
|
|
751
|
-
it('Should not be able to sort on a field value thats not added to returnFields', function (done) {
|
|
752
|
-
const users = new UserLib.Users({'db': db, 'log': log});
|
|
753
|
-
|
|
754
|
-
users.order = {
|
|
755
|
-
'by': 'sortable',
|
|
756
|
-
'direction': 'asc'
|
|
757
|
-
};
|
|
758
|
-
|
|
759
|
-
users.get(function (err) {
|
|
760
|
-
assert.notEqual(err, null);
|
|
761
|
-
done();
|
|
762
|
-
});
|
|
763
|
-
});
|
|
764
|
-
|
|
765
|
-
it('Should sort and concat fields with several values', function (done) {
|
|
766
|
-
const tasks = [];
|
|
767
|
-
|
|
768
|
-
// Get users and add sortable data
|
|
769
|
-
tasks.push(function (cb) {
|
|
770
|
-
const users = new UserLib.Users({'db': db, 'log': log}),
|
|
771
|
-
subtasks = [];
|
|
772
|
-
|
|
773
|
-
users.get(function (err, userList) {
|
|
774
|
-
if (err) throw err;
|
|
775
|
-
|
|
776
|
-
for (let i = 0; userList[i] !== undefined; i ++) {
|
|
777
|
-
subtasks.push(function (cb) {
|
|
778
|
-
userLib.addUserDataField(userList[i].uuid, 'sortable', 'other value', cb);
|
|
779
|
-
});
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
async.series(subtasks, cb);
|
|
783
|
-
});
|
|
784
|
-
});
|
|
785
|
-
|
|
786
|
-
// Get the same userlist with users sorted by "sortable" as ascending
|
|
787
|
-
tasks.push(function (cb) {
|
|
788
|
-
const users = new UserLib.Users({'db': db, 'log': log});
|
|
789
|
-
|
|
790
|
-
users.returnFields = ['sortable'];
|
|
791
|
-
|
|
792
|
-
users.order = {
|
|
793
|
-
'by': 'sortable',
|
|
794
|
-
'direction': 'asc'
|
|
795
|
-
};
|
|
796
|
-
|
|
797
|
-
users.get(function (err, userList, totalElements) {
|
|
798
|
-
if (err) throw err;
|
|
799
|
-
|
|
800
|
-
assert.strictEqual(totalElements, 8);
|
|
801
|
-
assert.deepStrictEqual(userList[0].sortable, ['sortvalue0', 'other value']);
|
|
802
|
-
cb();
|
|
803
|
-
});
|
|
804
|
-
});
|
|
805
|
-
|
|
806
|
-
// Get the same userlist with users sorted by "sortable" as descending
|
|
807
|
-
tasks.push(function (cb) {
|
|
808
|
-
const users = new UserLib.Users({'db': db, 'log': log});
|
|
809
|
-
|
|
810
|
-
users.returnFields = ['sortable'];
|
|
811
|
-
|
|
812
|
-
users.order = {
|
|
813
|
-
'by': 'sortable',
|
|
814
|
-
'direction': 'desc'
|
|
815
|
-
};
|
|
816
|
-
|
|
817
|
-
users.get(function (err, userList, totalElements) {
|
|
818
|
-
if (err) throw err;
|
|
819
|
-
|
|
820
|
-
assert.strictEqual(totalElements, 8);
|
|
821
|
-
assert.deepStrictEqual(userList[0].sortable, ['sortvalue7', 'other value']);
|
|
822
|
-
cb();
|
|
823
|
-
});
|
|
824
|
-
});
|
|
825
|
-
|
|
826
|
-
async.series(tasks, function (err) {
|
|
827
|
-
if (err) throw err;
|
|
828
|
-
done();
|
|
829
|
-
});
|
|
830
|
-
});
|
|
831
|
-
|
|
832
|
-
it('Get users by uuid', function (done) {
|
|
833
|
-
const users = new UserLib.Users({'db': db, 'log': log});
|
|
834
|
-
|
|
835
|
-
users.uuids = [uuids[1]];
|
|
836
|
-
|
|
837
|
-
users.get(function (err, userList) {
|
|
838
|
-
if (err) throw err;
|
|
839
|
-
|
|
840
|
-
assert.strictEqual(userList.length, 1);
|
|
841
|
-
assert.strictEqual(uuids[1], userList[0].uuid);
|
|
842
|
-
done();
|
|
843
|
-
});
|
|
844
|
-
});
|
|
845
|
-
});
|
|
846
|
-
});
|
|
847
|
-
|
|
848
|
-
after(function (done) {
|
|
849
|
-
db.removeAllTables(done);
|
|
850
|
-
});
|