|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Security\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Security\PasswordValidator; |
|
6
|
|
|
use SilverStripe\Security\Member; |
|
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
8
|
|
|
|
|
9
|
|
|
class PasswordValidatorTest extends SapphireTest |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* {@inheritDoc} |
|
13
|
|
|
* @var bool |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $usesDatabase = true; |
|
16
|
|
|
|
|
17
|
|
|
public function testValidate() |
|
18
|
|
|
{ |
|
19
|
|
|
$v = new PasswordValidator(); |
|
20
|
|
|
$r = $v->validate('', new Member()); |
|
21
|
|
|
$this->assertTrue($r->isValid(), 'Empty password is valid by default'); |
|
22
|
|
|
|
|
23
|
|
|
$r = $v->validate('mypassword', new Member()); |
|
24
|
|
|
$this->assertTrue($r->isValid(), 'Non-Empty password is valid by default'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testValidateMinLength() |
|
28
|
|
|
{ |
|
29
|
|
|
$v = new PasswordValidator(); |
|
30
|
|
|
|
|
31
|
|
|
$v->setMinLength(4); |
|
32
|
|
|
$r = $v->validate('123', new Member()); |
|
33
|
|
|
$this->assertFalse($r->isValid(), 'Password too short'); |
|
34
|
|
|
|
|
35
|
|
|
$v->setMinLength(4); |
|
36
|
|
|
$r = $v->validate('1234', new Member()); |
|
37
|
|
|
$this->assertTrue($r->isValid(), 'Password long enough'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testValidateMinScore() |
|
41
|
|
|
{ |
|
42
|
|
|
// Set both score and set of tests |
|
43
|
|
|
$v = new PasswordValidator(); |
|
44
|
|
|
$v->setMinTestScore(3); |
|
45
|
|
|
$v->setTestNames(["lowercase", "uppercase", "digits", "punctuation"]); |
|
46
|
|
|
|
|
47
|
|
|
$r = $v->validate('aA', new Member()); |
|
48
|
|
|
$this->assertFalse($r->isValid(), 'Passing too few tests'); |
|
49
|
|
|
|
|
50
|
|
|
$r = $v->validate('aA1', new Member()); |
|
51
|
|
|
$this->assertTrue($r->isValid(), 'Passing enough tests'); |
|
52
|
|
|
|
|
53
|
|
|
// Ensure min score without tests works (uses default tests) |
|
54
|
|
|
$v = new PasswordValidator(); |
|
55
|
|
|
$v->setMinTestScore(3); |
|
56
|
|
|
|
|
57
|
|
|
$r = $v->validate('aA', new Member()); |
|
58
|
|
|
$this->assertFalse($r->isValid(), 'Passing too few tests'); |
|
59
|
|
|
|
|
60
|
|
|
$r = $v->validate('aA1', new Member()); |
|
61
|
|
|
$this->assertTrue($r->isValid(), 'Passing enough tests'); |
|
62
|
|
|
|
|
63
|
|
|
// Ensure that min score is only triggered if there are any failing tests at all |
|
64
|
|
|
$v->setMinTestScore(1000); |
|
65
|
|
|
$r = $v->validate('aA1!', new Member()); |
|
66
|
|
|
$this->assertTrue($r->isValid(), 'Passing enough tests'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Test that a certain number of historical passwords are checked if specified |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testHistoricalPasswordCount() |
|
73
|
|
|
{ |
|
74
|
|
|
$validator = new PasswordValidator; |
|
75
|
|
|
$validator->setHistoricCount(3); |
|
76
|
|
|
Member::set_password_validator($validator); |
|
77
|
|
|
|
|
78
|
|
|
$member = new Member; |
|
79
|
|
|
$member->FirstName = 'Repeat'; |
|
80
|
|
|
$member->Surname = 'Password-Man'; |
|
81
|
|
|
$member->Password = 'honk'; |
|
82
|
|
|
$member->write(); |
|
83
|
|
|
|
|
84
|
|
|
// Create a set of used passwords |
|
85
|
|
|
$member->changePassword('foobar'); |
|
86
|
|
|
$member->changePassword('foobaz'); |
|
87
|
|
|
$member->changePassword('barbaz'); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertFalse($member->changePassword('barbaz')->isValid()); |
|
90
|
|
|
$this->assertFalse($member->changePassword('foobaz')->isValid()); |
|
91
|
|
|
$this->assertFalse($member->changePassword('foobar')->isValid()); |
|
92
|
|
|
$this->assertTrue($member->changePassword('honk')->isValid()); |
|
93
|
|
|
$this->assertTrue($member->changePassword('newpassword')->isValid()); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|