2015-11-22 20:17:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Auth;
|
|
|
|
|
|
2021-04-16 13:17:10 +00:00
|
|
|
use HashConfig;
|
2019-05-13 14:18:07 +00:00
|
|
|
use MediaWiki\Block\DatabaseBlock;
|
2020-03-31 18:51:49 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-05-13 16:02:38 +00:00
|
|
|
use MediaWiki\Tests\Unit\Auth\AuthenticationProviderTestTrait;
|
2017-04-19 19:37:35 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
/**
|
|
|
|
|
* @group AuthManager
|
|
|
|
|
* @group Database
|
2018-11-01 11:48:52 +00:00
|
|
|
* @covers \MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProvider
|
2015-11-22 20:17:00 +00:00
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class CheckBlocksSecondaryAuthenticationProviderTest extends \MediaWikiIntegrationTestCase {
|
2021-05-13 16:02:38 +00:00
|
|
|
use AuthenticationProviderTestTrait;
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
public function testConstructor() {
|
|
|
|
|
$provider = new CheckBlocksSecondaryAuthenticationProvider();
|
2017-04-19 19:37:35 +00:00
|
|
|
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
|
2015-11-22 20:17:00 +00:00
|
|
|
$config = new \HashConfig( [
|
|
|
|
|
'BlockDisablesLogin' => false
|
|
|
|
|
] );
|
2021-05-13 16:02:38 +00:00
|
|
|
$this->initProvider( $provider, $config );
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->assertSame( false, $providerPriv->blockDisablesLogin );
|
|
|
|
|
|
|
|
|
|
$provider = new CheckBlocksSecondaryAuthenticationProvider(
|
|
|
|
|
[ 'blockDisablesLogin' => true ]
|
|
|
|
|
);
|
2017-04-19 19:37:35 +00:00
|
|
|
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
|
2015-11-22 20:17:00 +00:00
|
|
|
$config = new \HashConfig( [
|
|
|
|
|
'BlockDisablesLogin' => false
|
|
|
|
|
] );
|
2021-05-13 16:02:38 +00:00
|
|
|
$this->initProvider( $provider, $config );
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->assertSame( true, $providerPriv->blockDisablesLogin );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBasics() {
|
|
|
|
|
$provider = new CheckBlocksSecondaryAuthenticationProvider();
|
|
|
|
|
$user = \User::newFromName( 'UTSysop' );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newAbstain(),
|
|
|
|
|
$provider->beginSecondaryAccountCreation( $user, $user, [] )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideGetAuthenticationRequests
|
|
|
|
|
* @param string $action
|
|
|
|
|
* @param array $response
|
|
|
|
|
*/
|
|
|
|
|
public function testGetAuthenticationRequests( $action, $response ) {
|
|
|
|
|
$provider = new CheckBlocksSecondaryAuthenticationProvider();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideGetAuthenticationRequests() {
|
|
|
|
|
return [
|
|
|
|
|
[ AuthManager::ACTION_LOGIN, [] ],
|
|
|
|
|
[ AuthManager::ACTION_CREATE, [] ],
|
|
|
|
|
[ AuthManager::ACTION_LINK, [] ],
|
|
|
|
|
[ AuthManager::ACTION_CHANGE, [] ],
|
|
|
|
|
[ AuthManager::ACTION_REMOVE, [] ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getBlockedUser() {
|
|
|
|
|
$user = \User::newFromName( 'UTBlockee' );
|
2020-05-26 13:14:46 +00:00
|
|
|
if ( $user->getId() == 0 ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$user->addToDatabase();
|
|
|
|
|
\TestUser::setPasswordForUser( $user, 'UTBlockeePassword' );
|
|
|
|
|
$user->saveSettings();
|
|
|
|
|
}
|
2020-08-27 09:27:10 +00:00
|
|
|
$blockStore = MediaWikiServices::getInstance()->getDatabaseBlockStore();
|
2019-05-13 14:18:07 +00:00
|
|
|
$oldBlock = DatabaseBlock::newFromTarget( 'UTBlockee' );
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( $oldBlock ) {
|
|
|
|
|
// An old block will prevent our new one from saving.
|
2020-08-27 09:27:10 +00:00
|
|
|
$blockStore->deleteBlock( $oldBlock );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
$blockOptions = [
|
|
|
|
|
'address' => 'UTBlockee',
|
2020-05-26 13:14:46 +00:00
|
|
|
'user' => $user->getId(),
|
2021-06-02 09:44:38 +00:00
|
|
|
'by' => $this->getTestSysop()->getUser(),
|
2015-11-22 20:17:00 +00:00
|
|
|
'reason' => __METHOD__,
|
|
|
|
|
'expiry' => time() + 100500,
|
|
|
|
|
'createAccount' => true,
|
|
|
|
|
];
|
2019-05-13 14:18:07 +00:00
|
|
|
$block = new DatabaseBlock( $blockOptions );
|
2020-08-27 09:27:10 +00:00
|
|
|
$blockStore->insertBlock( $block );
|
2015-11-22 20:17:00 +00:00
|
|
|
return $user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBeginSecondaryAuthentication() {
|
|
|
|
|
$unblockedUser = \User::newFromName( 'UTSysop' );
|
|
|
|
|
$blockedUser = $this->getBlockedUser();
|
|
|
|
|
|
|
|
|
|
$provider = new CheckBlocksSecondaryAuthenticationProvider(
|
|
|
|
|
[ 'blockDisablesLogin' => false ]
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newAbstain(),
|
|
|
|
|
$provider->beginSecondaryAuthentication( $unblockedUser, [] )
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newAbstain(),
|
|
|
|
|
$provider->beginSecondaryAuthentication( $blockedUser, [] )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$provider = new CheckBlocksSecondaryAuthenticationProvider(
|
|
|
|
|
[ 'blockDisablesLogin' => true ]
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newPass(),
|
|
|
|
|
$provider->beginSecondaryAuthentication( $unblockedUser, [] )
|
|
|
|
|
);
|
|
|
|
|
$ret = $provider->beginSecondaryAuthentication( $blockedUser, [] );
|
|
|
|
|
$this->assertEquals( AuthenticationResponse::FAIL, $ret->status );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTestUserForCreation() {
|
|
|
|
|
$provider = new CheckBlocksSecondaryAuthenticationProvider(
|
|
|
|
|
[ 'blockDisablesLogin' => false ]
|
|
|
|
|
);
|
2021-04-16 13:17:10 +00:00
|
|
|
|
2021-05-13 16:02:38 +00:00
|
|
|
$this->initProvider( $provider, new HashConfig(), null, $this->getServiceContainer()->getAuthManager() );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
$unblockedUser = \User::newFromName( 'UTSysop' );
|
|
|
|
|
$blockedUser = $this->getBlockedUser();
|
|
|
|
|
|
|
|
|
|
$user = \User::newFromName( 'RandomUser' );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
\StatusValue::newGood(),
|
|
|
|
|
$provider->testUserForCreation( $unblockedUser, AuthManager::AUTOCREATE_SOURCE_SESSION )
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
\StatusValue::newGood(),
|
|
|
|
|
$provider->testUserForCreation( $unblockedUser, false )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$status = $provider->testUserForCreation( $blockedUser, AuthManager::AUTOCREATE_SOURCE_SESSION );
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( \StatusValue::class, $status );
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->assertFalse( $status->isOK() );
|
2019-10-17 14:54:01 +00:00
|
|
|
$this->assertTrue( $status->hasMessage( 'blockedtext' ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
$status = $provider->testUserForCreation( $blockedUser, false );
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( \StatusValue::class, $status );
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->assertFalse( $status->isOK() );
|
2019-10-17 14:54:01 +00:00
|
|
|
$this->assertTrue( $status->hasMessage( 'blockedtext' ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-17 14:54:01 +00:00
|
|
|
public function testPartialBlock() {
|
2015-11-22 20:17:00 +00:00
|
|
|
$blockOptions = [
|
|
|
|
|
'address' => '127.0.0.0/24',
|
|
|
|
|
'reason' => __METHOD__,
|
2021-06-02 09:44:38 +00:00
|
|
|
'by' => $this->getTestSysop()->getUser(),
|
2015-11-22 20:17:00 +00:00
|
|
|
'expiry' => time() + 100500,
|
|
|
|
|
'createAccount' => true,
|
2019-10-17 14:54:01 +00:00
|
|
|
'sitewide' => false,
|
2015-11-22 20:17:00 +00:00
|
|
|
];
|
2019-05-13 14:18:07 +00:00
|
|
|
$block = new DatabaseBlock( $blockOptions );
|
2020-08-27 09:27:10 +00:00
|
|
|
MediaWikiServices::getInstance()->getDatabaseBlockStore()->insertBlock( $block );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
$user = \User::newFromName( 'UTNormalUser' );
|
2020-05-26 13:14:46 +00:00
|
|
|
if ( $user->getId() == 0 ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$user->addToDatabase();
|
|
|
|
|
\TestUser::setPasswordForUser( $user, 'UTNormalUserPassword' );
|
|
|
|
|
$user->saveSettings();
|
|
|
|
|
}
|
2019-03-18 21:50:48 +00:00
|
|
|
\RequestContext::getMain()->setUser( $user );
|
2015-11-22 20:17:00 +00:00
|
|
|
$newuser = \User::newFromName( 'RandomUser' );
|
|
|
|
|
|
|
|
|
|
$provider = new CheckBlocksSecondaryAuthenticationProvider(
|
|
|
|
|
[ 'blockDisablesLogin' => true ]
|
|
|
|
|
);
|
2021-05-13 16:02:38 +00:00
|
|
|
$this->initProvider( $provider, new HashConfig(), null, $this->getServiceContainer()->getAuthManager() );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
$ret = $provider->beginSecondaryAuthentication( $user, [] );
|
|
|
|
|
$this->assertEquals( AuthenticationResponse::FAIL, $ret->status );
|
|
|
|
|
|
|
|
|
|
$status = $provider->testUserForCreation( $newuser, AuthManager::AUTOCREATE_SOURCE_SESSION );
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( \StatusValue::class, $status );
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->assertFalse( $status->isOK() );
|
2019-10-17 14:54:01 +00:00
|
|
|
$this->assertTrue( $status->hasMessage( 'blockedtext-partial' ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
$status = $provider->testUserForCreation( $newuser, false );
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( \StatusValue::class, $status );
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->assertFalse( $status->isOK() );
|
2019-10-17 14:54:01 +00:00
|
|
|
$this->assertTrue( $status->hasMessage( 'blockedtext-partial' ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
}
|