1
+ import { countRequirement , getRandomChar , generatePassword } from '../static/password_generation.js' ;
2
+
3
+ const LOWERCASE = 'abcdefghijklmnopqrstuvwxyz' ;
4
+ const UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ;
5
+ const NUMBER = '0123456789' ;
6
+ const SPECIAL_CHAR = '#?!@$%^&*-\'+()_[]' ;
7
+
8
+
9
+ test ( 'getRandomChar return char if single possibility' , ( ) => {
10
+ let random_char = getRandomChar ( 'a' ) ;
11
+
12
+ expect ( random_char ) . toBe ( 'a' ) ;
13
+ } ) ;
14
+
15
+ test ( 'getRandomChar return one of given possibilities' , ( ) => {
16
+ let possibilities = 'abcdef' ;
17
+ let random_char = getRandomChar ( possibilities ) ;
18
+
19
+ expect ( possibilities . includes ( random_char ) ) . toBe ( true ) ;
20
+ } ) ;
21
+
22
+ test ( 'password length is correct' , async ( ) => {
23
+ let expected_length = 7 ;
24
+
25
+ let password = await generatePassword ( expected_length ) ;
26
+
27
+ expect ( password ) . toHaveLength ( expected_length ) ;
28
+ } ) ;
29
+
30
+ test ( 'password only have lowercase by default' , async ( ) => {
31
+ let expected_length = 7 ;
32
+
33
+ let password = await generatePassword ( expected_length ) ;
34
+
35
+ expect ( password ) . toMatch ( / [ a - z ] + / ) ;
36
+ } ) ;
37
+
38
+ test ( 'countRequirement is 0 if chars are not present' , async ( ) => {
39
+ expect ( countRequirement ( 'abc' , 'def' ) ) . toBe ( 0 ) ;
40
+ } ) ;
41
+
42
+ test ( 'countRequirement is 1 if chars match a single time' , async ( ) => {
43
+ expect ( countRequirement ( 'abcd' , 'def' ) ) . toBe ( 1 ) ;
44
+ } ) ;
45
+
46
+ test ( 'countRequirement counts duplicate' , async ( ) => {
47
+ expect ( countRequirement ( 'abba' , 'bcd' ) ) . toBe ( 2 ) ;
48
+ } ) ;
49
+
50
+ test ( 'password with minimum 1 uppercase' , async ( ) => {
51
+ let expected_length = 7 ;
52
+ let min_uppercase = 1 ;
53
+
54
+ let password = await generatePassword (
55
+ expected_length ,
56
+ 0 ,
57
+ true ,
58
+ min_uppercase ,
59
+ ) ;
60
+
61
+ let uppercase_count = countRequirement ( password , UPPERCASE ) ;
62
+ expect ( uppercase_count ) . toBeGreaterThanOrEqual ( min_uppercase ) ;
63
+ } ) ;
64
+
65
+ test ( 'password with minimum 5 uppercase' , async ( ) => {
66
+ let expected_length = 7 ;
67
+ let min_uppercase = 5 ;
68
+
69
+ let password = await generatePassword (
70
+ expected_length ,
71
+ 0 ,
72
+ true ,
73
+ min_uppercase ,
74
+ ) ;
75
+
76
+ let uppercase_count = countRequirement ( password , UPPERCASE ) ;
77
+ expect ( uppercase_count ) . toBeGreaterThanOrEqual ( min_uppercase ) ;
78
+ } ) ;
79
+
80
+ test ( 'password with minimum 3 uppercase and 3 numbers' , async ( ) => {
81
+ let expected_length = 7 ;
82
+ let min_uppercase = 3 ;
83
+ let min_number = 3 ;
84
+
85
+ let password = await generatePassword (
86
+ expected_length ,
87
+ 0 ,
88
+ true ,
89
+ min_uppercase ,
90
+ true ,
91
+ min_number ,
92
+ ) ;
93
+
94
+ let uppercase_count = countRequirement ( password , UPPERCASE ) ;
95
+ expect ( uppercase_count ) . toBeGreaterThanOrEqual ( min_uppercase ) ;
96
+ let number_count = countRequirement ( password , NUMBER ) ;
97
+ expect ( number_count ) . toBeGreaterThanOrEqual ( min_number ) ;
98
+ } ) ;
99
+
100
+ test ( 'password with minimum 3 lowercase and other chars allowed' , async ( ) => {
101
+ let expected_length = 7 ;
102
+ let min_lowercase = 3 ;
103
+
104
+ let password = await generatePassword (
105
+ expected_length ,
106
+ min_lowercase ,
107
+ true ,
108
+ 0 ,
109
+ true ,
110
+ 0 ,
111
+ true ,
112
+ 0 ,
113
+ ) ;
114
+
115
+ let lowercase_count = countRequirement ( password , LOWERCASE ) ;
116
+ expect ( lowercase_count ) . toBeGreaterThanOrEqual ( min_lowercase ) ;
117
+ } ) ;
118
+
119
+ test ( 'password with minimum 4 special chars and other chars allowed' , async ( ) => {
120
+ let expected_length = 7 ;
121
+ let min_special_char = 4 ;
122
+
123
+ let password = await generatePassword (
124
+ expected_length ,
125
+ 0 ,
126
+ true ,
127
+ 0 ,
128
+ true ,
129
+ 0 ,
130
+ true ,
131
+ min_special_char ,
132
+ ) ;
133
+
134
+ let special_char_count = countRequirement ( password , SPECIAL_CHAR ) ;
135
+ expect ( special_char_count ) . toBeGreaterThanOrEqual ( min_special_char ) ;
136
+ } ) ;
0 commit comments