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