Merge "Speed up password-handling in the unit tests"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 18 May 2016 12:46:28 +0000 (12:46 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 18 May 2016 12:46:28 +0000 (12:46 +0000)
tests/phpunit/MediaWikiTestCase.php
tests/phpunit/includes/TestUser.php
tests/phpunit/includes/api/ApiLoginTest.php
tests/phpunit/includes/session/BotPasswordSessionProviderTest.php
tests/phpunit/includes/user/BotPasswordTest.php

index 9f3aa11..c242923 100644 (file)
@@ -221,6 +221,9 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $defaultOverrides->set( 'ObjectCaches', $objectCaches );
                $defaultOverrides->set( 'MainCacheType', CACHE_NONE );
 
+               // Use a fast hash algorithm to hash passwords.
+               $defaultOverrides->set( 'PasswordDefault', 'A' );
+
                $testConfig = $customOverrides
                        ? new MultiConfig( [ $customOverrides, $defaultOverrides, $baseConfig ] )
                        : new MultiConfig( [ $defaultOverrides, $baseConfig ] );
index 142c77f..13bfa46 100644 (file)
@@ -129,14 +129,16 @@ class TestUser {
                        throw new MWException( "Passed User has not been added to the database yet!" );
                }
 
+               if ( $user->checkPassword( $password ) === true ) {
+                       return;  // Nothing to do.
+               }
+
                $passwordFactory = new PasswordFactory();
                $passwordFactory->init( RequestContext::getMain()->getConfig() );
-               // A is unsalted MD5 (thus fast) ... we don't care about security here, this is test only
-               $passwordFactory->setDefaultType( 'A' );
-               $pwhash = $passwordFactory->newFromPlaintext( $password );
+               $passwordHash = $passwordFactory->newFromPlaintext( $password );
                wfGetDB( DB_MASTER )->update(
                        'user',
-                       [ 'user_password' => $pwhash->toString() ],
+                       [ 'user_password' => $passwordHash->toString() ],
                        [ 'user_id' => $user->getId() ],
                        __METHOD__
                );
index e9afd45..2f8ffcc 100644 (file)
@@ -228,8 +228,7 @@ class ApiLoginTest extends ApiTestCase {
                $passwordFactory = new PasswordFactory();
                $passwordFactory->init( RequestContext::getMain()->getConfig() );
                // A is unsalted MD5 (thus fast) ... we don't care about security here, this is test only
-               $passwordFactory->setDefaultType( 'A' );
-               $pwhash = $passwordFactory->newFromPlaintext( 'foobaz' );
+               $passwordHash = $passwordFactory->newFromPlaintext( 'foobaz' );
 
                $dbw = wfGetDB( DB_MASTER );
                $dbw->insert(
@@ -237,7 +236,7 @@ class ApiLoginTest extends ApiTestCase {
                        [
                                'bp_user' => $centralId,
                                'bp_app_id' => 'foo',
-                               'bp_password' => $pwhash->toString(),
+                               'bp_password' => $passwordHash->toString(),
                                'bp_token' => '',
                                'bp_restrictions' => MWRestrictions::newDefault()->toJson(),
                                'bp_grants' => '["test"]',
index edab0dc..d4b1587 100644 (file)
@@ -65,9 +65,7 @@ class BotPasswordSessionProviderTest extends MediaWikiTestCase {
        public function addDBDataOnce() {
                $passwordFactory = new \PasswordFactory();
                $passwordFactory->init( \RequestContext::getMain()->getConfig() );
-               // A is unsalted MD5 (thus fast) ... we don't care about security here, this is test only
-               $passwordFactory->setDefaultType( 'A' );
-               $pwhash = $passwordFactory->newFromPlaintext( 'foobaz' );
+               $passwordHash = $passwordFactory->newFromPlaintext( 'foobaz' );
 
                $userId = \CentralIdLookup::factory( 'local' )->centralIdFromName( 'UTSysop' );
 
@@ -82,7 +80,7 @@ class BotPasswordSessionProviderTest extends MediaWikiTestCase {
                        [
                                'bp_user' => $userId,
                                'bp_app_id' => 'BotPasswordSessionProvider',
-                               'bp_password' => $pwhash->toString(),
+                               'bp_password' => $passwordHash->toString(),
                                'bp_token' => 'token!',
                                'bp_restrictions' => '{"IPAddresses":["127.0.0.0/8"]}',
                                'bp_grants' => '["test"]',
index 27ce287..629c6e5 100644 (file)
@@ -49,9 +49,7 @@ class BotPasswordTest extends MediaWikiTestCase {
        public function addDBData() {
                $passwordFactory = new \PasswordFactory();
                $passwordFactory->init( \RequestContext::getMain()->getConfig() );
-               // A is unsalted MD5 (thus fast) ... we don't care about security here, this is test only
-               $passwordFactory->setDefaultType( 'A' );
-               $pwhash = $passwordFactory->newFromPlaintext( 'foobaz' );
+               $passwordHash = $passwordFactory->newFromPlaintext( 'foobaz' );
 
                $dbw = wfGetDB( DB_MASTER );
                $dbw->delete(
@@ -65,7 +63,7 @@ class BotPasswordTest extends MediaWikiTestCase {
                                [
                                        'bp_user' => 42,
                                        'bp_app_id' => 'BotPassword',
-                                       'bp_password' => $pwhash->toString(),
+                                       'bp_password' => $passwordHash->toString(),
                                        'bp_token' => 'token!',
                                        'bp_restrictions' => '{"IPAddresses":["127.0.0.0/8"]}',
                                        'bp_grants' => '["test"]',
@@ -73,7 +71,7 @@ class BotPasswordTest extends MediaWikiTestCase {
                                [
                                        'bp_user' => 43,
                                        'bp_app_id' => 'BotPassword',
-                                       'bp_password' => $pwhash->toString(),
+                                       'bp_password' => $passwordHash->toString(),
                                        'bp_token' => 'token!',
                                        'bp_restrictions' => '{"IPAddresses":["127.0.0.0/8"]}',
                                        'bp_grants' => '["test"]',
@@ -311,8 +309,6 @@ class BotPasswordTest extends MediaWikiTestCase {
        public function testSave( $password ) {
                $passwordFactory = new \PasswordFactory();
                $passwordFactory->init( \RequestContext::getMain()->getConfig() );
-               // A is unsalted MD5 (thus fast) ... we don't care about security here, this is test only
-               $passwordFactory->setDefaultType( 'A' );
 
                $bp = BotPassword::newUnsaved( [
                        'centralId' => 42,
@@ -325,9 +321,9 @@ class BotPasswordTest extends MediaWikiTestCase {
                        BotPassword::newFromCentralId( 42, 'TestSave', BotPassword::READ_LATEST ), 'sanity check'
                );
 
-               $pwhash = $password ? $passwordFactory->newFromPlaintext( $password ) : null;
-               $this->assertFalse( $bp->save( 'update', $pwhash ) );
-               $this->assertTrue( $bp->save( 'insert', $pwhash ) );
+               $passwordHash = $password ? $passwordFactory->newFromPlaintext( $password ) : null;
+               $this->assertFalse( $bp->save( 'update', $passwordHash ) );
+               $this->assertTrue( $bp->save( 'insert', $passwordHash ) );
                $bp2 = BotPassword::newFromCentralId( 42, 'TestSave', BotPassword::READ_LATEST );
                $this->assertInstanceOf( 'BotPassword', $bp2 );
                $this->assertEquals( $bp->getUserCentralId(), $bp2->getUserCentralId() );
@@ -356,9 +352,9 @@ class BotPasswordTest extends MediaWikiTestCase {
                        $this->assertTrue( $pw->equals( $password ) );
                }
 
-               $pwhash = $passwordFactory->newFromPlaintext( 'XXX' );
+               $passwordHash = $passwordFactory->newFromPlaintext( 'XXX' );
                $token = $bp->getToken();
-               $this->assertTrue( $bp->save( 'update', $pwhash ) );
+               $this->assertTrue( $bp->save( 'update', $passwordHash ) );
                $this->assertNotEquals( $token, $bp->getToken() );
                $pw = TestingAccessWrapper::newFromObject( $bp )->getPassword();
                $this->assertTrue( $pw->equals( 'XXX' ) );