5 use Wikimedia\ScopedCallback;
6 use Wikimedia\TestingAccessWrapper;
24 'wgEnableBotPasswords' =>
true,
25 'wgBotPasswordsDatabase' =>
false,
26 'wgCentralIdLookupProvider' =>
'BotPasswordTest OkMock',
27 'wgGrantPermissions' => [
28 'test' => [
'read' =>
true ],
30 'wgUserrightsInterwikiDelimiter' =>
'@',
34 $this->testUserName = $this->testUser->getUser()->getName();
37 $mock1->expects( $this->
any() )->method(
'isAttached' )
38 ->will( $this->returnValue(
true ) );
39 $mock1->expects( $this->
any() )->method(
'lookupUserNames' )
40 ->will( $this->returnValue( [ $this->testUserName => 42,
'UTDummy' => 43,
'UTInvalid' => 0 ] ) );
41 $mock1->expects( $this->never() )->method(
'lookupCentralIds' );
44 $mock2->expects( $this->
any() )->method(
'isAttached' )
45 ->will( $this->returnValue(
false ) );
46 $mock2->expects( $this->
any() )->method(
'lookupUserNames' )
47 ->will( $this->returnArgument( 0 ) );
48 $mock2->expects( $this->never() )->method(
'lookupCentralIds' );
51 'BotPasswordTest OkMock' => [
'factory' =>
function ()
use ( $mock1 ) {
54 'BotPasswordTest FailMock' => [
'factory' =>
function ()
use ( $mock2 ) {
63 $passwordFactory = MediaWikiServices::getInstance()->getPasswordFactory();
64 $passwordHash = $passwordFactory->newFromPlaintext(
'foobaz' );
69 [
'bp_user' => [ 42, 43 ],
'bp_app_id' =>
'BotPassword' ],
77 'bp_app_id' =>
'BotPassword',
78 'bp_password' => $passwordHash->toString(),
79 'bp_token' =>
'token!',
80 'bp_restrictions' =>
'{"IPAddresses":["127.0.0.0/8"]}',
81 'bp_grants' =>
'["test"]',
85 'bp_app_id' =>
'BotPassword',
86 'bp_password' => $passwordHash->toString(),
87 'bp_token' =>
'token!',
88 'bp_restrictions' =>
'{"IPAddresses":["127.0.0.0/8"]}',
89 'bp_grants' =>
'["test"]',
97 $user = $this->testUser->getUser();
100 $this->assertTrue( $bp->isSaved() );
101 $this->assertSame( 42, $bp->getUserCentralId() );
102 $this->assertSame(
'BotPassword', $bp->getAppId() );
103 $this->assertSame(
'token!', trim( $bp->getToken(),
" \0" ) );
104 $this->assertEquals(
'{"IPAddresses":["127.0.0.0/8"]}', $bp->getRestrictions()->toJson() );
105 $this->assertSame( [
'test' ], $bp->getGrants() );
110 'wgCentralIdLookupProvider' =>
'BotPasswordTest FailMock'
116 'wgUserrightsInterwikiDelimiter' =>
'#',
122 $user = $this->testUser->getUser();
125 'appId' =>
'DoesNotExist'
128 $this->assertFalse( $bp->isSaved() );
129 $this->assertSame( 42, $bp->getUserCentralId() );
130 $this->assertSame(
'DoesNotExist', $bp->getAppId() );
132 $this->assertSame( [], $bp->getGrants() );
135 'username' =>
'UTDummy',
136 'appId' =>
'DoesNotExist2',
138 'grants' => [
'test' ],
141 $this->assertFalse( $bp->isSaved() );
142 $this->assertSame( 43, $bp->getUserCentralId() );
143 $this->assertSame(
'DoesNotExist2', $bp->getAppId() );
144 $this->assertEquals(
'{"IPAddresses":["127.0.0.0/8"]}', $bp->getRestrictions()->toJson() );
145 $this->assertSame( [
'test' ], $bp->getGrants() );
147 $user = $this->testUser->getUser();
150 'appId' =>
'DoesNotExist'
153 $this->assertFalse( $bp->isSaved() );
154 $this->assertSame( 45, $bp->getUserCentralId() );
155 $this->assertSame(
'DoesNotExist', $bp->getAppId() );
157 $user = $this->testUser->getUser();
160 'appId' =>
'BotPassword'
163 $this->assertFalse( $bp->isSaved() );
174 'user' => $this->testUserName,
178 'username' =>
'UTInvalid',
189 $password = $bp->getPassword();
191 $this->assertTrue( $password->equals(
'foobaz' ) );
194 $password = $bp->getPassword();
201 [
'bp_password' =>
'garbage' ],
202 [
'bp_user' => 42,
'bp_app_id' =>
'BotPassword' ],
205 $password = $bp->getPassword();
238 if ( is_array( $expectedResult ) ) {
241 $this->assertSame( $expectedResult,
$result );
247 [
'user',
'pass',
false ],
248 [
'user',
'abc@def',
false ],
249 [
'legacy@user',
'pass',
false ],
250 [
'user@bot',
'12345678901234567890123456789012',
251 [
'user@bot',
'12345678901234567890123456789012' ] ],
252 [
'user',
'bot@12345678901234567890123456789012',
253 [
'user@bot',
'12345678901234567890123456789012' ] ],
254 [
'user',
'bot@12345678901234567890123456789012345',
255 [
'user@bot',
'12345678901234567890123456789012345' ] ],
256 [
'user',
'bot@x@12345678901234567890123456789012',
257 [
'user@bot@x',
'12345678901234567890123456789012' ] ],
270 'logger' =>
new Psr\Log\NullLogger,
280 ScopedCallback::consume( $reset );
285 'SessionProviders' => $mainConfig->get(
'SessionProviders' ) + [
288 'args' => [ [
'priority' => 40 ] ],
294 'logger' =>
new Psr\Log\NullLogger,
310 Status::newFatal(
'botpasswords-not-exist', $this->testUserName,
'DoesNotExist' ),
316 ->setMethods( [
'getIP' ] )
318 $request->expects( $this->
any() )->method(
'getIP' )
319 ->will( $this->returnValue(
'10.0.0.1' ) );
325 "{$this->testUserName}@BotPassword", $this->testUser->getPassword(),
new FauxRequest );
330 $this->assertNotInstanceOf(
332 $request->getSession()->getProvider(),
337 $this->assertTrue(
$status->isGood() );
338 $session =
$status->getValue();
340 $this->assertInstanceOf(
343 $this->assertSame( $session->getId(),
$request->getSession()->getId() );
345 ScopedCallback::consume( $reset );
353 $passwordFactory = MediaWikiServices::getInstance()->getPasswordFactory();
357 'appId' =>
'TestSave',
359 'grants' => [
'test' ],
361 $this->assertFalse( $bp->isSaved(),
'sanity check' );
366 $passwordHash = $password ? $passwordFactory->newFromPlaintext( $password ) :
null;
367 $this->assertFalse( $bp->save(
'update', $passwordHash ) );
368 $this->assertTrue( $bp->save(
'insert', $passwordHash ) );
371 $this->assertEquals( $bp->getUserCentralId(), $bp2->getUserCentralId() );
372 $this->assertEquals( $bp->getAppId(), $bp2->getAppId() );
373 $this->assertEquals( $bp->getToken(), $bp2->getToken() );
374 $this->assertEquals( $bp->getRestrictions(), $bp2->getRestrictions() );
375 $this->assertEquals( $bp->getGrants(), $bp2->getGrants() );
376 $pw = TestingAccessWrapper::newFromObject( $bp )->getPassword();
377 if ( $password ===
null ) {
380 $this->assertTrue( $pw->equals( $password ) );
383 $token = $bp->getToken();
384 $this->assertEquals( 42, $bp->getUserCentralId() );
385 $this->assertEquals(
'TestSave', $bp->getAppId() );
386 $this->assertFalse( $bp->save(
'insert' ) );
387 $this->assertTrue( $bp->save(
'update' ) );
388 $this->assertNotEquals( $token, $bp->getToken() );
391 $this->assertEquals( $bp->getToken(), $bp2->getToken() );
392 $pw = TestingAccessWrapper::newFromObject( $bp )->getPassword();
393 if ( $password ===
null ) {
396 $this->assertTrue( $pw->equals( $password ) );
399 $passwordHash = $passwordFactory->newFromPlaintext(
'XXX' );
400 $token = $bp->getToken();
401 $this->assertTrue( $bp->save(
'update', $passwordHash ) );
402 $this->assertNotEquals( $token, $bp->getToken() );
403 $pw = TestingAccessWrapper::newFromObject( $bp )->getPassword();
404 $this->assertTrue( $pw->equals(
'XXX' ) );
406 $this->assertTrue( $bp->delete() );
407 $this->assertFalse( $bp->isSaved() );
410 $this->assertFalse( $bp->save(
'foobar' ) );