* Fix a bug to keep consecutive HTTP requests from sharing results
[lhc/web/wiklou.git] / maintenance / tests / ApiTest.php
index 4b838ce..bb8e37c 100644 (file)
@@ -112,8 +112,24 @@ class ApiTest extends ApiSetup {
                $sxe = simplexml_load_string( $resp );
                $this->assertNotType( "bool", $sxe );
                $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
-               $a = $sxe->login[0]->attributes()->result;
-               $this->assertEquals( ' result="WrongPass"', $a->asXML() );
+               $a = $sxe->login[0]->attributes()->result[0];
+               $this->assertEquals( ' result="NeedToken"', $a->asXML() );
+
+               $token = (string)$sxe->login[0]->attributes()->token;
+
+               $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
+                                                  array( "postData" => array(
+                                                                       "lgtoken" => $token,
+                                                                       "lgname" => self::$userName,
+                                                                       "lgpassword" => "bad" ) ) );
+
+
+               $sxe = simplexml_load_string( $resp );
+               $this->assertNotType( "bool", $sxe );
+               $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
+               $a = $sxe->login[0]->attributes()->result[0];
+
+               $this->assertEquals( ' result="NeedToken"', $a->asXML() );
        }
 
        function testApiLoginGoodPass() {
@@ -125,15 +141,34 @@ class ApiTest extends ApiSetup {
                        $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
                                                                          'be set in LocalSettings.php');
                }
-               $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
-                                                  array( "postData" => array(
-                                                                        "lgname" => self::$userName,
-                                                                        "lgpassword" => self::$passWord ) ) );
+               $req = HttpRequest::factory(self::$apiUrl . "?action=login&format=xml",
+                       array( "method" => "POST",
+                               "postData" => array(
+                               "lgname" => self::$userName,
+                               "lgpassword" => self::$passWord ) ) );
+               $req->execute();
+
                libxml_use_internal_errors( true );
-               $sxe = simplexml_load_string( $resp );
+               $sxe = simplexml_load_string( $req->getContent() );
                $this->assertNotType( "bool", $sxe );
                $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
-               $a = $sxe->login[0]->attributes()->result;
+
+               $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" => self::$userName,
+                       "lgpassword" => self::$passWord ) );
+               $req->execute();
+
+               $sxe = simplexml_load_string( $req->getContent() );
+
+               $this->assertNotType( "bool", $sxe );
+               $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
+               $a = $sxe->login[0]->attributes()->result[0];
+
                $this->assertEquals( ' result="Success"', $a->asXML() );
        }
 
@@ -146,11 +181,28 @@ class ApiTest extends ApiSetup {
                        $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
                                                                          'be set in LocalSettings.php');
                }
-               $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
-                                                                        array( "method" => "POST",
-                                                                                       "postData" => array( "lgname" => self::$userName,
-                                                                                                                                "lgpassword" => self::$passWord ) ) );
+               $req = HttpRequest::factory(self::$apiUrl . "?action=login&format=xml",
+                       array( "method" => "POST",
+                               "postData" => array(
+                               "lgname" => self::$userName,
+                               "lgpassword" => self::$passWord ) ) );
                $req->execute();
+
+               libxml_use_internal_errors( true );
+               $sxe = simplexml_load_string( $req->getContent() );
+               $this->assertNotType( "bool", $sxe );
+               $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
+
+               $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" => self::$userName,
+                       "lgpassword" => self::$passWord ) );
+               $req->execute();
+
                $cj = $req->getCookieJar();
                $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/',
                                                         $cj->serializeToHttpRequest( $wgScriptPath, $wgServerName ) );