Skip to content

Commit 48c44f5

Browse files
author
Payman IB
committed
Refactor password validation logic and enhance test coverage
1 parent 8f3d6cf commit 48c44f5

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

Project-CLI-Treasure-Hunt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 6668c42dc5412e54026ca0fa8cb5691017ef6350
Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
1+
// function isLongEnough(password) {
2+
// return password.length >= 5;
3+
// }
4+
// function isUpperCase(password) {
5+
// return /[A-Z]/.test(password);
6+
// }
7+
8+
// function isLowerCase(password) {
9+
// return /[a-z]/.test(password);
10+
// }
11+
12+
// function hasNumber(password) {
13+
// return /[0-9]/.test(password);
14+
// }
15+
16+
// function hasSpecialChar(password) {
17+
// return /[!#$%.*&]/.test(password);
18+
// }
19+
// function passwordValidator(password) {
20+
21+
// if (isLongEnough(password) && isUpperCase(password) && isLowerCase(password) && hasNumber(password) && hasSpecialChar(password))
22+
// return true
23+
// else
24+
// return false;
25+
// }
26+
27+
/////////////////////////////////////////////////////////////////////
28+
129
function passwordValidator(password) {
2-
return password.length < 5 ? false : true
30+
const isLongEnough = password.length >= 5;
31+
const hasUpper = /[A-Z]/.test(password);
32+
const hasLower = /[a-z]/.test(password);
33+
const hasNumber = /[0-9]/.test(password);
34+
const hasSpecial = /[!#$%.*&]/.test(password);
35+
36+
return isLongEnough && hasUpper && hasLower && hasNumber && hasSpecial;
337
}
438

39+
console.log(passwordValidator("Password123!"));
540

641
module.exports = passwordValidator;

Sprint-3/3-stretch/password-validator.test.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,32 @@ You must breakdown this problem in order to solve it. Find one test case first a
1616
*/
1717
const isValidPassword = require("./password-validator");
1818
test("password has at least 5 characters", () => {
19-
// Arrange
20-
const password = "12345";
21-
// Act
19+
const password = "12345Aa!";
2220
const result = isValidPassword(password);
23-
// Assert
2421
expect(result).toEqual(true);
2522
}
26-
);
23+
);
24+
test("password missing uppercase letter", () => {
25+
const password = "12345aa!";
26+
const result = isValidPassword(password);
27+
expect(result).toEqual(false);
28+
}
29+
);
30+
test("password missing lowercase letter", () => {
31+
const password = "12345AA!";
32+
const result = isValidPassword(password);
33+
expect(result).toEqual(false);
34+
}
35+
);
36+
test("password missing number", () => {
37+
const password = "Abcde!";
38+
const result = isValidPassword(password);
39+
expect(result).toEqual(false);
40+
}
41+
);
42+
test("password missing special character", () => {
43+
const password = "12345Aa";
44+
const result = isValidPassword(password);
45+
expect(result).toEqual(false);
46+
}
47+
);

0 commit comments

Comments
 (0)