Debian Codegraph @

tests/phpunit/maintenance/ResetAuthenticationThrottleTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Maintenance;
4
5 use MediaWiki\Context\RequestContext;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
8 use ResetAuthenticationThrottle;
9
10 /**
11 * @covers \ResetAuthenticationThrottle
12 * @group Database
13 * @author Dreamy Jazz
14 */
15 class ResetAuthenticationThrottleTest extends MaintenanceBaseTestCase {
16
17 use TempUserTestTrait;
18
19 public function getMaintenanceClass() {
20 return ResetAuthenticationThrottle::class;
21 }
22
23 /** @dataProvider provideExecuteForFatalError */
24 public function testExecuteForFatalError( $options, $expectedOutputRegex ) {
25 foreach ( $options as $name => $value ) {
26 $this->maintenance->setOption( $name, $value );
27 }
28 $this->expectCallToFatalError();
29 $this->expectOutputRegex( $expectedOutputRegex );
30 $this->maintenance->execute();
31 }
32
33 public static function provideExecuteForFatalError() {
34 return [
35 'No options' => [
36 [], '/At least one of --login, --signup, --tempaccount, or --tempaccountnameacquisition is required/',
37 ],
38 '--signup but no IP' => [ [ 'signup' => 1 ], '/--ip is required/' ],
39 'Invalid --ip argument' => [ [ 'signup' => 1, 'ip' => 'abcef' ], '/Not a valid IP/' ],
40 'Invalid --user argument' => [
41 [ 'login' => 1, 'user' => 'Template:Testing#test', 'ip' => '1.2.3.4' ], '/Not a valid username/',
42 ],
43 ];
44 }
45
46 public function testClearTempAccountNameAcquisitionThrottle() {
47 $this->enableAutoCreateTempUser();
48 $this->overrideConfigValue( MainConfigNames::TempAccountNameAcquisitionThrottle, [
49 'count' => 1,
50 'seconds' => 86400,
51 ] );
52 $request = RequestContext::getMain()->getRequest();
53 $request->setIP( '1.2.3.4' );
54 $temporaryAccountCreator = $this->getServiceContainer()->getTempUserCreator();
55 // Acquire a temporary account name to increase the throttle counter
56 $this->assertNotNull( $temporaryAccountCreator->acquireAndStashName( $request->getSession() ) );
57 // Verify that a second call does not get a name
58 $request->getSession()->clear();
59 $this->assertNull( $temporaryAccountCreator->acquireAndStashName( $request->getSession() ) );
60 // Run the maintenance script to clear the throttle
61 $this->maintenance->setOption( 'tempaccountnameacquisition', 1 );
62 $this->maintenance->setOption( 'ip', '1.2.3.4' );
63 $this->maintenance->execute();
64 $this->expectOutputRegex( '/Clearing temp account name acquisition throttle.*done/' );
65 // Verify that a third call works now
66 $this->assertNotNull( $temporaryAccountCreator->acquireAndStashName( $request->getSession() ) );
67 }
68
69 public function testClearTempAccountNameAcquisitionThrottleOnNoThrottle() {
70 // Disable the temporary account name acquisition throttle
71 $this->overrideConfigValue( MainConfigNames::TempAccountNameAcquisitionThrottle, [] );
72 // Run the maintenance script
73 $this->maintenance->setOption( 'tempaccountnameacquisition', 1 );
74 $this->maintenance->setOption( 'ip', '1.2.3.4' );
75 $this->maintenance->execute();
76 // Verify that the script finds no throttle set
77 $this->expectOutputRegex( '/Clearing temp account name acquisition throttle.*none set/' );
78 }
79
80 public function testClearTempAccountCreationThrottle() {
81 $this->enableAutoCreateTempUser();
82 $this->overrideConfigValue( MainConfigNames::TempAccountCreationThrottle, [
83 'count' => 1,
84 'seconds' => 86400,
85 ] );
86 $request = RequestContext::getMain()->getRequest();
87 $request->setIP( '1.2.3.4' );
88 $temporaryAccountCreator = $this->getServiceContainer()->getTempUserCreator();
89 // Acquire a temporary account to increase the throttle counter
90 $this->assertStatusGood( $temporaryAccountCreator->create( null, $request ) );
91 // Verify that a second call does not get a name
92 $request->getSession()->clear();
93 $this->assertStatusError(
94 'acct_creation_throttle_hit', $temporaryAccountCreator->create( null, $request )
95 );
96 // Run the maintenance script to clear the throttle
97 $this->maintenance->setOption( 'tempaccount', 1 );
98 $this->maintenance->setOption( 'ip', '1.2.3.4' );
99 $this->maintenance->execute();
100 $this->expectOutputRegex( '/Clearing temp account creation throttle.*done/' );
101 // Verify that a third call works now
102 $this->assertStatusGood( $temporaryAccountCreator->create( null, $request ) );
103 }
104
105 public function testClearTempAccountCreationThrottleOnNoThrottle() {
106 // Disable the temporary account creation throttle
107 $this->overrideConfigValue( MainConfigNames::TempAccountCreationThrottle, [] );
108 // Run the maintenance script
109 $this->maintenance->setOption( 'tempaccount', 1 );
110 $this->maintenance->setOption( 'ip', '1.2.3.4' );
111 $this->maintenance->execute();
112 // Verify that the script finds no throttle set
113 $this->expectOutputRegex( '/Clearing temp account creation throttle.*none set/' );
114 }
115 }