-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest.js
More file actions
73 lines (64 loc) · 2.19 KB
/
test.js
File metadata and controls
73 lines (64 loc) · 2.19 KB
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
var _ = require('lodash'),
assert = require('assert'),
crypto = require('crypto');
describe('Create a default random token generater', function() {
it('should create successfully', function() {
var randtoken = require('../index.js');
});
it('should create tokens', function() {
var randtoken = require('../index.js');
randtoken.generate(16);
});
it('should create tokens of the specified length', function() {
var randtoken = require('../index.js');
var token = randtoken.generate(16);
assert(token.length, 16);
});
it('should create tokens using the uid function', function() {
var randtoken = require('../index.js');
var token = randtoken.uid(16);
assert(token.length, 16);
});
it('should create sequentially sorted tokens', function(done) {
var randtoken = require('../index.js');
var token = randtoken.suid(16);
function genAnotherAndCompare() {
var secondToken = randtoken.suid(16);
assert(token < secondToken);
done();
};
setTimeout(genAnotherAndCompare, 10);
});
});
var randtoken = require('../index.js');
var randSourceToTest = [
'default',
'math',
'crypto',
crypto.randomBytes
];
var tokenCharsToTest = [
{chars: 'a', regex: /^a{16}$/ },
{chars: 'alpha', regex: /^[a-z]{16}$/ },
{chars: 'ALPHA', regex: /^[A-Z]{16}$/ },
{chars: 'numeric', regex: /^[0-9]{16}$/ },
{chars: ['ALPHA', 'numeric'], regex: /^[a-z0-9]{16}$/},
{chars: ['alpha', 'ALPHA', 'numeric'], regex: /^[a-zA-Z0-9]{16}$/},
];
_.each(randSourceToTest, function(randSourceTest) {
_.each(tokenCharsToTest, function(tokenCharTest) {
describe('Generate a 16 character token with tokeChars=' + tokenCharTest.chars +
' and a randSource=' + randSourceTest, function() {
var generator;
it('should create a generator successfully', function() {
generator = randtoken.generator({source: randSourceTest, chars: tokenCharTest.chars});
});
it('should generate tokens that match the regex ' + tokenCharTest.regex, function() {
for(var i=0;i<1000;i++) {
var token = generator.generate(16);
assert(tokenCharTest.regex.test(token));
}
});
});
});
});