Skip to content

Commit 78b6e92

Browse files
committed
writing conditions and tests for password validation
1 parent f1ac2fe commit 78b6e92

File tree

2 files changed

+65
-12
lines changed

2 files changed

+65
-12
lines changed
Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
11
function passwordValidator(password) {
2-
return password.length < 5 ? false : true
3-
}
2+
const lengthCondition = password.length >= 5;
3+
console.log(`length condition: ${lengthCondition}`);
4+
5+
const uppercaseCondition = /[A-Z]/.test(password);
6+
console.log(`upper case condition: ${uppercaseCondition}`);
7+
8+
const lowercaseCondition = /[a-z]/.test(password);
9+
console.log(`lower case condition: ${lowercaseCondition}`);
10+
11+
const numberCondition = /[0-9]/.test(password);
12+
console.log(`number condition: ${numberCondition}`);
413

14+
const symbolCondition = /[!#$%.*&]/.test(password);
15+
console.log(`symbol condition: ${symbolCondition}`);
16+
17+
const previousPasswords = [
18+
"123Ab!",
19+
"abcD1#",
20+
"Password1!",
21+
"Qwerty1*",
22+
"Zxcvbnm2$",
23+
];
24+
const notInPreviousPasswords = !previousPasswords.includes(password);
25+
console.log(`previous password condition: ${notInPreviousPasswords}`);
26+
27+
if (
28+
lengthCondition &&
29+
uppercaseCondition &&
30+
lowercaseCondition &&
31+
numberCondition &&
32+
symbolCondition &&
33+
notInPreviousPasswords
34+
) {
35+
return true;
36+
} else {
37+
return false;
38+
}
39+
}
540

6-
module.exports = passwordValidator;
41+
module.exports = passwordValidator;

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,30 @@ To be valid, a password must:
1515
You must breakdown this problem in order to solve it. Find one test case first and get that working
1616
*/
1717
const isValidPassword = require("./password-validator");
18-
test("password has at least 5 characters", () => {
19-
// Arrange
20-
const password = "12345";
21-
// Act
22-
const result = isValidPassword(password);
23-
// Assert
24-
expect(result).toEqual(true);
25-
}
26-
);
18+
describe("passwordValidator", () => {
19+
const password = "123Ab*";
20+
21+
test("returns true for passwords with at least 5 characters", () => {
22+
expect(isValidPassword(password)).toBe(true);
23+
});
24+
25+
test("return true for passwords with at least one uppercase letter", () => {
26+
expect(isValidPassword(password)).toBe(true);
27+
});
28+
29+
test("return true for passwords with at least one lowercase letter", () => {
30+
expect(isValidPassword(password)).toBe(true);
31+
});
32+
33+
test("return true for passwords with at least one number", () => {
34+
expect(isValidPassword(password)).toBe(true);
35+
});
36+
37+
test("return true for passwords with at least one non-alphanumeric symbol", () => {
38+
expect(isValidPassword(password)).toBe(true);
39+
});
40+
41+
test("return true for passwords that are not in the previous passwords array", () => {
42+
expect(isValidPassword(password)).toBe(true);
43+
});
44+
});

0 commit comments

Comments
 (0)