| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use CreateBotPassword; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\User\BotPassword; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class CreateBotPasswordTest extends MaintenanceBaseTestCase { |
| 15 |
|
| 16 |
protected function getMaintenanceClass() { |
| 17 |
return CreateBotPassword::class; |
| 18 |
} |
| 19 |
|
| 20 |
public function testExecuteForShowGrants() { |
| 21 |
$this->overrideConfigValue( MainConfigNames::GrantPermissions, [ |
| 22 |
'import' => [ |
| 23 |
'import' => true, |
| 24 |
'importupload' => true, |
| 25 |
], |
| 26 |
] ); |
| 27 |
$this->maintenance->setOption( 'showgrants', 1 ); |
| 28 |
$this->maintenance->execute(); |
| 29 |
$this->expectOutputString( |
| 30 |
str_pad( 'GRANT', 20 ) . " DESCRIPTION\n" . |
| 31 |
str_pad( 'import', 20 ) . " Import pages from other wikis\n" |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
public function testExecuteForFatalError( $args, $options, $expectedOutputRegex ) { |
| 37 |
foreach ( $args as $name => $value ) { |
| 38 |
$this->maintenance->setArg( $name, $value ); |
| 39 |
} |
| 40 |
foreach ( $options as $name => $value ) { |
| 41 |
$this->maintenance->setOption( $name, $value ); |
| 42 |
} |
| 43 |
$this->expectCallToFatalError(); |
| 44 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 45 |
$this->maintenance->execute(); |
| 46 |
} |
| 47 |
|
| 48 |
public static function provideExecuteForFatalError() { |
| 49 |
return [ |
| 50 |
'Invalid grants provided' => [ |
| 51 |
[ 'user' => 'Test' ], |
| 52 |
[ 'grants' => 'invalidgrant1234', 'appid' => 'abcdef' ], |
| 53 |
'/These grants are invalid: invalidgrant1234/', |
| 54 |
], |
| 55 |
'Non-existing user provided' => [ |
| 56 |
[ 'user' => 'Test' ], |
| 57 |
[ 'grants' => 'import', 'appid' => 'abcdef' ], |
| 58 |
'/Cannot create bot password for non-existent user/', |
| 59 |
], |
| 60 |
'No arguments or options provided' => [ |
| 61 |
[], [], "/Argument <user> required!\nParam appid required!\nParam grants required!/", |
| 62 |
], |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
public function testExecuteWhenBotPasswordToShort() { |
| 67 |
$this->testExecuteForFatalError( |
| 68 |
[ 'user' => $this->getTestUser()->getUserIdentity()->getName(), 'password' => 'abc' ], |
| 69 |
[ 'grants' => 'import', 'appid' => 'abc' ], |
| 70 |
'/Bot passwords must have at least ' . BotPassword::PASSWORD_MINLENGTH . |
| 71 |
' characters. Given password is 3 characters/' |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
public function testExecuteWhenAppIdTooLong() { |
| 76 |
$this->testExecuteForFatalError( |
| 77 |
[ 'user' => $this->getTestUser()->getUserIdentity()->getName() ], |
| 78 |
[ 'grants' => 'import', 'appid' => str_repeat( 'abc', 100 ) ], |
| 79 |
'/Bot password creation failed/' |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
public function testExecuteWhenAppIdAlreadyExists() { |
| 84 |
$this->overrideConfigValue( MainConfigNames::CentralIdLookupProvider, 'local' ); |
| 85 |
$username = $this->getMutableTestUser()->getUserIdentity()->getName(); |
| 86 |
|
| 87 |
$bp = BotPassword::newUnsaved( [ |
| 88 |
'username' => $username, |
| 89 |
'appId' => 'abc', |
| 90 |
'grants' => [ 'import', 'editpage' ] |
| 91 |
] ); |
| 92 |
$bp->save( 'insert' ); |
| 93 |
|
| 94 |
$this->testExecuteForFatalError( |
| 95 |
[ 'user' => $username ], |
| 96 |
[ 'grants' => 'import', 'appid' => 'abc' ], |
| 97 |
'/Bot password creation failed. Does this appid already exist for the user perhaps?/' |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
public function testExecuteForSuccess() { |
| 102 |
$this->overrideConfigValue( MainConfigNames::CentralIdLookupProvider, 'local' ); |
| 103 |
$username = $this->getMutableTestUser()->getUserIdentity()->getName(); |
| 104 |
|
| 105 |
$this->maintenance->setArg( 'user', $username ); |
| 106 |
$this->maintenance->setOption( 'grants', 'import,editpage' ); |
| 107 |
$this->maintenance->setOption( 'appid', 'abcdef' ); |
| 108 |
|
| 109 |
$this->maintenance->execute(); |
| 110 |
$this->expectOutputRegex( "/Success[\s\S]*Log in using username.*$username@abcdef/" ); |
| 111 |
} |
| 112 |
} |