Skip to content

Commit 4279ba8

Browse files
committed
Implement passwordValidator with all 6 rules and add tests for each rule
1 parent 85e4ce4 commit 4279ba8

2 files changed

Lines changed: 46 additions & 29 deletions

File tree

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
const previousPasswords = ["Pass1!", "Hello1#", "Secure1$"];
2+
13
function passwordValidator(password) {
2-
return password.length < 5 ? false : true
4+
if (password.length < 5) return false;
5+
if (!/[A-Z]/.test(password)) return false;
6+
if (!/[a-z]/.test(password)) return false;
7+
if (!/[0-9]/.test(password)) return false;
8+
if (!/[!#$%.\*&]/.test(password)) return false;
9+
if (previousPasswords.includes(password)) return false;
10+
return true;
311
}
412

5-
6-
module.exports = passwordValidator;
13+
module.exports = { passwordValidator, previousPasswords };
Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
1-
/*
2-
Password Validation
3-
4-
Write a program that should check if a password is valid
5-
and returns a boolean
6-
7-
To be valid, a password must:
8-
- Have at least 5 characters.
9-
- Have at least one English uppercase letter (A-Z)
10-
- Have at least one English lowercase letter (a-z)
11-
- Have at least one number (0-9)
12-
- Have at least one of the following non-alphanumeric symbols: ("!", "#", "$", "%", ".", "*", "&")
13-
- Must not be any previous password in the passwords array.
14-
15-
You must breakdown this problem in order to solve it. Find one test case first and get that working
16-
*/
17-
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-
);
1+
const { passwordValidator, previousPasswords } = require("./password-validator");
2+
3+
test("should return false when password has fewer than 5 characters", () => {
4+
expect(passwordValidator("Ab1!")).toEqual(false);
5+
});
6+
7+
test("should return false when password has no uppercase letter", () => {
8+
expect(passwordValidator("hello1!")).toEqual(false);
9+
});
10+
11+
test("should return false when password has no lowercase letter", () => {
12+
expect(passwordValidator("HELLO1!")).toEqual(false);
13+
});
14+
15+
test("should return false when password has no number", () => {
16+
expect(passwordValidator("Hello!")).toEqual(false);
17+
});
18+
19+
test("should return false when password has no special character", () => {
20+
expect(passwordValidator("Hello1")).toEqual(false);
21+
});
22+
23+
test("should return false when password is a previous password", () => {
24+
expect(passwordValidator("Pass1!")).toEqual(false);
25+
});
26+
27+
test("should return true for a valid password meeting all criteria", () => {
28+
expect(passwordValidator("Valid1!")).toEqual(true);
29+
expect(passwordValidator("MyP@ss1")).toEqual(false); // @ not in allowed symbols
30+
expect(passwordValidator("MyPass1$")).toEqual(true);
31+
});
32+
33+
test("should return false for password with exactly 5 chars but missing a required type", () => {
34+
expect(passwordValidator("ab1!A")).toEqual(true); // valid: 5 chars, all types present
35+
expect(passwordValidator("ab1!!")).toEqual(false); // no uppercase
36+
});

0 commit comments

Comments
 (0)