diff --git a/src/Http/Requests/MailCareRequest.php b/src/Http/Requests/MailCareRequest.php index fd4405b..313e93a 100644 --- a/src/Http/Requests/MailCareRequest.php +++ b/src/Http/Requests/MailCareRequest.php @@ -8,15 +8,22 @@ class MailCareRequest extends FormRequest { - public function validator() + public function rules() { - return Validator::make($this->all(), [ - 'email' => 'required', + return [ + "content_type" => "required|in:message/rfc2822", + ]; + } + + public function prepareForValidation() + { + $this->merge([ + "content_type" => $this->headers->get("Content-type"), ]); } public function email() { - return InboundEmail::fromMessage($this->get('email')); + return InboundEmail::fromMessage($this->getContent()); } } diff --git a/tests/Controllers/MailCareTest.php b/tests/Controllers/MailCareTest.php new file mode 100644 index 0000000..62b3f70 --- /dev/null +++ b/tests/Controllers/MailCareTest.php @@ -0,0 +1,51 @@ +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->callWithEmail('POST','/laravel-mailbox/mailcare',$message) + ->assertStatus(200); + } + + private function callWithEmail(string $method, string $url, Email $message): TestResponse + { + $server = $this->transformHeadersToServerVars([ + "Content-Type" => 'message/rfc2822' + ]); + return $this->call($method, $url,[],[],[], $server, $message->toString()); + } +} diff --git a/tests/Controllers/MailgunTest.php b/tests/Controllers/MailgunTest.php index af10733..d3490d6 100644 --- a/tests/Controllers/MailgunTest.php +++ b/tests/Controllers/MailgunTest.php @@ -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 { @@ -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'; @@ -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); + ]); } + }