From 48c44f58be3c34fc7ec8955ceefddc6186718b5b Mon Sep 17 00:00:00 2001 From: Payman IB Date: Sun, 16 Nov 2025 17:21:51 +0000 Subject: [PATCH] Refactor password validation logic and enhance test coverage --- Project-CLI-Treasure-Hunt | 1 + Sprint-3/3-stretch/password-validator.js | 37 ++++++++++++++++++- Sprint-3/3-stretch/password-validator.test.js | 31 +++++++++++++--- 3 files changed, 63 insertions(+), 6 deletions(-) create mode 160000 Project-CLI-Treasure-Hunt diff --git a/Project-CLI-Treasure-Hunt b/Project-CLI-Treasure-Hunt new file mode 160000 index 000000000..6668c42dc --- /dev/null +++ b/Project-CLI-Treasure-Hunt @@ -0,0 +1 @@ +Subproject commit 6668c42dc5412e54026ca0fa8cb5691017ef6350 diff --git a/Sprint-3/3-stretch/password-validator.js b/Sprint-3/3-stretch/password-validator.js index b55d527db..8824b87bc 100644 --- a/Sprint-3/3-stretch/password-validator.js +++ b/Sprint-3/3-stretch/password-validator.js @@ -1,6 +1,41 @@ +// function isLongEnough(password) { +// return password.length >= 5; +// } +// function isUpperCase(password) { +// return /[A-Z]/.test(password); +// } + +// function isLowerCase(password) { +// return /[a-z]/.test(password); +// } + +// function hasNumber(password) { +// return /[0-9]/.test(password); +// } + +// function hasSpecialChar(password) { +// return /[!#$%.*&]/.test(password); +// } +// function passwordValidator(password) { + +// if (isLongEnough(password) && isUpperCase(password) && isLowerCase(password) && hasNumber(password) && hasSpecialChar(password)) +// return true +// else +// return false; +// } + +///////////////////////////////////////////////////////////////////// + function passwordValidator(password) { - return password.length < 5 ? false : true + const isLongEnough = password.length >= 5; + const hasUpper = /[A-Z]/.test(password); + const hasLower = /[a-z]/.test(password); + const hasNumber = /[0-9]/.test(password); + const hasSpecial = /[!#$%.*&]/.test(password); + + return isLongEnough && hasUpper && hasLower && hasNumber && hasSpecial; } +console.log(passwordValidator("Password123!")); module.exports = passwordValidator; \ No newline at end of file diff --git a/Sprint-3/3-stretch/password-validator.test.js b/Sprint-3/3-stretch/password-validator.test.js index 8fa3089d6..079124604 100644 --- a/Sprint-3/3-stretch/password-validator.test.js +++ b/Sprint-3/3-stretch/password-validator.test.js @@ -16,11 +16,32 @@ You must breakdown this problem in order to solve it. Find one test case first a */ const isValidPassword = require("./password-validator"); test("password has at least 5 characters", () => { - // Arrange - const password = "12345"; - // Act + const password = "12345Aa!"; const result = isValidPassword(password); - // Assert expect(result).toEqual(true); } -); \ No newline at end of file +); +test("password missing uppercase letter", () => { + const password = "12345aa!"; + const result = isValidPassword(password); + expect(result).toEqual(false); +} +); +test("password missing lowercase letter", () => { + const password = "12345AA!"; + const result = isValidPassword(password); + expect(result).toEqual(false); +} +); +test("password missing number", () => { + const password = "Abcde!"; + const result = isValidPassword(password); + expect(result).toEqual(false); +} +); +test("password missing special character", () => { + const password = "12345Aa"; + const result = isValidPassword(password); + expect(result).toEqual(false); +} +); \ No newline at end of file