wiki.techinc.nl/tests/phpunit/includes/api/ApiLoginTest.php

183 lines
4.7 KiB
PHP
Raw Normal View History

<?php
/**
* @group API
* @group Database
* @group medium
*
* @covers ApiLogin
*/
class ApiLoginTest extends ApiTestCase {
/**
* Test result of attempted login with an empty username
*/
public function testApiLoginNoName() {
$data = $this->doApiRequest( array( 'action' => 'login',
'lgname' => '', 'lgpassword' => self::$users['sysop']->password,
Remove SessionManager, temporarily The plan here is to take it out of 1.27.0-wmf.12 and put it back in 1.27.0-wmf.13. Since BotPasswords depends on SessionManager, that's getting temporarily removed too. This reverts the following commits: * 6acd424e0dbc322e8b9a141bd2625453c1b9b6f1 SessionManager: Notify AuthPlugin before calling hooks * 4d1ad32d8acbd443346253d2f6a95024c833295c Close a loophole in CookieSessionProvider * fcdd643a46d87b677f6cdcc3ba9440e1472d8df7 SessionManager: Don't save non-persisted sessions to backend storage * 058aec4c76129b7ee8541692a8a48f8046e15bb6 MessageCache: Don't get a ParserOptions for $wgUser before the end of Setup.php * b5c0c03bb708f8dad6e404969df8addc123984db SessionManager: Save user name to metadata even if the user doesn't exist locally * 13f2f09a193215aa7a061d10a1955e172d06fa0a SECURITY: Fix User::setToken() call on User::newSystemUser * 305bc75b27903237a9683ec1f329bcbec0ecd266 SessionManager: Don't generate user tokens when checking the tokens * 7c4bd85d2152fd9fa975ea0fb5ffb1a0b804f99b RequestContext::exportSession() should only export persisted session IDs * 296ccfd4a9a6ad3ae412db7e2408c923aaa61f64 SessionManager: Save 'persisted' flag in session metadata * 94ba53f67731b0553a6178841d9506e384f74496 Move CSRF token handling into MediaWiki\Session\Session * 46a565d6b00174e631d2022b47677e1a78e73897 Avoid false "added in both Session and $_SESSION" when value is null * c00d0b5d94c946b8883dd7062bf7160a199aa5c2 Log backtrace for "User::loadFromSession called before the end of Setup.php" * 4eeff5b559e2ae7b8fa1f45572968ba28573a421 Use $wgSecureCookie to decide whether to actually mark secure cookies as 'secure' * 7491b52f700e220814a8190781fd794b4dd88a20 Call session_cache_limiter() before starting a session * 2c34aeea72471f9a598e67bdbf34bc5f9fb3f0c5 SessionManager: Abstract forceHTTPS cookie setting * 9aa53627a53aabec0273cecf45a86e77927ef406 Ignore auth cookies with value 'deleted' * 43f904b51a746d7f71ea2ab9951c5c98d269765b SessionManager: Kill getPersistedSessionId() * 50c52563528ba3d765c3762211f98d6f3c0e39fd SessionManager: Add SessionBackend::setProviderMetadata() * f640d403154bc0a2b4f6d399582797a9e3bc6fcb SessionManager: Notify AuthPlugin when auto-creating accounts * 70b05d1ac1e859bac2185b246e9b93ec9051e4d8 Add checks of $wgEnableBotPasswords in more places * bfed32eb78b6c720b16bc7ed60153fd2fe257a9e Do not raise a PHP warning when session write fails * 722a7331ad8d98228511f8da38adc7a3c64dd617 Only check LoggedOut timestamp on the user loaded from session * 4f5057b84b36eccd16627a6b29831dfdb4483b02 SessionManager: Change behavior of getSessionById() * 66e82e614e157e39b03d813e71ddf23f53cf640b Fix typo in [[MediaWiki:Botpasswords-editexisting/en]] * f9fd9516d922d36291037baca7205a2b0ac9f15f Add "bot passwords" * d7716f1df0b692902571bf415a0984071e3e9a60 Add missing argument for wfDebugLog * a73c5b7395a07d490f7052fd3b2491ebd656b190 Add SessionManager Change-Id: I2389a8133e25ab929e9f27f41fa9a05df8147a50
2016-02-01 17:28:29 +00:00
) );
$this->assertEquals( 'NoName', $data[0]['login']['result'] );
}
public function testApiLoginBadPass() {
global $wgServer;
$user = self::$users['sysop'];
$user->getUser()->logOut();
if ( !isset( $wgServer ) ) {
$this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
}
$ret = $this->doApiRequest( array(
"action" => "login",
"lgname" => $user->username,
"lgpassword" => "bad",
) );
$result = $ret[0];
$this->assertNotInternalType( "bool", $result );
$a = $result["login"]["result"];
$this->assertEquals( "NeedToken", $a );
$token = $result["login"]["token"];
$ret = $this->doApiRequest(
array(
"action" => "login",
"lgtoken" => $token,
"lgname" => $user->username,
"lgpassword" => "badnowayinhell",
),
$ret[2]
);
$result = $ret[0];
$this->assertNotInternalType( "bool", $result );
$a = $result["login"]["result"];
$this->assertEquals( "WrongPass", $a );
}
public function testApiLoginGoodPass() {
global $wgServer;
if ( !isset( $wgServer ) ) {
$this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
}
$user = self::$users['sysop'];
$user->getUser()->logOut();
$ret = $this->doApiRequest( array(
"action" => "login",
"lgname" => $user->username,
"lgpassword" => $user->password,
)
);
$result = $ret[0];
$this->assertNotInternalType( "bool", $result );
$this->assertNotInternalType( "null", $result["login"] );
$a = $result["login"]["result"];
$this->assertEquals( "NeedToken", $a );
$token = $result["login"]["token"];
$ret = $this->doApiRequest(
array(
"action" => "login",
"lgtoken" => $token,
"lgname" => $user->username,
"lgpassword" => $user->password,
),
$ret[2]
);
$result = $ret[0];
$this->assertNotInternalType( "bool", $result );
$a = $result["login"]["result"];
$this->assertEquals( "Success", $a );
}
/**
* @group Broken
*/
public function testApiLoginGotCookie() {
$this->markTestIncomplete( "The server can't do external HTTP requests, "
. "and the internal one won't give cookies" );
global $wgServer, $wgScriptPath;
if ( !isset( $wgServer ) ) {
$this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
}
$user = self::$users['sysop'];
$req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
array( "method" => "POST",
"postData" => array(
"lgname" => $user->username,
"lgpassword" => $user->password
)
),
__METHOD__
);
$req->execute();
libxml_use_internal_errors( true );
$sxe = simplexml_load_string( $req->getContent() );
$this->assertNotInternalType( "bool", $sxe );
$this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
$this->assertNotInternalType( "null", $sxe->login[0] );
$a = $sxe->login[0]->attributes()->result[0];
$this->assertEquals( ' result="NeedToken"', $a->asXML() );
$token = (string)$sxe->login[0]->attributes()->token;
$req->setData( array(
"lgtoken" => $token,
"lgname" => $user->username,
"lgpassword" => $user->password ) );
$req->execute();
$cj = $req->getCookieJar();
$serverName = parse_url( $wgServer, PHP_URL_HOST );
$this->assertNotEquals( false, $serverName );
$serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
$this->assertNotEquals( '', $serializedCookie );
$this->assertRegexp(
'/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/',
$serializedCookie
);
}
public function testRunLogin() {
$sysopUser = self::$users['sysop'];
$data = $this->doApiRequest( array(
'action' => 'login',
'lgname' => $sysopUser->username,
'lgpassword' => $sysopUser->password ) );
$this->assertArrayHasKey( "login", $data[0] );
$this->assertArrayHasKey( "result", $data[0]['login'] );
$this->assertEquals( "NeedToken", $data[0]['login']['result'] );
$token = $data[0]['login']['token'];
$data = $this->doApiRequest( array(
'action' => 'login',
"lgtoken" => $token,
"lgname" => $sysopUser->username,
"lgpassword" => $sysopUser->password ), $data[2] );
$this->assertArrayHasKey( "login", $data[0] );
$this->assertArrayHasKey( "result", $data[0]['login'] );
$this->assertEquals( "Success", $data[0]['login']['result'] );
$this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
}
}