From 19fc76f4669d545b56cfd79c55377f46379d546d Mon Sep 17 00:00:00 2001 From: usmangani1 Date: Thu, 29 Oct 2020 14:22:02 +0530 Subject: [PATCH] Fix: SNS Delete subscriptions on topic deletion (#3410) * Fix:Delete subscriptions on delete topic * Changed tests Co-authored-by: usmankb --- moto/sns/models.py | 7 +++++ tests/test_sns/test_subscriptions.py | 4 +-- tests/test_sns/test_subscriptions_boto3.py | 30 ++++++++++++++++++---- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/moto/sns/models.py b/moto/sns/models.py index ea0790c6a..7d297fbdc 100644 --- a/moto/sns/models.py +++ b/moto/sns/models.py @@ -426,8 +426,15 @@ class SNSBackend(BaseBackend): def list_topics(self, next_token=None): return self._get_values_nexttoken(self.topics, next_token) + def delete_topic_subscriptions(self, topic): + for key, value in self.subscriptions.items(): + if value.topic == topic: + self.subscriptions.pop(key) + def delete_topic(self, arn): try: + topic = self.get_topic(arn) + self.delete_topic_subscriptions(topic) self.topics.pop(arn) except KeyError: raise SNSNotFoundError("Topic with arn {0} not found".format(arn)) diff --git a/tests/test_sns/test_subscriptions.py b/tests/test_sns/test_subscriptions.py index f773438d7..d11830dc6 100644 --- a/tests/test_sns/test_subscriptions.py +++ b/tests/test_sns/test_subscriptions.py @@ -72,9 +72,7 @@ def test_deleting_subscriptions_by_deleting_topic(): subscriptions = conn.get_all_subscriptions()["ListSubscriptionsResponse"][ "ListSubscriptionsResult" ]["Subscriptions"] - subscriptions.should.have.length_of(1) - subscription = subscriptions[0] - subscription["SubscriptionArn"].should.equal(subscription_arn) + subscriptions.should.have.length_of(0) # Now delete hanging subscription conn.unsubscribe(subscription_arn) diff --git a/tests/test_sns/test_subscriptions_boto3.py b/tests/test_sns/test_subscriptions_boto3.py index d91b3566b..c15658dca 100644 --- a/tests/test_sns/test_subscriptions_boto3.py +++ b/tests/test_sns/test_subscriptions_boto3.py @@ -7,7 +7,7 @@ import sure # noqa from botocore.exceptions import ClientError from nose.tools import assert_raises -from moto import mock_sns +from moto import mock_sns, mock_sqs from moto.sns.models import ( DEFAULT_PAGE_SIZE, DEFAULT_EFFECTIVE_DELIVERY_POLICY, @@ -124,11 +124,9 @@ def test_unsubscribe_from_deleted_topic(): topics = topics_json["Topics"] topics.should.have.length_of(0) - # And the subscription should still be left + # as per the documentation deleting a topic deletes all the subscriptions subscriptions = client.list_subscriptions()["Subscriptions"] - subscriptions.should.have.length_of(1) - subscription = subscriptions[0] - subscription["SubscriptionArn"].should.equal(subscription_arn) + subscriptions.should.have.length_of(0) # Now delete hanging subscription client.unsubscribe(SubscriptionArn=subscription_arn) @@ -304,6 +302,28 @@ def test_creating_subscription_with_attributes(): ) +@mock_sns +@mock_sqs +def test_delete_subscriptions_on_delete_topic(): + sqs = boto3.client("sqs", region_name="us-east-1") + conn = boto3.client("sns", region_name="us-east-1") + + queue = sqs.create_queue(QueueName="test-queue") + topic = conn.create_topic(Name="some-topic") + + conn.subscribe( + TopicArn=topic.get("TopicArn"), Protocol="sqs", Endpoint=queue.get("QueueUrl") + ) + subscriptions = conn.list_subscriptions()["Subscriptions"] + + subscriptions.should.have.length_of(1) + + conn.delete_topic(TopicArn=topic.get("TopicArn")) + + subscriptions = conn.list_subscriptions()["Subscriptions"] + subscriptions.should.have.length_of(0) + + @mock_sns def test_set_subscription_attributes(): conn = boto3.client("sns", region_name="us-east-1")