MediaWiki  REL1_23
ApiLoginTest.php
Go to the documentation of this file.
00001 <?php
00002 
00010 class ApiLoginTest extends ApiTestCase {
00011 
00015     public function testApiLoginNoName() {
00016         $data = $this->doApiRequest( array( 'action' => 'login',
00017             'lgname' => '', 'lgpassword' => self::$users['sysop']->password,
00018         ) );
00019         $this->assertEquals( 'NoName', $data[0]['login']['result'] );
00020     }
00021 
00022     public function testApiLoginBadPass() {
00023         global $wgServer;
00024 
00025         $user = self::$users['sysop'];
00026         $user->user->logOut();
00027 
00028         if ( !isset( $wgServer ) ) {
00029             $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
00030         }
00031         $ret = $this->doApiRequest( array(
00032             "action" => "login",
00033             "lgname" => $user->username,
00034             "lgpassword" => "bad",
00035         ) );
00036 
00037         $result = $ret[0];
00038 
00039         $this->assertNotInternalType( "bool", $result );
00040         $a = $result["login"]["result"];
00041         $this->assertEquals( "NeedToken", $a );
00042 
00043         $token = $result["login"]["token"];
00044 
00045         $ret = $this->doApiRequest(
00046             array(
00047                 "action" => "login",
00048                 "lgtoken" => $token,
00049                 "lgname" => $user->username,
00050                 "lgpassword" => "badnowayinhell",
00051             ),
00052             $ret[2]
00053         );
00054 
00055         $result = $ret[0];
00056 
00057         $this->assertNotInternalType( "bool", $result );
00058         $a = $result["login"]["result"];
00059 
00060         $this->assertEquals( "WrongPass", $a );
00061     }
00062 
00063     public function testApiLoginGoodPass() {
00064         global $wgServer;
00065 
00066         if ( !isset( $wgServer ) ) {
00067             $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
00068         }
00069 
00070         $user = self::$users['sysop'];
00071         $user->user->logOut();
00072 
00073         $ret = $this->doApiRequest( array(
00074                 "action" => "login",
00075                 "lgname" => $user->username,
00076                 "lgpassword" => $user->password,
00077             )
00078         );
00079 
00080         $result = $ret[0];
00081         $this->assertNotInternalType( "bool", $result );
00082         $this->assertNotInternalType( "null", $result["login"] );
00083 
00084         $a = $result["login"]["result"];
00085         $this->assertEquals( "NeedToken", $a );
00086         $token = $result["login"]["token"];
00087 
00088         $ret = $this->doApiRequest(
00089             array(
00090                 "action" => "login",
00091                 "lgtoken" => $token,
00092                 "lgname" => $user->username,
00093                 "lgpassword" => $user->password,
00094             ),
00095             $ret[2]
00096         );
00097 
00098         $result = $ret[0];
00099 
00100         $this->assertNotInternalType( "bool", $result );
00101         $a = $result["login"]["result"];
00102 
00103         $this->assertEquals( "Success", $a );
00104     }
00105 
00109     public function testApiLoginGotCookie() {
00110         $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
00111 
00112         global $wgServer, $wgScriptPath;
00113 
00114         if ( !isset( $wgServer ) ) {
00115             $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
00116         }
00117         $user = self::$users['sysop'];
00118 
00119         $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
00120             array( "method" => "POST",
00121                 "postData" => array(
00122                     "lgname" => $user->username,
00123                     "lgpassword" => $user->password
00124                 )
00125             )
00126         );
00127         $req->execute();
00128 
00129         libxml_use_internal_errors( true );
00130         $sxe = simplexml_load_string( $req->getContent() );
00131         $this->assertNotInternalType( "bool", $sxe );
00132         $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
00133         $this->assertNotInternalType( "null", $sxe->login[0] );
00134 
00135         $a = $sxe->login[0]->attributes()->result[0];
00136         $this->assertEquals( ' result="NeedToken"', $a->asXML() );
00137         $token = (string)$sxe->login[0]->attributes()->token;
00138 
00139         $req->setData( array(
00140             "lgtoken" => $token,
00141             "lgname" => $user->username,
00142             "lgpassword" => $user->password ) );
00143         $req->execute();
00144 
00145         $cj = $req->getCookieJar();
00146         $serverName = parse_url( $wgServer, PHP_URL_HOST );
00147         $this->assertNotEquals( false, $serverName );
00148         $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
00149         $this->assertNotEquals( '', $serializedCookie );
00150         $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/', $serializedCookie );
00151     }
00152 
00153     public function testRunLogin() {
00154         $sysopUser = self::$users['sysop'];
00155         $data = $this->doApiRequest( array(
00156             'action' => 'login',
00157             'lgname' => $sysopUser->username,
00158             'lgpassword' => $sysopUser->password ) );
00159 
00160         $this->assertArrayHasKey( "login", $data[0] );
00161         $this->assertArrayHasKey( "result", $data[0]['login'] );
00162         $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
00163         $token = $data[0]['login']['token'];
00164 
00165         $data = $this->doApiRequest( array(
00166             'action' => 'login',
00167             "lgtoken" => $token,
00168             "lgname" => $sysopUser->username,
00169             "lgpassword" => $sysopUser->password ), $data[2] );
00170 
00171         $this->assertArrayHasKey( "login", $data[0] );
00172         $this->assertArrayHasKey( "result", $data[0]['login'] );
00173         $this->assertEquals( "Success", $data[0]['login']['result'] );
00174         $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
00175     }
00176 
00177 }