3 * Testing for password-policy enforcement, based on a user's groups.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 class UserPasswordPolicyTest
extends MediaWikiTestCase
{
25 protected $policies = [
27 'MinimalPasswordLength' => 10,
28 'MinimumPasswordLengthToLogin' => 6,
29 'PasswordCannotMatchUsername' => true,
32 'MinimalPasswordLength' => 8,
33 'MinimumPasswordLengthToLogin' => 1,
34 'PasswordCannotMatchUsername' => true,
37 'MinimalPasswordLength' => 4,
38 'MinimumPasswordLengthToLogin' => 1,
39 'PasswordCannotMatchBlacklist' => true,
40 'MaximalPasswordLength' => 4096,
45 'MinimalPasswordLength' => 'PasswordPolicyChecks::checkMinimalPasswordLength',
46 'MinimumPasswordLengthToLogin' => 'PasswordPolicyChecks::checkMinimumPasswordLengthToLogin',
47 'PasswordCannotMatchUsername' => 'PasswordPolicyChecks::checkPasswordCannotMatchUsername',
48 'PasswordCannotMatchBlacklist' => 'PasswordPolicyChecks::checkPasswordCannotMatchBlacklist',
49 'MaximalPasswordLength' => 'PasswordPolicyChecks::checkMaximalPasswordLength',
52 private function getUserPasswordPolicy() {
53 return new UserPasswordPolicy( $this->policies
, $this->checks
);
57 * @covers UserPasswordPolicy::getPoliciesForUser
59 public function testGetPoliciesForUser() {
61 $upp = $this->getUserPasswordPolicy();
63 $user = User
::newFromName( 'TestUserPolicy' );
64 $user->addToDatabase();
65 $user->addGroup( 'sysop' );
67 $this->assertArrayEquals(
69 'MinimalPasswordLength' => 8,
70 'MinimumPasswordLengthToLogin' => 1,
71 'PasswordCannotMatchUsername' => 1,
72 'PasswordCannotMatchBlacklist' => true,
73 'MaximalPasswordLength' => 4096,
75 $upp->getPoliciesForUser( $user )
80 * @covers UserPasswordPolicy::getPoliciesForGroups
82 public function testGetPoliciesForGroups() {
83 $effective = UserPasswordPolicy
::getPoliciesForGroups(
85 [ 'user', 'checkuser' ],
86 $this->policies
['default']
89 $this->assertArrayEquals(
91 'MinimalPasswordLength' => 10,
92 'MinimumPasswordLengthToLogin' => 6,
93 'PasswordCannotMatchUsername' => true,
94 'PasswordCannotMatchBlacklist' => true,
95 'MaximalPasswordLength' => 4096,
102 * @dataProvider provideCheckUserPassword
103 * @covers UserPasswordPolicy::checkUserPassword
105 public function testCheckUserPassword( $username, $groups, $password, $valid, $ok, $msg ) {
107 $upp = $this->getUserPasswordPolicy();
109 $user = User
::newFromName( $username );
110 $user->addToDatabase();
111 foreach ( $groups as $group ) {
112 $user->addGroup( $group );
115 $status = $upp->checkUserPassword( $user, $password );
116 $this->assertSame( $valid, $status->isGood(), $msg . ' - password valid' );
117 $this->assertSame( $ok, $status->isOK(), $msg . ' - can login' );
120 public function provideCheckUserPassword() {
128 'No groups, default policy, password too short to login'
136 'Default policy, short password'
144 'Sysop with good password'
152 'Sysop with short password'
156 [ 'sysop', 'checkuser' ],
160 'Checkuser with short password'
164 [ 'sysop', 'checkuser' ],
168 'Checkuser with too short password to login'
176 'Username & password on blacklist'
182 * @dataProvider provideMaxOfPolicies
183 * @covers UserPasswordPolicy::maxOfPolicies
185 public function testMaxOfPolicies( $p1, $p2, $max, $msg ) {
186 $this->assertArrayEquals(
188 UserPasswordPolicy
::maxOfPolicies( $p1, $p2 ),
193 public function provideMaxOfPolicies() {
196 [ 'MinimalPasswordLength' => 8 ], // p1
197 [ 'MinimalPasswordLength' => 2 ], // p2
198 [ 'MinimalPasswordLength' => 8 ], // max
202 [ 'MinimalPasswordLength' => 2 ], // p1
203 [ 'MinimalPasswordLength' => 8 ], // p2
204 [ 'MinimalPasswordLength' => 8 ], // max
208 [ 'MinimalPasswordLength' => 8 ], // p1
210 'MinimalPasswordLength' => 2,
211 'PasswordCannotMatchUsername' => 1,
214 'MinimalPasswordLength' => 8,
215 'PasswordCannotMatchUsername' => 1,
217 'Missing items in p1'
221 'MinimalPasswordLength' => 8,
222 'PasswordCannotMatchUsername' => 1,
225 'MinimalPasswordLength' => 2,
228 'MinimalPasswordLength' => 8,
229 'PasswordCannotMatchUsername' => 1,
231 'Missing items in p2'