Fix: SNS Delete subscriptions on topic deletion (#3410)

* Fix:Delete subscriptions on delete topic

* Changed tests

Co-authored-by: usmankb <usman@krazybee.com>
This commit is contained in:
usmangani1 2020-10-29 14:22:02 +05:30 committed by GitHub
parent 795ccd0e2b
commit 19fc76f466
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 8 deletions

View File

@ -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))

View File

@ -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)

View File

@ -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")