| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Auth; |
| 4 |
|
| 5 |
use BadMethodCallException; |
| 6 |
use MediaWiki\Auth\AbstractPrimaryAuthenticationProvider; |
| 7 |
use MediaWiki\Auth\AuthenticationRequest; |
| 8 |
use MediaWiki\Auth\AuthenticationResponse; |
| 9 |
use MediaWiki\Auth\AuthManager; |
| 10 |
use MediaWiki\Auth\PrimaryAuthenticationProvider; |
| 11 |
use MediaWiki\Tests\Unit\Auth\AuthenticationProviderTestTrait; |
| 12 |
use MediaWiki\Tests\Unit\DummyServicesTrait; |
| 13 |
use MediaWiki\User\User; |
| 14 |
use MediaWikiIntegrationTestCase; |
| 15 |
use StatusValue; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class AbstractPrimaryAuthenticationProviderTest extends MediaWikiIntegrationTestCase { |
| 22 |
use DummyServicesTrait; |
| 23 |
use AuthenticationProviderTestTrait; |
| 24 |
|
| 25 |
public function testAbstractPrimaryAuthenticationProvider() { |
| 26 |
$user = $this->createMock( User::class ); |
| 27 |
|
| 28 |
$provider = $this->getMockForAbstractClass( AbstractPrimaryAuthenticationProvider::class ); |
| 29 |
|
| 30 |
try { |
| 31 |
$provider->continuePrimaryAuthentication( [] ); |
| 32 |
$this->fail( 'Expected exception not thrown' ); |
| 33 |
} catch ( BadMethodCallException $ex ) { |
| 34 |
} |
| 35 |
|
| 36 |
try { |
| 37 |
$provider->continuePrimaryAccountCreation( $user, $user, [] ); |
| 38 |
$this->fail( 'Expected exception not thrown' ); |
| 39 |
} catch ( BadMethodCallException $ex ) { |
| 40 |
} |
| 41 |
|
| 42 |
$this->assertTrue( $provider->providerAllowsPropertyChange( 'foo' ) ); |
| 43 |
$this->assertEquals( |
| 44 |
StatusValue::newGood(), |
| 45 |
$provider->testForAccountCreation( $user, $user, [] ) |
| 46 |
); |
| 47 |
$this->assertEquals( |
| 48 |
StatusValue::newGood(), |
| 49 |
$provider->testUserForCreation( $user, AuthManager::AUTOCREATE_SOURCE_SESSION ) |
| 50 |
); |
| 51 |
$this->assertEquals( |
| 52 |
StatusValue::newGood(), |
| 53 |
$provider->testUserForCreation( $user, false ) |
| 54 |
); |
| 55 |
|
| 56 |
$this->assertNull( |
| 57 |
$provider->finishAccountCreation( $user, $user, AuthenticationResponse::newPass() ) |
| 58 |
); |
| 59 |
$provider->autoCreatedAccount( $user, AuthManager::AUTOCREATE_SOURCE_SESSION ); |
| 60 |
|
| 61 |
$res = AuthenticationResponse::newPass(); |
| 62 |
$provider->postAuthentication( $user, $res ); |
| 63 |
$provider->postAccountCreation( $user, $user, $res ); |
| 64 |
$provider->postAccountLink( $user, $res ); |
| 65 |
|
| 66 |
$provider->expects( $this->once() ) |
| 67 |
->method( 'testUserExists' ) |
| 68 |
->with( 'foo' ) |
| 69 |
->willReturn( true ); |
| 70 |
$this->assertTrue( $provider->testUserCanAuthenticate( 'foo' ) ); |
| 71 |
} |
| 72 |
|
| 73 |
public function testProviderRevokeAccessForUser() { |
| 74 |
$reqs = []; |
| 75 |
for ( $i = 0; $i < 3; $i++ ) { |
| 76 |
$reqs[$i] = $this->createMock( AuthenticationRequest::class ); |
| 77 |
} |
| 78 |
$username = 'TestProviderRevokeAccessForUser'; |
| 79 |
|
| 80 |
$provider = $this->getMockForAbstractClass( AbstractPrimaryAuthenticationProvider::class ); |
| 81 |
$provider->expects( $this->once() )->method( 'getAuthenticationRequests' ) |
| 82 |
->with( |
| 83 |
$this->identicalTo( AuthManager::ACTION_REMOVE ), |
| 84 |
$this->identicalTo( [ 'username' => $username ] ) |
| 85 |
) |
| 86 |
->willReturn( $reqs ); |
| 87 |
$provider->expects( $this->exactly( 3 ) )->method( 'providerChangeAuthenticationData' ) |
| 88 |
->willReturnCallback( function ( $req ) use ( $username ) { |
| 89 |
$this->assertSame( $username, $req->username ); |
| 90 |
} ); |
| 91 |
|
| 92 |
$provider->providerRevokeAccessForUser( $username ); |
| 93 |
|
| 94 |
foreach ( $reqs as $i => $req ) { |
| 95 |
$this->assertNotNull( $req->username, "#$i" ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function testPrimaryAccountLink( $type, $msg ) { |
| 105 |
$provider = $this->getMockForAbstractClass( AbstractPrimaryAuthenticationProvider::class ); |
| 106 |
$provider->method( 'accountCreationType' ) |
| 107 |
->willReturn( $type ); |
| 108 |
|
| 109 |
$class = AbstractPrimaryAuthenticationProvider::class; |
| 110 |
$msg1 = "{$class}::beginPrimaryAccountLink $msg"; |
| 111 |
$msg2 = "{$class}::continuePrimaryAccountLink is not implemented."; |
| 112 |
|
| 113 |
$user = User::newFromName( 'Whatever' ); |
| 114 |
|
| 115 |
try { |
| 116 |
$provider->beginPrimaryAccountLink( $user, [] ); |
| 117 |
$this->fail( 'Expected exception not thrown' ); |
| 118 |
} catch ( BadMethodCallException $ex ) { |
| 119 |
$this->assertSame( $msg1, $ex->getMessage() ); |
| 120 |
} |
| 121 |
try { |
| 122 |
$provider->continuePrimaryAccountLink( $user, [] ); |
| 123 |
$this->fail( 'Expected exception not thrown' ); |
| 124 |
} catch ( BadMethodCallException $ex ) { |
| 125 |
$this->assertSame( $msg2, $ex->getMessage() ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
public static function providePrimaryAccountLink() { |
| 130 |
return [ |
| 131 |
[ |
| 132 |
PrimaryAuthenticationProvider::TYPE_NONE, |
| 133 |
'should not be called on a non-link provider.', |
| 134 |
], |
| 135 |
[ |
| 136 |
PrimaryAuthenticationProvider::TYPE_CREATE, |
| 137 |
'should not be called on a non-link provider.', |
| 138 |
], |
| 139 |
[ |
| 140 |
PrimaryAuthenticationProvider::TYPE_LINK, |
| 141 |
'is not implemented.', |
| 142 |
], |
| 143 |
]; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function testProviderNormalizeUsername( $name, $expect ) { |
| 150 |
|
| 151 |
$interwikiLookup = $this->getDummyInterwikiLookup( [ 'interwiki' ] ); |
| 152 |
$this->setService( 'InterwikiLookup', $interwikiLookup ); |
| 153 |
|
| 154 |
$provider = $this->getMockForAbstractClass( AbstractPrimaryAuthenticationProvider::class ); |
| 155 |
$this->initProvider( $provider, null, null, null, null, $this->getServiceContainer()->getUserNameUtils() ); |
| 156 |
$this->assertSame( $expect, $provider->providerNormalizeUsername( $name ) ); |
| 157 |
} |
| 158 |
|
| 159 |
public static function provideProviderNormalizeUsername() { |
| 160 |
return [ |
| 161 |
'Leading space' => [ ' Leading space', 'Leading space' ], |
| 162 |
'Trailing space ' => [ 'Trailing space ', 'Trailing space' ], |
| 163 |
'Namespace prefix' => [ 'Talk:Username', null ], |
| 164 |
'Interwiki prefix' => [ 'interwiki:Username', null ], |
| 165 |
'With hash' => [ 'name with # hash', null ], |
| 166 |
'Multi spaces' => [ 'Multi spaces', 'Multi spaces' ], |
| 167 |
'Lowercase' => [ 'lowercase', 'Lowercase' ], |
| 168 |
'Invalid character' => [ 'in[]valid', null ], |
| 169 |
'With slash' => [ 'with / slash', null ], |
| 170 |
'Underscores' => [ '___under__scores___', 'Under scores' ], |
| 171 |
]; |
| 172 |
} |
| 173 |
|
| 174 |
} |