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