Skip to content

Commit 5310d64

Browse files
committed
Added trait for testing
1 parent e9b0cdf commit 5310d64

File tree

2 files changed

+154
-123
lines changed

2 files changed

+154
-123
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
namespace Rennokki\LaravelSnsEvents\Tests\Concerns;
4+
5+
use Aws\Sns\Message;
6+
use Aws\Sns\MessageValidator;
7+
8+
trait GeneratesSnsMessages
9+
{
10+
/**
11+
* Get the private key to sign the request.
12+
*
13+
* @var string
14+
*/
15+
protected static $privateKey;
16+
17+
/**
18+
* The certificate to sign the request.
19+
*
20+
* @var string
21+
*/
22+
protected static $certificate;
23+
24+
/**
25+
* An valid certificate URL for test.
26+
*
27+
* @var string
28+
*/
29+
public static $validCertUrl = 'https://sns.us-west-2.amazonaws.com/bar.pem';
30+
31+
/**
32+
* Initialize the SSL keys and private keys.
33+
*
34+
* @return void
35+
*/
36+
protected static function initializeSsl(): void
37+
{
38+
self::$privateKey = openssl_pkey_new();
39+
40+
$csr = openssl_csr_new([], self::$privateKey);
41+
42+
$x509 = openssl_csr_sign($csr, null, self::$privateKey, 1);
43+
44+
openssl_x509_export($x509, self::$certificate);
45+
46+
openssl_x509_free($x509);
47+
}
48+
49+
/**
50+
* Deinitialize the SSL keys.
51+
*
52+
* @return void
53+
*/
54+
protected static function tearDownSsl(): void
55+
{
56+
openssl_pkey_free(self::$privateKey);
57+
}
58+
59+
/**
60+
* Get the signature for the message.
61+
*
62+
* @param string $stringToSign
63+
* @return string
64+
*/
65+
protected function getSignature($stringToSign)
66+
{
67+
openssl_sign($stringToSign, $signature, self::$privateKey);
68+
69+
return base64_encode($signature);
70+
}
71+
72+
/**
73+
* Get an example subscription payload for testing.
74+
*
75+
* @param array $custom
76+
* @return array
77+
*/
78+
protected function getSubscriptionConfirmationPayload(array $custom = []): array
79+
{
80+
$validator = new MessageValidator;
81+
82+
$message = array_merge([
83+
'Type' => 'SubscriptionConfirmation',
84+
'MessageId' => '165545c9-2a5c-472c-8df2-7ff2be2b3b1b',
85+
'Token' => '2336412f37...',
86+
'TopicArn' => 'arn:aws:sns:us-west-2:123456789012:MyTopic',
87+
'Message' => 'You have chosen to subscribe to the topic arn:aws:sns:us-west-2:123456789012:MyTopic.\nTo confirm the subscription, visit the SubscribeURL included in this message.',
88+
'SubscribeURL' => 'https://example.com',
89+
'Timestamp' => now()->toDateTimeString(),
90+
'SignatureVersion' => '1',
91+
'Signature' => true,
92+
'SigningCertURL' => static::$validCertUrl,
93+
], $custom);
94+
95+
$message['Signature'] = $this->getSignature(
96+
$validator->getStringToSign(new Message($message))
97+
);
98+
99+
return $message;
100+
}
101+
102+
/**
103+
* Get an example notification payload for testing.
104+
*
105+
* @param array $payload
106+
* @param array $custom
107+
* @return array
108+
*/
109+
protected function getNotificationPayload(array $payload = [], array $custom = []): array
110+
{
111+
$validator = new MessageValidator;
112+
113+
$payload = json_encode($payload);
114+
115+
$message = array_merge([
116+
'Type' => 'Notification',
117+
'MessageId' => '22b80b92-fdea-4c2c-8f9d-bdfb0c7bf324',
118+
'TopicArn' => 'arn:aws:sns:us-west-2:123456789012:MyTopic',
119+
'Subject' => 'My First Message',
120+
'Message' => "{$payload}",
121+
'Timestamp' => now()->toDateTimeString(),
122+
'SignatureVersion' => '1',
123+
'Token' => '2336412f37...',
124+
'Signature' => true,
125+
'SigningCertURL' => static::$validCertUrl,
126+
'UnsubscribeURL' => 'https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:123456789012:MyTopic:c9135db0-26c4-47ec-8998-413945fb5a96',
127+
], $custom);
128+
129+
$message['Signature'] = $this->getSignature(
130+
$validator->getStringToSign(new Message($message))
131+
);
132+
133+
return $message;
134+
}
135+
136+
/**
137+
* Get the right headers for a SNS message.
138+
*
139+
* @param array $message
140+
* @return array
141+
*/
142+
protected function getHeadersForMessage(array $message): array
143+
{
144+
return [
145+
'X-AMZ-SNS-MESSAGE-TYPE' => $message['Type'],
146+
'X-AMZ-SNS-MESSAGE-ID' => $message['MessageId'],
147+
'X-AMZ-SNS-TOPIC-ARN' => $message['TopicArn'],
148+
'X-AMZ-SNS-SUBSCRIPTION-ARN' => "{$message['TopicArn']}:c9135db0-26c4-47ec-8998-413945fb5a96",
149+
];
150+
}
151+
}

tests/TestCase.php

Lines changed: 3 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,26 @@
22

33
namespace Rennokki\LaravelSnsEvents\Tests;
44

5-
use Aws\Sns\Message;
6-
use Aws\Sns\MessageValidator;
75
use Orchestra\Testbench\TestCase as Orchestra;
86

97
class TestCase extends Orchestra
108
{
11-
/**
12-
* Get the private key to sign the request.
13-
*
14-
* @var string
15-
*/
16-
protected static $privateKey;
17-
18-
/**
19-
* The certificate to sign the request.
20-
*
21-
* @var string
22-
*/
23-
protected static $certificate;
24-
25-
/**
26-
* @var string
27-
*/
28-
const VALID_CERT_URL = 'https://sns.us-west-2.amazonaws.com/bar.pem';
9+
use Concerns\GeneratesSnsMessages;
2910

3011
/**
3112
* {@inheritdoc}
3213
*/
3314
public static function setUpBeforeClass(): void
3415
{
35-
self::$privateKey = openssl_pkey_new();
36-
37-
$csr = openssl_csr_new([], self::$privateKey);
38-
39-
$x509 = openssl_csr_sign($csr, null, self::$privateKey, 1);
40-
41-
openssl_x509_export($x509, self::$certificate);
42-
43-
openssl_x509_free($x509);
16+
static::initializeSsl();
4417
}
4518

4619
/**
4720
* {@inheritdoc}
4821
*/
4922
public static function tearDownAfterClass(): void
5023
{
51-
openssl_pkey_free(self::$privateKey);
24+
static::tearDownSsl();
5225
}
5326

5427
/**
@@ -73,97 +46,4 @@ protected function getEnvironmentSetUp($app)
7346
{
7447
$app['config']->set('app.key', '6rE9Nz59bGRbeMATftriyQjrpF7DcOQm');
7548
}
76-
77-
/**
78-
* Get the signature for the message.
79-
*
80-
* @param string $stringToSign
81-
* @return string
82-
*/
83-
protected function getSignature($stringToSign)
84-
{
85-
openssl_sign($stringToSign, $signature, self::$privateKey);
86-
87-
return base64_encode($signature);
88-
}
89-
90-
/**
91-
* Get an example subscription payload for testing.
92-
*
93-
* @param array $custom
94-
* @return array
95-
*/
96-
protected function getSubscriptionConfirmationPayload(array $custom = []): array
97-
{
98-
$validator = new MessageValidator;
99-
100-
$message = array_merge([
101-
'Type' => 'SubscriptionConfirmation',
102-
'MessageId' => '165545c9-2a5c-472c-8df2-7ff2be2b3b1b',
103-
'Token' => '2336412f37...',
104-
'TopicArn' => 'arn:aws:sns:us-west-2:123456789012:MyTopic',
105-
'Message' => 'You have chosen to subscribe to the topic arn:aws:sns:us-west-2:123456789012:MyTopic.\nTo confirm the subscription, visit the SubscribeURL included in this message.',
106-
'SubscribeURL' => 'https://example.com',
107-
'Timestamp' => now()->toDateTimeString(),
108-
'SignatureVersion' => '1',
109-
'Signature' => true,
110-
'SigningCertURL' => self::VALID_CERT_URL,
111-
], $custom);
112-
113-
$message['Signature'] = $this->getSignature(
114-
$validator->getStringToSign(new Message($message))
115-
);
116-
117-
return $message;
118-
}
119-
120-
/**
121-
* Get an example notification payload for testing.
122-
*
123-
* @param array $payload
124-
* @param array $custom
125-
* @return array
126-
*/
127-
protected function getNotificationPayload(array $payload = [], array $custom = []): array
128-
{
129-
$validator = new MessageValidator;
130-
131-
$payload = json_encode($payload);
132-
133-
$message = array_merge([
134-
'Type' => 'Notification',
135-
'MessageId' => '22b80b92-fdea-4c2c-8f9d-bdfb0c7bf324',
136-
'TopicArn' => 'arn:aws:sns:us-west-2:123456789012:MyTopic',
137-
'Subject' => 'My First Message',
138-
'Message' => "{$payload}",
139-
'Timestamp' => now()->toDateTimeString(),
140-
'SignatureVersion' => '1',
141-
'Token' => '2336412f37...',
142-
'Signature' => true,
143-
'SigningCertURL' => self::VALID_CERT_URL,
144-
'UnsubscribeURL' => 'https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:123456789012:MyTopic:c9135db0-26c4-47ec-8998-413945fb5a96',
145-
], $custom);
146-
147-
$message['Signature'] = $this->getSignature(
148-
$validator->getStringToSign(new Message($message))
149-
);
150-
151-
return $message;
152-
}
153-
154-
/**
155-
* Get the right headers for a SNS message.
156-
*
157-
* @param array $message
158-
* @return array
159-
*/
160-
protected function getHeadersForMessage(array $message): array
161-
{
162-
return [
163-
'X-AMZ-SNS-MESSAGE-TYPE' => $message['Type'],
164-
'X-AMZ-SNS-MESSAGE-ID' => $message['MessageId'],
165-
'X-AMZ-SNS-TOPIC-ARN' => $message['TopicArn'],
166-
'X-AMZ-SNS-SUBSCRIPTION-ARN' => "{$message['TopicArn']}:c9135db0-26c4-47ec-8998-413945fb5a96",
167-
];
168-
}
16949
}

0 commit comments

Comments
 (0)