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

Commit

Permalink
Merge pull request #5 from joelharkes/fix/mailcare_requeust
Browse files Browse the repository at this point in the history
Fix mailcare Raw message/rfc2822 request
  • Loading branch information
joelharkes authored Mar 24, 2023
2 parents 07844bb + a55122d commit 2278134
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 12 deletions.
15 changes: 11 additions & 4 deletions src/Http/Requests/MailCareRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
51 changes: 51 additions & 0 deletions tests/Controllers/MailCareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);

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 MailCareTest extends TestCase
{
protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app['config']['mailbox.driver'] = 'mailcare';
$app['config']['mailbox.basic_auth.username'] = null;
}

/**
* @test
*/
public function it_accepts_raw_email_requests(): void
{

$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->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());
}
}
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 2278134

Please sign in to comment.