]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | import { HttpClientTestingModule } from '@angular/common/http/testing'; |
2 | import { TestBed } from '@angular/core/testing'; | |
3 | ||
4 | import { of as observableOf } from 'rxjs'; | |
5 | ||
f67539c2 | 6 | import { configureTestBed } from '~/testing/unit-test-helper'; |
9f95a23c TL |
7 | import { SettingsService } from '../api/settings.service'; |
8 | import { SharedModule } from '../shared.module'; | |
9 | import { PasswordPolicyService } from './password-policy.service'; | |
10 | ||
11 | describe('PasswordPolicyService', () => { | |
12 | let service: PasswordPolicyService; | |
13 | let settingsService: SettingsService; | |
14 | ||
15 | const helpTextHelper = { | |
16 | get: (chk: string) => { | |
17 | const chkTexts: { [key: string]: string } = { | |
18 | chk_length: 'Must contain at least 10 characters', | |
19 | chk_oldpwd: 'Must not be the same as the previous one', | |
20 | chk_username: 'Cannot contain the username', | |
21 | chk_exclusion_list: 'Cannot contain any configured keyword', | |
22 | chk_repetitive: 'Cannot contain any repetitive characters e.g. "aaa"', | |
23 | chk_sequential: 'Cannot contain any sequential characters e.g. "abc"', | |
24 | chk_complexity: | |
25 | 'Must consist of characters from the following groups:\n' + | |
26 | ' * Alphabetic a-z, A-Z\n' + | |
27 | ' * Numbers 0-9\n' + | |
28 | ' * Special chars: !"#$%& \'()*+,-./:;<=>?@[\\]^_`{{|}}~\n' + | |
29 | ' * Any other characters (signs)' | |
30 | }; | |
31 | return ['Required rules for passwords:', '- ' + chkTexts[chk]].join('\n'); | |
32 | } | |
33 | }; | |
34 | ||
35 | configureTestBed({ | |
f67539c2 | 36 | imports: [HttpClientTestingModule, SharedModule] |
9f95a23c TL |
37 | }); |
38 | ||
39 | beforeEach(() => { | |
f67539c2 TL |
40 | service = TestBed.inject(PasswordPolicyService); |
41 | settingsService = TestBed.inject(SettingsService); | |
9f95a23c TL |
42 | settingsService['settings'] = {}; |
43 | }); | |
44 | ||
45 | it('should be created', () => { | |
46 | expect(service).toBeTruthy(); | |
47 | }); | |
48 | ||
49 | it('should not get help text', () => { | |
50 | let helpText = ''; | |
51 | spyOn(settingsService, 'getStandardSettings').and.returnValue( | |
52 | observableOf({ | |
53 | pwd_policy_enabled: false | |
54 | }) | |
55 | ); | |
56 | service.getHelpText().subscribe((text) => (helpText = text)); | |
57 | expect(helpText).toBe(''); | |
58 | }); | |
59 | ||
60 | it('should get help text chk_length', () => { | |
61 | let helpText = ''; | |
62 | const expectedHelpText = helpTextHelper.get('chk_length'); | |
63 | spyOn(settingsService, 'getStandardSettings').and.returnValue( | |
64 | observableOf({ | |
65 | user_pwd_expiration_warning_1: 10, | |
66 | user_pwd_expiration_warning_2: 5, | |
67 | user_pwd_expiration_span: 90, | |
68 | pwd_policy_enabled: true, | |
69 | pwd_policy_min_length: 10, | |
70 | pwd_policy_check_length_enabled: true, | |
71 | pwd_policy_check_oldpwd_enabled: false, | |
72 | pwd_policy_check_sequential_chars_enabled: false, | |
73 | pwd_policy_check_complexity_enabled: false | |
74 | }) | |
75 | ); | |
76 | service.getHelpText().subscribe((text) => (helpText = text)); | |
77 | expect(helpText).toBe(expectedHelpText); | |
78 | }); | |
79 | ||
80 | it('should get help text chk_oldpwd', () => { | |
81 | let helpText = ''; | |
82 | const expectedHelpText = helpTextHelper.get('chk_oldpwd'); | |
83 | spyOn(settingsService, 'getStandardSettings').and.returnValue( | |
84 | observableOf({ | |
85 | pwd_policy_enabled: true, | |
86 | pwd_policy_check_oldpwd_enabled: true, | |
87 | pwd_policy_check_username_enabled: false, | |
88 | pwd_policy_check_exclusion_list_enabled: false, | |
89 | pwd_policy_check_complexity_enabled: false | |
90 | }) | |
91 | ); | |
92 | service.getHelpText().subscribe((text) => (helpText = text)); | |
93 | expect(helpText).toBe(expectedHelpText); | |
94 | }); | |
95 | ||
96 | it('should get help text chk_username', () => { | |
97 | let helpText = ''; | |
98 | const expectedHelpText = helpTextHelper.get('chk_username'); | |
99 | spyOn(settingsService, 'getStandardSettings').and.returnValue( | |
100 | observableOf({ | |
101 | pwd_policy_enabled: true, | |
102 | pwd_policy_check_oldpwd_enabled: false, | |
103 | pwd_policy_check_username_enabled: true, | |
104 | pwd_policy_check_exclusion_list_enabled: false | |
105 | }) | |
106 | ); | |
107 | service.getHelpText().subscribe((text) => (helpText = text)); | |
108 | expect(helpText).toBe(expectedHelpText); | |
109 | }); | |
110 | ||
111 | it('should get help text chk_exclusion_list', () => { | |
112 | let helpText = ''; | |
113 | const expectedHelpText = helpTextHelper.get('chk_exclusion_list'); | |
114 | spyOn(settingsService, 'getStandardSettings').and.returnValue( | |
115 | observableOf({ | |
116 | pwd_policy_enabled: true, | |
117 | pwd_policy_check_username_enabled: false, | |
118 | pwd_policy_check_exclusion_list_enabled: true, | |
119 | pwd_policy_check_repetitive_chars_enabled: false | |
120 | }) | |
121 | ); | |
122 | service.getHelpText().subscribe((text) => (helpText = text)); | |
123 | expect(helpText).toBe(expectedHelpText); | |
124 | }); | |
125 | ||
126 | it('should get help text chk_repetitive', () => { | |
127 | let helpText = ''; | |
128 | const expectedHelpText = helpTextHelper.get('chk_repetitive'); | |
129 | spyOn(settingsService, 'getStandardSettings').and.returnValue( | |
130 | observableOf({ | |
131 | user_pwd_expiration_warning_1: 10, | |
132 | pwd_policy_enabled: true, | |
133 | pwd_policy_check_oldpwd_enabled: false, | |
134 | pwd_policy_check_exclusion_list_enabled: false, | |
135 | pwd_policy_check_repetitive_chars_enabled: true, | |
136 | pwd_policy_check_sequential_chars_enabled: false, | |
137 | pwd_policy_check_complexity_enabled: false | |
138 | }) | |
139 | ); | |
140 | service.getHelpText().subscribe((text) => (helpText = text)); | |
141 | expect(helpText).toBe(expectedHelpText); | |
142 | }); | |
143 | ||
144 | it('should get help text chk_sequential', () => { | |
145 | let helpText = ''; | |
146 | const expectedHelpText = helpTextHelper.get('chk_sequential'); | |
147 | spyOn(settingsService, 'getStandardSettings').and.returnValue( | |
148 | observableOf({ | |
149 | pwd_policy_enabled: true, | |
150 | pwd_policy_min_length: 8, | |
151 | pwd_policy_check_length_enabled: false, | |
152 | pwd_policy_check_oldpwd_enabled: false, | |
153 | pwd_policy_check_username_enabled: false, | |
154 | pwd_policy_check_exclusion_list_enabled: false, | |
155 | pwd_policy_check_repetitive_chars_enabled: false, | |
156 | pwd_policy_check_sequential_chars_enabled: true, | |
157 | pwd_policy_check_complexity_enabled: false | |
158 | }) | |
159 | ); | |
160 | service.getHelpText().subscribe((text) => (helpText = text)); | |
161 | expect(helpText).toBe(expectedHelpText); | |
162 | }); | |
163 | ||
164 | it('should get help text chk_complexity', () => { | |
165 | let helpText = ''; | |
166 | const expectedHelpText = helpTextHelper.get('chk_complexity'); | |
167 | spyOn(settingsService, 'getStandardSettings').and.returnValue( | |
168 | observableOf({ | |
169 | pwd_policy_enabled: true, | |
170 | pwd_policy_min_length: 8, | |
171 | pwd_policy_check_length_enabled: false, | |
172 | pwd_policy_check_oldpwd_enabled: false, | |
173 | pwd_policy_check_username_enabled: false, | |
174 | pwd_policy_check_exclusion_list_enabled: false, | |
175 | pwd_policy_check_repetitive_chars_enabled: false, | |
176 | pwd_policy_check_sequential_chars_enabled: false, | |
177 | pwd_policy_check_complexity_enabled: true | |
178 | }) | |
179 | ); | |
180 | service.getHelpText().subscribe((text) => (helpText = text)); | |
181 | expect(helpText).toBe(expectedHelpText); | |
182 | }); | |
183 | ||
184 | it('should get too-weak class', () => { | |
185 | expect(service.mapCreditsToCssClass(0)).toBe('too-weak'); | |
186 | expect(service.mapCreditsToCssClass(9)).toBe('too-weak'); | |
187 | }); | |
188 | ||
189 | it('should get weak class', () => { | |
190 | expect(service.mapCreditsToCssClass(10)).toBe('weak'); | |
191 | expect(service.mapCreditsToCssClass(14)).toBe('weak'); | |
192 | }); | |
193 | ||
194 | it('should get ok class', () => { | |
195 | expect(service.mapCreditsToCssClass(15)).toBe('ok'); | |
196 | expect(service.mapCreditsToCssClass(19)).toBe('ok'); | |
197 | }); | |
198 | ||
199 | it('should get strong class', () => { | |
200 | expect(service.mapCreditsToCssClass(20)).toBe('strong'); | |
201 | expect(service.mapCreditsToCssClass(24)).toBe('strong'); | |
202 | }); | |
203 | ||
204 | it('should get very-strong class', () => { | |
205 | expect(service.mapCreditsToCssClass(25)).toBe('very-strong'); | |
206 | expect(service.mapCreditsToCssClass(30)).toBe('very-strong'); | |
207 | }); | |
208 | }); |