jenkins-bot has submitted this change and it was merged.

Change subject: Route bounce job to the proper wiki queues
......................................................................


Route bounce job to the proper wiki queues

* This makes the calls to the User class methods work properly
* Updated Tests - Fixed.
* Edited the test bounce email to use a dynamically created VERP address for 
jenkins/tests
* Added a new bounce with static conditions.

Change-Id: I9f6881d7adb6471ac4fb79b7d0e099f4f8560342
---
M includes/ApiBounceHandler.php
M includes/ProcessBounceEmails.php
M tests/ApiBounceHandlerTest.php
M tests/ProcessBounceWithRegexTest.php
M tests/bounce_emails/email1
A tests/bounce_emails/email2
6 files changed, 132 insertions(+), 28 deletions(-)

Approvals:
  01tonythomas: Looks good to me, but someone else must approve
  Legoktm: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/ApiBounceHandler.php b/includes/ApiBounceHandler.php
index 403058e..b3bcbe6 100644
--- a/includes/ApiBounceHandler.php
+++ b/includes/ApiBounceHandler.php
@@ -6,10 +6,11 @@
 class ApiBounceHandler extends ApiBase {
        public function execute() {
                global $wgBounceHandlerInternalIPs;
+
                $requestIP = $this->getRequest()->getIP();
                $inRangeIP = false;
-               foreach( $wgBounceHandlerInternalIPs as 
$BounceHandlerInternalIPs ) {
-                       if ( IP::isInRange( $requestIP, 
$BounceHandlerInternalIPs ) ) {
+               foreach( $wgBounceHandlerInternalIPs as $internalIP ) {
+                       if ( IP::isInRange( $requestIP, $internalIP ) ) {
                                $inRangeIP = true;
                                break;
                        }
@@ -21,15 +22,31 @@
 
                $params = $this->extractRequestParams();
 
-               $title = Title::newFromText( 'BounceHandler Job' );
-               $job = new BounceHandlerJob( $title, $params );
-               JobQueueGroup::singleton()->push( $job );
+               // Extract the wiki ID from the Verp address (also verifies the 
hash)
+               $bounceProcessor = new ProcessBounceWithRegex();
+               $emailHeaders = $bounceProcessor->extractHeaders( 
$params['email'] );
+               $to = isset( $emailHeaders['to'] ) ? $emailHeaders['to'] : '';
+               $failedUser = strlen( $to ) ? $bounceProcessor->getUserDetails( 
$to ) : array();
 
-               $this->getResult()->addValue(
-                       null,
-                       $this->getModuleName(),
-                       array ( 'submitted' => 'job' )
-               );
+               // Route the job to the wiki that the email was sent from.
+               // This way it can easily unconfirm the user's email using the 
User methods.
+               if ( isset( $failedUser['wikiId'] ) ) {
+                       $title = Title::newFromText( 'BounceHandler Job' );
+                       $job = new BounceHandlerJob( $title, $params );
+                       JobQueueGroup::singleton( $failedUser['wikiId'] 
)->push( $job );
+
+                       $this->getResult()->addValue(
+                               null,
+                               $this->getModuleName(),
+                               array ( 'submitted' => 'job' )
+                       );
+               } else {
+                       $this->getResult()->addValue(
+                               null,
+                               $this->getModuleName(),
+                               array ( 'submitted' => 'failure' )
+                       );
+               }
 
                return true;
        }
diff --git a/includes/ProcessBounceEmails.php b/includes/ProcessBounceEmails.php
index 3538466..d52ed91 100644
--- a/includes/ProcessBounceEmails.php
+++ b/includes/ProcessBounceEmails.php
@@ -84,8 +84,10 @@
         * */
        public function getUserDetails( $hashedEmail ) {
                global $wgVERPalgorithm, $wgVERPsecret, $wgVERPAcceptTime;
-               $currentTime = wfTimestamp();
+
                $failedUser = array();
+
+               $currentTime = wfTimestamp();
                preg_match( '~(.*?)@~', $hashedEmail, $hashedPart );
                $hashedVERPPart = explode( '-', $hashedPart[1] );
                $hashedData = $hashedVERPPart[0]. '-'. $hashedVERPPart[1]. '-'. 
$hashedVERPPart[2]. '-'. $hashedVERPPart[3];
@@ -104,6 +106,7 @@
                                "Error: Hash validation failed. Expected hash 
of $hashedData, got $hashedVERPPart[3]."
                        );
                }
+
                return $failedUser;
        }
 
diff --git a/tests/ApiBounceHandlerTest.php b/tests/ApiBounceHandlerTest.php
index 5a96345..94da074 100644
--- a/tests/ApiBounceHandlerTest.php
+++ b/tests/ApiBounceHandlerTest.php
@@ -1,31 +1,67 @@
 <?php
 /**
  * Tests for API module
+ *
  * @group API
  * @group medium
+ * @group Database
  */
 class ApiBounceHandlerTest extends ApiTestCase {
 
-       /**
-        * @var string
-        */
-       static $bounceEmail = "This is a test email";
 
        function setUp() {
                parent::setUp();
                $this->doLogin( 'sysop' );
        }
 
+       public static function provideBounceEmails() {
+               $email = file_get_contents( __DIR__ .'/bounce_emails/email1' );
+               return array (
+                       array ( $email )
+               );
+       }
+
        /**
-        * Tests API request from an allowed IP
-        *
-        *
+        * @dataProvider provideBounceEmails
+        * @param $email
         */
-       function testBounceHandlerWithGoodIPPasses() {
-               $this->setMwGlobals( 'wgBounceHandlerInternalIPs', array( 
'127.0.0.1' ) );
+       function testBounceHandlerWithGoodIPPasses( $email ) {
+
+               $user = User::newFromName( 'TestUser' );
+               $user->setEmail( '[email protected]' );
+               $user->addToDatabase();
+
+               $uid = $user->getId();
+
+               $prefix = 'wiki';
+               $algorithm = 'md5';
+               $secretKey = 'mySecret';
+               $domain = 'testwiki.org';
+               $bounceRecordPeriod = 604800;
+               $bounceRecordLimit = 3;
+
+               $this->setMwGlobals(
+                       array(
+                               'wgVERPprefix' => $prefix,
+                               'wgVERPalgorithm' => $algorithm,
+                               'wgVERPsecret' => $secretKey,
+                               'wgVERPdomainPart' => $domain,
+                               'wgBounceHandlerUnconfirmUsers' => true,
+                               'wgBounceRecordPeriod' => $bounceRecordPeriod,
+                               'wgBounceRecordLimit' => $bounceRecordLimit,
+                               'wgBounceHandlerInternalIPs'=> array( 
'127.0.0.1' )
+                       )
+               );
+
+               $encodeVERP = new VerpAddressGenerator( $prefix, $algorithm, 
$secretKey, $domain );
+               $encodedAddress = $encodeVERP->generateVERP( $uid );
+
+               $replace = array( "{VERP_ADDRESS}" => $encodedAddress );
+               $email = strtr( $email, $replace );
+
                list( $apiResult ) = $this->doApiRequest( array(
                        'action' => 'bouncehandler',
-                       'email' => self::$bounceEmail
+                       'email' => $email
                ) );
 
                $this->assertEquals( 'job', 
$apiResult['bouncehandler']['submitted'] );
@@ -34,14 +70,16 @@
        /**
         * Tests API request from an unknown IP
         *
+        * @dataProvider provideBounceEmails
+        * @param $email
         * @expectedException UsageException
         * @expectedExceptionMessage This API module is for internal use only.
         */
-       function testBounceHandlerWithBadIPPasses() {
+       function testBounceHandlerWithBadIPPasses( $email ) {
                $this->setMwGlobals( 'wgBounceHandlerInternalIPs', array( 
'111.111.111.111' ) );
                $this->doApiRequest( array(
                        'action' => 'bouncehandler',
-                       'email' => self::$bounceEmail
+                       'email' => $email
                ) );
        }
 
@@ -63,14 +101,16 @@
        /**
         * Tests API with Wrong params
         *
+        * @dataProvider provideBounceEmails
+        * @param $email
         * @expectedException UsageException
         * @expectedException The email parameter must be set
         */
-       function testBounceHandlerWithWrongParams() {
+       function testBounceHandlerWithWrongParams( $email ) {
                $this->setMwGlobals( 'wgBounceHandlerInternalIPs', array( 
'127.0.0.1' ) );
                $this->doApiRequest( array(
                        'action' => 'bouncehandler',
-                       'foo' => self::$bounceEmail
+                       'foo' => $email
                ) );
        }
 
diff --git a/tests/ProcessBounceWithRegexTest.php 
b/tests/ProcessBounceWithRegexTest.php
index 3af3ff1..032ac09 100644
--- a/tests/ProcessBounceWithRegexTest.php
+++ b/tests/ProcessBounceWithRegexTest.php
@@ -5,7 +5,7 @@
        }
 
        public static function provideBounceEmails() {
-               $email = file_get_contents( __DIR__ .'/bounce_emails/email1' );
+               $email = file_get_contents( __DIR__ .'/bounce_emails/email2' );
                return array (
                        array ( $email )
                );
diff --git a/tests/bounce_emails/email1 b/tests/bounce_emails/email1
index 88325f7..0bd3e0d 100755
--- a/tests/bounce_emails/email1
+++ b/tests/bounce_emails/email1
@@ -7,7 +7,7 @@
 X-Failed-Recipients: [email protected]
 Auto-Submitted: auto-replied
 From: Mail Delivery System <[email protected]>
-To: my_wiki.wiki_-1-750h18h52-d3435fa90ccfd281874315d26c2f4889@localhost
+To: {VERP_ADDRESS}
 Subject: Mail delivery failed: returning message to sender
 Message-Id: <E1Wy2bH-0002q0-8l@router-ultra>
 Date: Fri, 20 Jun 2014 22:57:43 +0530
@@ -36,7 +36,7 @@
 MIME-Version: 1.0
 Content-type: text/plain; charset=UTF-8
 Content-transfer-encoding: 8bit
-From: Bob 
<my_wiki.wiki_-1-750h18h52-d3435fa90ccfd281874315d26c2f4889@localhost>
+From: Bob <[email protected]>
 
 Hi
 
diff --git a/tests/bounce_emails/email2 b/tests/bounce_emails/email2
new file mode 100644
index 0000000..f8b4912
--- /dev/null
+++ b/tests/bounce_emails/email2
@@ -0,0 +1,44 @@
+Return-path: <>
+Envelope-to: [email protected]
+Delivery-date: Fri, 20 Jun 2014 22:57:43 +0530
+Received: from Debian-exim by router-ultra with local (Exim 4.80)
+       id 1Wy2bH-0002q0-8l
+       for [email protected]; Fri, 20 Jun 2014 22:57:43 +0530
+X-Failed-Recipients: [email protected]
+Auto-Submitted: auto-replied
+From: Mail Delivery System <[email protected]>
+To: [email protected]
+Subject: Mail delivery failed: returning message to sender
+Message-Id: <E1Wy2bH-0002q0-8l@router-ultra>
+Date: Fri, 20 Jun 2014 22:57:43 +0530
+
+This message was created automatically by mail delivery software.
+
+A message that you sent could not be delivered to one or more of its
+recipients. This is a permanent error. The following address(es) failed:
+
+  [email protected]
+    Mailing to remote domains not supported
+
+------ This is a copy of the message, including all the headers. ------
+
+Return-path: <[email protected]>
+Received: from www-data by router-ultra with local (Exim 4.80)
+       (envelope-from <[email protected]>)
+       id 1Wy2bG-0002pw-RR
+       for [email protected]; Fri, 20 Jun 2014 22:57:42 +0530
+To: Bob <[email protected]>
+Subject: My Test email from user "Bob"
+X-PHP-Originating-Script: 1000:UserMailer.php
+Date: Fri, 20 Jun 2014 22:57:42 +0530
+Message-ID: <my_wiki-wiki_.53a46f0eaf55d8.84359228@localhost>
+X-Mailer: MediaWiki mailer
+MIME-Version: 1.0
+Content-type: text/plain; charset=UTF-8
+Content-transfer-encoding: 8bit
+From: Bob <[email protected]>
+
+Hi
+
+--
+This email was sent by Bob to Bob by the "Email user" function at My Test.

-- 
To view, visit https://gerrit.wikimedia.org/r/171455
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I9f6881d7adb6471ac4fb79b7d0e099f4f8560342
Gerrit-PatchSet: 15
Gerrit-Project: mediawiki/extensions/BounceHandler
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <[email protected]>
Gerrit-Reviewer: 01tonythomas <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Jgreen <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to