aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/auth/TemporaryPasswordAuthenticationRequestTest.php
blob: 6324b7ba762bf0bd59fb7fbfb53c69bd0258acc0 (plain) (blame)
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
<?php

namespace MediaWiki\Tests\Auth;

use MediaWiki\Auth\AuthManager;
use MediaWiki\Auth\TemporaryPasswordAuthenticationRequest;
use MediaWiki\MainConfigNames;
use MediaWiki\Message\Message;

/**
 * @group AuthManager
 * @covers \MediaWiki\Auth\TemporaryPasswordAuthenticationRequest
 */
class TemporaryPasswordAuthenticationRequestTest extends AuthenticationRequestTestCase {

	protected function getInstance( array $args = [] ) {
		$ret = new TemporaryPasswordAuthenticationRequest;
		$ret->action = $args[0];
		return $ret;
	}

	public static function provideGetFieldInfo() {
		return [
			[ [ AuthManager::ACTION_CREATE ] ],
			[ [ AuthManager::ACTION_CHANGE ] ],
			[ [ AuthManager::ACTION_REMOVE ] ],
		];
	}

	public function testNewRandom() {
		global $wgPasswordPolicy;

		$policy = $wgPasswordPolicy;
		unset( $policy['policies'] );
		$policy['policies']['default'] = [
			'MinimalPasswordLength' => 1,
			'MinimumPasswordLengthToLogin' => 1,
		];

		$this->overrideConfigValues( [
			MainConfigNames::PasswordPolicy => $policy,
		] );

		$ret1 = TemporaryPasswordAuthenticationRequest::newRandom();
		$ret2 = TemporaryPasswordAuthenticationRequest::newRandom();
		$this->assertEquals( 10, strlen( $ret1->password ) );
		$this->assertEquals( 10, strlen( $ret2->password ) );
		$this->assertNotSame( $ret1->password, $ret2->password );

		$policy['policies']['default']['MinimalPasswordLength'] = 15;
		$this->overrideConfigValue( MainConfigNames::PasswordPolicy, $policy );
		$ret = TemporaryPasswordAuthenticationRequest::newRandom();
		$this->assertEquals( 15, strlen( $ret->password ) );

		$policy['policies']['default']['MinimalPasswordLength'] = [ 'value' => 20 ];
		$this->overrideConfigValue( MainConfigNames::PasswordPolicy, $policy );
		$ret = TemporaryPasswordAuthenticationRequest::newRandom();
		$this->assertEquals( 20, strlen( $ret->password ) );
	}

	public function testNewInvalid() {
		$ret = TemporaryPasswordAuthenticationRequest::newInvalid();
		$this->assertNull( $ret->password );
	}

	public static function provideLoadFromSubmission() {
		return [
			'Empty request' => [
				[ AuthManager::ACTION_REMOVE ],
				[],
				false,
			],
			'Create, empty request' => [
				[ AuthManager::ACTION_CREATE ],
				[],
				false,
			],
			'Create, mailpassword set' => [
				[ AuthManager::ACTION_CREATE ],
				[ 'mailpassword' => 1 ],
				[ 'mailpassword' => true, 'action' => AuthManager::ACTION_CREATE ],
			],
		];
	}

	public function testDescribeCredentials() {
		$username = 'TestDescribeCredentials';
		$req = new TemporaryPasswordAuthenticationRequest;
		$req->action = AuthManager::ACTION_LOGIN;
		$req->username = $username;
		$ret = $req->describeCredentials();
		$this->assertIsArray( $ret );
		$this->assertArrayHasKey( 'provider', $ret );
		$this->assertInstanceOf( Message::class, $ret['provider'] );
		$this->assertSame( 'authmanager-provider-temporarypassword', $ret['provider']->getKey() );
		$this->assertArrayHasKey( 'account', $ret );
		$this->assertInstanceOf( Message::class, $ret['account'] );
		$this->assertSame( [ $username ], $ret['account']->getParams() );
	}
}