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, and the internal one won't give cookies" );
112 global $wgServer, $wgScriptPath;
114 if ( !isset( $wgServer ) ) {
115 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
117 $user = self
::$users['sysop'];
119 $req = MWHttpRequest
::factory( self
::$apiUrl . "?action=login&format=xml",
120 array( "method" => "POST",
122 "lgname" => $user->username
,
123 "lgpassword" => $user->password
129 libxml_use_internal_errors( true );
130 $sxe = simplexml_load_string( $req->getContent() );
131 $this->assertNotInternalType( "bool", $sxe );
132 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
133 $this->assertNotInternalType( "null", $sxe->login
[0] );
135 $a = $sxe->login
[0]->attributes()->result
[0];
136 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
137 $token = (string)$sxe->login
[0]->attributes()->token
;
139 $req->setData( array(
141 "lgname" => $user->username
,
142 "lgpassword" => $user->password
) );
145 $cj = $req->getCookieJar();
146 $serverName = parse_url( $wgServer, PHP_URL_HOST
);
147 $this->assertNotEquals( false, $serverName );
148 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
149 $this->assertNotEquals( '', $serializedCookie );
150 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName
. '; .*Token=/', $serializedCookie );
153 public function testRunLogin() {
154 $sysopUser = self
::$users['sysop'];
155 $data = $this->doApiRequest( array(
157 'lgname' => $sysopUser->username
,
158 'lgpassword' => $sysopUser->password
) );
160 $this->assertArrayHasKey( "login", $data[0] );
161 $this->assertArrayHasKey( "result", $data[0]['login'] );
162 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
163 $token = $data[0]['login']['token'];
165 $data = $this->doApiRequest( array(
168 "lgname" => $sysopUser->username
,
169 "lgpassword" => $sysopUser->password
), $data[2] );
171 $this->assertArrayHasKey( "login", $data[0] );
172 $this->assertArrayHasKey( "result", $data[0]['login'] );
173 $this->assertEquals( "Success", $data[0]['login']['result'] );
174 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );