10 class ApiLoginTest
extends ApiTestCase
{
13 * Test result of attempted login with an empty username
15 public function testApiLoginNoName() {
16 $data = $this->doApiRequest( array( 'action' => 'login',
17 'lgname' => '', 'lgpassword' => self
::$users['sysop']->password
,
19 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
22 public function testApiLoginBadPass() {
25 $user = self
::$users['sysop'];
26 $user->user
->logOut();
28 if ( !isset( $wgServer ) ) {
29 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
31 $ret = $this->doApiRequest( array(
33 "lgname" => $user->username
,
34 "lgpassword" => "bad",
39 $this->assertNotInternalType( "bool", $result );
40 $a = $result["login"]["result"];
41 $this->assertEquals( "NeedToken", $a );
43 $token = $result["login"]["token"];
45 $ret = $this->doApiRequest(
49 "lgname" => $user->username
,
50 "lgpassword" => "badnowayinhell",
57 $this->assertNotInternalType( "bool", $result );
58 $a = $result["login"]["result"];
60 $this->assertEquals( "WrongPass", $a );
63 public function testApiLoginGoodPass() {
66 if ( !isset( $wgServer ) ) {
67 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
70 $user = self
::$users['sysop'];
71 $user->user
->logOut();
73 $ret = $this->doApiRequest( array(
75 "lgname" => $user->username
,
76 "lgpassword" => $user->password
,
81 $this->assertNotInternalType( "bool", $result );
82 $this->assertNotInternalType( "null", $result["login"] );
84 $a = $result["login"]["result"];
85 $this->assertEquals( "NeedToken", $a );
86 $token = $result["login"]["token"];
88 $ret = $this->doApiRequest(
92 "lgname" => $user->username
,
93 "lgpassword" => $user->password
,
100 $this->assertNotInternalType( "bool", $result );
101 $a = $result["login"]["result"];
103 $this->assertEquals( "Success", $a );
109 public function testApiLoginGotCookie() {
110 $this->markTestIncomplete( "The server can't do external HTTP requests, "
111 . "and the internal one won't give cookies" );
113 global $wgServer, $wgScriptPath;
115 if ( !isset( $wgServer ) ) {
116 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
118 $user = self
::$users['sysop'];
120 $req = MWHttpRequest
::factory( self
::$apiUrl . "?action=login&format=xml",
121 array( "method" => "POST",
123 "lgname" => $user->username
,
124 "lgpassword" => $user->password
130 libxml_use_internal_errors( true );
131 $sxe = simplexml_load_string( $req->getContent() );
132 $this->assertNotInternalType( "bool", $sxe );
133 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
134 $this->assertNotInternalType( "null", $sxe->login
[0] );
136 $a = $sxe->login
[0]->attributes()->result
[0];
137 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
138 $token = (string)$sxe->login
[0]->attributes()->token
;
140 $req->setData( array(
142 "lgname" => $user->username
,
143 "lgpassword" => $user->password
) );
146 $cj = $req->getCookieJar();
147 $serverName = parse_url( $wgServer, PHP_URL_HOST
);
148 $this->assertNotEquals( false, $serverName );
149 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
150 $this->assertNotEquals( '', $serializedCookie );
152 '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName
. '; .*Token=/',
157 public function testRunLogin() {
158 $sysopUser = self
::$users['sysop'];
159 $data = $this->doApiRequest( array(
161 'lgname' => $sysopUser->username
,
162 'lgpassword' => $sysopUser->password
) );
164 $this->assertArrayHasKey( "login", $data[0] );
165 $this->assertArrayHasKey( "result", $data[0]['login'] );
166 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
167 $token = $data[0]['login']['token'];
169 $data = $this->doApiRequest( array(
172 "lgname" => $sysopUser->username
,
173 "lgpassword" => $sysopUser->password
), $data[2] );
175 $this->assertArrayHasKey( "login", $data[0] );
176 $this->assertArrayHasKey( "result", $data[0]['login'] );
177 $this->assertEquals( "Success", $data[0]['login']['result'] );
178 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );