From 2d2ca9476de9cfc02a1fbc85562a0ed7630a4b1d Mon Sep 17 00:00:00 2001 From: Sam Reed Date: Sun, 3 Oct 2010 15:24:47 +0000 Subject: [PATCH] Revert r74122, making r74117 live again Some refactoring, to encapsulate users. Added a UserWrapper, contains the setup username/password and user object. Allows for ease of adding of multiple users (and groups) Per Brion, if logging in user == wguser, we skip login tests, so use a secondary user Fixup testApiLoginBadPass, we should get a WrongPass when the passwords wrong, but we've given a token --- .../tests/phpunit/includes/api/ApiSetup.php | 43 +++++++----- .../tests/phpunit/includes/api/ApiTest.php | 66 ++++++++++--------- .../phpunit/includes/api/ApiWatchTest.php | 10 +-- 3 files changed, 66 insertions(+), 53 deletions(-) diff --git a/maintenance/tests/phpunit/includes/api/ApiSetup.php b/maintenance/tests/phpunit/includes/api/ApiSetup.php index 5524469562..8bdc107ad0 100644 --- a/maintenance/tests/phpunit/includes/api/ApiSetup.php +++ b/maintenance/tests/phpunit/includes/api/ApiSetup.php @@ -1,9 +1,8 @@ getID() ) { - self::$user = User::createNew( self::$userName, array( - "email" => "test@example.com", - "real_name" => "Test User" ) ); - } - self::$user->setPassword( self::$passWord ); - self::$user->addGroup( 'sysop' ); - self::$user->saveSettings(); + if ( self::$user == NULL || self::$sysopUser == NULL ) { + self::$user = new UserWrapper( 'Useruser', 'Passpass' ); + self::$sysopUser = new UserWrapper( 'Useruser1', 'Passpass1', 'sysop' ); } - $GLOBALS['wgUser'] = self::$user; + $GLOBALS['wgUser'] = self::$user->user; } function tearDown() { @@ -56,3 +45,25 @@ abstract class ApiTestSetup extends PHPUnit_Framework_TestCase { $wgMemc = null; } } + +class UserWrapper { + var $userName, $password, $user; + + public function __construct( $userName, $password, $group = '' ) { + $this->userName = $userName; + $this->password = $password; + + $this->user = User::newFromName( $this->userName ); + if ( !$this->user->getID() ) { + $this->user = User::createNew( $this->userName, array( + "email" => "test@example.com", + "real_name" => "Test User" ) ); + } + $this->user->setPassword( $this->password ); + + if ( $group !== '' ) { + $this->user->addGroup( $group ); + } + $this->user->saveSettings(); + } +} diff --git a/maintenance/tests/phpunit/includes/api/ApiTest.php b/maintenance/tests/phpunit/includes/api/ApiTest.php index 178e6ed302..7de790635e 100644 --- a/maintenance/tests/phpunit/includes/api/ApiTest.php +++ b/maintenance/tests/phpunit/includes/api/ApiTest.php @@ -79,7 +79,7 @@ class ApiTest extends ApiTestSetup { */ function testApiLoginNoName() { $data = $this->doApiRequest( array( 'action' => 'login', - 'lgname' => '', 'lgpassword' => self::$passWord + 'lgname' => '', 'lgpassword' => self::$user->password, ) ); $this->assertEquals( 'NoName', $data[0]['login']['result'] ); } @@ -90,32 +90,35 @@ class ApiTest extends ApiTestSetup { if ( !isset( $wgServer ) ) { $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' ); } - $resp = Http::post( self::$apiUrl . "?action=login&format=xml", - array( "postData" => array( - "lgname" => self::$userName, - "lgpassword" => "bad" ) ) ); - libxml_use_internal_errors( true ); - $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() ); + $ret = $this->doApiRequest( array( + "action" => "login", + "lgname" => self::$sysopUser->userName, + "lgpassword" => "bad", + ) + ); + + $result = $ret[0]; - $token = (string)$sxe->login[0]->attributes()->token; + $this->assertNotType( "bool", $result ); + $a = $result["login"]["result"]; + $this->assertEquals( "NeedToken", $a ); - $resp = Http::post( self::$apiUrl . "?action=login&format=xml", - array( "postData" => array( - "lgtoken" => $token, - "lgname" => self::$userName, - "lgpassword" => "bad" ) ) ); + $token = $result["login"]["token"]; + $ret = $this->doApiRequest( array( + "action" => "login", + "lgtoken" => $token, + "lgname" => self::$sysopUser->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]; + $result = $ret[0]; - $this->assertEquals( ' result="NeedToken"', $a->asXML() ); + $this->assertNotType( "bool", $result ); + $a = $result["login"]["result"]; + + $this->assertEquals( "WrongPass", $a ); } function testApiLoginGoodPass() { @@ -127,12 +130,11 @@ class ApiTest extends ApiTestSetup { $ret = $this->doApiRequest( array( "action" => "login", - "lgname" => self::$userName, - "lgpassword" => self::$passWord, + "lgname" => self::$user->userName, + "lgpassword" => self::$user->password, ) ); - libxml_use_internal_errors( true ); $result = $ret[0]; $this->assertNotType( "bool", $result ); $this->assertNotType( "null", $result["login"] ); @@ -144,8 +146,8 @@ class ApiTest extends ApiTestSetup { $ret = $this->doApiRequest( array( "action" => "login", "lgtoken" => $token, - "lgname" => self::$userName, - "lgpassword" => self::$passWord, + "lgname" => self::$user->userName, + "lgpassword" => self::$user->password, ) ); @@ -166,8 +168,8 @@ class ApiTest extends ApiTestSetup { $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml", array( "method" => "POST", "postData" => array( - "lgname" => self::$userName, - "lgpassword" => self::$passWord ) ) ); + "lgname" => self::$user->userName, + "lgpassword" => self::$user->password ) ) ); $req->execute(); libxml_use_internal_errors( true ); @@ -182,8 +184,8 @@ class ApiTest extends ApiTestSetup { $req->setData( array( "lgtoken" => $token, - "lgname" => self::$userName, - "lgpassword" => self::$passWord ) ); + "lgname" => self::$user->userName, + "lgpassword" => self::$user->password ) ); $req->execute(); $cj = $req->getCookieJar(); @@ -191,7 +193,7 @@ class ApiTest extends ApiTestSetup { $this->assertNotEquals( false, $serverName ); $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName ); $this->assertNotEquals( '', $serializedCookie ); - $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/', $serializedCookie ); + $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$user->userName . '; .*Token=/', $serializedCookie ); return $cj; } diff --git a/maintenance/tests/phpunit/includes/api/ApiWatchTest.php b/maintenance/tests/phpunit/includes/api/ApiWatchTest.php index 032e24400c..bf1d010c23 100644 --- a/maintenance/tests/phpunit/includes/api/ApiWatchTest.php +++ b/maintenance/tests/phpunit/includes/api/ApiWatchTest.php @@ -11,8 +11,8 @@ class ApiWatchTest extends ApiTestSetup { function testLogin() { $data = $this->doApiRequest( array( 'action' => 'login', - 'lgname' => self::$userName, - 'lgpassword' => self::$passWord ) ); + 'lgname' => self::$user->userName, + 'lgpassword' => self::$user->password ) ); $this->assertArrayHasKey( "login", $data[0] ); $this->assertArrayHasKey( "result", $data[0]['login'] ); @@ -22,8 +22,8 @@ class ApiWatchTest extends ApiTestSetup { $data = $this->doApiRequest( array( 'action' => 'login', "lgtoken" => $token, - "lgname" => self::$userName, - "lgpassword" => self::$passWord ), $data ); + "lgname" => self::$user->userName, + "lgpassword" => self::$user->password ), $data ); $this->assertArrayHasKey( "login", $data[0] ); $this->assertArrayHasKey( "result", $data[0]['login'] ); @@ -163,7 +163,7 @@ class ApiWatchTest extends ApiTestSetup { $data = $this->doApiRequest( array( 'action' => 'rollback', 'title' => 'Main Page', - 'user' => self::$userName, + 'user' => self::$user->userName, 'token' => $pageinfo['rollbacktoken'], 'watchlist' => 'watch' ), $data ); } catch( UsageException $ue ) { -- 2.20.1