| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Tests\Unit; |
| 25 |
|
| 26 |
use MediaWiki\Password\PasswordPolicyChecks; |
| 27 |
use MediaWiki\User\User; |
| 28 |
use MediaWikiUnitTestCase; |
| 29 |
use PHPUnit\Framework\MockObject\MockObject; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class PasswordPolicyChecksTest extends MediaWikiUnitTestCase { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private function getUser( $name = 'user' ) { |
| 43 |
$user = $this->createMock( User::class ); |
| 44 |
$user->method( 'getName' )->willReturn( $name ); |
| 45 |
return $user; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function testCheckMinimalPasswordLength() { |
| 52 |
$statusOK = PasswordPolicyChecks::checkMinimalPasswordLength( |
| 53 |
3, |
| 54 |
$this->getUser(), |
| 55 |
'password' |
| 56 |
); |
| 57 |
$this->assertStatusGood( $statusOK, 'Password is longer than minimal policy' ); |
| 58 |
$statusShort = PasswordPolicyChecks::checkMinimalPasswordLength( |
| 59 |
10, |
| 60 |
$this->getUser(), |
| 61 |
'password' |
| 62 |
); |
| 63 |
$this->assertStatusWarning( 'passwordtooshort', $statusShort, |
| 64 |
'Password is shorter than minimal policy, not fatal' |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function testCheckMinimumPasswordLengthToLogin() { |
| 72 |
$statusOK = PasswordPolicyChecks::checkMinimumPasswordLengthToLogin( |
| 73 |
3, |
| 74 |
$this->getUser(), |
| 75 |
'password' |
| 76 |
); |
| 77 |
$this->assertStatusGood( $statusOK, 'Password is longer than minimal policy' ); |
| 78 |
$statusShort = PasswordPolicyChecks::checkMinimumPasswordLengthToLogin( |
| 79 |
10, |
| 80 |
$this->getUser(), |
| 81 |
'password' |
| 82 |
); |
| 83 |
$this->assertStatusError( 'passwordtooshort', $statusShort, |
| 84 |
'Password is shorter than minimum login policy, fatal' |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function testCheckMaximalPasswordLength() { |
| 92 |
$statusOK = PasswordPolicyChecks::checkMaximalPasswordLength( |
| 93 |
100, |
| 94 |
$this->getUser(), |
| 95 |
'password' |
| 96 |
); |
| 97 |
$this->assertStatusGood( $statusOK, 'Password is shorter than maximal policy' ); |
| 98 |
$statusLong = PasswordPolicyChecks::checkMaximalPasswordLength( |
| 99 |
4, |
| 100 |
$this->getUser(), |
| 101 |
'password' |
| 102 |
); |
| 103 |
$this->assertStatusError( 'passwordtoolong', $statusLong, |
| 104 |
'Password is longer than maximal policy, fatal' |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function testCheckPasswordCannotBeSubstringInUsername() { |
| 112 |
$statusOK = PasswordPolicyChecks::checkPasswordCannotBeSubstringInUsername( |
| 113 |
1, |
| 114 |
$this->getUser(), |
| 115 |
'password' |
| 116 |
); |
| 117 |
$this->assertStatusGood( $statusOK, 'Password is not a substring of username' ); |
| 118 |
$statusLong = PasswordPolicyChecks::checkPasswordCannotBeSubstringInUsername( |
| 119 |
1, |
| 120 |
$this->getUser( '123user123' ), |
| 121 |
'user' |
| 122 |
); |
| 123 |
$this->assertStatusWarning( 'password-substring-username-match', $statusLong, |
| 124 |
'Password is a substring of username, not fatal' ); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function testCheckPasswordCannotMatchDefaults( |
| 132 |
bool $failureExpected, |
| 133 |
bool $policyValue, |
| 134 |
string $username, |
| 135 |
string $password |
| 136 |
) { |
| 137 |
$status = PasswordPolicyChecks::checkPasswordCannotMatchDefaults( |
| 138 |
$policyValue, |
| 139 |
$this->getUser( $username ), |
| 140 |
$password |
| 141 |
); |
| 142 |
|
| 143 |
if ( $failureExpected ) { |
| 144 |
$this->assertStatusWarning( 'password-login-forbidden', $status ); |
| 145 |
} else { |
| 146 |
$this->assertStatusGood( $status, 'Password is not on defaults list' ); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
public static function provideCheckPasswordCannotMatchDefaults() { |
| 151 |
return [ |
| 152 |
'Unique username and password' => [ false, true, 'Unique username', 'AUniquePassword' ], |
| 153 |
'Invalid combination' => [ true, true, 'Useruser1', 'Passpass1' ], |
| 154 |
'Invalid password' => [ true, true, 'Whatever username', 'ExamplePassword' ], |
| 155 |
'Uniques but no policy' => [ false, false, 'Unique username', 'AUniquePassword' ], |
| 156 |
'Invalid combination but no policy' => [ false, false, 'Useruser1', 'Passpass1' ], |
| 157 |
'Invalid password but no policy' => [ false, false, 'Whatever username', 'ExamplePassword' ], |
| 158 |
]; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function testCheckNotInCommonList( $expected, $password ) { |
| 166 |
$status = PasswordPolicyChecks::checkPasswordNotInCommonList( |
| 167 |
true, |
| 168 |
$this->getUser( 'username' ), |
| 169 |
$password |
| 170 |
); |
| 171 |
$this->assertSame( $expected, $status->isGood() ); |
| 172 |
} |
| 173 |
|
| 174 |
public static function provideCommonList() { |
| 175 |
return [ |
| 176 |
[ false, 'testpass' ], |
| 177 |
[ false, 'password' ], |
| 178 |
[ false, '12345' ], |
| 179 |
[ true, 'DKn17egcA4' ], |
| 180 |
[ true, 'testwikijenkinspass' ], |
| 181 |
]; |
| 182 |
} |
| 183 |
} |