3 use MediaWiki\Session\SessionManager
;
9 class BotPasswordTest
extends MediaWikiTestCase
{
10 protected function setUp() {
13 $this->setMwGlobals( [
14 'wgEnableBotPasswords' => true,
15 'wgBotPasswordsDatabase' => false,
16 'wgCentralIdLookupProvider' => 'BotPasswordTest OkMock',
17 'wgGrantPermissions' => [
18 'test' => [ 'read' => true ],
20 'wgUserrightsInterwikiDelimiter' => '@',
23 $mock1 = $this->getMockForAbstractClass( 'CentralIdLookup' );
24 $mock1->expects( $this->any() )->method( 'isAttached' )
25 ->will( $this->returnValue( true ) );
26 $mock1->expects( $this->any() )->method( 'lookupUserNames' )
27 ->will( $this->returnValue( [ 'UTSysop' => 42, 'UTDummy' => 43, 'UTInvalid' => 0 ] ) );
28 $mock1->expects( $this->never() )->method( 'lookupCentralIds' );
30 $mock2 = $this->getMockForAbstractClass( 'CentralIdLookup' );
31 $mock2->expects( $this->any() )->method( 'isAttached' )
32 ->will( $this->returnValue( false ) );
33 $mock2->expects( $this->any() )->method( 'lookupUserNames' )
34 ->will( $this->returnArgument( 0 ) );
35 $mock2->expects( $this->never() )->method( 'lookupCentralIds' );
37 $this->mergeMwGlobalArrayValue( 'wgCentralIdLookupProviders', [
38 'BotPasswordTest OkMock' => [ 'factory' => function () use ( $mock1 ) {
41 'BotPasswordTest FailMock' => [ 'factory' => function () use ( $mock2 ) {
46 CentralIdLookup
::resetCache();
49 public function addDBData() {
50 $passwordFactory = new \
PasswordFactory();
51 $passwordFactory->init( \RequestContext
::getMain()->getConfig() );
52 $passwordHash = $passwordFactory->newFromPlaintext( 'foobaz' );
54 $dbw = wfGetDB( DB_MASTER
);
57 [ 'bp_user' => [ 42, 43 ], 'bp_app_id' => 'BotPassword' ],
65 'bp_app_id' => 'BotPassword',
66 'bp_password' => $passwordHash->toString(),
67 'bp_token' => 'token!',
68 'bp_restrictions' => '{"IPAddresses":["127.0.0.0/8"]}',
69 'bp_grants' => '["test"]',
73 'bp_app_id' => 'BotPassword',
74 'bp_password' => $passwordHash->toString(),
75 'bp_token' => 'token!',
76 'bp_restrictions' => '{"IPAddresses":["127.0.0.0/8"]}',
77 'bp_grants' => '["test"]',
84 public function testBasics() {
85 $user = User
::newFromName( 'UTSysop' );
86 $bp = BotPassword
::newFromUser( $user, 'BotPassword' );
87 $this->assertInstanceOf( 'BotPassword', $bp );
88 $this->assertTrue( $bp->isSaved() );
89 $this->assertSame( 42, $bp->getUserCentralId() );
90 $this->assertSame( 'BotPassword', $bp->getAppId() );
91 $this->assertSame( 'token!', trim( $bp->getToken(), " \0" ) );
92 $this->assertEquals( '{"IPAddresses":["127.0.0.0/8"]}', $bp->getRestrictions()->toJson() );
93 $this->assertSame( [ 'test' ], $bp->getGrants() );
95 $this->assertNull( BotPassword
::newFromUser( $user, 'DoesNotExist' ) );
97 $this->setMwGlobals( [
98 'wgCentralIdLookupProvider' => 'BotPasswordTest FailMock'
100 $this->assertNull( BotPassword
::newFromUser( $user, 'BotPassword' ) );
102 $this->assertSame( '@', BotPassword
::getSeparator() );
103 $this->setMwGlobals( [
104 'wgUserrightsInterwikiDelimiter' => '#',
106 $this->assertSame( '#', BotPassword
::getSeparator() );
109 public function testUnsaved() {
110 $user = User
::newFromName( 'UTSysop' );
111 $bp = BotPassword
::newUnsaved( [
113 'appId' => 'DoesNotExist'
115 $this->assertInstanceOf( 'BotPassword', $bp );
116 $this->assertFalse( $bp->isSaved() );
117 $this->assertSame( 42, $bp->getUserCentralId() );
118 $this->assertSame( 'DoesNotExist', $bp->getAppId() );
119 $this->assertEquals( MWRestrictions
::newDefault(), $bp->getRestrictions() );
120 $this->assertSame( [], $bp->getGrants() );
122 $bp = BotPassword
::newUnsaved( [
123 'username' => 'UTDummy',
124 'appId' => 'DoesNotExist2',
125 'restrictions' => MWRestrictions
::newFromJson( '{"IPAddresses":["127.0.0.0/8"]}' ),
126 'grants' => [ 'test' ],
128 $this->assertInstanceOf( 'BotPassword', $bp );
129 $this->assertFalse( $bp->isSaved() );
130 $this->assertSame( 43, $bp->getUserCentralId() );
131 $this->assertSame( 'DoesNotExist2', $bp->getAppId() );
132 $this->assertEquals( '{"IPAddresses":["127.0.0.0/8"]}', $bp->getRestrictions()->toJson() );
133 $this->assertSame( [ 'test' ], $bp->getGrants() );
135 $user = User
::newFromName( 'UTSysop' );
136 $bp = BotPassword
::newUnsaved( [
138 'appId' => 'DoesNotExist'
140 $this->assertInstanceOf( 'BotPassword', $bp );
141 $this->assertFalse( $bp->isSaved() );
142 $this->assertSame( 45, $bp->getUserCentralId() );
143 $this->assertSame( 'DoesNotExist', $bp->getAppId() );
145 $user = User
::newFromName( 'UTSysop' );
146 $bp = BotPassword
::newUnsaved( [
148 'appId' => 'BotPassword'
150 $this->assertInstanceOf( 'BotPassword', $bp );
151 $this->assertFalse( $bp->isSaved() );
153 $this->assertNull( BotPassword
::newUnsaved( [
157 $this->assertNull( BotPassword
::newUnsaved( [
159 'appId' => str_repeat( 'X', BotPassword
::APPID_MAXLENGTH +
1 ),
161 $this->assertNull( BotPassword
::newUnsaved( [
165 $this->assertNull( BotPassword
::newUnsaved( [
166 'username' => 'UTInvalid',
169 $this->assertNull( BotPassword
::newUnsaved( [
174 public function testGetPassword() {
175 $bp = TestingAccessWrapper
::newFromObject( BotPassword
::newFromCentralId( 42, 'BotPassword' ) );
177 $password = $bp->getPassword();
178 $this->assertInstanceOf( 'Password', $password );
179 $this->assertTrue( $password->equals( 'foobaz' ) );
182 $password = $bp->getPassword();
183 $this->assertInstanceOf( 'InvalidPassword', $password );
185 $bp = TestingAccessWrapper
::newFromObject( BotPassword
::newFromCentralId( 42, 'BotPassword' ) );
186 $dbw = wfGetDB( DB_MASTER
);
189 [ 'bp_password' => 'garbage' ],
190 [ 'bp_user' => 42, 'bp_app_id' => 'BotPassword' ],
193 $password = $bp->getPassword();
194 $this->assertInstanceOf( 'InvalidPassword', $password );
197 public function testInvalidateAllPasswordsForUser() {
198 $bp1 = TestingAccessWrapper
::newFromObject( BotPassword
::newFromCentralId( 42, 'BotPassword' ) );
199 $bp2 = TestingAccessWrapper
::newFromObject( BotPassword
::newFromCentralId( 43, 'BotPassword' ) );
201 $this->assertNotInstanceOf( 'InvalidPassword', $bp1->getPassword(), 'sanity check' );
202 $this->assertNotInstanceOf( 'InvalidPassword', $bp2->getPassword(), 'sanity check' );
203 BotPassword
::invalidateAllPasswordsForUser( 'UTSysop' );
204 $this->assertInstanceOf( 'InvalidPassword', $bp1->getPassword() );
205 $this->assertNotInstanceOf( 'InvalidPassword', $bp2->getPassword() );
207 $bp = TestingAccessWrapper
::newFromObject( BotPassword
::newFromCentralId( 42, 'BotPassword' ) );
208 $this->assertInstanceOf( 'InvalidPassword', $bp->getPassword() );
211 public function testRemoveAllPasswordsForUser() {
212 $this->assertNotNull( BotPassword
::newFromCentralId( 42, 'BotPassword' ), 'sanity check' );
213 $this->assertNotNull( BotPassword
::newFromCentralId( 43, 'BotPassword' ), 'sanity check' );
215 BotPassword
::removeAllPasswordsForUser( 'UTSysop' );
217 $this->assertNull( BotPassword
::newFromCentralId( 42, 'BotPassword' ) );
218 $this->assertNotNull( BotPassword
::newFromCentralId( 43, 'BotPassword' ) );
221 public function testLogin() {
222 // Test failure when bot passwords aren't enabled
223 $this->setMwGlobals( 'wgEnableBotPasswords', false );
224 $status = BotPassword
::login( 'UTSysop@BotPassword', 'foobaz', new FauxRequest
);
225 $this->assertEquals( Status
::newFatal( 'botpasswords-disabled' ), $status );
226 $this->setMwGlobals( 'wgEnableBotPasswords', true );
228 // Test failure when BotPasswordSessionProvider isn't configured
229 $manager = new SessionManager( [
230 'logger' => new Psr\Log\NullLogger
,
231 'store' => new EmptyBagOStuff
,
233 $reset = MediaWiki\Session\TestUtils
::setSessionManagerSingleton( $manager );
235 $manager->getProvider( MediaWiki\Session\BotPasswordSessionProvider
::class ),
238 $status = BotPassword
::login( 'UTSysop@BotPassword', 'foobaz', new FauxRequest
);
239 $this->assertEquals( Status
::newFatal( 'botpasswords-no-provider' ), $status );
240 ScopedCallback
::consume( $reset );
242 // Now configure BotPasswordSessionProvider for further tests...
243 $mainConfig = RequestContext
::getMain()->getConfig();
244 $config = new HashConfig( [
245 'SessionProviders' => $mainConfig->get( 'SessionProviders' ) +
[
246 MediaWiki\Session\BotPasswordSessionProvider
::class => [
247 'class' => MediaWiki\Session\BotPasswordSessionProvider
::class,
248 'args' => [ [ 'priority' => 40 ] ],
252 $manager = new SessionManager( [
253 'config' => new MultiConfig( [ $config, RequestContext
::getMain()->getConfig() ] ),
254 'logger' => new Psr\Log\NullLogger
,
255 'store' => new EmptyBagOStuff
,
257 $reset = MediaWiki\Session\TestUtils
::setSessionManagerSingleton( $manager );
259 // No "@"-thing in the username
260 $status = BotPassword
::login( 'UTSysop', 'foobaz', new FauxRequest
);
261 $this->assertEquals( Status
::newFatal( 'botpasswords-invalid-name', '@' ), $status );
264 $status = BotPassword
::login( 'UTDummy@BotPassword', 'foobaz', new FauxRequest
);
265 $this->assertEquals( Status
::newFatal( 'nosuchuser', 'UTDummy' ), $status );
268 $status = BotPassword
::login( 'UTSysop@DoesNotExist', 'foobaz', new FauxRequest
);
270 Status
::newFatal( 'botpasswords-not-exist', 'UTSysop', 'DoesNotExist' ),
274 // Failed restriction
275 $request = $this->getMock( 'FauxRequest', [ 'getIP' ] );
276 $request->expects( $this->any() )->method( 'getIP' )
277 ->will( $this->returnValue( '10.0.0.1' ) );
278 $status = BotPassword
::login( 'UTSysop@BotPassword', 'foobaz', $request );
279 $this->assertEquals( Status
::newFatal( 'botpasswords-restriction-failed' ), $status );
282 $status = BotPassword
::login( 'UTSysop@BotPassword', 'UTSysopPassword', new FauxRequest
);
283 $this->assertEquals( Status
::newFatal( 'wrongpassword' ), $status );
286 $request = new FauxRequest
;
287 $this->assertNotInstanceOf(
288 MediaWiki\Session\BotPasswordSessionProvider
::class,
289 $request->getSession()->getProvider(),
292 $status = BotPassword
::login( 'UTSysop@BotPassword', 'foobaz', $request );
293 $this->assertInstanceOf( 'Status', $status );
294 $this->assertTrue( $status->isGood() );
295 $session = $status->getValue();
296 $this->assertInstanceOf( MediaWiki\Session\Session
::class, $session );
297 $this->assertInstanceOf(
298 MediaWiki\Session\BotPasswordSessionProvider
::class, $session->getProvider()
300 $this->assertSame( $session->getId(), $request->getSession()->getId() );
302 ScopedCallback
::consume( $reset );
306 * @dataProvider provideSave
307 * @param string|null $password
309 public function testSave( $password ) {
310 $passwordFactory = new \
PasswordFactory();
311 $passwordFactory->init( \RequestContext
::getMain()->getConfig() );
313 $bp = BotPassword
::newUnsaved( [
315 'appId' => 'TestSave',
316 'restrictions' => MWRestrictions
::newFromJson( '{"IPAddresses":["127.0.0.0/8"]}' ),
317 'grants' => [ 'test' ],
319 $this->assertFalse( $bp->isSaved(), 'sanity check' );
321 BotPassword
::newFromCentralId( 42, 'TestSave', BotPassword
::READ_LATEST
), 'sanity check'
324 $passwordHash = $password ?
$passwordFactory->newFromPlaintext( $password ) : null;
325 $this->assertFalse( $bp->save( 'update', $passwordHash ) );
326 $this->assertTrue( $bp->save( 'insert', $passwordHash ) );
327 $bp2 = BotPassword
::newFromCentralId( 42, 'TestSave', BotPassword
::READ_LATEST
);
328 $this->assertInstanceOf( 'BotPassword', $bp2 );
329 $this->assertEquals( $bp->getUserCentralId(), $bp2->getUserCentralId() );
330 $this->assertEquals( $bp->getAppId(), $bp2->getAppId() );
331 $this->assertEquals( $bp->getToken(), $bp2->getToken() );
332 $this->assertEquals( $bp->getRestrictions(), $bp2->getRestrictions() );
333 $this->assertEquals( $bp->getGrants(), $bp2->getGrants() );
334 $pw = TestingAccessWrapper
::newFromObject( $bp )->getPassword();
335 if ( $password === null ) {
336 $this->assertInstanceOf( 'InvalidPassword', $pw );
338 $this->assertTrue( $pw->equals( $password ) );
341 $token = $bp->getToken();
342 $this->assertFalse( $bp->save( 'insert' ) );
343 $this->assertTrue( $bp->save( 'update' ) );
344 $this->assertNotEquals( $token, $bp->getToken() );
345 $bp2 = BotPassword
::newFromCentralId( 42, 'TestSave', BotPassword
::READ_LATEST
);
346 $this->assertInstanceOf( 'BotPassword', $bp2 );
347 $this->assertEquals( $bp->getToken(), $bp2->getToken() );
348 $pw = TestingAccessWrapper
::newFromObject( $bp )->getPassword();
349 if ( $password === null ) {
350 $this->assertInstanceOf( 'InvalidPassword', $pw );
352 $this->assertTrue( $pw->equals( $password ) );
355 $passwordHash = $passwordFactory->newFromPlaintext( 'XXX' );
356 $token = $bp->getToken();
357 $this->assertTrue( $bp->save( 'update', $passwordHash ) );
358 $this->assertNotEquals( $token, $bp->getToken() );
359 $pw = TestingAccessWrapper
::newFromObject( $bp )->getPassword();
360 $this->assertTrue( $pw->equals( 'XXX' ) );
362 $this->assertTrue( $bp->delete() );
363 $this->assertFalse( $bp->isSaved() );
364 $this->assertNull( BotPassword
::newFromCentralId( 42, 'TestSave', BotPassword
::READ_LATEST
) );
366 $this->assertFalse( $bp->save( 'foobar' ) );
369 public static function provideSave() {