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
25 * @covers UserPasswordPolicy
27 class UserPasswordPolicyTest
extends MediaWikiTestCase
{
29 protected $policies = [
31 'MinimalPasswordLength' => 10,
32 'MinimumPasswordLengthToLogin' => 6,
33 'PasswordCannotMatchUsername' => true,
36 'MinimalPasswordLength' => 8,
37 'MinimumPasswordLengthToLogin' => 1,
38 'PasswordCannotMatchUsername' => true,
41 'MinimalPasswordLength' => 4,
42 'MinimumPasswordLengthToLogin' => 1,
43 'PasswordCannotMatchBlacklist' => true,
44 'MaximalPasswordLength' => 4096,
49 'MinimalPasswordLength' => 'PasswordPolicyChecks::checkMinimalPasswordLength',
50 'MinimumPasswordLengthToLogin' => 'PasswordPolicyChecks::checkMinimumPasswordLengthToLogin',
51 'PasswordCannotMatchUsername' => 'PasswordPolicyChecks::checkPasswordCannotMatchUsername',
52 'PasswordCannotMatchBlacklist' => 'PasswordPolicyChecks::checkPasswordCannotMatchBlacklist',
53 'MaximalPasswordLength' => 'PasswordPolicyChecks::checkMaximalPasswordLength',
56 private function getUserPasswordPolicy() {
57 return new UserPasswordPolicy( $this->policies
, $this->checks
);
60 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 )
79 public function testGetPoliciesForGroups() {
80 $effective = UserPasswordPolicy
::getPoliciesForGroups(
82 [ 'user', 'checkuser' ],
83 $this->policies
['default']
86 $this->assertArrayEquals(
88 'MinimalPasswordLength' => 10,
89 'MinimumPasswordLengthToLogin' => 6,
90 'PasswordCannotMatchUsername' => true,
91 'PasswordCannotMatchBlacklist' => true,
92 'MaximalPasswordLength' => 4096,
99 * @dataProvider provideCheckUserPassword
101 public function testCheckUserPassword( $username, $groups, $password, $valid, $ok, $msg ) {
102 $upp = $this->getUserPasswordPolicy();
104 $user = User
::newFromName( $username );
105 $user->addToDatabase();
106 foreach ( $groups as $group ) {
107 $user->addGroup( $group );
110 $status = $upp->checkUserPassword( $user, $password );
111 $this->assertSame( $valid, $status->isGood(), $msg . ' - password valid' );
112 $this->assertSame( $ok, $status->isOK(), $msg . ' - can login' );
115 public function provideCheckUserPassword() {
123 'No groups, default policy, password too short to login'
131 'Default policy, short password'
139 'Sysop with good password'
147 'Sysop with short password'
151 [ 'sysop', 'checkuser' ],
155 'Checkuser with short password'
159 [ 'sysop', 'checkuser' ],
163 'Checkuser with too short password to login'
171 'Username & password on blacklist'
177 * @dataProvider provideMaxOfPolicies
179 public function testMaxOfPolicies( $p1, $p2, $max, $msg ) {
180 $this->assertArrayEquals(
182 UserPasswordPolicy
::maxOfPolicies( $p1, $p2 ),
187 public function provideMaxOfPolicies() {
190 [ 'MinimalPasswordLength' => 8 ], // p1
191 [ 'MinimalPasswordLength' => 2 ], // p2
192 [ 'MinimalPasswordLength' => 8 ], // max
196 [ 'MinimalPasswordLength' => 2 ], // p1
197 [ 'MinimalPasswordLength' => 8 ], // p2
198 [ 'MinimalPasswordLength' => 8 ], // max
202 [ 'MinimalPasswordLength' => 8 ], // p1
204 'MinimalPasswordLength' => 2,
205 'PasswordCannotMatchUsername' => 1,
208 'MinimalPasswordLength' => 8,
209 'PasswordCannotMatchUsername' => 1,
211 'Missing items in p1'
215 'MinimalPasswordLength' => 8,
216 'PasswordCannotMatchUsername' => 1,
219 'MinimalPasswordLength' => 2,
222 'MinimalPasswordLength' => 8,
223 'PasswordCannotMatchUsername' => 1,
225 'Missing items in p2'