| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Auth; |
| 4 |
|
| 5 |
use MediaWiki\Auth\AuthManager; |
| 6 |
use MediaWiki\Auth\TemporaryPasswordAuthenticationRequest; |
| 7 |
use MediaWiki\MainConfigNames; |
| 8 |
use MediaWiki\Message\Message; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class TemporaryPasswordAuthenticationRequestTest extends AuthenticationRequestTestCase { |
| 15 |
|
| 16 |
protected function getInstance( array $args = [] ) { |
| 17 |
$ret = new TemporaryPasswordAuthenticationRequest; |
| 18 |
$ret->action = $args[0]; |
| 19 |
return $ret; |
| 20 |
} |
| 21 |
|
| 22 |
public static function provideGetFieldInfo() { |
| 23 |
return [ |
| 24 |
[ [ AuthManager::ACTION_CREATE ] ], |
| 25 |
[ [ AuthManager::ACTION_CHANGE ] ], |
| 26 |
[ [ AuthManager::ACTION_REMOVE ] ], |
| 27 |
]; |
| 28 |
} |
| 29 |
|
| 30 |
public function testNewRandom() { |
| 31 |
global $wgPasswordPolicy; |
| 32 |
|
| 33 |
$policy = $wgPasswordPolicy; |
| 34 |
unset( $policy['policies'] ); |
| 35 |
$policy['policies']['default'] = [ |
| 36 |
'MinimalPasswordLength' => 1, |
| 37 |
'MinimumPasswordLengthToLogin' => 1, |
| 38 |
]; |
| 39 |
|
| 40 |
$this->overrideConfigValues( [ |
| 41 |
MainConfigNames::PasswordPolicy => $policy, |
| 42 |
] ); |
| 43 |
|
| 44 |
$ret1 = TemporaryPasswordAuthenticationRequest::newRandom(); |
| 45 |
$ret2 = TemporaryPasswordAuthenticationRequest::newRandom(); |
| 46 |
$this->assertEquals( 10, strlen( $ret1->password ) ); |
| 47 |
$this->assertEquals( 10, strlen( $ret2->password ) ); |
| 48 |
$this->assertNotSame( $ret1->password, $ret2->password ); |
| 49 |
|
| 50 |
$policy['policies']['default']['MinimalPasswordLength'] = 15; |
| 51 |
$this->overrideConfigValue( MainConfigNames::PasswordPolicy, $policy ); |
| 52 |
$ret = TemporaryPasswordAuthenticationRequest::newRandom(); |
| 53 |
$this->assertEquals( 15, strlen( $ret->password ) ); |
| 54 |
|
| 55 |
$policy['policies']['default']['MinimalPasswordLength'] = [ 'value' => 20 ]; |
| 56 |
$this->overrideConfigValue( MainConfigNames::PasswordPolicy, $policy ); |
| 57 |
$ret = TemporaryPasswordAuthenticationRequest::newRandom(); |
| 58 |
$this->assertEquals( 20, strlen( $ret->password ) ); |
| 59 |
} |
| 60 |
|
| 61 |
public function testNewInvalid() { |
| 62 |
$ret = TemporaryPasswordAuthenticationRequest::newInvalid(); |
| 63 |
$this->assertNull( $ret->password ); |
| 64 |
} |
| 65 |
|
| 66 |
public static function provideLoadFromSubmission() { |
| 67 |
return [ |
| 68 |
'Empty request' => [ |
| 69 |
[ AuthManager::ACTION_REMOVE ], |
| 70 |
[], |
| 71 |
false, |
| 72 |
], |
| 73 |
'Create, empty request' => [ |
| 74 |
[ AuthManager::ACTION_CREATE ], |
| 75 |
[], |
| 76 |
false, |
| 77 |
], |
| 78 |
'Create, mailpassword set' => [ |
| 79 |
[ AuthManager::ACTION_CREATE ], |
| 80 |
[ 'mailpassword' => 1 ], |
| 81 |
[ 'mailpassword' => true, 'action' => AuthManager::ACTION_CREATE ], |
| 82 |
], |
| 83 |
]; |
| 84 |
} |
| 85 |
|
| 86 |
public function testDescribeCredentials() { |
| 87 |
$username = 'TestDescribeCredentials'; |
| 88 |
$req = new TemporaryPasswordAuthenticationRequest; |
| 89 |
$req->action = AuthManager::ACTION_LOGIN; |
| 90 |
$req->username = $username; |
| 91 |
$ret = $req->describeCredentials(); |
| 92 |
$this->assertIsArray( $ret ); |
| 93 |
$this->assertArrayHasKey( 'provider', $ret ); |
| 94 |
$this->assertInstanceOf( Message::class, $ret['provider'] ); |
| 95 |
$this->assertSame( 'authmanager-provider-temporarypassword', $ret['provider']->getKey() ); |
| 96 |
$this->assertArrayHasKey( 'account', $ret ); |
| 97 |
$this->assertInstanceOf( Message::class, $ret['account'] ); |
| 98 |
$this->assertSame( [ $username ], $ret['account']->getParams() ); |
| 99 |
} |
| 100 |
} |