1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
|
<?php
use MediaWiki\Auth\AuthManager;
use MediaWiki\Auth\TemporaryPasswordAuthenticationRequest;
use MediaWiki\Block\CompositeBlock;
use MediaWiki\Block\DatabaseBlock;
use MediaWiki\Block\SystemBlock;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\MainConfigNames;
use MediaWiki\Request\WebRequest;
use MediaWiki\Status\Status;
use MediaWiki\Tests\Unit\DummyServicesTrait;
use MediaWiki\User\Options\StaticUserOptionsLookup;
use MediaWiki\User\Options\UserOptionsLookup;
use MediaWiki\User\PasswordReset;
use MediaWiki\User\User;
use MediaWiki\User\UserFactory;
use MediaWiki\User\UserIdentityLookup;
use MediaWiki\User\UserNameUtils;
use Psr\Log\NullLogger;
/**
* TODO make this a unit test, all dependencies are injected, but DatabaseBlock::__construct()
* can't be used in unit tests.
*
* @covers \MediaWiki\User\PasswordReset
* @group Database
*/
class PasswordResetTest extends MediaWikiIntegrationTestCase {
use DummyServicesTrait;
private const VALID_IP = '1.2.3.4';
private const VALID_EMAIL = 'foo@bar.baz';
/**
* @dataProvider provideIsAllowed
*/
public function testIsAllowed( $passwordResetRoutes, $enableEmail,
$allowsAuthenticationDataChange, $canEditPrivate, $block, $isAllowed
) {
$config = $this->makeConfig( $enableEmail, $passwordResetRoutes );
$authManager = $this->createMock( AuthManager::class );
$authManager->method( 'allowsAuthenticationDataChange' )
->willReturn( $allowsAuthenticationDataChange ? Status::newGood() : Status::newFatal( 'foo' ) );
$user = $this->createMock( User::class );
$user->method( 'getName' )->willReturn( 'Foo' );
$user->method( 'getBlock' )->willReturn( $block );
$user->method( 'isAllowed' )->with( 'editmyprivateinfo' )->willReturn( $canEditPrivate );
$passwordReset = new PasswordReset(
$config,
new NullLogger(),
$authManager,
$this->createHookContainer(),
$this->createNoOpMock( UserIdentityLookup::class ),
$this->createNoOpMock( UserFactory::class ),
$this->createNoOpMock( UserNameUtils::class ),
$this->createNoOpMock( UserOptionsLookup::class )
);
$this->assertSame( $isAllowed, $passwordReset->isAllowed( $user )->isGood() );
}
public static function provideIsAllowed() {
return [
'no routes' => [
'passwordResetRoutes' => [],
'enableEmail' => true,
'allowsAuthenticationDataChange' => true,
'canEditPrivate' => true,
'block' => null,
'isAllowed' => false,
],
'email disabled' => [
'passwordResetRoutes' => [ 'username' => true ],
'enableEmail' => false,
'allowsAuthenticationDataChange' => true,
'canEditPrivate' => true,
'block' => null,
'isAllowed' => false,
],
'auth data change disabled' => [
'passwordResetRoutes' => [ 'username' => true ],
'enableEmail' => true,
'allowsAuthenticationDataChange' => false,
'canEditPrivate' => true,
'block' => null,
'isAllowed' => false,
],
'cannot edit private data' => [
'passwordResetRoutes' => [ 'username' => true ],
'enableEmail' => true,
'allowsAuthenticationDataChange' => true,
'canEditPrivate' => false,
'block' => null,
'isAllowed' => false,
],
'blocked with account creation disabled' => [
'passwordResetRoutes' => [ 'username' => true ],
'enableEmail' => true,
'allowsAuthenticationDataChange' => true,
'canEditPrivate' => true,
'block' => new DatabaseBlock( [ 'createAccount' => true ] ),
'isAllowed' => false,
],
'blocked w/o account creation disabled' => [
'passwordResetRoutes' => [ 'username' => true ],
'enableEmail' => true,
'allowsAuthenticationDataChange' => true,
'canEditPrivate' => true,
'block' => new DatabaseBlock( [] ),
'isAllowed' => true,
],
'using blocked proxy' => [
'passwordResetRoutes' => [ 'username' => true ],
'enableEmail' => true,
'allowsAuthenticationDataChange' => true,
'canEditPrivate' => true,
'block' => new SystemBlock(
[ 'systemBlock' => 'proxy' ]
),
'isAllowed' => false,
],
'globally blocked with account creation not disabled' => [
'passwordResetRoutes' => [ 'username' => true ],
'enableEmail' => true,
'allowsAuthenticationDataChange' => true,
'canEditPrivate' => true,
'block' => null,
'isAllowed' => true,
],
'blocked via wgSoftBlockRanges' => [
'passwordResetRoutes' => [ 'username' => true ],
'enableEmail' => true,
'allowsAuthenticationDataChange' => true,
'canEditPrivate' => true,
'block' => new SystemBlock(
[ 'systemBlock' => 'wgSoftBlockRanges', 'anonOnly' => true ]
),
'isAllowed' => true,
],
'blocked with an unknown system block type' => [
'passwordResetRoutes' => [ 'username' => true ],
'enableEmail' => true,
'allowsAuthenticationDataChange' => true,
'canEditPrivate' => true,
'block' => new SystemBlock( [ 'systemBlock' => 'unknown' ] ),
'isAllowed' => false,
],
'blocked with multiple blocks, all allowing password reset' => [
'passwordResetRoutes' => [ 'username' => true ],
'enableEmail' => true,
'allowsAuthenticationDataChange' => true,
'canEditPrivate' => true,
'block' => new CompositeBlock( [
'originalBlocks' => [
new SystemBlock( [ 'systemBlock' => 'wgSoftBlockRanges', 'anonOnly' => true ] ),
new DatabaseBlock( [] ),
]
] ),
'isAllowed' => true,
],
'blocked with multiple blocks, not all allowing password reset' => [
'passwordResetRoutes' => [ 'username' => true ],
'enableEmail' => true,
'allowsAuthenticationDataChange' => true,
'canEditPrivate' => true,
'block' => new CompositeBlock( [
'originalBlocks' => [
new SystemBlock( [ 'systemBlock' => 'wgSoftBlockRanges', 'anonOnly' => true ] ),
new SystemBlock( [ 'systemBlock' => 'proxy' ] ),
]
] ),
'isAllowed' => false,
],
'all OK' => [
'passwordResetRoutes' => [ 'username' => true ],
'enableEmail' => true,
'allowsAuthenticationDataChange' => true,
'canEditPrivate' => true,
'block' => null,
'isAllowed' => true,
],
];
}
public function testExecute_notAllowed() {
$user = $this->createMock( User::class );
/** @var User $user */
$passwordReset = $this->getMockBuilder( PasswordReset::class )
->disableOriginalConstructor()
->onlyMethods( [ 'isAllowed' ] )
->getMock();
$passwordReset->method( 'isAllowed' )
->with( $user )
->willReturn( Status::newFatal( 'somestatuscode' ) );
/** @var PasswordReset $passwordReset */
$this->expectException( LogicException::class );
$passwordReset->execute( $user );
}
/**
* @dataProvider provideExecute
* @param string|bool $expectedError
* @param ServiceOptions $config
* @param User $performingUser
* @param AuthManager $authManager
* @param string|null $username
* @param string|null $email
* @param User[] $usersWithEmail
* @covers \MediaWiki\Deferred\SendPasswordResetEmailUpdate
*/
public function testExecute(
$expectedError,
ServiceOptions $config,
User $performingUser,
AuthManager $authManager,
$username = '',
$email = '',
array $usersWithEmail = []
) {
$users = $this->makeUsers();
// Only User1 has `requireemail` true, everything else false (so that is the default)
$userOptionsLookup = new StaticUserOptionsLookup(
[ 'User1' => [ 'requireemail' => true ] ],
[ 'requireemail' => false ]
);
// Similar to $lookupUser callback, but with null instead of false
$userFactory = $this->createMock( UserFactory::class );
$userFactory->method( 'newFromName' )
->willReturnCallback(
static function ( $username ) use ( $users ) {
return $users[ $username ] ?? null;
}
);
$userIdentityLookup = $this->createMock( UserIdentityLookup::class );
$userFactory->method( 'newFromUserIdentity' )
->willReturnArgument( 0 );
$lookupUser = static function ( $username ) use ( $users ) {
return $users[ $username ] ?? false;
};
$passwordReset = $this->getMockBuilder( PasswordReset::class )
->onlyMethods( [ 'getUsersByEmail', 'isAllowed' ] )
->setConstructorArgs( [
$config,
new NullLogger(),
$authManager,
$this->createHookContainer(),
$userIdentityLookup,
$userFactory,
$this->getDummyUserNameUtils(),
$userOptionsLookup
] )
->getMock();
$passwordReset->method( 'getUsersByEmail' )->with( $email )
->willReturn( array_map( $lookupUser, $usersWithEmail ) );
$passwordReset->method( 'isAllowed' )
->willReturn( Status::newGood() );
/** @var PasswordReset $passwordReset */
$status = $passwordReset->execute( $performingUser, $username, $email );
if ( is_string( $expectedError ) ) {
$this->assertStatusError( $expectedError, $status );
} elseif ( $expectedError ) {
$this->assertStatusNotOk( $status );
} else {
$this->assertStatusGood( $status );
}
}
public function provideExecute() {
// 'User1' has the 'requireemail' preference set (see testExecute()). Other users do not.
$defaultConfig = $this->makeConfig( true, [ 'username' => true, 'email' => true ] );
$performingUser = $this->makePerformingUser( self::VALID_IP, false );
$throttledUser = $this->makePerformingUser( self::VALID_IP, true );
return [
'Throttled, pretend everything is ok' => [
'expectedError' => false,
'config' => $defaultConfig,
'performingUser' => $throttledUser,
'authManager' => $this->makeAuthManager(),
'username' => 'User1',
'email' => '',
'usersWithEmail' => [],
],
'Throttled, email required for resets, is invalid, pretend everything is ok' => [
'expectedError' => false,
'config' => $defaultConfig,
'performingUser' => $throttledUser,
'authManager' => $this->makeAuthManager(),
'username' => 'User1',
'email' => '[invalid email]',
'usersWithEmail' => [],
],
'Invalid email' => [
'expectedError' => 'passwordreset-invalidemail',
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager(),
'username' => '',
'email' => '[invalid email]',
'usersWithEmail' => [],
],
'No username, no email' => [
'expectedError' => 'passwordreset-nodata',
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager(),
'username' => '',
'email' => '',
'usersWithEmail' => [],
],
'Email route not enabled' => [
'expectedError' => 'passwordreset-nodata',
'config' => $this->makeConfig( true, [ 'username' => true ] ),
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager(),
'username' => '',
'email' => self::VALID_EMAIL,
'usersWithEmail' => [],
],
'Username route not enabled' => [
'expectedError' => 'passwordreset-nodata',
'config' => $this->makeConfig( true, [ 'email' => true ] ),
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager(),
'username' => 'User1',
'email' => '',
'usersWithEmail' => [],
],
'No routes enabled' => [
'expectedError' => 'passwordreset-nodata',
'config' => $this->makeConfig( true, [] ),
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager(),
'username' => 'User1',
'email' => self::VALID_EMAIL,
'usersWithEmail' => [],
],
'Email required for resets but is empty, pretend everything is OK' => [
'expectedError' => false,
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager(),
'username' => 'User1',
'email' => '',
'usersWithEmail' => [],
],
'Email required for resets but is invalid' => [
'expectedError' => 'passwordreset-invalidemail',
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager(),
'username' => 'User1',
'email' => '[invalid email]',
'usersWithEmail' => [],
],
'Password email already sent within 24 hours, pretend everything is ok' => [
'expectedError' => false,
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager( [ 'User1' ], 0, [], [ 'User1' ] ),
'username' => 'User1',
'email' => '',
'usersWithEmail' => [ 'User1' ],
],
'No user by this username, pretend everything is OK' => [
'expectedError' => false,
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager(),
'username' => 'Nonexistent user',
'email' => '',
'usersWithEmail' => [],
],
'Username is not valid' => [
'expectedError' => 'noname',
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager(),
'username' => 'Invalid|username',
'email' => '',
'usersWithEmail' => [],
],
'If no users with this email found, pretend everything is OK' => [
'expectedError' => false,
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager(),
'username' => '',
'email' => 'some@not.found.email',
'usersWithEmail' => [],
],
'No email for the user, pretend everything is OK' => [
'expectedError' => false,
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager(),
'username' => 'BadUser',
'email' => '',
'usersWithEmail' => [],
],
'Email required for resets, no match' => [
'expectedError' => false,
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager(),
'username' => 'User1',
'email' => 'some@other.email',
'usersWithEmail' => [],
],
"Couldn't determine the performing user's IP" => [
'expectedError' => 'badipaddress',
'config' => $defaultConfig,
'performingUser' => $this->makePerformingUser( '', false ),
'authManager' => $this->makeAuthManager(),
'username' => 'User1',
'email' => '',
'usersWithEmail' => [],
],
'User is allowed, but ignored' => [
'expectedError' => 'passwordreset-ignored',
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager( [ 'User2' ], 0, [ 'User2' ] ),
'username' => 'User2',
'email' => '',
'usersWithEmail' => [],
],
'One of users is ignored' => [
'expectedError' => 'passwordreset-ignored',
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager( [ 'User1', 'User2' ], 0, [ 'User2' ] ),
'username' => '',
'email' => self::VALID_EMAIL,
'usersWithEmail' => [ 'User1', 'User2' ],
],
'User is rejected' => [
'expectedError' => 'rejected by test mock',
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager(),
'username' => 'User2',
'email' => '',
'usersWithEmail' => [],
],
'One of users is rejected' => [
'expectedError' => 'rejected by test mock',
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager( [ 'User1' ] ),
'username' => '',
'email' => self::VALID_EMAIL,
'usersWithEmail' => [ 'User1', 'User2' ],
],
'Reset one user via password' => [
'expectedError' => false,
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager( [ 'User1' ], 1 ),
'username' => 'User1',
'email' => self::VALID_EMAIL,
// Make sure that only the user specified by username is reset
'usersWithEmail' => [ 'User1', 'User2' ],
],
'Reset one user via email' => [
'expectedError' => false,
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager( [ 'User2' ], 1 ),
'username' => '',
'email' => self::VALID_EMAIL,
'usersWithEmail' => [ 'User2' ],
],
'Reset multiple users via email' => [
'expectedError' => false,
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager( [ 'User2', 'User3' ], 2 ),
'username' => '',
'email' => self::VALID_EMAIL,
'usersWithEmail' => [ 'User2', 'User3' ],
],
"Email is not required for resets, this user didn't opt in" => [
'expectedError' => false,
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager( [ 'User2' ], 1 ),
'username' => 'User2',
'email' => self::VALID_EMAIL,
'usersWithEmail' => [ 'User2' ],
],
'Reset three users via email that did not opt in, multiple users with same email' => [
'expectedError' => false,
'config' => $defaultConfig,
'performingUser' => $performingUser,
'authManager' => $this->makeAuthManager( [ 'User2', 'User3', 'User4' ], 3, [ 'User1' ] ),
'username' => '',
'email' => self::VALID_EMAIL,
'usersWithEmail' => [ 'User1', 'User2', 'User3', 'User4' ],
],
];
}
private function makeConfig( $enableEmail, array $passwordResetRoutes ) {
$hash = [
MainConfigNames::EnableEmail => $enableEmail,
MainConfigNames::PasswordResetRoutes => $passwordResetRoutes,
];
return new ServiceOptions( PasswordReset::CONSTRUCTOR_OPTIONS, $hash );
}
/**
* @param string $ip
* @param bool $pingLimited
* @return User
*/
private function makePerformingUser( string $ip, $pingLimited ): User {
$request = $this->createMock( WebRequest::class );
$request->method( 'getIP' )
->willReturn( $ip );
/** @var WebRequest $request */
$user = $this->getMockBuilder( User::class )
->onlyMethods( [ 'getName', 'pingLimiter', 'getRequest', 'isAllowed' ] )
->getMock();
$user->method( 'getName' )
->willReturn( 'SomeUser' );
$user->method( 'pingLimiter' )
->with( 'mailpassword' )
->willReturn( $pingLimited );
$user->method( 'getRequest' )
->willReturn( $request );
// Always has the relevant rights, just checking based on rate limits
$user->method( 'isAllowed' )->with( 'editmyprivateinfo' )->willReturn( true );
/** @var User $user */
return $user;
}
/**
* @param string[] $allowed Usernames that are allowed to send password reset email
* by AuthManager's allowsAuthenticationDataChange method.
* @param int $numUsersToAuth Number of users that will receive email
* @param string[] $ignored Usernames that are allowed but ignored by AuthManager's
* allowsAuthenticationDataChange method and will not receive password reset email.
* @param string[] $mailThrottledLimited Usernames that have already
* received the password reset email within a given time, and AuthManager
* changeAuthenticationData method will mark them as 'throttled-mailpassword.'
* @return AuthManager
*/
private function makeAuthManager(
array $allowed = [],
$numUsersToAuth = 0,
array $ignored = [],
array $mailThrottledLimited = []
): AuthManager {
$authManager = $this->createMock( AuthManager::class );
$authManager->method( 'allowsAuthenticationDataChange' )
->willReturnCallback(
static function ( TemporaryPasswordAuthenticationRequest $req )
use ( $allowed, $ignored, $mailThrottledLimited ) {
if ( in_array( $req->username, $mailThrottledLimited, true ) ) {
return Status::newGood( 'throttled-mailpassword' );
}
$value = in_array( $req->username, $ignored, true )
? 'ignored'
: 'okie dokie';
return in_array( $req->username, $allowed, true )
? Status::newGood( $value )
: Status::newFatal( 'rejected by test mock' );
} );
// changeAuthenticationData is executed in the deferred update class
// SendPasswordResetEmailUpdate
$authManager->expects( $this->exactly( $numUsersToAuth ) )
->method( 'changeAuthenticationData' );
/** @var AuthManager $authManager */
return $authManager;
}
/**
* @return User[]
*/
private function makeUsers() {
$getGoodUserCb = function ( int $num ) {
$user = $this->createMock( User::class );
$user->method( 'getName' )->willReturn( "User$num" );
$user->method( 'getId' )->willReturn( $num );
$user->method( 'isRegistered' )->willReturn( true );
$user->method( 'getEmail' )->willReturn( self::VALID_EMAIL );
return $user;
};
$user1 = $getGoodUserCb( 1 );
$user2 = $getGoodUserCb( 2 );
$user3 = $getGoodUserCb( 3 );
$user4 = $getGoodUserCb( 4 );
$badUser = $this->createMock( User::class );
$badUser->method( 'getName' )->willReturn( 'BadUser' );
$badUser->method( 'getId' )->willReturn( 5 );
$badUser->method( 'isRegistered' )->willReturn( true );
$badUser->method( 'getEmail' )->willReturn( '' );
$nonexistUser = $this->createMock( User::class );
$nonexistUser->method( 'getName' )->willReturn( 'Nonexistent user' );
$nonexistUser->method( 'getId' )->willReturn( 0 );
$nonexistUser->method( 'isRegistered' )->willReturn( false );
$nonexistUser->method( 'getEmail' )->willReturn( '' );
return [
'User1' => $user1,
'User2' => $user2,
'User3' => $user3,
'User4' => $user4,
'BadUser' => $badUser,
'Nonexistent user' => $nonexistUser,
];
}
}
|