r74213 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r74212‎ | r74213 | r74214 >
Date:15:24, 3 October 2010
Author:reedy
Status:resolved (Comments)
Tags:
Comment:
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
Modified paths:
  • /trunk/phase3/maintenance/tests/phpunit/includes/api/ApiSetup.php (modified) (history)
  • /trunk/phase3/maintenance/tests/phpunit/includes/api/ApiTest.php (modified) (history)
  • /trunk/phase3/maintenance/tests/phpunit/includes/api/ApiWatchTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/tests/phpunit/includes/api/ApiTest.php
@@ -79,7 +79,7 @@
8080 */
8181 function testApiLoginNoName() {
8282 $data = $this->doApiRequest( array( 'action' => 'login',
83 - 'lgname' => '', 'lgpassword' => self::$passWord
 83+ 'lgname' => '', 'lgpassword' => self::$user->password,
8484 ) );
8585 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
8686 }
@@ -90,32 +90,35 @@
9191 if ( !isset( $wgServer ) ) {
9292 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
9393 }
94 - $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
95 - array( "postData" => array(
96 - "lgname" => self::$userName,
97 - "lgpassword" => "bad" ) ) );
98 - libxml_use_internal_errors( true );
99 - $sxe = simplexml_load_string( $resp );
100 - $this->assertNotType( "bool", $sxe );
101 - $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
102 - $a = $sxe->login[0]->attributes()->result[0];
103 - $this->assertEquals( ' result="NeedToken"', $a->asXML() );
 94+ $ret = $this->doApiRequest( array(
 95+ "action" => "login",
 96+ "lgname" => self::$sysopUser->userName,
 97+ "lgpassword" => "bad",
 98+ )
 99+ );
 100+
 101+ $result = $ret[0];
104102
105 - $token = (string)$sxe->login[0]->attributes()->token;
 103+ $this->assertNotType( "bool", $result );
 104+ $a = $result["login"]["result"];
 105+ $this->assertEquals( "NeedToken", $a );
106106
107 - $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
108 - array( "postData" => array(
109 - "lgtoken" => $token,
110 - "lgname" => self::$userName,
111 - "lgpassword" => "bad" ) ) );
 107+ $token = $result["login"]["token"];
112108
 109+ $ret = $this->doApiRequest( array(
 110+ "action" => "login",
 111+ "lgtoken" => $token,
 112+ "lgname" => self::$sysopUser->userName,
 113+ "lgpassword" => "bad",
 114+ )
 115+ );
113116
114 - $sxe = simplexml_load_string( $resp );
115 - $this->assertNotType( "bool", $sxe );
116 - $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
117 - $a = $sxe->login[0]->attributes()->result[0];
 117+ $result = $ret[0];
118118
119 - $this->assertEquals( ' result="NeedToken"', $a->asXML() );
 119+ $this->assertNotType( "bool", $result );
 120+ $a = $result["login"]["result"];
 121+
 122+ $this->assertEquals( "WrongPass", $a );
120123 }
121124
122125 function testApiLoginGoodPass() {
@@ -127,12 +130,11 @@
128131
129132 $ret = $this->doApiRequest( array(
130133 "action" => "login",
131 - "lgname" => self::$userName,
132 - "lgpassword" => self::$passWord,
 134+ "lgname" => self::$user->userName,
 135+ "lgpassword" => self::$user->password,
133136 )
134137 );
135138
136 - libxml_use_internal_errors( true );
137139 $result = $ret[0];
138140 $this->assertNotType( "bool", $result );
139141 $this->assertNotType( "null", $result["login"] );
@@ -144,8 +146,8 @@
145147 $ret = $this->doApiRequest( array(
146148 "action" => "login",
147149 "lgtoken" => $token,
148 - "lgname" => self::$userName,
149 - "lgpassword" => self::$passWord,
 150+ "lgname" => self::$user->userName,
 151+ "lgpassword" => self::$user->password,
150152 )
151153 );
152154
@@ -166,8 +168,8 @@
167169 $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
168170 array( "method" => "POST",
169171 "postData" => array(
170 - "lgname" => self::$userName,
171 - "lgpassword" => self::$passWord ) ) );
 172+ "lgname" => self::$user->userName,
 173+ "lgpassword" => self::$user->password ) ) );
172174 $req->execute();
173175
174176 libxml_use_internal_errors( true );
@@ -182,8 +184,8 @@
183185
184186 $req->setData( array(
185187 "lgtoken" => $token,
186 - "lgname" => self::$userName,
187 - "lgpassword" => self::$passWord ) );
 188+ "lgname" => self::$user->userName,
 189+ "lgpassword" => self::$user->password ) );
188190 $req->execute();
189191
190192 $cj = $req->getCookieJar();
@@ -191,7 +193,7 @@
192194 $this->assertNotEquals( false, $serverName );
193195 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
194196 $this->assertNotEquals( '', $serializedCookie );
195 - $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/', $serializedCookie );
 197+ $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$user->userName . '; .*Token=/', $serializedCookie );
196198
197199 return $cj;
198200 }
Index: trunk/phase3/maintenance/tests/phpunit/includes/api/ApiSetup.php
@@ -1,9 +1,8 @@
22 <?php
33
44 abstract class ApiTestSetup extends PHPUnit_Framework_TestCase {
5 - protected static $userName;
6 - protected static $passWord;
75 protected static $user;
 6+ protected static $sysopUser;
87 protected static $apiUrl;
98
109 function setUp() {
@@ -33,22 +32,12 @@
3433 }
3534
3635 static function setupUser() {
37 - if ( self::$user == NULL ) {
38 - self::$userName = "Useruser";
39 - self::$passWord = 'Passpass';
40 -
41 - self::$user = User::newFromName( self::$userName );
42 - if ( !self::$user->getID() ) {
43 - self::$user = User::createNew( self::$userName, array(
44 - "email" => "test@example.com",
45 - "real_name" => "Test User" ) );
46 - }
47 - self::$user->setPassword( self::$passWord );
48 - self::$user->addGroup( 'sysop' );
49 - self::$user->saveSettings();
 36+ if ( self::$user == NULL || self::$sysopUser == NULL ) {
 37+ self::$user = new UserWrapper( 'Useruser', 'Passpass' );
 38+ self::$sysopUser = new UserWrapper( 'Useruser1', 'Passpass1', 'sysop' );
5039 }
5140
52 - $GLOBALS['wgUser'] = self::$user;
 41+ $GLOBALS['wgUser'] = self::$user->user;
5342 }
5443
5544 function tearDown() {
@@ -56,3 +45,25 @@
5746 $wgMemc = null;
5847 }
5948 }
 49+
 50+class UserWrapper {
 51+ var $userName, $password, $user;
 52+
 53+ public function __construct( $userName, $password, $group = '' ) {
 54+ $this->userName = $userName;
 55+ $this->password = $password;
 56+
 57+ $this->user = User::newFromName( $this->userName );
 58+ if ( !$this->user->getID() ) {
 59+ $this->user = User::createNew( $this->userName, array(
 60+ "email" => "test@example.com",
 61+ "real_name" => "Test User" ) );
 62+ }
 63+ $this->user->setPassword( $this->password );
 64+
 65+ if ( $group !== '' ) {
 66+ $this->user->addGroup( $group );
 67+ }
 68+ $this->user->saveSettings();
 69+ }
 70+}
Index: trunk/phase3/maintenance/tests/phpunit/includes/api/ApiWatchTest.php
@@ -11,8 +11,8 @@
1212 function testLogin() {
1313 $data = $this->doApiRequest( array(
1414 'action' => 'login',
15 - 'lgname' => self::$userName,
16 - 'lgpassword' => self::$passWord ) );
 15+ 'lgname' => self::$user->userName,
 16+ 'lgpassword' => self::$user->password ) );
1717
1818 $this->assertArrayHasKey( "login", $data[0] );
1919 $this->assertArrayHasKey( "result", $data[0]['login'] );
@@ -22,8 +22,8 @@
2323 $data = $this->doApiRequest( array(
2424 'action' => 'login',
2525 "lgtoken" => $token,
26 - "lgname" => self::$userName,
27 - "lgpassword" => self::$passWord ), $data );
 26+ "lgname" => self::$user->userName,
 27+ "lgpassword" => self::$user->password ), $data );
2828
2929 $this->assertArrayHasKey( "login", $data[0] );
3030 $this->assertArrayHasKey( "result", $data[0]['login'] );
@@ -163,7 +163,7 @@
164164 $data = $this->doApiRequest( array(
165165 'action' => 'rollback',
166166 'title' => 'Main Page',
167 - 'user' => self::$userName,
 167+ 'user' => self::$user->userName,
168168 'token' => $pageinfo['rollbacktoken'],
169169 'watchlist' => 'watch' ), $data );
170170 } catch( UsageException $ue ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r74223Fiuxp issues from r74213 CR...reedy16:32, 3 October 2010
r75588Add feature to block common (weak) passwords....platonides22:26, 27 October 2010
r75589Do NOT use hardcoded passwords! Not even if the user agreed to run destructiv...platonides22:27, 27 October 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r74117Fixup testApiLoginBadPass per r74113, still failing, it's checking for "NeedT...reedy22:27, 1 October 2010
r74122Revert r74117. Breaks the test. Using an internal request it doesn't fail wit...platonides23:11, 1 October 2010

Comments

#Comment by Nikerabbit (talk | contribs)   16:25, 3 October 2010
+		if ( self::$user == NULL || self::$sysopUser == NULL ) {

Lowercase null is preferred.

+		$this->userName = $userName;
+	    $this->password = $password;

Inconsistent indentation.

+	var $userName, $password, $user;

Var is discouraged. protected should do fine here?

Status & tagging log