10 class ApiLoginTest
extends ApiTestCase
{
13 * Test result of attempted login with an empty username
15 public function testApiLoginNoName() {
17 'wsTokenSecrets' => array( 'login' => 'foobar' ),
19 $data = $this->doApiRequest( array( 'action' => 'login',
20 'lgname' => '', 'lgpassword' => self
::$users['sysop']->password
,
21 'lgtoken' => (string)( new MediaWiki\Session\
Token( 'foobar', '' ) )
23 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
26 public function testApiLoginBadPass() {
29 $user = self
::$users['sysop'];
30 $user->getUser()->logOut();
32 if ( !isset( $wgServer ) ) {
33 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
35 $ret = $this->doApiRequest( array(
37 "lgname" => $user->username
,
38 "lgpassword" => "bad",
43 $this->assertNotInternalType( "bool", $result );
44 $a = $result["login"]["result"];
45 $this->assertEquals( "NeedToken", $a );
47 $token = $result["login"]["token"];
49 $ret = $this->doApiRequest(
53 "lgname" => $user->username
,
54 "lgpassword" => "badnowayinhell",
61 $this->assertNotInternalType( "bool", $result );
62 $a = $result["login"]["result"];
64 $this->assertEquals( "WrongPass", $a );
67 public function testApiLoginGoodPass() {
70 if ( !isset( $wgServer ) ) {
71 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
74 $user = self
::$users['sysop'];
75 $user->getUser()->logOut();
77 $ret = $this->doApiRequest( array(
79 "lgname" => $user->username
,
80 "lgpassword" => $user->password
,
85 $this->assertNotInternalType( "bool", $result );
86 $this->assertNotInternalType( "null", $result["login"] );
88 $a = $result["login"]["result"];
89 $this->assertEquals( "NeedToken", $a );
90 $token = $result["login"]["token"];
92 $ret = $this->doApiRequest(
96 "lgname" => $user->username
,
97 "lgpassword" => $user->password
,
104 $this->assertNotInternalType( "bool", $result );
105 $a = $result["login"]["result"];
107 $this->assertEquals( "Success", $a );
113 public function testApiLoginGotCookie() {
114 $this->markTestIncomplete( "The server can't do external HTTP requests, "
115 . "and the internal one won't give cookies" );
117 global $wgServer, $wgScriptPath;
119 if ( !isset( $wgServer ) ) {
120 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
122 $user = self
::$users['sysop'];
124 $req = MWHttpRequest
::factory( self
::$apiUrl . "?action=login&format=xml",
125 array( "method" => "POST",
127 "lgname" => $user->username
,
128 "lgpassword" => $user->password
135 libxml_use_internal_errors( true );
136 $sxe = simplexml_load_string( $req->getContent() );
137 $this->assertNotInternalType( "bool", $sxe );
138 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
139 $this->assertNotInternalType( "null", $sxe->login
[0] );
141 $a = $sxe->login
[0]->attributes()->result
[0];
142 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
143 $token = (string)$sxe->login
[0]->attributes()->token
;
145 $req->setData( array(
147 "lgname" => $user->username
,
148 "lgpassword" => $user->password
) );
151 $cj = $req->getCookieJar();
152 $serverName = parse_url( $wgServer, PHP_URL_HOST
);
153 $this->assertNotEquals( false, $serverName );
154 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
155 $this->assertNotEquals( '', $serializedCookie );
157 '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName
. '; .*Token=/',
162 public function testRunLogin() {
163 $sysopUser = self
::$users['sysop'];
164 $data = $this->doApiRequest( array(
166 'lgname' => $sysopUser->username
,
167 'lgpassword' => $sysopUser->password
) );
169 $this->assertArrayHasKey( "login", $data[0] );
170 $this->assertArrayHasKey( "result", $data[0]['login'] );
171 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
172 $token = $data[0]['login']['token'];
174 $data = $this->doApiRequest( array(
177 "lgname" => $sysopUser->username
,
178 "lgpassword" => $sysopUser->password
), $data[2] );
180 $this->assertArrayHasKey( "login", $data[0] );
181 $this->assertArrayHasKey( "result", $data[0]['login'] );
182 $this->assertEquals( "Success", $data[0]['login']['result'] );
183 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
186 public function testBotPassword() {
187 global $wgServer, $wgSessionProviders;
189 if ( !isset( $wgServer ) ) {
190 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
193 $this->setMwGlobals( array(
194 'wgSessionProviders' => array_merge( $wgSessionProviders, array(
196 'class' => 'MediaWiki\\Session\\BotPasswordSessionProvider',
197 'args' => array( array( 'priority' => 40 ) ),
200 'wgEnableBotPasswords' => true,
201 'wgBotPasswordsDatabase' => false,
202 'wgCentralIdLookupProvider' => 'local',
203 'wgGrantPermissions' => array(
204 'test' => array( 'read' => true ),
208 // Make sure our session provider is present
209 $manager = TestingAccessWrapper
::newFromObject( MediaWiki\Session\SessionManager
::singleton() );
210 if ( !isset( $manager->sessionProviders
['MediaWiki\\Session\\BotPasswordSessionProvider'] ) ) {
211 $tmp = $manager->sessionProviders
;
212 $manager->sessionProviders
= null;
213 $manager->sessionProviders
= $tmp +
$manager->getProviders();
215 $this->assertNotNull(
216 MediaWiki\Session\SessionManager
::singleton()->getProvider(
217 'MediaWiki\\Session\\BotPasswordSessionProvider'
222 $user = self
::$users['sysop'];
223 $centralId = CentralIdLookup
::factory()->centralIdFromLocalUser( $user->getUser() );
224 $this->assertNotEquals( 0, $centralId, 'sanity check' );
226 $passwordFactory = new PasswordFactory();
227 $passwordFactory->init( RequestContext
::getMain()->getConfig() );
228 // A is unsalted MD5 (thus fast) ... we don't care about security here, this is test only
229 $passwordFactory->setDefaultType( 'A' );
230 $pwhash = $passwordFactory->newFromPlaintext( 'foobaz' );
232 $dbw = wfGetDB( DB_MASTER
);
236 'bp_user' => $centralId,
237 'bp_app_id' => 'foo',
238 'bp_password' => $pwhash->toString(),
240 'bp_restrictions' => MWRestrictions
::newDefault()->toJson(),
241 'bp_grants' => '["test"]',
246 $lgName = $user->username
. BotPassword
::getSeparator() . 'foo';
248 $ret = $this->doApiRequest( array(
251 'lgpassword' => 'foobaz',
255 $this->assertNotInternalType( 'bool', $result );
256 $this->assertNotInternalType( 'null', $result['login'] );
258 $a = $result['login']['result'];
259 $this->assertEquals( 'NeedToken', $a );
260 $token = $result['login']['token'];
262 $ret = $this->doApiRequest( array(
266 'lgpassword' => 'foobaz',
270 $this->assertNotInternalType( 'bool', $result );
271 $a = $result['login']['result'];
273 $this->assertEquals( 'Success', $a );