Debian Codegraph @

tests/phpunit/unit/includes/password/PasswordPolicyChecksTest.php

raw symbols SCIP
1 <?php
2
3 /**
4 * Testing password-policy check functions
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
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 * Split from \PasswordPolicyChecksTest integration tests
33 *
34 * @coversDefaultClass \MediaWiki\Password\PasswordPolicyChecks
35 */
36 class PasswordPolicyChecksTest extends MediaWikiUnitTestCase {
37
38 /**
39 * @param string $name
40 * @return User|MockObject
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 * @covers ::checkMinimalPasswordLength
50 */
51 public function testCheckMinimalPasswordLength() {
52 $statusOK = PasswordPolicyChecks::checkMinimalPasswordLength(
53 3, // policy value
54 $this->getUser(), // User
55 'password' // password
56 );
57 $this->assertStatusGood( $statusOK, 'Password is longer than minimal policy' );
58 $statusShort = PasswordPolicyChecks::checkMinimalPasswordLength(
59 10, // policy value
60 $this->getUser(), // User
61 'password' // password
62 );
63 $this->assertStatusWarning( 'passwordtooshort', $statusShort,
64 'Password is shorter than minimal policy, not fatal'
65 );
66 }
67
68 /**
69 * @covers ::checkMinimumPasswordLengthToLogin
70 */
71 public function testCheckMinimumPasswordLengthToLogin() {
72 $statusOK = PasswordPolicyChecks::checkMinimumPasswordLengthToLogin(
73 3, // policy value
74 $this->getUser(), // User
75 'password' // password
76 );
77 $this->assertStatusGood( $statusOK, 'Password is longer than minimal policy' );
78 $statusShort = PasswordPolicyChecks::checkMinimumPasswordLengthToLogin(
79 10, // policy value
80 $this->getUser(), // User
81 'password' // password
82 );
83 $this->assertStatusError( 'passwordtooshort', $statusShort,
84 'Password is shorter than minimum login policy, fatal'
85 );
86 }
87
88 /**
89 * @covers ::checkMaximalPasswordLength
90 */
91 public function testCheckMaximalPasswordLength() {
92 $statusOK = PasswordPolicyChecks::checkMaximalPasswordLength(
93 100, // policy value
94 $this->getUser(), // User
95 'password' // password
96 );
97 $this->assertStatusGood( $statusOK, 'Password is shorter than maximal policy' );
98 $statusLong = PasswordPolicyChecks::checkMaximalPasswordLength(
99 4, // policy value
100 $this->getUser(), // User
101 'password' // password
102 );
103 $this->assertStatusError( 'passwordtoolong', $statusLong,
104 'Password is longer than maximal policy, fatal'
105 );
106 }
107
108 /**
109 * @covers ::checkPasswordCannotBeSubstringInUsername
110 */
111 public function testCheckPasswordCannotBeSubstringInUsername() {
112 $statusOK = PasswordPolicyChecks::checkPasswordCannotBeSubstringInUsername(
113 1, // policy value
114 $this->getUser(), // User
115 'password' // password
116 );
117 $this->assertStatusGood( $statusOK, 'Password is not a substring of username' );
118 $statusLong = PasswordPolicyChecks::checkPasswordCannotBeSubstringInUsername(
119 1, // policy value
120 $this->getUser( '123user123' ), // User
121 'user' // password
122 );
123 $this->assertStatusWarning( 'password-substring-username-match', $statusLong,
124 'Password is a substring of username, not fatal' );
125 }
126
127 /**
128 * @covers ::checkPasswordCannotMatchDefaults
129 * @dataProvider provideCheckPasswordCannotMatchDefaults
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 * @covers ::checkPasswordNotInCommonList
163 * @dataProvider provideCommonList
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 }