Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Also add complete test to mailgun
Browse files Browse the repository at this point in the history
  • Loading branch information
joelharkes committed Mar 24, 2023
1 parent a0026f7 commit a55122d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
3 changes: 2 additions & 1 deletion tests/Controllers/MailCareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use BeyondCode\Mailbox\Facades\Mailbox;
use BeyondCode\Mailbox\InboundEmail;
use BeyondCode\Mailbox\Tests\TestCase;
use Illuminate\Testing\TestResponse;
use Symfony\Component\Mime\Email;

class MailCareTest extends TestCase
Expand Down Expand Up @@ -40,7 +41,7 @@ public function it_accepts_raw_email_requests(): void
->assertStatus(200);
}

private function callWithEmail(string $method, string $url, Email $message): \Illuminate\Testing\TestResponse
private function callWithEmail(string $method, string $url, Email $message): TestResponse
{
$server = $this->transformHeadersToServerVars([
"Content-Type" => 'message/rfc2822'
Expand Down
52 changes: 44 additions & 8 deletions tests/Controllers/MailgunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace BeyondCode\Mailbox\Tests\Controllers;

use BeyondCode\Mailbox\Facades\Mailbox;
use BeyondCode\Mailbox\InboundEmail;
use BeyondCode\Mailbox\Tests\TestCase;
use Illuminate\Testing\TestResponse;
use Symfony\Component\Mime\Email;

class MailgunTest extends TestCase
{
Expand All @@ -23,7 +27,15 @@ public function it_verifies_mailgun_signatures()
'signature' => 'something',
])->assertStatus(401);

$timestamp = time();

$this->callWithValidToken('mime')
->assertStatus(200);
}

/** @test */
public function it_verifies_fresh_timestamps()
{
$timestamp = now()->subMinutes(5)->timestamp;
$token = uniqid();

$this->app['config']['mailbox.services.mailgun.key'] = '12345';
Expand All @@ -35,24 +47,48 @@ public function it_verifies_mailgun_signatures()
'timestamp' => $timestamp,
'token' => $token,
'signature' => $validSignature,
])->assertStatus(200);
])->assertStatus(401);
}

/** @test */
public function it_verifies_fresh_timestamps()

/**
* @test
*/
public function it_processes_mails_correctly()
{
$timestamp = now()->subMinutes(5)->timestamp;
$message = new Email();
$message->subject("subject");
$message->from("from@example.com");
$message->to("to@example.com");
$message->text("this is body text");

Mailbox::shouldReceive("callMailboxes", function(InboundEmail $email){
return $email->subject() === 'this is body text'
&& $email->from() === 'from@example.com'
&& $email->to()[0]->getEmail() === 'to@example.com'
&& $email->body() === 'this is body text';
});

$this->callWithValidToken($message->toString())
->assertStatus(200);
}


private function callWithValidToken($mimeMail = 'mime'): TestResponse
{
$timestamp = time();
$token = uniqid();

$this->app['config']['mailbox.services.mailgun.key'] = '12345';

$validSignature = hash_hmac('sha256', $timestamp.$token, '12345');

$this->post('/laravel-mailbox/mailgun/mime', [
'body-mime' => 'mime',
return $this->post('/laravel-mailbox/mailgun/mime', [
'body-mime' => $mimeMail,
'timestamp' => $timestamp,
'token' => $token,
'signature' => $validSignature,
])->assertStatus(401);
]);
}

}

0 comments on commit a55122d

Please sign in to comment.