|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace Tests\MiamiOH\SnsHandler\Feature; |
| 5 | + |
| 6 | +use Aws\Sns\MessageValidator; |
| 7 | +use Illuminate\Http\Client\Request; |
| 8 | +use Illuminate\Support\Facades\Http; |
| 9 | +use MiamiOH\SnsHandler\NullMessageValidator; |
| 10 | +use MiamiOH\SnsHandler\ServiceProvider; |
| 11 | +use MiamiOH\SnsHandler\SnsMessage; |
| 12 | +use Tests\MiamiOH\SnsHandler\MakesSnsTests; |
| 13 | +use Tests\MiamiOH\SnsHandler\Unit\SnsMessageHandlerStub; |
| 14 | + |
| 15 | + |
| 16 | +class SnsHandlerTest extends \Tests\MiamiOH\SnsHandler\TestCase |
| 17 | +{ |
| 18 | + use MakesSnsTests; |
| 19 | + |
| 20 | + public function setUp(): void |
| 21 | + { |
| 22 | + parent::setUp(); |
| 23 | + $this->app->bind(MessageValidator::class, NullMessageValidator::class); |
| 24 | + } |
| 25 | + |
| 26 | + protected function getEnvironmentSetUp($app) |
| 27 | + { |
| 28 | + $app['config']->set('sns-handler.sns-class-map', [ |
| 29 | + 'arn:aws:sns:us-west-2:123456789012:MyTopic' => SnsMessageHandlerStub::class, |
| 30 | + ]); |
| 31 | + } |
| 32 | + |
| 33 | + protected function getPackageProviders($app) |
| 34 | + { |
| 35 | + return [ServiceProvider::class]; |
| 36 | + } |
| 37 | + |
| 38 | + public function testReturnsNotFoundForUnknownTopicArn(): void |
| 39 | + { |
| 40 | + |
| 41 | + |
| 42 | + $data = $this->makeSnsMessageData([ |
| 43 | + 'Type' => SnsMessage::NOTIFICATION_TYPE, |
| 44 | + 'TopicArn' => 'arn:aws:sns:us-west-2:123456789012:Unknown', |
| 45 | + ]); |
| 46 | + $response = $this->postJson('/sns/message', $data); |
| 47 | + |
| 48 | + $this->assertEquals(404, $response->status()); |
| 49 | + } |
| 50 | + |
| 51 | + public function testConfirmsSubscriptionForKnownTopicArn(): void |
| 52 | + { |
| 53 | + Http::fake(); |
| 54 | + |
| 55 | + $data = $this->makeSnsMessageData([ |
| 56 | + 'Type' => SnsMessage::SUBSCRIBE_TYPE, |
| 57 | + 'Message' => json_encode(['id' => 123, 'color' => 'red'], true), |
| 58 | + 'SubscribeURL' => 'https://aws.amazon.com/sns/register/abc123' |
| 59 | + ]); |
| 60 | + |
| 61 | + $this->postJson('/sns/message', $data); |
| 62 | + |
| 63 | + Http::assertSent(function (Request $request) { |
| 64 | + return $request->url() === 'https://aws.amazon.com/sns/register/abc123'; |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + public function testRespondsWithOkAfterConfirmsSubscription(): void |
| 69 | + { |
| 70 | + Http::fake(); |
| 71 | + |
| 72 | + $data = $this->makeSnsMessageData([ |
| 73 | + 'Type' => SnsMessage::SUBSCRIBE_TYPE, |
| 74 | + 'Message' => json_encode(['id' => 123, 'color' => 'red'], true), |
| 75 | + 'SubscribeURL' => 'https://aws.amazon.com/sns/register/abc123' |
| 76 | + ]); |
| 77 | + |
| 78 | + $response = $this->postJson('/sns/message', $data); |
| 79 | + print_r($response->getContent()); |
| 80 | + |
| 81 | + $this->assertEquals(200, $response->status()); |
| 82 | + } |
| 83 | + |
| 84 | + public function testReturnsBadGatewayResponseIfConfirmationFails(): void |
| 85 | + { |
| 86 | + Http::fake([ |
| 87 | + '*' => Http::response(null, 404), |
| 88 | + ]); |
| 89 | + |
| 90 | + $data = $this->makeSnsMessageData([ |
| 91 | + 'Type' => SnsMessage::SUBSCRIBE_TYPE, |
| 92 | + 'Message' => json_encode(['id' => 123, 'color' => 'red'], true), |
| 93 | + 'SubscribeURL' => 'https://aws.amazon.com/sns/register/abc123' |
| 94 | + ]); |
| 95 | + |
| 96 | + $response = $this->postJson('/sns/message', $data); |
| 97 | + |
| 98 | + $this->assertEquals(502, $response->status()); |
| 99 | + } |
| 100 | + |
| 101 | + public function testSendsMessageToRegisteredHandler(): void |
| 102 | + { |
| 103 | + $data = $this->makeSnsMessageData([ |
| 104 | + 'Type' => SnsMessage::NOTIFICATION_TYPE, |
| 105 | + 'TopicArn' => 'arn:aws:sns:us-west-2:123456789012:MyTopic', |
| 106 | + 'Message' => 'Test message', |
| 107 | + ]); |
| 108 | + |
| 109 | + SnsMessageHandlerStub::handleCallback(function (SnsMessage $message) { |
| 110 | + $this->assertEquals('Test message', $message->message()); |
| 111 | + }); |
| 112 | + |
| 113 | + $response = $this->postJson('/sns/message', $data); |
| 114 | + |
| 115 | + $this->assertEquals(200, $response->status()); |
| 116 | + } |
| 117 | +} |
0 commit comments