3 namespace MediaWiki\Auth
;
5 use MediaWiki\MediaWikiServices
;
6 use Wikimedia\TestingAccessWrapper
;
10 * @covers MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProvider
12 class AbstractPasswordPrimaryAuthenticationProviderTest
extends \MediaWikiTestCase
{
13 public function testConstructor() {
14 $provider = $this->getMockForAbstractClass(
15 AbstractPasswordPrimaryAuthenticationProvider
::class
17 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
18 $this->assertTrue( $providerPriv->authoritative
);
20 $provider = $this->getMockForAbstractClass(
21 AbstractPasswordPrimaryAuthenticationProvider
::class,
22 [ [ 'authoritative' => false ] ]
24 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
25 $this->assertFalse( $providerPriv->authoritative
);
28 public function testGetPasswordFactory() {
29 $provider = $this->getMockForAbstractClass(
30 AbstractPasswordPrimaryAuthenticationProvider
::class
32 $provider->setConfig( MediaWikiServices
::getInstance()->getMainConfig() );
33 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
35 $obj = $providerPriv->getPasswordFactory();
36 $this->assertInstanceOf( 'PasswordFactory', $obj );
37 $this->assertSame( $obj, $providerPriv->getPasswordFactory() );
40 public function testGetPassword() {
41 $provider = $this->getMockForAbstractClass(
42 AbstractPasswordPrimaryAuthenticationProvider
::class
44 $provider->setConfig( MediaWikiServices
::getInstance()->getMainConfig() );
45 $provider->setLogger( new \Psr\Log\
NullLogger() );
46 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
48 $obj = $providerPriv->getPassword( null );
49 $this->assertInstanceOf( 'Password', $obj );
51 $obj = $providerPriv->getPassword( 'invalid' );
52 $this->assertInstanceOf( 'Password', $obj );
55 public function testGetNewPasswordExpiry() {
56 $config = new \HashConfig
;
57 $provider = $this->getMockForAbstractClass(
58 AbstractPasswordPrimaryAuthenticationProvider
::class
60 $provider->setConfig( new \
MultiConfig( [
62 MediaWikiServices
::getInstance()->getMainConfig()
64 $provider->setLogger( new \Psr\Log\
NullLogger() );
65 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
67 $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'ResetPasswordExpiration' => [] ] );
69 $config->set( 'PasswordExpirationDays', 0 );
70 $this->assertNull( $providerPriv->getNewPasswordExpiry( 'UTSysop' ) );
72 $config->set( 'PasswordExpirationDays', 5 );
75 wfTimestamp( TS_UNIX
, $providerPriv->getNewPasswordExpiry( 'UTSysop' ) ),
80 $this->mergeMwGlobalArrayValue( 'wgHooks', [
81 'ResetPasswordExpiration' => [ function ( $user, &$expires ) {
82 $this->assertSame( 'UTSysop', $user->getName() );
83 $expires = '30001231235959';
86 $this->assertEquals( '30001231235959', $providerPriv->getNewPasswordExpiry( 'UTSysop' ) );
89 public function testCheckPasswordValidity() {
91 $uppStatus = \Status
::newGood();
92 $this->setMwGlobals( [
93 'wgPasswordPolicy' => [
100 'Check' => function () use ( &$uppCalled, &$uppStatus ) {
108 $provider = $this->getMockForAbstractClass(
109 AbstractPasswordPrimaryAuthenticationProvider
::class
111 $provider->setConfig( MediaWikiServices
::getInstance()->getMainConfig() );
112 $provider->setLogger( new \Psr\Log\
NullLogger() );
113 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
115 $this->assertEquals( $uppStatus, $providerPriv->checkPasswordValidity( 'foo', 'bar' ) );
117 $uppStatus->fatal( 'arbitrary-warning' );
118 $this->assertEquals( $uppStatus, $providerPriv->checkPasswordValidity( 'foo', 'bar' ) );
121 public function testSetPasswordResetFlag() {
122 $config = new \
HashConfig( [
123 'InvalidPasswordReset' => true,
126 $manager = new AuthManager(
128 MediaWikiServices
::getInstance()->getMainConfig()
131 $provider = $this->getMockForAbstractClass(
132 AbstractPasswordPrimaryAuthenticationProvider
::class
134 $provider->setConfig( $config );
135 $provider->setLogger( new \Psr\Log\
NullLogger() );
136 $provider->setManager( $manager );
137 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
139 $manager->removeAuthenticationSessionData( null );
140 $status = \Status
::newGood();
141 $providerPriv->setPasswordResetFlag( 'Foo', $status );
142 $this->assertNull( $manager->getAuthenticationSessionData( 'reset-pass' ) );
144 $manager->removeAuthenticationSessionData( null );
145 $status = \Status
::newGood();
146 $status->error( 'testing' );
147 $providerPriv->setPasswordResetFlag( 'Foo', $status );
148 $ret = $manager->getAuthenticationSessionData( 'reset-pass' );
149 $this->assertNotNull( $ret );
150 $this->assertSame( 'resetpass-validity-soft', $ret->msg
->getKey() );
151 $this->assertFalse( $ret->hard
);
153 $config->set( 'InvalidPasswordReset', false );
154 $manager->removeAuthenticationSessionData( null );
155 $providerPriv->setPasswordResetFlag( 'Foo', $status );
156 $ret = $manager->getAuthenticationSessionData( 'reset-pass' );
157 $this->assertNull( $ret );
160 public function testFailResponse() {
161 $provider = $this->getMockForAbstractClass(
162 AbstractPasswordPrimaryAuthenticationProvider
::class,
163 [ [ 'authoritative' => false ] ]
165 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
167 $req = new PasswordAuthenticationRequest
;
169 $ret = $providerPriv->failResponse( $req );
170 $this->assertSame( AuthenticationResponse
::ABSTAIN
, $ret->status
);
172 $provider = $this->getMockForAbstractClass(
173 AbstractPasswordPrimaryAuthenticationProvider
::class,
174 [ [ 'authoritative' => true ] ]
176 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
179 $ret = $providerPriv->failResponse( $req );
180 $this->assertSame( AuthenticationResponse
::FAIL
, $ret->status
);
181 $this->assertSame( 'wrongpasswordempty', $ret->message
->getKey() );
183 $req->password
= 'X';
184 $ret = $providerPriv->failResponse( $req );
185 $this->assertSame( AuthenticationResponse
::FAIL
, $ret->status
);
186 $this->assertSame( 'wrongpassword', $ret->message
->getKey() );
190 * @dataProvider provideGetAuthenticationRequests
191 * @param string $action
192 * @param array $response
194 public function testGetAuthenticationRequests( $action, $response ) {
195 $provider = $this->getMockForAbstractClass(
196 AbstractPasswordPrimaryAuthenticationProvider
::class
199 $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
202 public static function provideGetAuthenticationRequests() {
204 [ AuthManager
::ACTION_LOGIN
, [ new PasswordAuthenticationRequest() ] ],
205 [ AuthManager
::ACTION_CREATE
, [ new PasswordAuthenticationRequest() ] ],
206 [ AuthManager
::ACTION_LINK
, [] ],
207 [ AuthManager
::ACTION_CHANGE
, [ new PasswordAuthenticationRequest() ] ],
208 [ AuthManager
::ACTION_REMOVE
, [ new PasswordAuthenticationRequest() ] ],
212 public function testProviderRevokeAccessForUser() {
213 $req = new PasswordAuthenticationRequest
;
214 $req->action
= AuthManager
::ACTION_REMOVE
;
215 $req->username
= 'foo';
216 $req->password
= null;
218 $provider = $this->getMockForAbstractClass(
219 AbstractPasswordPrimaryAuthenticationProvider
::class
221 $provider->expects( $this->once() )
222 ->method( 'providerChangeAuthenticationData' )
223 ->with( $this->equalTo( $req ) );
225 $provider->providerRevokeAccessForUser( 'foo' );