forked from mkropat/secure-random-password
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.js
313 lines (260 loc) · 9.14 KB
/
index.spec.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
const randomPassword = require('./index').randomPassword;
describe('passwordGenerator', () => {
let random;
beforeEach(() => {
random = {
choose: jest.fn(),
shuffle: jest.fn(x => x),
};
random.choose.mockReturnValue('Q');
});
it('returns a string when called with no options', () => {
var actual = randomPassword();
expect(typeof actual).toBe('string');
});
it('throws an error when passed a length that is not an integer', () => {
expect(() => randomPassword({ length: 'not-an-integer' })).toThrow('length must be an integer');
});
it('throws an error when passed a length less than 1', () => {
expect(() => randomPassword({ length: 0 })).toThrow('length must be > 0');
});
it('throws an error when passed characters that is falsy', () => {
const expectedError = 'Must pass one or more character sets';
expect(() => randomPassword({ characters: null })).toThrow(expectedError);
expect(() => randomPassword({ characters: false })).toThrow(expectedError);
expect(() => randomPassword({ characters: 0 })).toThrow(expectedError);
});
it('throws an error when passed a characters set array with no items', () => {
expect(() => randomPassword({ characters: [] })).toThrow('Must pass one or more character sets');
});
it('returns sequence with 1 character of given length when passed only one allowed character', () => {
random.choose.mockImplementation(x => x);
expect(randomPassword({
length: 3,
characters: 'a',
random
})).toBe('aaa');
expect(randomPassword({
length: 3,
characters: 'b',
random
})).toBe('bbb');
});
it('returns result of random.choose on characters for length = 1', () => {
let chooseReturnValues = {
'abc': 'b'
};
random.choose.mockImplementation(arg => chooseReturnValues[arg] || '');
let result = randomPassword({
length: 1,
characters: 'abc',
random
});
expect(result).toBe('b');
});
it('returns combined string of characters returned from random.choose (for `length` many characters)', () => {
let chooseReturnValues = {
'abc': ['c', 'b', 'a', 'a', 'b', 'c']
};
random.choose.mockImplementation(arg => chooseReturnValues[arg].shift() || '');
let result = randomPassword({
length: 6,
characters: 'abc',
random
});
expect(result).toBe('cbaabc');
});
describe('when passed multiple character sets', () => {
it('throws an error if the passed length is fewer than the number of sets passed', () => {
expect(() => randomPassword({
length: 1,
characters: ['abc', '123'],
random
})).toThrow('length must be >= # of character sets passed');
});
it('calls random.choose on each passed set once, then on a combination of all character sets for the remaining length, then shuffles the result', () => {
let chooseReturnValues = {
'abc': ['b'],
'123': ['2'],
'abc123': ['3', 'a', '2', 'b']
};
random.choose.mockImplementation(arg => chooseReturnValues[arg].shift() || '');
let shuffleReturnValues = {
'b23a2b': Array.from('b2a32b')
};
random.shuffle.mockImplementation(arg => shuffleReturnValues[Array.from(arg).join('')] || []);
let result = randomPassword({
length: 6,
characters: ['abc', '123'],
random
});
expect(result).toBe('b2a32b');
});
});
describe('charcters rules', () => {
it('accepts objects with a characters property in place of a string', () => {
random.choose.mockImplementation(x => x);
expect(randomPassword({
length: 3,
characters: { characters: 'a' },
random
})).toBe('aaa');
});
it('accepts an array of objects with a characters property in place of an array of strings', () => {
random.choose.mockImplementation(x => x);
expect(randomPassword({
length: 3,
characters: [{ characters: 'a' }],
random
})).toBe('aaa');
let chooseReturnValues = {
'abc': ['b'],
'123': ['2'],
'abc123': ['3', 'a', '2', 'b']
};
random.choose.mockImplementation(arg => chooseReturnValues[arg].shift() || '');
let result = randomPassword({
length: 6,
characters: [
{ characters: 'abc' },
{ characters: '123'}
],
random
});
expect(result).toBe('b23a2b');
});
it('accepts an exactly property that causes the associated character set to be chosen from exactly that many times', () => {
let chooseReturnValues = {
'abc': ['c', 'b', 'a'],
'123': ['3', '2', '1'],
};
random.choose.mockImplementation(arg => chooseReturnValues[arg].shift() || '');
let shuffleReturnValues = {
'c321ba': Array.from('abc123')
};
random.shuffle.mockImplementation(arg => shuffleReturnValues[Array.from(arg).join('')] || []);
let result = randomPassword({
length: 6,
characters: [
'abc',
{ characters: '123', exactly: 3 }
],
random
});
expect(result).toBe('abc123');
});
it('throws an error when length is less than the totaled number of exactly character sets + required sets', () => {
expect(() => randomPassword({
length: 42,
characters: [
'abc',
{ characters: '123', exactly: 42 }
]
})).toThrow('length is too short for character set rules');
});
it('throws an error when all sets are exactly sets and they are less than length', () => {
expect(() => randomPassword({
length: 42,
characters: [
{ characters: 'abc', exactly: 20 },
{ characters: '123', exactly: 21 },
]
})).toThrow('Must pass a set without exactly rule to generate the specified length');
});
});
describe('avoidAmbiguous option', () => {
it('accepts an avoidAmbiguous array of sets that prevents ambiguous characters from being chosen if multiple characters from any avoidAmbiguous set appear in the passed characters', () => {
let chooseReturnValues = {
'cd': ['c', 'd', 'c', 'd', 'c', 'd']
};
random.choose.mockImplementation(arg => chooseReturnValues[arg].shift() || '');
let result = randomPassword({
length: 6,
characters: 'abcdef',
avoidAmbiguous: ['ab', 'ef'],
random
});
expect(result).toBe('cdcdcd');
});
it('does not prevent characters from being chosen if only one charcter in avoidAmbiguous appears in the passed charcters', () => {
let chooseReturnValues = {
'abc': ['a', 'b', 'c', 'a', 'b', 'c']
};
random.choose.mockImplementation(arg => chooseReturnValues[arg].shift() || '');
let result = randomPassword({
length: 6,
characters: 'abc',
avoidAmbiguous: ['cd'],
random
});
expect(result).toBe('abcabc');
});
it('matches ambiguous characters across all sets', () => {
let chooseReturnValues = {
'b': ['b'],
'2': ['2'],
'b2': ['b', '2', 'b', '2']
};
random.choose.mockImplementation(arg => chooseReturnValues[arg].shift() || '');
let result = randomPassword({
length: 6,
characters: ['abc', '123'],
avoidAmbiguous: ['a1', 'c3'],
random
});
expect(result).toBe('b2b2b2');
});
it('throws an error if any character set is empty after excluding ambiguous characters', () => {
expect(() => randomPassword({
length: 42,
characters: [
'ab',
'cd',
'ef'
],
avoidAmbiguous: ['cd']
})).toThrow('No character set may be empty');
});
it('provides default ambiguous characters sets when true is passed for avoidAmbiguous', () => {
random.choose.mockImplementation(x => x);
let result = randomPassword({
length: 6,
characters: 'aIl1|O0',
avoidAmbiguous: true,
random
});
expect(result).toBe('aaaaaa');
});
it('provides an empty ambiguous character set when false value is passed for avoidAmbiguous', () => {
let chooseReturnValues = {
'abc': ['a', 'b', 'c', 'a', 'b', 'c']
};
random.choose.mockImplementation(arg => chooseReturnValues[arg].shift() || '');
let result = randomPassword({
length: 6,
characters: 'abc',
avoidAmbiguous: false,
random
});
expect(result).toBe('abcabc');
});
});
describe('predicate option', () => {
it('throws an error if predicate is not a function', () => {
expect(() => randomPassword({ predicate: null })).toThrow('predicate must be a function');
});
it('keeps generating passwords until they pass the predicate', () => {
let chooseReturnValues = {
'abc': ['a', 'b', 'c', 'a', 'b', 'c', 'c', 'b', 'a']
};
random.choose.mockImplementation(arg => chooseReturnValues[arg].shift() || '');
let result = randomPassword({
length: 3,
characters: 'abc',
predicate: (x => x !== 'abc'),
random
});
expect(result).toBe('cba');
});
});
});