Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
olieady committed Oct 20, 2024
1 parent a6c25f5 commit 68295f0
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 71 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ use Illuminate\Support\Facades\Mail;

### Batching Mail Notifications

You can add batching to your notifcations using the mail channel. Instead of using `MailMessage` you need to use `OpeTech\LaravelSes\Notifications\MailMessageWithBatching`
You can add batching to your notiflacations using the mail channel. Instead of using `MailMessage` you need to use `OpeTech\LaravelSes\Notifications\MailMessageWithBatching`

```php
public function toMail(object $notifiable)
Expand Down
11 changes: 10 additions & 1 deletion src/Http/Controllers/Notifications/NotificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@ class NotificationController extends Controller
public function notification(Request $request)
{


$content = json_decode($request->getContent(), true);

if ($content['Type'] == 'Notification') {
$content['Message'] = json_decode($content['Message'], true);
$content['Message'] = json_decode($content['Message'], true) ?? $content['Message'];
}


$snsMessage = new Message($content);


if ($snsMessage['Message'] == 'Successfully validated SNS topic for Amazon SES event publishing.') {
return response()->json([
'message' => 'Success',
]);
}

if ($snsMessage['Type'] == 'SubscriptionConfirmation') {
return $this->confirmSubscription($snsMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
->assertExitCode(0);
});

it('creates SNS topic and subscribes to the HTTP endpoint for receiveing notifications', function () {
it('creates SNS topic and subscribes to the HTTP endpoint for receiving notifications', function () {
$sesMock = Mockery::mock(SesV2Client::class);
$snsMock = Mockery::mock(SnsClient::class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use OpeTech\LaravelSes\Models\LaravelSesEmailOpen;
use OpeTech\LaravelSes\Models\LaravelSesEmailReject;
use OpeTech\LaravelSes\Models\LaravelSesSentEmail;

use function Pest\Laravel\withHeaders;

describe('persisting notifications', function () {
Expand All @@ -24,98 +23,98 @@
postNotification(SesEvents::Reject);

expect(LaravelSesEmailReject::count())
->toBe(1);
->toBe(1)
->and(LaravelSesEmailReject::first())->toMatchArray([
'message_id' => 'EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000',
'reason' => 'Bad content',
]);

expect(LaravelSesEmailReject::first())->toMatchArray([
'message_id' => 'EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000',
'reason' => 'Bad content',
]);
});

it('persists the click in the database', function () {
postNotification(SesEvents::Click);

expect(LaravelSesEmailClick::count())
->toBe(1);

expect(LaravelSesEmailClick::first())->toMatchArray([
'clicked_at' => '2017-08-09T23:51:25.000000Z',
'link' => 'http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-email-smtp.html',
'sns_raw_data' => null,
'message_id' => 'EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000',
'link_tags' => [
'samplekey0' => ['samplevalue0'],
'samplekey1' => ['samplevalue1'],
],
]);
->toBe(1)
->and(LaravelSesEmailClick::first())->toMatchArray([
'clicked_at' => '2017-08-09T23:51:25.000000Z',
'link' => 'http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-email-smtp.html',
'sns_raw_data' => null,
'message_id' => 'EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000',
'link_tags' => [
'samplekey0' => ['samplevalue0'],
'samplekey1' => ['samplevalue1'],
],
]);

});

it('persists the bounce in the database', function () {
postNotification(SesEvents::Bounce);

expect(LaravelSesEmailBounce::count())
->toBe(1);

expect(LaravelSesEmailBounce::first())->toMatchArray([
'bounced_at' => '2016-01-27T14:59:38.000000Z',
'type' => 'Permanent',
'sns_raw_data' => null,
'message_id' => '00000138111222aa-33322211-cccc-cccc-cccc-ddddaaaa0680-000000',
]);
->toBe(1)
->and(LaravelSesEmailBounce::first())->toMatchArray([
'bounced_at' => '2016-01-27T14:59:38.000000Z',
'type' => 'Permanent',
'sns_raw_data' => null,
'message_id' => '00000138111222aa-33322211-cccc-cccc-cccc-ddddaaaa0680-000000',
]);

});

it('persists the complaint to the database', function () {
postNotification(SesEvents::Complaint);

expect(LaravelSesEmailComplaint::count())
->toBe(1);

expect(LaravelSesEmailComplaint::first())->toMatchArray([
'complained_at' => '2017-08-05T00:41:02.000000Z',
'type' => 'abuse',
'sns_raw_data' => null,
'message_id' => 'EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000',
]);
->toBe(1)
->and(LaravelSesEmailComplaint::first())->toMatchArray([
'complained_at' => '2017-08-05T00:41:02.000000Z',
'type' => 'abuse',
'sns_raw_data' => null,
'message_id' => 'EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000',
]);

});

it('persists the complaint with sub type to the database', function () {
postNotification(sesEvent: SesEvents::Complaint, variation: 'WithComplaintSubType');

expect(LaravelSesEmailComplaint::count())
->toBe(1);

expect(LaravelSesEmailComplaint::first())->toMatchArray([
'complained_at' => '2017-08-05T00:41:02.000000Z',
'type' => 'OnAccountSuppressionList',
'sns_raw_data' => null,
'message_id' => 'EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000',
]);
->toBe(1)
->and(LaravelSesEmailComplaint::first())->toMatchArray([
'complained_at' => '2017-08-05T00:41:02.000000Z',
'type' => 'OnAccountSuppressionList',
'sns_raw_data' => null,
'message_id' => 'EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000',
]);

});

it('persists the open to the database', function () {
postNotification(SesEvents::Open);

expect(LaravelSesEmailOpen::count())
->toBe(1);
->toBe(1)
->and(LaravelSesEmailOpen::first())->toMatchArray([
'opened_at' => '2017-08-09T22:00:19.000000Z',
'sns_raw_data' => null,
'message_id' => 'EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000',
]);

expect(LaravelSesEmailOpen::first())->toMatchArray([
'opened_at' => '2017-08-09T22:00:19.000000Z',
'sns_raw_data' => null,
'message_id' => 'EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000',
]);
});

it('persists the delivery to the database', function () {
postNotification(SesEvents::Delivery);

expect(LaravelSesEmailDelivery::count())
->toBe(1);
->toBe(1)
->and(LaravelSesEmailDelivery::first())->toMatchArray([
'delivered_at' => '2016-10-19T23:21:04.000000Z',
'sns_raw_data' => null,
'message_id' => 'EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000',
]);

expect(LaravelSesEmailDelivery::first())->toMatchArray([
'delivered_at' => '2016-10-19T23:21:04.000000Z',
'sns_raw_data' => null,
'message_id' => 'EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000',
]);
});

it('persist action is dispatched on a custom queue if the setting is enabled', function (SesEvents $sesEvent) {
Expand All @@ -125,7 +124,7 @@

postNotification($sesEvent);

$action = 'OpeTech\LaravelSes\Actions\SesEvents\Persist'.Str::ucfirst($sesEvent->value).'Notification';
$action = 'OpeTech\LaravelSes\Actions\SesEvents\Persist' . Str::ucfirst($sesEvent->value) . 'Notification';

$action::assertPushedOn('custom-queue');

Expand All @@ -138,7 +137,7 @@
postNotification($sesEvent);

Queue::assertPushed(JobDecorator::class, function ($job) use ($sesEvent) {
$action = 'OpeTech\LaravelSes\Actions\SesEvents\Persist'.Str::ucfirst($sesEvent->value).'Notification';
$action = 'OpeTech\LaravelSes\Actions\SesEvents\Persist' . Str::ucfirst($sesEvent->value) . 'Notification';

return $job->connection == 'sync'
&& $job->queue == null
Expand All @@ -163,9 +162,9 @@

it('persists raw sns message when the config option is on', function (SesEvents $sesEvent) {

$configKey = 'laravelses.log_raw_data.'.Str::plural(Str::lower($sesEvent->value));
$configKey = 'laravelses.log_raw_data.' . Str::plural(Str::lower($sesEvent->value));

$model = 'OpeTech\LaravelSes\Models\LaravelSesEmail'.ucfirst($sesEvent->value);
$model = 'OpeTech\LaravelSes\Models\LaravelSesEmail' . ucfirst($sesEvent->value);

config([$configKey => true]);

Expand All @@ -180,12 +179,12 @@
$messageId = getMessageIdFromSesEvent($sesEvent);

//uses a different id for the sent message id.
expect(fn () => postNotification(
expect(fn() => postNotification(
sesEvent: $sesEvent,
messageId: '00000138111222aa-33322211-cccc-cccc-cccc-ddddaaaa0680404'
))->toThrow(
LaravelSesSentEmailNotFoundException::class,
'Sent Email with message id: '.$messageId
'Sent Email with message id: ' . $messageId
);
})->with(SesEvents::cases());

Expand All @@ -201,25 +200,53 @@
it('returns the correct response', function () {

expect($this->response->status())
->toBe(200);

expect($this->response->json())
->toBe(200)
->and($this->response->json())
->message
->toBe('Subscription Confirmed.');

});

it('hits the subscription confirmation endpoint', function () {
Http::assertSent(function ($request) {
$data = json_decode(file_get_contents(__DIR__.'/../../../../Resources/Sns/SnsConfirmationExample.json'), true);
$data = json_decode(file_get_contents(__DIR__ . '/../../../../Resources/Sns/SnsConfirmationExample.json'), true);

return $request->url() == $data['SubscribeURL'];
});
});

it('handles the notification confirmation successfully', function () {
postNotificationConfirmation();


expect($this->response->status())
->toBe(200)
->and($this->response->json())
->message
->toBe('Success');
});
});

function postSubscriptionConfirmation()
{
$rawContent = file_get_contents(__DIR__.'/../../../../Resources/Sns/SnsConfirmationExample.json');
$rawContent = file_get_contents(__DIR__ . '/../../../../Resources/Sns/SnsConfirmationExample.json');


test()->response = withHeaders([
'x-amz-sns-message-type' => 'SubscriptionConfirmation',
])
->call(
method: 'post',
uri: '/laravel-ses/sns-notification',
content: $rawContent,
);
}

function postNotificationConfirmation()
{

$rawContent = file_get_contents(__DIR__ . '/../../../../Resources/Sns/NotificationConfirmationExample.json');


test()->response = withHeaders([
'x-amz-sns-message-type' => 'SubscriptionConfirmation',
Expand All @@ -244,10 +271,10 @@ function postNotification(SesEvents $sesEvent, ?string $messageId = null, ?strin
test()->response = test()->call(
method: 'post',
uri: '/laravel-ses/sns-notification',
content: $rawContent,
server: [
'HTTP_X_AMZ_SNS_MESSAGE_TYPE' => 'Notification',
]
],
content: $rawContent
);
}

Expand All @@ -262,7 +289,7 @@ function getMessageIdFromSesEvent(SesEvents $sesEvent)

function rawSesEventContent(SesEvents $sesEvent, ?string $variation = null)
{
$rawContent = file_get_contents(__DIR__.'/../../../../Resources/Sns/Sns'.$sesEvent->value.($variation ? $variation : '').'Example.json');
$rawContent = file_get_contents(__DIR__ . '/../../../../Resources/Sns/Sns' . $sesEvent->value . ($variation ? $variation : '') . 'Example.json');

return $rawContent;
}
11 changes: 11 additions & 0 deletions tests/Resources/Sns/NotificationConfirmationExample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Type": "Notification",
"MessageId": "0ec3ac6d-9f51-5389-a925-f287c0aac55e",
"TopicArn": "arn:aws:sns:eu-west-2:12345:laravel-ses-local-topic",
"Message": "Successfully validated SNS topic for Amazon SES event publishing.",
"Timestamp": "2024-06-17T16:00:59.499Z",
"SignatureVersion": "1",
"Signature": "NXcpGOautj7zfa+1NWF1QPZ4GJlDM17uz2cpGwFigqkD0mQHiZ4iMDnozUuj8FPuxfWhgtojBA1lCUERAjSar23rqtMhco/SUxTpTNbA2CULGiNZ763liXQP2phLCpqR8Zaz1lq9Srfus0gt5m2hTasn1vrIr7dqCM9U8oUZ/UCXejzqyt60hXDGSEQ3B07KrQtXSKWr5VEgFp65Kvs9OGTNUxZwBSGAKO+UilYP4O5vyyvBB0hldw94mUaJrt/StweFcNB0xUOMDCNEtdBv/VmWh33WCyLFI8aV23sV5ISfr6xrIk6v1bdR9hi1k7XDd7pDzFv29pA3x/JnRDZNGg==",
"SigningCertURL": "https://sns.eu-west-2.amazonaws.com/SimpleNotificationService-60eadc530605d63b8e62a523676ef735.pem",
"UnsubscribeURL": "https://sns.eu-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-2:123345:laravel-ses-local-topic:a504a9b0-73d7-4e0e-b73b-5afab59ae555"
}

0 comments on commit 68295f0

Please sign in to comment.