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
26 class UserPasswordPolicyTest
extends MediaWikiTestCase
{
28 protected $policies = [
30 'MinimalPasswordLength' => 10,
31 'MinimumPasswordLengthToLogin' => 6,
32 'PasswordCannotMatchUsername' => true,
35 'MinimalPasswordLength' => 8,
36 'MinimumPasswordLengthToLogin' => 1,
37 'PasswordCannotMatchUsername' => true,
40 'MinimalPasswordLength' => 4,
41 'MinimumPasswordLengthToLogin' => 1,
42 'PasswordCannotMatchBlacklist' => true,
43 'MaximalPasswordLength' => 4096,
48 'MinimalPasswordLength' => 'PasswordPolicyChecks::checkMinimalPasswordLength',
49 'MinimumPasswordLengthToLogin' => 'PasswordPolicyChecks::checkMinimumPasswordLengthToLogin',
50 'PasswordCannotMatchUsername' => 'PasswordPolicyChecks::checkPasswordCannotMatchUsername',
51 'PasswordCannotMatchBlacklist' => 'PasswordPolicyChecks::checkPasswordCannotMatchBlacklist',
52 'MaximalPasswordLength' => 'PasswordPolicyChecks::checkMaximalPasswordLength',
55 private function getUserPasswordPolicy() {
56 return new UserPasswordPolicy( $this->policies
, $this->checks
);
60 * @covers UserPasswordPolicy::getPoliciesForUser
62 public function testGetPoliciesForUser() {
64 $upp = $this->getUserPasswordPolicy();
66 $user = User
::newFromName( 'TestUserPolicy' );
67 $user->addToDatabase();
68 $user->addGroup( 'sysop' );
70 $this->assertArrayEquals(
72 'MinimalPasswordLength' => 8,
73 'MinimumPasswordLengthToLogin' => 1,
74 'PasswordCannotMatchUsername' => 1,
75 'PasswordCannotMatchBlacklist' => true,
76 'MaximalPasswordLength' => 4096,
78 $upp->getPoliciesForUser( $user )
83 * @covers UserPasswordPolicy::getPoliciesForGroups
85 public function testGetPoliciesForGroups() {
86 $effective = UserPasswordPolicy
::getPoliciesForGroups(
88 [ 'user', 'checkuser' ],
89 $this->policies
['default']
92 $this->assertArrayEquals(
94 'MinimalPasswordLength' => 10,
95 'MinimumPasswordLengthToLogin' => 6,
96 'PasswordCannotMatchUsername' => true,
97 'PasswordCannotMatchBlacklist' => true,
98 'MaximalPasswordLength' => 4096,
105 * @dataProvider provideCheckUserPassword
106 * @covers UserPasswordPolicy::checkUserPassword
108 public function testCheckUserPassword( $username, $groups, $password, $valid, $ok, $msg ) {
110 $upp = $this->getUserPasswordPolicy();
112 $user = User
::newFromName( $username );
113 $user->addToDatabase();
114 foreach ( $groups as $group ) {
115 $user->addGroup( $group );
118 $status = $upp->checkUserPassword( $user, $password );
119 $this->assertSame( $valid, $status->isGood(), $msg . ' - password valid' );
120 $this->assertSame( $ok, $status->isOK(), $msg . ' - can login' );
123 public function provideCheckUserPassword() {
131 'No groups, default policy, password too short to login'
139 'Default policy, short password'
147 'Sysop with good password'
155 'Sysop with short password'
159 [ 'sysop', 'checkuser' ],
163 'Checkuser with short password'
167 [ 'sysop', 'checkuser' ],
171 'Checkuser with too short password to login'
179 'Username & password on blacklist'
185 * @dataProvider provideMaxOfPolicies
186 * @covers UserPasswordPolicy::maxOfPolicies
188 public function testMaxOfPolicies( $p1, $p2, $max, $msg ) {
189 $this->assertArrayEquals(
191 UserPasswordPolicy
::maxOfPolicies( $p1, $p2 ),
196 public function provideMaxOfPolicies() {
199 [ 'MinimalPasswordLength' => 8 ], // p1
200 [ 'MinimalPasswordLength' => 2 ], // p2
201 [ 'MinimalPasswordLength' => 8 ], // max
205 [ 'MinimalPasswordLength' => 2 ], // p1
206 [ 'MinimalPasswordLength' => 8 ], // p2
207 [ 'MinimalPasswordLength' => 8 ], // max
211 [ 'MinimalPasswordLength' => 8 ], // p1
213 'MinimalPasswordLength' => 2,
214 'PasswordCannotMatchUsername' => 1,
217 'MinimalPasswordLength' => 8,
218 'PasswordCannotMatchUsername' => 1,
220 'Missing items in p1'
224 'MinimalPasswordLength' => 8,
225 'PasswordCannotMatchUsername' => 1,
228 'MinimalPasswordLength' => 2,
231 'MinimalPasswordLength' => 8,
232 'PasswordCannotMatchUsername' => 1,
234 'Missing items in p2'