4 use Wikimedia\ScopedCallback;
5 use Wikimedia\TestingAccessWrapper;
23 'wgEnableBotPasswords' =>
true,
24 'wgBotPasswordsDatabase' =>
false,
25 'wgCentralIdLookupProvider' =>
'BotPasswordTest OkMock',
26 'wgGrantPermissions' => [
27 'test' => [
'read' =>
true ],
29 'wgUserrightsInterwikiDelimiter' =>
'@',
33 $this->testUserName = $this->testUser->getUser()->getName();
35 $mock1 = $this->getMockForAbstractClass(
'CentralIdLookup' );
36 $mock1->expects( $this->
any() )->method(
'isAttached' )
37 ->will( $this->returnValue(
true ) );
38 $mock1->expects( $this->
any() )->method(
'lookupUserNames' )
39 ->will( $this->returnValue( [ $this->testUserName => 42,
'UTDummy' => 43,
'UTInvalid' => 0 ] ) );
40 $mock1->expects( $this->never() )->method(
'lookupCentralIds' );
42 $mock2 = $this->getMockForAbstractClass(
'CentralIdLookup' );
43 $mock2->expects( $this->
any() )->method(
'isAttached' )
44 ->will( $this->returnValue(
false ) );
45 $mock2->expects( $this->
any() )->method(
'lookupUserNames' )
46 ->will( $this->returnArgument( 0 ) );
47 $mock2->expects( $this->never() )->method(
'lookupCentralIds' );
50 'BotPasswordTest OkMock' => [
'factory' =>
function ()
use ( $mock1 ) {
53 'BotPasswordTest FailMock' => [
'factory' =>
function ()
use ( $mock2 ) {
62 $passwordFactory = new \PasswordFactory();
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();
99 $this->assertInstanceOf(
'BotPassword', $bp );
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'
127 $this->assertInstanceOf(
'BotPassword', $bp );
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' ],
140 $this->assertInstanceOf(
'BotPassword', $bp );
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'
152 $this->assertInstanceOf(
'BotPassword', $bp );
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'
162 $this->assertInstanceOf(
'BotPassword', $bp );
163 $this->assertFalse( $bp->isSaved() );
174 'user' => $this->testUserName,
178 'username' =>
'UTInvalid',
189 $password = $bp->getPassword();
190 $this->assertInstanceOf(
'Password', $password );
191 $this->assertTrue( $password->equals(
'foobaz' ) );
194 $password = $bp->getPassword();
195 $this->assertInstanceOf(
'InvalidPassword', $password );
201 [
'bp_password' =>
'garbage' ],
202 [
'bp_user' => 42,
'bp_app_id' =>
'BotPassword' ],
205 $password = $bp->getPassword();
206 $this->assertInstanceOf(
'InvalidPassword', $password );
213 $this->assertNotInstanceOf(
'InvalidPassword', $bp1->getPassword(),
'sanity check' );
214 $this->assertNotInstanceOf(
'InvalidPassword', $bp2->getPassword(),
'sanity check' );
216 $this->assertInstanceOf(
'InvalidPassword', $bp1->getPassword() );
217 $this->assertNotInstanceOf(
'InvalidPassword', $bp2->getPassword() );
220 $this->assertInstanceOf(
'InvalidPassword', $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',
true ] ],
252 [
'user',
'bot@12345678901234567890123456789012',
253 [
'user@bot',
'12345678901234567890123456789012',
true ] ],
254 [
'user',
'bot@12345678901234567890123456789012345',
255 [
'user@bot',
'12345678901234567890123456789012345',
true ] ],
256 [
'user',
'bot@x@12345678901234567890123456789012',
257 [
'user@bot@x',
'12345678901234567890123456789012',
true ] ],
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' ),
315 $request = $this->getMockBuilder(
'FauxRequest' )
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(),
336 $this->assertInstanceOf(
'Status',
$status );
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 = new \PasswordFactory();
358 'appId' =>
'TestSave',
360 'grants' => [
'test' ],
362 $this->assertFalse( $bp->isSaved(),
'sanity check' );
367 $passwordHash = $password ? $passwordFactory->newFromPlaintext( $password ) :
null;
368 $this->assertFalse( $bp->save(
'update', $passwordHash ) );
369 $this->assertTrue( $bp->save(
'insert', $passwordHash ) );
371 $this->assertInstanceOf(
'BotPassword', $bp2 );
372 $this->assertEquals( $bp->getUserCentralId(), $bp2->getUserCentralId() );
373 $this->assertEquals( $bp->getAppId(), $bp2->getAppId() );
374 $this->assertEquals( $bp->getToken(), $bp2->getToken() );
375 $this->assertEquals( $bp->getRestrictions(), $bp2->getRestrictions() );
376 $this->assertEquals( $bp->getGrants(), $bp2->getGrants() );
377 $pw = TestingAccessWrapper::newFromObject( $bp )->getPassword();
378 if ( $password ===
null ) {
379 $this->assertInstanceOf(
'InvalidPassword', $pw );
381 $this->assertTrue( $pw->equals( $password ) );
384 $token = $bp->getToken();
385 $this->assertEquals( 42, $bp->getUserCentralId() );
386 $this->assertEquals(
'TestSave', $bp->getAppId() );
387 $this->assertFalse( $bp->save(
'insert' ) );
388 $this->assertTrue( $bp->save(
'update' ) );
389 $this->assertNotEquals( $token, $bp->getToken() );
391 $this->assertInstanceOf(
'BotPassword', $bp2 );
392 $this->assertEquals( $bp->getToken(), $bp2->getToken() );
393 $pw = TestingAccessWrapper::newFromObject( $bp )->getPassword();
394 if ( $password ===
null ) {
395 $this->assertInstanceOf(
'InvalidPassword', $pw );
397 $this->assertTrue( $pw->equals( $password ) );
400 $passwordHash = $passwordFactory->newFromPlaintext(
'XXX' );
401 $token = $bp->getToken();
402 $this->assertTrue( $bp->save(
'update', $passwordHash ) );
403 $this->assertNotEquals( $token, $bp->getToken() );
404 $pw = TestingAccessWrapper::newFromObject( $bp )->getPassword();
405 $this->assertTrue( $pw->equals(
'XXX' ) );
407 $this->assertTrue( $bp->delete() );
408 $this->assertFalse( $bp->isSaved() );
411 $this->assertFalse( $bp->save(
'foobar' ) );