-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathPasswordStrengthTest.php
302 lines (251 loc) · 10.3 KB
/
PasswordStrengthTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/*
* This file is part of the RollerworksPasswordStrengthValidator package.
*
* (c) Sebastiaan Stok <s.stok@rollerscapes.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Rollerworks\Component\PasswordStrength\Tests\Validator;
use Rollerworks\Component\PasswordStrength\Validator\Constraints\PasswordStrength;
use Rollerworks\Component\PasswordStrength\Validator\Constraints\PasswordStrengthValidator;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
/**
* @internal
*
* @template-extends ConstraintValidatorTestCase<PasswordStrengthValidator>
*/
final class PasswordStrengthTest extends ConstraintValidatorTestCase
{
/**
* @var array<int, string>
*/
private static $levelToLabel = [
1 => 'very_weak',
2 => 'weak',
3 => 'medium',
4 => 'strong',
5 => 'very_strong',
];
protected function createValidator(): ConstraintValidatorInterface
{
return new PasswordStrengthValidator(new Translator('en'));
}
/** @test */
public function constraints_options_are_properly_resolved(): void
{
// Default option
$constraint = new PasswordStrength(3);
self::assertEquals(3, $constraint->minStrength);
// By option
$constraint = new PasswordStrength(['minStrength' => 3]);
self::assertEquals(3, $constraint->minStrength);
// Specific argument
$constraint = new PasswordStrength(null, null, null, 3);
self::assertEquals(3, $constraint->minStrength);
}
/** @test */
public function null_is_valid(): void
{
$this->validator->validate(null, new PasswordStrength(6));
$this->assertNoViolation();
}
/** @test */
public function empty_is_valid(): void
{
$this->validator->validate('', new PasswordStrength(6));
$this->assertNoViolation();
}
/** @test */
public function expects_string_compatible_type(): void
{
$this->expectException(UnexpectedTypeException::class);
$this->validator->validate(new \stdClass(), new PasswordStrength(5));
}
/**
* @return iterable<int, array{0: int, 1: string, 2: int, 3: string}>
*/
public function provideWeak_passwords_will_not_passCases(): iterable
{
$pre = 'rollerworks_password.tip.';
return [
// Very weak
[2, 'weaker', 1, "{$pre}uppercase_letters, {$pre}numbers, {$pre}special_chars, {$pre}length"],
[2, '123456', 1, "{$pre}letters, {$pre}special_chars, {$pre}length"],
[2, 'foobar', 1, "{$pre}uppercase_letters, {$pre}numbers, {$pre}special_chars, {$pre}length"],
[2, '!.!.!.', 1, "{$pre}letters, {$pre}numbers, {$pre}length"],
// Weak
[3, 'wee6eak', 2, "{$pre}uppercase_letters, {$pre}special_chars, {$pre}length"],
[3, 'foobar!', 2, "{$pre}uppercase_letters, {$pre}numbers, {$pre}length"],
[3, 'Foobar', 2, "{$pre}numbers, {$pre}special_chars, {$pre}length"],
[3, '123456!', 2, "{$pre}letters, {$pre}length"],
[3, '7857375923752947', 2, "{$pre}letters, {$pre}special_chars"],
[3, 'FSDFJSLKFFSDFDSF', 2, "{$pre}lowercase_letters, {$pre}numbers, {$pre}special_chars"],
[3, 'fjsfjdljfsjsjjlsj', 2, "{$pre}uppercase_letters, {$pre}numbers, {$pre}special_chars"],
// Medium
[4, 'Foobar!', 3, "{$pre}numbers, {$pre}length"],
[4, 'foo-b0r!', 3, "{$pre}uppercase_letters, {$pre}length"],
[4, 'fjsfjdljfsjsjjls1', 3, "{$pre}uppercase_letters, {$pre}special_chars"],
[4, '785737592375294b', 3, "{$pre}uppercase_letters, {$pre}special_chars"],
];
}
/**
* @return iterable<int, array{0: int, 1: string, 2: int, 3: string}>
*/
public function provideWeak_passwords_with_unicode_will_not_passCases(): iterable
{
$pre = 'rollerworks_password.tip.';
// \u{FD3E} = ﴾ = Arabic ornate left parenthesis
return [
// Very weak
[2, 'weaker', 1, "{$pre}uppercase_letters, {$pre}numbers, {$pre}special_chars, {$pre}length"],
[2, '123456', 1, "{$pre}letters, {$pre}special_chars, {$pre}length"],
[2, '²²²²²²', 1, "{$pre}letters, {$pre}special_chars, {$pre}length"],
[2, 'foobar', 1, "{$pre}uppercase_letters, {$pre}numbers, {$pre}special_chars, {$pre}length"],
[2, 'ömgwat', 1, "{$pre}uppercase_letters, {$pre}numbers, {$pre}special_chars, {$pre}length"],
[2, '!.!.!.', 1, "{$pre}letters, {$pre}numbers, {$pre}length"],
[2, '!.!.!﴾', 1, "{$pre}letters, {$pre}numbers, {$pre}length"],
// Weak
[3, 'wee6eak', 2, "{$pre}uppercase_letters, {$pre}special_chars, {$pre}length"],
[3, 'foobar!', 2, "{$pre}uppercase_letters, {$pre}numbers, {$pre}length"],
[3, 'Foobar', 2, "{$pre}numbers, {$pre}special_chars, {$pre}length"],
[3, '123456!', 2, "{$pre}letters, {$pre}length"],
[3, '7857375923752947', 2, "{$pre}letters, {$pre}special_chars"],
[3, 'FSDFJSLKFFSDFDSF', 2, "{$pre}lowercase_letters, {$pre}numbers, {$pre}special_chars"],
[3, 'FÜKFJSLKFFSDFDSF', 2, "{$pre}lowercase_letters, {$pre}numbers, {$pre}special_chars"],
[3, 'fjsfjdljfsjsjjlsj', 2, "{$pre}uppercase_letters, {$pre}numbers, {$pre}special_chars"],
// Medium
[4, 'Foobar﴾', 3, "{$pre}numbers, {$pre}length"],
[4, 'foo-b0r!', 3, "{$pre}uppercase_letters, {$pre}length"],
[4, 'fjsfjdljfsjsjjls1', 3, "{$pre}uppercase_letters, {$pre}special_chars"],
[4, '785737592375294b', 3, "{$pre}uppercase_letters, {$pre}special_chars"],
];
}
/**
* @return iterable<int, array{0: string}>
*/
public static function provideStrongPasswords(): iterable
{
return [
['Foobar!55!'],
['Foobar$55'],
['Foobar€55'],
['Foobar€55'],
];
}
/**
* @return iterable<int, array{0: string}>
*/
public static function provideStrong_passwords_will_passCases(): iterable
{
return [
['Foobar$55_4&F'],
['L33RoyJ3Jenkins!'],
];
}
/** @test */
public function short_password_will_not_pass(): void
{
$constraint = new PasswordStrength(['minStrength' => 5, 'minLength' => 6]);
$this->validator->validate('foo', $constraint);
$parameters = [
'{{length}}' => 6,
];
$this->buildViolation('Your password must be at least {{length}} characters long.')
->setParameters($parameters)
->assertRaised()
;
}
/** @test */
public function short_password_in_multi_byte_will_not_pass(): void
{
$constraint = new PasswordStrength(['minStrength' => 5, 'minLength' => 7]);
$this->validator->validate('foöled', $constraint);
$parameters = [
'{{length}}' => 7,
];
$this->buildViolation('Your password must be at least {{length}} characters long.')
->setParameters($parameters)
->assertRaised()
;
}
/**
* @dataProvider provideWeak_passwords_will_not_passCases
*
* @test
*/
public function weak_passwords_will_not_pass(int $minStrength, string $value, int $currentStrength, string $tips = ''): void
{
$constraint = new PasswordStrength(['minStrength' => $minStrength, 'minLength' => 6]);
$this->validator->validate($value, $constraint);
$parameters = [
'{{ length }}' => 6,
'{{ min_strength }}' => 'rollerworks_password.strength_level.' . self::$levelToLabel[$minStrength],
'{{ current_strength }}' => 'rollerworks_password.strength_level.' . self::$levelToLabel[$currentStrength],
'{{ strength_tips }}' => $tips,
];
$this->buildViolation('password_too_weak')
->setParameters($parameters)
->assertRaised()
;
}
/**
* @dataProvider provideWeak_passwords_with_unicode_will_not_passCases
*
* @test
*/
public function weak_passwords_with_unicode_will_not_pass(int $minStrength, string $value, int $currentStrength, string $tips = ''): void
{
$constraint = new PasswordStrength(['minStrength' => $minStrength, 'minLength' => 6, 'unicodeEquality' => true]);
$this->validator->validate($value, $constraint);
$parameters = [
'{{ length }}' => 6,
'{{ min_strength }}' => 'rollerworks_password.strength_level.' . self::$levelToLabel[$minStrength],
'{{ current_strength }}' => 'rollerworks_password.strength_level.' . self::$levelToLabel[$currentStrength],
'{{ strength_tips }}' => $tips,
];
$this->buildViolation('password_too_weak')
->setParameters($parameters)
->assertRaised()
;
}
/**
* @dataProvider provideStrong_passwords_will_passCases
*
* @test
*/
public function strong_passwords_will_pass(string $value): void
{
$constraint = new PasswordStrength(5);
$this->validator->validate($value, $constraint);
$this->assertNoViolation();
}
/** @test */
public function constraint_get_default_option(): void
{
$constraint = new PasswordStrength(5);
self::assertEquals(5, $constraint->minStrength);
}
/** @test */
public function parameters_are_translated_when_translator_is_missing(): void
{
$this->validator = new PasswordStrengthValidator();
$this->validator->initialize($this->context);
$constraint = new PasswordStrength(['minStrength' => 5, 'minLength' => 6]);
$this->validator->validate('FD43f.!', $constraint);
$parameters = [
'{{ length }}' => 6,
'{{ current_strength }}' => 'Strong',
'{{ min_strength }}' => 'Very strong',
'{{ strength_tips }}' => 'add more characters',
];
$this->buildViolation('password_too_weak')
->setParameters($parameters)
->assertRaised()
;
}
}