Skip to content

Commit 3fb4767

Browse files
Implement password validator with full rule validation and tests
1 parent 08a9134 commit 3fb4767

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
function passwordValidator(password) {
2-
return password.length < 5 ? false : true
2+
const previousPassword =["Arun!1621"];
3+
const specialChar = ["!", "#", "$", "%", ".", "*", "&"];
4+
if (password.length<5)
5+
{
6+
throw new Error("minimum length is 5");
7+
}
8+
else if(!/[A-Z]/.test(password))
9+
{
10+
throw new Error("Need at least one uppercase");
11+
}
12+
else if (!/[a-z]/.test(password))
13+
{
14+
throw new Error("Need at least one lower case");
15+
}
16+
else if (!/[0-9]/.test(password))
17+
{
18+
throw new Error("need at least one number");
19+
}
20+
else if(!/["!", "#", "$", "%", ".", "*", "&"]/.test(password))
21+
{
22+
throw new Error("need at least one special character");
23+
}
24+
else if (previousPassword.includes(password))
25+
{
26+
throw new Error("no previous password");
27+
28+
}
29+
else
30+
{
31+
return true;
32+
}
333
}
434

535

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,33 @@ You must breakdown this problem in order to solve it. Find one test case first a
1717
const isValidPassword = require("./password-validator");
1818
test("password has at least 5 characters", () => {
1919
// Arrange
20-
const password = "12345";
21-
// Act
22-
const result = isValidPassword(password);
23-
// Assert
24-
expect(result).toEqual(true);
20+
const password = "Ha!1";
21+
expect( ()=> isValidPassword(password)).toThrow();
2522
}
26-
);
23+
);
24+
test("password has at least one uppercase letter(A-Z)",()=>{
25+
const password = "harju!1621";
26+
27+
28+
expect(()=> isValidPassword(password)).toThrow();
29+
});
30+
test("password has at least one lowercase letter (a-z)", ()=>{
31+
const password = "HARJU!1621";
32+
expect(()=> isValidPassword(password)).toThrow();
33+
});
34+
test("password has at least one number (0-9)", ()=>{
35+
const password = "Harju!";
36+
expect(()=>isValidPassword(password)).toThrow();
37+
});
38+
test("password has at least one special character ",()=>{
39+
const password = "Harju1621";
40+
expect(()=> isValidPassword(password)).toThrow();
41+
});
42+
test("password not match with previous password", ()=>{
43+
const password = "Arun!1621";
44+
expect(()=> isValidPassword(password)).toThrow();
45+
});
46+
test("password meet all the condition", ()=>{
47+
const password ="Harju!1621";
48+
expect(isValidPassword(password)).toEqual(true);
49+
});

0 commit comments

Comments
 (0)