From 9d087b072913ab1f64606087c233a111b040345c Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Sun, 10 Dec 2017 13:59:04 -0800 Subject: [PATCH] add subject support (#1387) --- moto/awslambda/models.py | 4 ++-- moto/sns/models.py | 18 +++++++++--------- tests/test_sns/test_publishing.py | 15 ++++++++------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/moto/awslambda/models.py b/moto/awslambda/models.py index 6c4086fd5..947691bcf 100644 --- a/moto/awslambda/models.py +++ b/moto/awslambda/models.py @@ -603,7 +603,7 @@ class LambdaBackend(BaseBackend): def list_functions(self): return self._lambdas.all() - def send_message(self, function_name, message): + def send_message(self, function_name, message, subject=None): event = { "Records": [ { @@ -630,7 +630,7 @@ class LambdaBackend(BaseBackend): "Type": "Notification", "UnsubscribeUrl": "EXAMPLE", "TopicArn": "arn:aws:sns:EXAMPLE", - "Subject": "TestInvoke" + "Subject": subject or "TestInvoke" } } ] diff --git a/moto/sns/models.py b/moto/sns/models.py index 80da5f92f..3d6f6507e 100644 --- a/moto/sns/models.py +++ b/moto/sns/models.py @@ -42,11 +42,11 @@ class Topic(BaseModel): self.subscriptions_confimed = 0 self.subscriptions_deleted = 0 - def publish(self, message): + def publish(self, message, subject=None): message_id = six.text_type(uuid.uuid4()) subscriptions, _ = self.sns_backend.list_subscriptions(self.arn) for subscription in subscriptions: - subscription.publish(message, message_id) + subscription.publish(message, message_id, subject=subject) return message_id def get_cfn_attribute(self, attribute_name): @@ -83,27 +83,27 @@ class Subscription(BaseModel): self.attributes = {} self.confirmed = False - def publish(self, message, message_id): + def publish(self, message, message_id, subject=None): if self.protocol == 'sqs': queue_name = self.endpoint.split(":")[-1] region = self.endpoint.split(":")[3] - enveloped_message = json.dumps(self.get_post_data(message, message_id), sort_keys=True, indent=2, separators=(',', ': ')) + enveloped_message = json.dumps(self.get_post_data(message, message_id, subject), sort_keys=True, indent=2, separators=(',', ': ')) sqs_backends[region].send_message(queue_name, enveloped_message) elif self.protocol in ['http', 'https']: - post_data = self.get_post_data(message, message_id) + post_data = self.get_post_data(message, message_id, subject) requests.post(self.endpoint, json=post_data) elif self.protocol == 'lambda': # TODO: support bad function name function_name = self.endpoint.split(":")[-1] region = self.arn.split(':')[3] - lambda_backends[region].send_message(function_name, message) + lambda_backends[region].send_message(function_name, message, subject=subject) - def get_post_data(self, message, message_id): + def get_post_data(self, message, message_id, subject): return { "Type": "Notification", "MessageId": message_id, "TopicArn": self.topic.arn, - "Subject": "my subject", + "Subject": subject or "my subject", "Message": message, "Timestamp": iso_8601_datetime_with_milliseconds(datetime.datetime.utcnow()), "SignatureVersion": "1", @@ -270,7 +270,7 @@ class SNSBackend(BaseBackend): try: topic = self.get_topic(arn) - message_id = topic.publish(message) + message_id = topic.publish(message, subject=subject) except SNSNotFoundError: endpoint = self.get_endpoint(arn) message_id = endpoint.publish(message) diff --git a/tests/test_sns/test_publishing.py b/tests/test_sns/test_publishing.py index b626e2fac..964296837 100644 --- a/tests/test_sns/test_publishing.py +++ b/tests/test_sns/test_publishing.py @@ -1,16 +1,15 @@ from __future__ import unicode_literals -from six.moves.urllib.parse import parse_qs import boto +import json import re from freezegun import freeze_time import sure # noqa -from moto.packages.responses import responses from moto import mock_sns_deprecated, mock_sqs_deprecated -MESSAGE_FROM_SQS_TEMPLATE = '{\n "Message": "%s",\n "MessageId": "%s",\n "Signature": "EXAMPLElDMXvB8r9R83tGoNn0ecwd5UjllzsvSvbItzfaMpN2nk5HVSw7XnOn/49IkxDKz8YrlH2qJXj2iZB0Zo2O71c4qQk1fMUDi3LGpij7RCW7AW9vYYsSqIKRnFS94ilu7NFhUzLiieYr4BKHpdTmdD6c0esKEYBpabxDSc=",\n "SignatureVersion": "1",\n "SigningCertURL": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem",\n "Subject": "my subject",\n "Timestamp": "2015-01-01T12:00:00.000Z",\n "TopicArn": "arn:aws:sns:%s:123456789012:some-topic",\n "Type": "Notification",\n "UnsubscribeURL": "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:123456789012:some-topic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55"\n}' +MESSAGE_FROM_SQS_TEMPLATE = '{\n "Message": "%s",\n "MessageId": "%s",\n "Signature": "EXAMPLElDMXvB8r9R83tGoNn0ecwd5UjllzsvSvbItzfaMpN2nk5HVSw7XnOn/49IkxDKz8YrlH2qJXj2iZB0Zo2O71c4qQk1fMUDi3LGpij7RCW7AW9vYYsSqIKRnFS94ilu7NFhUzLiieYr4BKHpdTmdD6c0esKEYBpabxDSc=",\n "SignatureVersion": "1",\n "SigningCertURL": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem",\n "Subject": "%s",\n "Timestamp": "2015-01-01T12:00:00.000Z",\n "TopicArn": "arn:aws:sns:%s:123456789012:some-topic",\n "Type": "Notification",\n "UnsubscribeURL": "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:123456789012:some-topic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55"\n}' @mock_sqs_deprecated @@ -29,13 +28,14 @@ def test_publish_to_sqs(): "arn:aws:sqs:us-east-1:123456789012:test-queue") message_to_publish = 'my message' + subject_to_publish = "test subject" with freeze_time("2015-01-01 12:00:00"): - published_message = conn.publish(topic=topic_arn, message=message_to_publish) + published_message = conn.publish(topic=topic_arn, message=message_to_publish, subject=subject_to_publish) published_message_id = published_message['PublishResponse']['PublishResult']['MessageId'] queue = sqs_conn.get_queue("test-queue") message = queue.read(1) - expected = MESSAGE_FROM_SQS_TEMPLATE % (message_to_publish, published_message_id, 'us-east-1') + expected = MESSAGE_FROM_SQS_TEMPLATE % (message_to_publish, published_message_id, subject_to_publish, 'us-east-1') acquired_message = re.sub("\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z", '2015-01-01T12:00:00.000Z', message.get_body()) acquired_message.should.equal(expected) @@ -56,13 +56,14 @@ def test_publish_to_sqs_in_different_region(): "arn:aws:sqs:us-west-2:123456789012:test-queue") message_to_publish = 'my message' + subject_to_publish = "test subject" with freeze_time("2015-01-01 12:00:00"): - published_message = conn.publish(topic=topic_arn, message=message_to_publish) + published_message = conn.publish(topic=topic_arn, message=message_to_publish, subject=subject_to_publish) published_message_id = published_message['PublishResponse']['PublishResult']['MessageId'] queue = sqs_conn.get_queue("test-queue") message = queue.read(1) - expected = MESSAGE_FROM_SQS_TEMPLATE % (message_to_publish, published_message_id, 'us-west-1') + expected = MESSAGE_FROM_SQS_TEMPLATE % (message_to_publish, published_message_id, subject_to_publish, 'us-west-1') acquired_message = re.sub("\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z", '2015-01-01T12:00:00.000Z', message.get_body()) acquired_message.should.equal(expected)