| Server IP : 68.183.124.220 / Your IP : 216.73.217.137 Web Server : Apache/2.4.18 (Ubuntu) System : Linux Sandbox-A 4.4.0-210-generic #242-Ubuntu SMP Fri Apr 16 09:57:56 UTC 2021 x86_64 User : gavin ( 1000) PHP Version : 7.0.33-0ubuntu0.16.04.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/gavin/workspace/happymandarin/node_modules/generate-password/test/ |
Upload File : |
var assert = require('chai').assert,
_ = require('underscore');
// We use a different require path for code coverage.
var generator = process.env.JSCOV ? require('../src-cov/generate') : require('../main');
describe('generate-password', function() {
describe('generate()', function() {
it('should give password of correct length', function() {
var length = 12;
var password = generator.generate({length: length});
assert.equal(password.length, length);
});
it('should generate strict random sequence that is correct length', function() {
var length = 12;
var password = generator.generate({length: length, strict: true});
assert.equal(password.length, length);
});
it('should remove possible similar characters from the sequences', function() {
var password = generator.generate({length: 10000, excludeSimilarCharacters: true});
assert.notMatch(password, /[ilLI|`oO0]/, 'password does not contain similar characters');
});
describe('strict mode', function() {
// Testing randomly generated passwords and entropy isn't perfect,
// thus in order to get a good sample of whether or not a rule
// is passing correctly, we generate lots of passwords and check
// each individually. If we're seeing spotty tests, this number should
// be increased accordingly.
var amountToGenerate = 500;
it('should generate strict random sequence that has strictly at least one number', function() {
var passwords = generator.generateMultiple(amountToGenerate, {length: 4, strict: true, uppercase: false, numbers: true});
passwords.forEach(function(password) {
assert.match(password, /[0-9]/, 'password has a number');
});
assert.equal(passwords.length, amountToGenerate);
});
it('should generate strict random sequence that has strictly at least one lowercase letter', function() {
var passwords = generator.generateMultiple(amountToGenerate, {length: 4, strict: true, uppercase: false});
passwords.forEach(function(password) {
assert.match(password, /[a-z]/, 'password has a lowercase letter');
});
assert.equal(passwords.length, amountToGenerate);
});
it('should generate strict random sequence that has strictly at least one uppercase letter', function() {
var passwords = generator.generateMultiple(amountToGenerate, {length: 4, strict: true, uppercase: true});
passwords.forEach(function(password) {
assert.match(password, /[A-Z]/, 'password has an uppercase letter');
});
assert.equal(passwords.length, amountToGenerate);
});
it('should generate strict random sequence that has strictly at least one special symbol', function() {
var passwords = generator.generateMultiple(amountToGenerate, {length: 4, strict: true, symbols: true});
passwords.forEach(function(password) {
assert.match(password, /[!@#$%^&*()+_\-=}{[\]|:;"/?.><,`~]/, 'password has a symbol');
});
assert.equal(passwords.length, amountToGenerate);
});
it('should throw an error if rules don\'t correlate with length', function() {
assert.throws(function() {
generator.generate({length: 2, strict: true, symbols: true, numbers: true});
}, TypeError, 'Length must correlate with strict guidelines');
});
it('should generate short strict passwords without stack overflow', function(){
assert.doesNotThrow(function() {
generator.generate({length: 4, strict: true, uppercase: true, numbers: true, symbols: true});
}, Error);
});
});
});
describe('generateMultiple()', function() {
// should give right amount
it('should give right amount of passwords', function() {
var amount = 34;
var passwords = generator.generateMultiple(amount, {});
assert.equal(passwords.length, amount);
});
// shouldn't give duplicates in pool of 250 (extremely rare)
it('should not give duplicates in pool', function() {
var passwords = generator.generateMultiple(250, {length: 10, numbers: true, symbols: true});
var unique = _.uniq(passwords);
assert.equal(unique.length, passwords.length);
});
});
});