1- /*
1+ /*
22Password Validation
33
44Write a program that should check if a password is valid
@@ -10,17 +10,61 @@ To be valid, a password must:
1010- Have at least one English lowercase letter (a-z)
1111- Have at least one number (0-9)
1212- Have at least one of the following non-alphanumeric symbols: ("!", "#", "$", "%", ".", "*", "&")
13- - Must not be any previous password in the passwords array.
13+ - Must not be any previous password in the passwords array.
1414
1515You must breakdown this problem in order to solve it. Find one test case first and get that working
1616*/
1717const 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+
19+ test ( "password is valid when it meets all rules" , ( ) => {
20+ // Arrange
21+ const password = "Ab1!c" ;
22+
23+ // Act
24+ const result = isValidPassword ( password ) ;
25+
26+ // Assert
27+ expect ( result ) . toEqual ( true ) ;
28+ } ) ;
29+
30+ test ( "password is invalid when it is shorter than 5 characters" , ( ) => {
31+ const password = "A1!a" ;
32+ const result = isValidPassword ( password ) ;
33+
34+ expect ( result ) . toEqual ( false ) ;
35+ } ) ;
36+
37+ test ( "password is invalid when it has no uppercase letter" , ( ) => {
38+ const password = "abcde1!" ;
39+ const result = isValidPassword ( password ) ;
40+
41+ expect ( result ) . toEqual ( false ) ;
42+ } ) ;
43+
44+ test ( "password is invalid when it has no lowercase letter" , ( ) => {
45+ const password = "ABCDE1!" ;
46+ const result = isValidPassword ( password ) ;
47+
48+ expect ( result ) . toEqual ( false ) ;
49+ } ) ;
50+
51+ test ( "password is invalid when it has no number" , ( ) => {
52+ const password = "Abcde!" ;
53+ const result = isValidPassword ( password ) ;
54+
55+ expect ( result ) . toEqual ( false ) ;
56+ } ) ;
57+
58+ test ( "password is invalid when it has no required symbol" , ( ) => {
59+ const password = "Abcde1" ;
60+ const result = isValidPassword ( password ) ;
61+
62+ expect ( result ) . toEqual ( false ) ;
63+ } ) ;
64+
65+ test ( "password is invalid when it has been used before" , ( ) => {
66+ const password = "Password1!" ;
67+ const result = isValidPassword ( password ) ;
68+
69+ expect ( result ) . toEqual ( false ) ;
70+ } ) ;
0 commit comments