From dc94cca90913243db40ce1b0ff180bda1e2f77e0 Mon Sep 17 00:00:00 2001 From: Paul Roberts Date: Wed, 27 Oct 2021 11:57:25 -0700 Subject: [PATCH] Publishing to SNS topics with a null subject should be allowed (#4486) --- moto/sns/models.py | 2 +- tests/test_sns/test_publishing_boto3.py | 41 +++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/moto/sns/models.py b/moto/sns/models.py index 9cae8b298..dcc0a09d4 100644 --- a/moto/sns/models.py +++ b/moto/sns/models.py @@ -308,7 +308,7 @@ class Subscription(BaseModel): "Type": "Notification", "MessageId": message_id, "TopicArn": self.topic.arn, - "Subject": subject or "my subject", + "Subject": subject, "Message": message, "Timestamp": iso_8601_datetime_with_milliseconds( datetime.datetime.utcnow() diff --git a/tests/test_sns/test_publishing_boto3.py b/tests/test_sns/test_publishing_boto3.py index 0d50fc2ed..ffb09468b 100644 --- a/tests/test_sns/test_publishing_boto3.py +++ b/tests/test_sns/test_publishing_boto3.py @@ -40,7 +40,9 @@ def test_publish_to_sqs(): ) message = "my message" with freeze_time("2015-01-01 12:00:00"): - published_message = conn.publish(TopicArn=topic_arn, Message=message) + published_message = conn.publish( + TopicArn=topic_arn, Message=message, Subject="my subject" + ) published_message_id = published_message["MessageId"] queue = sqs_conn.get_queue_by_name(QueueName="test-queue") @@ -275,7 +277,9 @@ def test_publish_to_sqs_dump_json(): sort_keys=True, ) with freeze_time("2015-01-01 12:00:00"): - published_message = conn.publish(TopicArn=topic_arn, Message=message) + published_message = conn.publish( + TopicArn=topic_arn, Message=message, Subject="my subject" + ) published_message_id = published_message["MessageId"] queue = sqs_conn.get_queue_by_name(QueueName="test-queue") @@ -311,7 +315,9 @@ def test_publish_to_sqs_in_different_region(): message = "my message" with freeze_time("2015-01-01 12:00:00"): - published_message = conn.publish(TopicArn=topic_arn, Message=message) + published_message = conn.publish( + TopicArn=topic_arn, Message=message, Subject="my subject" + ) published_message_id = published_message["MessageId"] queue = sqs_conn.get_queue_by_name(QueueName="test-queue") @@ -384,6 +390,35 @@ def test_publish_subject(): raise RuntimeError("Should have raised an InvalidParameter exception") +@mock_sqs +@mock_sns +def test_publish_null_subject(): + conn = boto3.client("sns", region_name="us-east-1") + conn.create_topic(Name="some-topic") + response = conn.list_topics() + topic_arn = response["Topics"][0]["TopicArn"] + + sqs_conn = boto3.resource("sqs", region_name="us-east-1") + sqs_conn.create_queue(QueueName="test-queue") + + conn.subscribe( + TopicArn=topic_arn, + Protocol="sqs", + Endpoint="arn:aws:sqs:us-east-1:{}:test-queue".format(ACCOUNT_ID), + ) + message = "my message" + with freeze_time("2015-01-01 12:00:00"): + conn.publish(TopicArn=topic_arn, Message=message) + + queue = sqs_conn.get_queue_by_name(QueueName="test-queue") + with freeze_time("2015-01-01 12:00:01"): + messages = queue.receive_messages(MaxNumberOfMessages=1) + + acquired_message = json.loads(messages[0].body) + acquired_message["Message"].should.equal(message) + acquired_message["Subject"].should.equal(None) + + @mock_sns def test_publish_message_too_long(): sns = boto3.resource("sns", region_name="us-east-1")