File: ResetAuthenticationThrottleTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (115 lines) | stat: -rw-r--r-- 4,747 bytes parent folder | download
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
<?php

namespace MediaWiki\Tests\Maintenance;

use MediaWiki\Context\RequestContext;
use MediaWiki\MainConfigNames;
use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
use ResetAuthenticationThrottle;

/**
 * @covers \ResetAuthenticationThrottle
 * @group Database
 * @author Dreamy Jazz
 */
class ResetAuthenticationThrottleTest extends MaintenanceBaseTestCase {

	use TempUserTestTrait;

	public function getMaintenanceClass() {
		return ResetAuthenticationThrottle::class;
	}

	/** @dataProvider provideExecuteForFatalError */
	public function testExecuteForFatalError( $options, $expectedOutputRegex ) {
		foreach ( $options as $name => $value ) {
			$this->maintenance->setOption( $name, $value );
		}
		$this->expectCallToFatalError();
		$this->expectOutputRegex( $expectedOutputRegex );
		$this->maintenance->execute();
	}

	public static function provideExecuteForFatalError() {
		return [
			'No options' => [
				[], '/At least one of --login, --signup, --tempaccount, or --tempaccountnameacquisition is required/',
			],
			'--signup but no IP' => [ [ 'signup' => 1 ], '/--ip is required/' ],
			'Invalid --ip argument' => [ [ 'signup' => 1, 'ip' => 'abcef' ], '/Not a valid IP/' ],
			'Invalid --user argument' => [
				[ 'login' => 1, 'user' => 'Template:Testing#test', 'ip' => '1.2.3.4' ], '/Not a valid username/',
			],
		];
	}

	public function testClearTempAccountNameAcquisitionThrottle() {
		$this->enableAutoCreateTempUser();
		$this->overrideConfigValue( MainConfigNames::TempAccountNameAcquisitionThrottle, [
			'count' => 1,
			'seconds' => 86400,
		] );
		$request = RequestContext::getMain()->getRequest();
		$request->setIP( '1.2.3.4' );
		$temporaryAccountCreator = $this->getServiceContainer()->getTempUserCreator();
		// Acquire a temporary account name to increase the throttle counter
		$this->assertNotNull( $temporaryAccountCreator->acquireAndStashName( $request->getSession() ) );
		// Verify that a second call does not get a name
		$request->getSession()->clear();
		$this->assertNull( $temporaryAccountCreator->acquireAndStashName( $request->getSession() ) );
		// Run the maintenance script to clear the throttle
		$this->maintenance->setOption( 'tempaccountnameacquisition', 1 );
		$this->maintenance->setOption( 'ip', '1.2.3.4' );
		$this->maintenance->execute();
		$this->expectOutputRegex( '/Clearing temp account name acquisition throttle.*done/' );
		// Verify that a third call works now
		$this->assertNotNull( $temporaryAccountCreator->acquireAndStashName( $request->getSession() ) );
	}

	public function testClearTempAccountNameAcquisitionThrottleOnNoThrottle() {
		// Disable the temporary account name acquisition throttle
		$this->overrideConfigValue( MainConfigNames::TempAccountNameAcquisitionThrottle, [] );
		// Run the maintenance script
		$this->maintenance->setOption( 'tempaccountnameacquisition', 1 );
		$this->maintenance->setOption( 'ip', '1.2.3.4' );
		$this->maintenance->execute();
		// Verify that the script finds no throttle set
		$this->expectOutputRegex( '/Clearing temp account name acquisition throttle.*none set/' );
	}

	public function testClearTempAccountCreationThrottle() {
		$this->enableAutoCreateTempUser();
		$this->overrideConfigValue( MainConfigNames::TempAccountCreationThrottle, [
			'count' => 1,
			'seconds' => 86400,
		] );
		$request = RequestContext::getMain()->getRequest();
		$request->setIP( '1.2.3.4' );
		$temporaryAccountCreator = $this->getServiceContainer()->getTempUserCreator();
		// Acquire a temporary account to increase the throttle counter
		$this->assertStatusGood( $temporaryAccountCreator->create( null, $request ) );
		// Verify that a second call does not get a name
		$request->getSession()->clear();
		$this->assertStatusError(
			'acct_creation_throttle_hit', $temporaryAccountCreator->create( null, $request )
		);
		// Run the maintenance script to clear the throttle
		$this->maintenance->setOption( 'tempaccount', 1 );
		$this->maintenance->setOption( 'ip', '1.2.3.4' );
		$this->maintenance->execute();
		$this->expectOutputRegex( '/Clearing temp account creation throttle.*done/' );
		// Verify that a third call works now
		$this->assertStatusGood( $temporaryAccountCreator->create( null, $request ) );
	}

	public function testClearTempAccountCreationThrottleOnNoThrottle() {
		// Disable the temporary account creation throttle
		$this->overrideConfigValue( MainConfigNames::TempAccountCreationThrottle, [] );
		// Run the maintenance script
		$this->maintenance->setOption( 'tempaccount', 1 );
		$this->maintenance->setOption( 'ip', '1.2.3.4' );
		$this->maintenance->execute();
		// Verify that the script finds no throttle set
		$this->expectOutputRegex( '/Clearing temp account creation throttle.*none set/' );
	}
}