Added new OptInHandler and tests.

This commit is contained in:
Travis Swientek 2013-08-16 11:20:01 -07:00
parent afef8b4c88
commit 07837600e6
3 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,34 @@
<?PHP
namespace Mailgun\Lists;
use Mailgun\Messages\Expcetions\InvalidParameter;
use Mailgun\Messages\Exceptions\TooManyParameters;
use Mailgun\Messages\Expcetions\InvalidParameterType;
/*
This class is used for creating a unique hash for
mailing list subscription double-opt in requests.
*/
class OptInHandler{
function __construct(){
}
public function generateHash($mailingList, $secretAppId, $recipientAddress){
$concatStrings = $secretAppId . "" . $recipientAddress;
return urlencode(base64_encode(json_encode(array('s' => hash('md5', $concatStrings), 'l' => $mailingList, 'r' => $recipientAddress))));
}
public function validateHash($secretAppId, $uniqueHash){
$urlParameters = json_decode(base64_decode(urldecode($uniqueHash)));
$concatStrings = $secretAppId . "" . $urlParameters->r;
if($urlParameters->s == hash('md5', $concatStrings)){
return true;
}
return false;
}
}

View File

@ -8,6 +8,7 @@ use Mailgun\Messages\Messages;
use Mailgun\Connection\Exceptions; use Mailgun\Connection\Exceptions;
use Mailgun\Connection\RestClient; use Mailgun\Connection\RestClient;
use Mailgun\Messages\BatchMessage; use Mailgun\Messages\BatchMessage;
use Mailgun\Lists\OptInHandler;
use Mailgun\Messages\MessageBuilder; use Mailgun\Messages\MessageBuilder;
/* /*
@ -75,6 +76,10 @@ class Mailgun{
return new MessageBuilder(); return new MessageBuilder();
} }
public function OptInHandler(){
return new OptInHandler();
}
public function BatchMessage($workingDomain, $autoSend = true){ public function BatchMessage($workingDomain, $autoSend = true){
return new BatchMessage($this->restClient, $workingDomain, $autoSend); return new BatchMessage($this->restClient, $workingDomain, $autoSend);
} }

View File

@ -0,0 +1,32 @@
<?PHP
namespace Mailgun\Tests\Lists;
use Mailgun\Tests\MailgunTest;
class OptInHandler extends \Mailgun\Tests\MailgunTestCase{
private $client;
private $sampleDomain = "samples.mailgun.org";
private $optInHandler;
public function setUp(){
$this->client = new MailgunTest("My-Super-Awesome-API-Key");
$this->optInHandler = $this->client->OptInHandler();
}
public function testReturnOfGenerateHash(){
$generatedHash = $this->optInHandler->generateHash('mytestlist@example.com', 'mysupersecretappid', 'testrecipient@example.com');
$knownHash = "eyJzIjoiOGM2NmVmYzYwNzhmNGVkYjFkZGJiY2RhM2M2MmMzMTQiLCJsIjoibXl0ZXN0bGlzdEBleGFtcGxlLmNvbSIsInIiOiJ0ZXN0cmVjaXBpZW50QGV4YW1wbGUuY29tIn0%3D";
$this->assertEquals($generatedHash, $knownHash);
}
public function testGoodHash(){
$validation = $this->optInHandler->validateHash('mysupersecretappid', 'eyJzIjoiOGM2NmVmYzYwNzhmNGVkYjFkZGJiY2RhM2M2MmMzMTQiLCJsIjoibXl0ZXN0bGlzdEBleGFtcGxlLmNvbSIsInIiOiJ0ZXN0cmVjaXBpZW50QGV4YW1wbGUuY29tIn0%3D');
$this->assertTrue($validation);
}
public function testBadHash(){
$validation = $this->optInHandler->validateHash('mybadsecretappid', 'eyJzIjoiOGM2NmVmYzYwNzhmNGVkYjFkZGJiY2RhM2M2MmMzMTQiLCJsIjoibXl0ZXN0bGlzdEBleGFtcGxlLmNvbSIsInIiOiJ0ZXN0cmVjaXBpZW50QGV4YW1wbGUuY29tIn0%3D');
$this->assertFalse($validation);
}
}
?>