From 26a84b00dffbff47b3074e0d9cab197e143b89c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Polanco=20Montiel?= Date: Fri, 18 Jun 2021 14:48:08 +0200 Subject: [PATCH] Fixed errors testing password with length less than the allowed length and allErrors flag to true. Updated and fixed docs and test examples. --- README.md | 4 ++-- index.js | 7 +++++-- package.json | 4 ++-- src/index.js | 50 ++++++++++++++++++++++++------------------------ test/pwd-spec.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index c2387b6..f33c733 100644 --- a/README.md +++ b/README.md @@ -169,14 +169,14 @@ This is the only case where the `message` property will be an array of strings. Success result, with weak password: ```js - console.log(passwordStrength('testtest', { minUpperChars: 0, minSpecialChars: 0 })); + console.log(passwordStrength('testtest', { minUpperChars: 0, minSpecialChars: 0, minNumberChars: 0 })); // {"success":true,"key":"weak","message":"Weak","color":"#c43d4b"} ``` Success result, with average password: ```js - console.log(passwordStrength('testest1', { minUpperChars: 0 })); + console.log(passwordStrength('testest1', { minUpperChars: 0, minSpecialChars: 0 })); // {"success":true,"key":"average","message":"Average","color":"#cc9900"} ``` diff --git a/index.js b/index.js index aa567b5..fdc8e94 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ function test(title, password, options = {}) { process.stdout.write('\n'); } +// Error examples test('Empty password', ''); test('Min password length', 'test'); test('Min upper chars', 'testtest', { minUpperChars: 1 }); @@ -19,7 +20,9 @@ test('Min numbers', 'TestTest', { minNumberChars: 1 }); test('Min special chars', 'TestTest1', { minSpecialChars: 1 }); test('Max consecutive repeating chars', 'TestTTTTest1!', { maxConsecutiveRepeatingChars: 2 }); test('Empty password with all possible errors', '', { allErrors: true }); -test('Weak password', 'testtest', { minUpperChars: 0, minNumberChars: 0, minSpecialChars: 0 }); -test('Average password', 'testest1', { minUpperChars: 0, minNumberChars: 0, minSpecialChars: 0 }); + +// Success examples +test('Weak password', 'testtest', { minUpperChars: 0, minSpecialChars: 0, minNumberChars: 0 }); +test('Average password', 'testest1', { minUpperChars: 0, minSpecialChars: 0 }); test('Strong password', 'TestTest1!'); test('Secure password', 'Test123%$/)=!'); diff --git a/package.json b/package.json index a5c1298..1f128be 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "#USER", "/" ], - "_resolved": "https://registry.npmjs.org/pwd-strength/-/pwd-strength-1.1.1.tgz", + "_resolved": "https://registry.npmjs.org/pwd-strength/-/pwd-strength-1.1.2.tgz", "_shasum": "1ccc118c7a90b02d22c3ae1815af237321e32878", "_spec": "pwd-strength", "_where": "C:\\nodejs\\pwd-strength", @@ -57,5 +57,5 @@ "demo": "node ./index.js", "test": "tape ./test/*spec.js | tap-spec" }, - "version": "1.1.1" + "version": "1.1.2" } diff --git a/src/index.js b/src/index.js index eea95d8..f2896bc 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ module.exports = function passwordStrength(value, options) { let defaults = { - debug: false, + debug: false, // Debug mode allErrors: false, // Return all errors instead the first error found minUpperChars: 1, // Upper characters ([A-Z]) minLowerChars: 1, // Lower characters ([a-z]) @@ -76,6 +76,7 @@ module.exports = function passwordStrength(value, options) { for (let i=0; i < charPassword.length; i++) { let currentChar = charPassword[i]; + // Counting chars if (currentChar.match(/[A-Z]/g)) { num.upper++; } else if (currentChar.match(/[a-z]/g)) { @@ -86,15 +87,14 @@ module.exports = function passwordStrength(value, options) { num.symbols++; } - if(previousChar !== null && num.repeatingChars <= settings.maxConsecutiveRepeatingChars && charPassword[i] === previousChar) { - if(num.repeatingChars === 0) num.repeatingChars = 1; - num.repeatingChars++; - } - if(num.repeatingChars > settings.maxConsecutiveRepeatingChars) { - continue; - } - if(previousChar !== null && charPassword[i] !== previousChar) { - num.repeatingChars = 0; + // Repeating chars + if (previousChar !== null && num.repeatingChars <= settings.maxConsecutiveRepeatingChars) { + if (charPassword[i] === previousChar) { + if (num.repeatingChars === 0) num.repeatingChars = 1; + num.repeatingChars++; + } else { + num.repeatingChars = 0; + } } previousChar = charPassword[i]; } @@ -123,7 +123,7 @@ module.exports = function passwordStrength(value, options) { } function format(msg, variable) { - if(typeof(msg) === 'string') { + if (typeof(msg) === 'string') { return msg.replace('%s', variable); } else { return ''; @@ -153,43 +153,43 @@ module.exports = function passwordStrength(value, options) { color: '' }; - if(strPassword === '') { + if (strPassword === '') { let message = lang.enterPassword; result.message.push(message); result.color = colors.error; } - if(charPassword.length < settings.minPasswordLength) { + if (charPassword.length < settings.minPasswordLength) { let message = settings.minPasswordLength !== 1 ? lang.minPasswordChars : lang.minPasswordChar; result.message.push(format(message, settings.minPasswordLength)); result.color = colors.error; } - if(num.lower < settings.minLowerChars) { + if (num.lower < settings.minLowerChars) { let message = settings.minLowerChars !== 1 ? lang.minLowerChars : lang.minLowerChar; result.message.push(format(message, settings.minLowerChars)); result.color = colors.error; } - if(num.upper < settings.minUpperChars) { + if (num.upper < settings.minUpperChars) { let message = settings.minUpperChars !== 1 ? lang.minUpperChars : lang.minUpperChar; result.message.push(format(message, settings.minUpperChars)); result.color = colors.error; } - if(num.numbers < settings.minNumberChars) { + if (num.numbers < settings.minNumberChars) { let message = settings.minNumberChars !== 1 ? lang.minNumberChars : lang.minNumberChar; result.message.push(format(message, settings.minNumberChars)); result.color = colors.error; } - if(num.symbols < settings.minSpecialChars) { + if (num.symbols < settings.minSpecialChars) { let message = settings.minSpecialChars !== 1 ? lang.minSpecialChars : lang.minSpecialChar; result.message.push(format(message, settings.minSpecialChars)); result.color = colors.error; } - if(settings.maxConsecutiveRepeatingChars > 1 && num.repeatingChars > settings.maxConsecutiveRepeatingChars) { + if (settings.maxConsecutiveRepeatingChars > 1 && num.repeatingChars > settings.maxConsecutiveRepeatingChars) { let message = lang.maxConsecutiveRepeatingChars; result.message.push(format(message, settings.maxConsecutiveRepeatingChars)); result.color = colors.error; } - if(result.message.length === 0) { + if (result.message.length === 0) { if (score < 50) { result.success = true; result.key = 'weak'; @@ -213,9 +213,9 @@ module.exports = function passwordStrength(value, options) { } } - if(settings.debug) { + if (settings.debug) { - result.debug = 'Base score:' + baseScore + '\n' + result.debug = 'Base score: ' + baseScore + '\n' + 'Length bonus: ' + (num.excess*settings.excess) + ' ['+num.excess+'x'+settings.excess+']\n' + 'Upper case bonus: ' + (num.upper*settings.minUpperChars) + ' ['+num.upper+'x'+settings.minUpperChars+']\n' + 'Lower case bonus: ' + (num.lower*settings.minLowerChars) + ' ['+num.lower+'x'+settings.minLowerChars+']\n' @@ -229,24 +229,24 @@ module.exports = function passwordStrength(value, options) { console.log(result.debug); } - if(!result.success && !settings.allErrors) { + if (!result.success && !settings.allErrors) { result.message = result.message.length > 0 ? result.message[0] : ''; } return result; - } init(); if (charPassword.length >= settings.minPasswordLength) { baseScore = 50; - analyzeString(); - calcComplexity(); } else { baseScore = 0; } + analyzeString(); + calcComplexity(); + return outputResult(); }; diff --git a/test/pwd-spec.js b/test/pwd-spec.js index 64a04a1..1c928ed 100644 --- a/test/pwd-spec.js +++ b/test/pwd-spec.js @@ -5,6 +5,7 @@ test('validate min password length', function(t) { const result = passwordStrength('Test', { minPasswordLength: 5 }); + t.equal(result.key, 'error'); t.equal(result.success, false); t.end(); }); @@ -13,6 +14,7 @@ test('validate min lower chars', function(t) { const result = passwordStrength('TEST', { minLowerChars: 1 }); + t.equal(result.key, 'error'); t.equal(result.success, false); t.end(); }); @@ -21,6 +23,7 @@ test('validate min upper chars', function(t) { const result = passwordStrength('test', { minUpperChars: 1 }); + t.equal(result.key, 'error'); t.equal(result.success, false); t.end(); }); @@ -29,6 +32,7 @@ test('validate min numbers', function(t) { const result = passwordStrength('Test', { minNumberChars: 1 }); + t.equal(result.key, 'error'); t.equal(result.success, false); t.end(); }); @@ -44,6 +48,52 @@ test('validate max consecutive repeating chars', function(t) { const result = passwordStrength('Test', { maxConsecutiveRepeatingChars: 2 }); + t.equal(result.key, 'error'); t.equal(result.success, false); t.end(); }); + +test('validate all errors with empty password', function(t) { + const result = passwordStrength('', { + allErrors: true + }); + t.equal(result.key, 'error'); + t.equal(result.success, false); + t.equal(result.message.length, 6); + t.end(); +}); + +test('validate weak', function(t) { + const result = passwordStrength('testtest', { + minUpperChars: 0, + minSpecialChars: 0, + minNumberChars: 0 + }); + t.equal(result.key, 'weak'); + t.equal(result.success, true); + t.end(); +}); + +test('validate average', function(t) { + const result = passwordStrength('testest1', { + minUpperChars: 0, + minSpecialChars: 0 + }); + t.equal(result.key, 'average'); + t.equal(result.success, true); + t.end(); +}); + +test('validate strong', function(t) { + const result = passwordStrength('TestTest1!'); + t.equal(result.key, 'strong'); + t.equal(result.success, true); + t.end(); +}); + +test('validate secure', function(t) { + const result = passwordStrength('Test123%$/)=!'); + t.equal(result.key, 'secure'); + t.equal(result.success, true); + t.end(); +});