/
misc.test.js
119 lines (102 loc) · 3.16 KB
/
misc.test.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
/* eslint-disable security/detect-non-literal-fs-filename */
const chai = require('chai')
const misc = require('../server/utils/misc')
const rewire = require('rewire')
const rewiredMisc = rewire('../server/utils/misc.js')
const sinon = require('sinon')
const assert = chai.assert
describe('misc.randomSecret', () => {
it('should exist', () => {
assert.exists(misc.randomSecret)
})
it('should be function', () => {
assert.isFunction(misc.randomSecret)
})
it('should call crypto once', () => {
const crypto = rewiredMisc.__get__('crypto')
const randomBytesSpy = sinon.spy(crypto, 'randomBytes')
const randomSecret = rewiredMisc.__get__('randomSecret')
randomSecret()
sinon.assert.calledOnce(randomBytesSpy)
})
})
describe('misc.arrify', () => {
/* This functions aims at transforming every key value
of an object in the following way:
"" --> []
"hi" --> ["hi"]
["hi", "there"] --> ["hi", "there"]
[{"attr":"hi"}, {"attr":"there"}] --> ['{"attr":"hi"}', '{"attr":"there"}']
{"attr":"hi"} --> ['{"attr":"hi"}']
[] --> []
null --> []
undefined --> []
Object members which are functions are dropped
*/
it('arrify should transform { "mail" : ""} in { mail : [] } ', () => {
assert.deepEqual(
misc.arrify({ mail: '' }),
{ mail: [] }
)
})
it('arrify should transform { "username" : "johndoe" }' +
' in { "username" : ["johndoe"] }', () => {
assert.deepEqual(
misc.arrify({ username: 'johndoe' }),
{ username: ['johndoe'] }
)
})
it('arrify should transform ' +
'{ "mail" : [ "business@mail.com" , "personal@mail.com"] }' +
' in { "mail" : [ "business@mail.com" , "personal@mail.com"] }', () => {
assert.deepEqual(
misc.arrify({
mail:
['business@mail.com', 'personal@mail.com']
}),
{ mail: ['business@mail.com', 'personal@mail.com'] }
)
})
it(' { "job" : [{"company":"gluu"}, {"role":"devops"}] } ' +
'in { "job" : ["{"company":"gluu"}", "{"role":"devops"}"] }', () => {
assert.deepEqual(
misc.arrify(
{ job: [{ company: 'gluu' }, { role: 'devops' }] }
),
{ job: ['{"company":"gluu"}', '{"role":"devops"}'] }
)
})
it(' { "mail" : { "gmail" : "johndoe@gmail.com" } } ' +
'in { "mail" : ["{"gmail" : "johndoe@gmail.com"}"] }', () => {
assert.deepEqual(
misc.arrify({ mail: { gmail: 'johndoe@gmail.com' } }),
{ mail: ['{"gmail":"johndoe@gmail.com"}'] }
)
})
it(' { profiles : [] } --> { profiles : [] }', () => {
assert.deepEqual(
misc.arrify({ profiles: [] }),
{ profiles: [] }
)
})
it(' { profiles : undefined } --> { profiles : [] }', () => {
assert.deepEqual(
misc.arrify({ profiles: undefined }),
{ profiles: [] }
)
})
it(' { profiles : null } --> { profiles : [] }', () => {
assert.deepEqual(
misc.arrify({ profiles: null }),
{ profiles: [] }
)
})
// Object members which are functions are dropped
it(' { key: function() { return 0; }, key2: "hi" } --> { key2: ["hi"] }',
() => {
assert.deepEqual(
misc.arrify({ key: function () { return 0 }, key2: 'hi' }),
{ key2: ['hi'] }
)
})
})