2016-02-01 20:44:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
2024-02-16 16:49:05 +00:00
|
|
|
namespace MediaWiki\Tests\Session;
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2024-02-16 21:57:59 +00:00
|
|
|
use InvalidArgumentException;
|
2023-09-20 07:54:42 +00:00
|
|
|
use MediaWiki\Config\HashConfig;
|
|
|
|
|
use MediaWiki\Config\MultiConfig;
|
2022-08-05 19:33:43 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2024-02-16 21:57:59 +00:00
|
|
|
use MediaWiki\Request\FauxRequest;
|
2024-02-16 16:49:05 +00:00
|
|
|
use MediaWiki\Session\BotPasswordSessionProvider;
|
|
|
|
|
use MediaWiki\Session\Session;
|
|
|
|
|
use MediaWiki\Session\SessionInfo;
|
|
|
|
|
use MediaWiki\Session\SessionManager;
|
|
|
|
|
use MediaWiki\Session\UserInfo;
|
2023-09-19 16:42:44 +00:00
|
|
|
use MediaWiki\User\BotPassword;
|
2020-06-30 15:09:24 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2020-01-10 00:00:51 +00:00
|
|
|
use Psr\Log\LogLevel;
|
2024-02-16 21:57:59 +00:00
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
|
use TestLogger;
|
2017-04-19 19:37:35 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Session
|
|
|
|
|
* @group Database
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \MediaWiki\Session\BotPasswordSessionProvider
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class BotPasswordSessionProviderTest extends MediaWikiIntegrationTestCase {
|
2021-05-26 15:50:28 +00:00
|
|
|
use SessionProviderTestTrait;
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
private $config;
|
2022-09-23 12:10:19 +00:00
|
|
|
private $configHash;
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2022-09-23 12:10:19 +00:00
|
|
|
private function getProvider( $name = null, $prefix = null, $isApiRequest = true ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
global $wgSessionProviders;
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$params = [
|
2016-02-01 20:44:03 +00:00
|
|
|
'priority' => 40,
|
|
|
|
|
'sessionCookieName' => $name,
|
2016-02-17 09:09:32 +00:00
|
|
|
'sessionCookieOptions' => [],
|
2022-09-23 12:10:19 +00:00
|
|
|
'isApiRequest' => $isApiRequest,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
if ( $prefix !== null ) {
|
|
|
|
|
$params['sessionCookieOptions']['prefix'] = $prefix;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 12:10:19 +00:00
|
|
|
$configHash = json_encode( [ $name, $prefix, $isApiRequest ] );
|
|
|
|
|
if ( !$this->config || $this->configHash !== $configHash ) {
|
2023-09-20 07:54:42 +00:00
|
|
|
$this->config = new HashConfig( [
|
2023-06-19 19:21:47 +00:00
|
|
|
MainConfigNames::CookiePrefix => 'wgCookiePrefix',
|
|
|
|
|
MainConfigNames::EnableBotPasswords => true,
|
|
|
|
|
MainConfigNames::SessionProviders => $wgSessionProviders + [
|
2016-03-28 18:53:04 +00:00
|
|
|
BotPasswordSessionProvider::class => [
|
|
|
|
|
'class' => BotPasswordSessionProvider::class,
|
2016-02-17 09:09:32 +00:00
|
|
|
'args' => [ $params ],
|
2022-04-12 16:17:54 +00:00
|
|
|
'services' => [ 'GrantsInfo' ],
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
] );
|
2022-09-23 12:10:19 +00:00
|
|
|
$this->configHash = $configHash;
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$manager = new SessionManager( [
|
2022-05-24 20:00:50 +00:00
|
|
|
'config' => new MultiConfig( [ $this->config, $this->getServiceContainer()->getMainConfig() ] ),
|
2024-02-16 21:57:59 +00:00
|
|
|
'logger' => new NullLogger,
|
2016-02-01 20:44:03 +00:00
|
|
|
'store' => new TestBagOStuff,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2016-03-28 18:53:04 +00:00
|
|
|
return $manager->getProvider( BotPasswordSessionProvider::class );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function setUp(): void {
|
2016-02-01 20:44:03 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2022-08-05 19:33:43 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
|
MainConfigNames::EnableBotPasswords => true,
|
|
|
|
|
MainConfigNames::CentralIdLookupProvider => 'local',
|
|
|
|
|
MainConfigNames::GrantPermissions => [
|
2016-02-17 09:09:32 +00:00
|
|
|
'test' => [ 'read' => true ],
|
|
|
|
|
],
|
|
|
|
|
] );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 17:26:25 +00:00
|
|
|
public function addDBDataOnce() {
|
2022-01-12 20:13:39 +00:00
|
|
|
$passwordFactory = $this->getServiceContainer()->getPasswordFactory();
|
2016-05-12 10:40:41 +00:00
|
|
|
$passwordHash = $passwordFactory->newFromPlaintext( 'foobaz' );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
$sysop = static::getTestSysop()->getUser();
|
2021-06-25 15:36:19 +00:00
|
|
|
$userId = $this->getServiceContainer()
|
|
|
|
|
->getCentralIdLookupFactory()
|
|
|
|
|
->getLookup( 'local' )
|
|
|
|
|
->centralIdFromName( $sysop->getName() );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2023-09-25 18:49:16 +00:00
|
|
|
$dbw = $this->getDb();
|
2023-07-14 12:48:42 +00:00
|
|
|
$dbw->newDeleteQueryBuilder()
|
In query builders, use insertInto() and deleteFrom() instead of insert() and delete()
The design principle for SelectQueryBuilder was to make the chained
builder calls look as much like SQL as possible, so that developers
could leverage their knowledge of SQL to understand what the query
builder is doing.
That's why SelectQueryBuilder::select() takes a list of fields, and by
the same principle, it makes sense for UpdateQueryBuilder::update() to
take a table. However with "insert" and "delete", the SQL designers
chose to add prepositions "into" and "from", and I think it makes sense
to follow that here.
In terms of natural language, we update a table, but we don't delete a
table, or insert a table. We delete rows from a table, or insert rows
into a table. The table is not the object of the verb.
So, add insertInto() as an alias for insert(), and add deleteFrom() as
an alias for delete(). Use the new methods in MW core callers where
PHPStorm knows the type.
Change-Id: Idb327a54a57a0fb2288ea067472c1e9727016000
2023-09-08 00:06:59 +00:00
|
|
|
->deleteFrom( 'bot_passwords' )
|
2023-07-14 12:48:42 +00:00
|
|
|
->where( [ 'bp_user' => $userId, 'bp_app_id' => 'BotPasswordSessionProvider' ] )
|
|
|
|
|
->caller( __METHOD__ )->execute();
|
2024-04-14 19:33:50 +00:00
|
|
|
$dbw->newInsertQueryBuilder()
|
|
|
|
|
->insertInto( 'bot_passwords' )
|
|
|
|
|
->row( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'bp_user' => $userId,
|
|
|
|
|
'bp_app_id' => 'BotPasswordSessionProvider',
|
2016-05-12 10:40:41 +00:00
|
|
|
'bp_password' => $passwordHash->toString(),
|
2016-02-01 20:44:03 +00:00
|
|
|
'bp_token' => 'token!',
|
|
|
|
|
'bp_restrictions' => '{"IPAddresses":["127.0.0.0/8"]}',
|
|
|
|
|
'bp_grants' => '["test"]',
|
2024-04-14 19:33:50 +00:00
|
|
|
] )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->execute();
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConstructor() {
|
2022-04-12 16:17:54 +00:00
|
|
|
$grantsInfo = $this->getServiceContainer()->getGrantsInfo();
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
try {
|
2022-04-12 16:17:54 +00:00
|
|
|
$provider = new BotPasswordSessionProvider( $grantsInfo );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->fail( 'Expected exception not thrown' );
|
2024-02-16 21:57:59 +00:00
|
|
|
} catch ( InvalidArgumentException $ex ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertSame(
|
|
|
|
|
'MediaWiki\\Session\\BotPasswordSessionProvider::__construct: priority must be specified',
|
|
|
|
|
$ex->getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2022-04-12 16:17:54 +00:00
|
|
|
$provider = new BotPasswordSessionProvider(
|
|
|
|
|
$grantsInfo,
|
|
|
|
|
[
|
|
|
|
|
'priority' => SessionInfo::MIN_PRIORITY - 1
|
|
|
|
|
]
|
|
|
|
|
);
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->fail( 'Expected exception not thrown' );
|
2024-02-16 21:57:59 +00:00
|
|
|
} catch ( InvalidArgumentException $ex ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertSame(
|
|
|
|
|
'MediaWiki\\Session\\BotPasswordSessionProvider::__construct: Invalid priority',
|
|
|
|
|
$ex->getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2022-04-12 16:17:54 +00:00
|
|
|
$provider = new BotPasswordSessionProvider(
|
|
|
|
|
$grantsInfo,
|
|
|
|
|
[
|
|
|
|
|
'priority' => SessionInfo::MAX_PRIORITY + 1
|
|
|
|
|
]
|
|
|
|
|
);
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->fail( 'Expected exception not thrown' );
|
2024-02-16 21:57:59 +00:00
|
|
|
} catch ( InvalidArgumentException $ex ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertSame(
|
|
|
|
|
'MediaWiki\\Session\\BotPasswordSessionProvider::__construct: Invalid priority',
|
|
|
|
|
$ex->getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-12 16:17:54 +00:00
|
|
|
$provider = new BotPasswordSessionProvider(
|
|
|
|
|
$grantsInfo,
|
|
|
|
|
[ 'priority' => 40 ]
|
|
|
|
|
);
|
2017-04-19 19:37:35 +00:00
|
|
|
$priv = TestingAccessWrapper::newFromObject( $provider );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertSame( 40, $priv->priority );
|
|
|
|
|
$this->assertSame( '_BPsession', $priv->sessionCookieName );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $priv->sessionCookieOptions );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2022-04-12 16:17:54 +00:00
|
|
|
$provider = new BotPasswordSessionProvider(
|
|
|
|
|
$grantsInfo,
|
|
|
|
|
[
|
|
|
|
|
'priority' => 40,
|
|
|
|
|
'sessionCookieName' => null,
|
|
|
|
|
]
|
|
|
|
|
);
|
2017-04-19 19:37:35 +00:00
|
|
|
$priv = TestingAccessWrapper::newFromObject( $provider );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertSame( '_BPsession', $priv->sessionCookieName );
|
|
|
|
|
|
2022-04-12 16:17:54 +00:00
|
|
|
$provider = new BotPasswordSessionProvider(
|
|
|
|
|
$grantsInfo,
|
|
|
|
|
[
|
|
|
|
|
'priority' => 40,
|
|
|
|
|
'sessionCookieName' => 'Foo',
|
|
|
|
|
'sessionCookieOptions' => [ 'Bar' ],
|
|
|
|
|
]
|
|
|
|
|
);
|
2017-04-19 19:37:35 +00:00
|
|
|
$priv = TestingAccessWrapper::newFromObject( $provider );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertSame( 'Foo', $priv->sessionCookieName );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [ 'Bar' ], $priv->sessionCookieOptions );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBasics() {
|
|
|
|
|
$provider = $this->getProvider();
|
|
|
|
|
|
2016-03-19 00:08:06 +00:00
|
|
|
$this->assertTrue( $provider->persistsSessionId() );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertFalse( $provider->canChangeUser() );
|
|
|
|
|
|
|
|
|
|
$this->assertNull( $provider->newSessionInfo() );
|
|
|
|
|
$this->assertNull( $provider->newSessionInfo( 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testProvideSessionInfo() {
|
2024-02-16 21:57:59 +00:00
|
|
|
$request = new FauxRequest;
|
2016-02-01 20:44:03 +00:00
|
|
|
$request->setCookie( '_BPsession', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'wgCookiePrefix' );
|
|
|
|
|
|
2022-09-23 12:10:19 +00:00
|
|
|
$provider = $this->getProvider( null, null, false );
|
|
|
|
|
$this->assertNull( $provider->provideSessionInfo( $request ) );
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider();
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$info = $provider->provideSessionInfo( $request );
|
2016-03-28 18:53:04 +00:00
|
|
|
$this->assertInstanceOf( SessionInfo::class, $info );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertSame( 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', $info->getId() );
|
|
|
|
|
|
2024-08-06 13:04:05 +00:00
|
|
|
$this->config->set( MainConfigNames::EnableBotPasswords, false );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertNull( $provider->provideSessionInfo( $request ) );
|
2024-08-06 13:04:05 +00:00
|
|
|
$this->config->set( MainConfigNames::EnableBotPasswords, true );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2024-02-16 21:57:59 +00:00
|
|
|
$this->assertNull( $provider->provideSessionInfo( new FauxRequest ) );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNewSessionInfoForRequest() {
|
|
|
|
|
$provider = $this->getProvider();
|
2016-05-18 09:19:20 +00:00
|
|
|
$user = static::getTestSysop()->getUser();
|
2024-02-16 21:57:59 +00:00
|
|
|
$request = $this->getMockBuilder( FauxRequest::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getIP' ] )->getMock();
|
2021-04-22 08:28:11 +00:00
|
|
|
$request->method( 'getIP' )
|
|
|
|
|
->willReturn( '127.0.0.1' );
|
2023-09-19 16:42:44 +00:00
|
|
|
$bp = BotPassword::newFromUser( $user, 'BotPasswordSessionProvider' );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$session = $provider->newSessionForRequest( $user, $bp, $request );
|
2016-03-28 18:53:04 +00:00
|
|
|
$this->assertInstanceOf( Session::class, $session );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( $session->getId(), $request->getSession()->getId() );
|
|
|
|
|
$this->assertEquals( $user->getName(), $session->getUser()->getName() );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [
|
2016-02-01 20:44:03 +00:00
|
|
|
'centralId' => $bp->getUserCentralId(),
|
|
|
|
|
'appId' => $bp->getAppId(),
|
|
|
|
|
'token' => $bp->getToken(),
|
2016-02-17 09:09:32 +00:00
|
|
|
'rights' => [ 'read' ],
|
2023-11-01 17:23:46 +00:00
|
|
|
'restrictions' => $bp->getRestrictions()->toJson(),
|
2016-02-17 09:09:32 +00:00
|
|
|
], $session->getProviderMetadata() );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [ 'read' ], $session->getAllowedUserRights() );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCheckSessionInfo() {
|
2024-02-16 21:57:59 +00:00
|
|
|
$logger = new TestLogger( true );
|
2016-02-01 20:44:03 +00:00
|
|
|
$provider = $this->getProvider();
|
2021-05-26 15:50:28 +00:00
|
|
|
$this->initProvider( $provider, $logger );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
$user = static::getTestSysop()->getUser();
|
2024-02-16 21:57:59 +00:00
|
|
|
$request = $this->getMockBuilder( FauxRequest::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getIP' ] )->getMock();
|
2021-04-22 08:28:11 +00:00
|
|
|
$request->method( 'getIP' )
|
|
|
|
|
->willReturn( '127.0.0.1' );
|
2023-09-19 16:42:44 +00:00
|
|
|
$bp = BotPassword::newFromUser( $user, 'BotPasswordSessionProvider' );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$data = [
|
2016-02-01 20:44:03 +00:00
|
|
|
'provider' => $provider,
|
|
|
|
|
'id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
|
|
|
|
|
'userInfo' => UserInfo::newFromUser( $user, true ),
|
|
|
|
|
'persisted' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
'metadata' => [
|
2016-02-01 20:44:03 +00:00
|
|
|
'centralId' => $bp->getUserCentralId(),
|
|
|
|
|
'appId' => $bp->getAppId(),
|
|
|
|
|
'token' => $bp->getToken(),
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
$dataMD = $data['metadata'];
|
|
|
|
|
|
2023-07-08 19:54:11 +00:00
|
|
|
foreach ( $data['metadata'] as $key => $_ ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
$data['metadata'] = $dataMD;
|
|
|
|
|
unset( $data['metadata'][$key] );
|
|
|
|
|
$info = new SessionInfo( SessionInfo::MIN_PRIORITY, $data );
|
|
|
|
|
$metadata = $info->getProviderMetadata();
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $provider->refreshSessionInfo( $info, $request, $metadata ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [
|
|
|
|
|
[ LogLevel::INFO, 'Session "{session}": Missing metadata: {missing}' ]
|
|
|
|
|
], $logger->getBuffer() );
|
2016-02-01 20:44:03 +00:00
|
|
|
$logger->clearBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data['metadata'] = $dataMD;
|
|
|
|
|
$data['metadata']['appId'] = 'Foobar';
|
|
|
|
|
$info = new SessionInfo( SessionInfo::MIN_PRIORITY, $data );
|
|
|
|
|
$metadata = $info->getProviderMetadata();
|
|
|
|
|
$this->assertFalse( $provider->refreshSessionInfo( $info, $request, $metadata ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [
|
|
|
|
|
[ LogLevel::INFO, 'Session "{session}": No BotPassword for {centralId} {appId}' ],
|
|
|
|
|
], $logger->getBuffer() );
|
2016-02-01 20:44:03 +00:00
|
|
|
$logger->clearBuffer();
|
|
|
|
|
|
|
|
|
|
$data['metadata'] = $dataMD;
|
|
|
|
|
$data['metadata']['token'] = 'Foobar';
|
|
|
|
|
$info = new SessionInfo( SessionInfo::MIN_PRIORITY, $data );
|
|
|
|
|
$metadata = $info->getProviderMetadata();
|
|
|
|
|
$this->assertFalse( $provider->refreshSessionInfo( $info, $request, $metadata ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [
|
|
|
|
|
[ LogLevel::INFO, 'Session "{session}": BotPassword token check failed' ],
|
|
|
|
|
], $logger->getBuffer() );
|
2016-02-01 20:44:03 +00:00
|
|
|
$logger->clearBuffer();
|
|
|
|
|
|
2024-02-16 21:57:59 +00:00
|
|
|
$request2 = $this->getMockBuilder( FauxRequest::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getIP' ] )->getMock();
|
2021-04-22 08:28:11 +00:00
|
|
|
$request2->method( 'getIP' )
|
|
|
|
|
->willReturn( '10.0.0.1' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$data['metadata'] = $dataMD;
|
|
|
|
|
$info = new SessionInfo( SessionInfo::MIN_PRIORITY, $data );
|
|
|
|
|
$metadata = $info->getProviderMetadata();
|
|
|
|
|
$this->assertFalse( $provider->refreshSessionInfo( $info, $request2, $metadata ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [
|
|
|
|
|
[ LogLevel::INFO, 'Session "{session}": Restrictions check failed' ],
|
|
|
|
|
], $logger->getBuffer() );
|
2016-02-01 20:44:03 +00:00
|
|
|
$logger->clearBuffer();
|
|
|
|
|
|
|
|
|
|
$info = new SessionInfo( SessionInfo::MIN_PRIORITY, $data );
|
|
|
|
|
$metadata = $info->getProviderMetadata();
|
|
|
|
|
$this->assertTrue( $provider->refreshSessionInfo( $info, $request, $metadata ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $logger->getBuffer() );
|
|
|
|
|
$this->assertEquals( $dataMD + [ 'rights' => [ 'read' ] ], $metadata );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
2016-02-26 20:02:56 +00:00
|
|
|
|
|
|
|
|
public function testGetAllowedUserRights() {
|
2024-02-16 21:57:59 +00:00
|
|
|
$logger = new TestLogger( true );
|
2016-02-26 20:02:56 +00:00
|
|
|
$provider = $this->getProvider();
|
2021-05-26 15:50:28 +00:00
|
|
|
$this->initProvider( $provider, $logger );
|
2016-02-26 20:02:56 +00:00
|
|
|
|
|
|
|
|
$backend = TestUtils::getDummySessionBackend();
|
2017-04-19 19:37:35 +00:00
|
|
|
$backendPriv = TestingAccessWrapper::newFromObject( $backend );
|
2016-02-26 20:02:56 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$provider->getAllowedUserRights( $backend );
|
|
|
|
|
$this->fail( 'Expected exception not thrown' );
|
2024-02-16 21:57:59 +00:00
|
|
|
} catch ( InvalidArgumentException $ex ) {
|
2016-02-26 20:02:56 +00:00
|
|
|
$this->assertSame( 'Backend\'s provider isn\'t $this', $ex->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$backendPriv->provider = $provider;
|
|
|
|
|
$backendPriv->providerMetadata = [ 'rights' => [ 'foo', 'bar', 'baz' ] ];
|
|
|
|
|
$this->assertSame( [ 'foo', 'bar', 'baz' ], $provider->getAllowedUserRights( $backend ) );
|
|
|
|
|
$this->assertSame( [], $logger->getBuffer() );
|
|
|
|
|
|
|
|
|
|
$backendPriv->providerMetadata = [ 'foo' => 'bar' ];
|
|
|
|
|
$this->assertSame( [], $provider->getAllowedUserRights( $backend ) );
|
|
|
|
|
$this->assertSame( [
|
|
|
|
|
[
|
|
|
|
|
LogLevel::DEBUG,
|
|
|
|
|
'MediaWiki\\Session\\BotPasswordSessionProvider::getAllowedUserRights: ' .
|
|
|
|
|
'No provider metadata, returning no rights allowed'
|
|
|
|
|
]
|
|
|
|
|
], $logger->getBuffer() );
|
|
|
|
|
$logger->clearBuffer();
|
|
|
|
|
|
|
|
|
|
$backendPriv->providerMetadata = [ 'rights' => 'bar' ];
|
|
|
|
|
$this->assertSame( [], $provider->getAllowedUserRights( $backend ) );
|
|
|
|
|
$this->assertSame( [
|
|
|
|
|
[
|
|
|
|
|
LogLevel::DEBUG,
|
|
|
|
|
'MediaWiki\\Session\\BotPasswordSessionProvider::getAllowedUserRights: ' .
|
|
|
|
|
'No provider metadata, returning no rights allowed'
|
|
|
|
|
]
|
|
|
|
|
], $logger->getBuffer() );
|
|
|
|
|
$logger->clearBuffer();
|
|
|
|
|
|
|
|
|
|
$backendPriv->providerMetadata = null;
|
|
|
|
|
$this->assertSame( [], $provider->getAllowedUserRights( $backend ) );
|
|
|
|
|
$this->assertSame( [
|
|
|
|
|
[
|
|
|
|
|
LogLevel::DEBUG,
|
|
|
|
|
'MediaWiki\\Session\\BotPasswordSessionProvider::getAllowedUserRights: ' .
|
|
|
|
|
'No provider metadata, returning no rights allowed'
|
|
|
|
|
]
|
|
|
|
|
], $logger->getBuffer() );
|
|
|
|
|
$logger->clearBuffer();
|
|
|
|
|
}
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|