1 | var _ = require('lodash'),
|
2 | assert = require('assert'),
|
3 | crypto = require('crypto');
|
4 |
|
5 | describe('Create a default random token generater', function() {
|
6 | it('should create successfully', function() {
|
7 | var randtoken = require('../index.js');
|
8 | });
|
9 |
|
10 | it('should create tokens', function() {
|
11 | var randtoken = require('../index.js');
|
12 | randtoken.generate(16);
|
13 | });
|
14 |
|
15 | it('should create tokens of the specified length', function() {
|
16 | var randtoken = require('../index.js');
|
17 | var token = randtoken.generate(16);
|
18 | assert(token.length, 16);
|
19 | });
|
20 | });
|
21 |
|
22 | var randtoken = require('../index.js');
|
23 |
|
24 | var randSourceToTest = [
|
25 | 'default',
|
26 | 'math',
|
27 | 'crypto',
|
28 | crypto.randomBytes
|
29 | ];
|
30 | var tokenCharsToTest = [
|
31 | {chars: 'a', regex: /^a{16}$/ },
|
32 | {chars: 'alpha', regex: /^[a-z]{16}$/ },
|
33 | {chars: 'ALPHA', regex: /^[A-Z]{16}$/ },
|
34 | {chars: 'numeric', regex: /^[0-9]{16}$/ },
|
35 | ];
|
36 |
|
37 | _.each(randSourceToTest, function(randSourceTest) {
|
38 | _.each(tokenCharsToTest, function(tokenCharTest) {
|
39 | describe('Generate a 16 character token with tokeChars=' + tokenCharTest.chars +
|
40 | ' and a randSource=' + randSourceTest, function() {
|
41 | var generator;
|
42 | it('should create a generator successfully', function() {
|
43 | generator = randtoken.generator({source: randSourceTest, chars: tokenCharTest.chars});
|
44 | });
|
45 |
|
46 | it('should generate tokens that match the regex ' + tokenCharTest.regex, function() {
|
47 | for(var i=0;i<1000;i++) {
|
48 | var token = generator.generate(16);
|
49 | assert(tokenCharTest.regex.test(token));
|
50 | }
|
51 | });
|
52 | });
|
53 | });
|
54 | });
|