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