MediaWiki  REL1_24
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, "
00111             . "and the internal one won't give cookies" );
00112 
00113         global $wgServer, $wgScriptPath;
00114 
00115         if ( !isset( $wgServer ) ) {
00116             $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
00117         }
00118         $user = self::$users['sysop'];
00119 
00120         $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
00121             array( "method" => "POST",
00122                 "postData" => array(
00123                     "lgname" => $user->username,
00124                     "lgpassword" => $user->password
00125                 )
00126             )
00127         );
00128         $req->execute();
00129 
00130         libxml_use_internal_errors( true );
00131         $sxe = simplexml_load_string( $req->getContent() );
00132         $this->assertNotInternalType( "bool", $sxe );
00133         $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
00134         $this->assertNotInternalType( "null", $sxe->login[0] );
00135 
00136         $a = $sxe->login[0]->attributes()->result[0];
00137         $this->assertEquals( ' result="NeedToken"', $a->asXML() );
00138         $token = (string)$sxe->login[0]->attributes()->token;
00139 
00140         $req->setData( array(
00141             "lgtoken" => $token,
00142             "lgname" => $user->username,
00143             "lgpassword" => $user->password ) );
00144         $req->execute();
00145 
00146         $cj = $req->getCookieJar();
00147         $serverName = parse_url( $wgServer, PHP_URL_HOST );
00148         $this->assertNotEquals( false, $serverName );
00149         $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
00150         $this->assertNotEquals( '', $serializedCookie );
00151         $this->assertRegexp(
00152             '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/',
00153             $serializedCookie
00154         );
00155     }
00156 
00157     public function testRunLogin() {
00158         $sysopUser = self::$users['sysop'];
00159         $data = $this->doApiRequest( array(
00160             'action' => 'login',
00161             'lgname' => $sysopUser->username,
00162             'lgpassword' => $sysopUser->password ) );
00163 
00164         $this->assertArrayHasKey( "login", $data[0] );
00165         $this->assertArrayHasKey( "result", $data[0]['login'] );
00166         $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
00167         $token = $data[0]['login']['token'];
00168 
00169         $data = $this->doApiRequest( array(
00170             'action' => 'login',
00171             "lgtoken" => $token,
00172             "lgname" => $sysopUser->username,
00173             "lgpassword" => $sysopUser->password ), $data[2] );
00174 
00175         $this->assertArrayHasKey( "login", $data[0] );
00176         $this->assertArrayHasKey( "result", $data[0]['login'] );
00177         $this->assertEquals( "Success", $data[0]['login']['result'] );
00178         $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
00179     }
00180 
00181 }