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:
parent
795ccd0e2b
commit
19fc76f466
@ -426,8 +426,15 @@ class SNSBackend(BaseBackend):
|
|||||||
def list_topics(self, next_token=None):
|
def list_topics(self, next_token=None):
|
||||||
return self._get_values_nexttoken(self.topics, next_token)
|
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):
|
def delete_topic(self, arn):
|
||||||
try:
|
try:
|
||||||
|
topic = self.get_topic(arn)
|
||||||
|
self.delete_topic_subscriptions(topic)
|
||||||
self.topics.pop(arn)
|
self.topics.pop(arn)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise SNSNotFoundError("Topic with arn {0} not found".format(arn))
|
raise SNSNotFoundError("Topic with arn {0} not found".format(arn))
|
||||||
|
@ -72,9 +72,7 @@ def test_deleting_subscriptions_by_deleting_topic():
|
|||||||
subscriptions = conn.get_all_subscriptions()["ListSubscriptionsResponse"][
|
subscriptions = conn.get_all_subscriptions()["ListSubscriptionsResponse"][
|
||||||
"ListSubscriptionsResult"
|
"ListSubscriptionsResult"
|
||||||
]["Subscriptions"]
|
]["Subscriptions"]
|
||||||
subscriptions.should.have.length_of(1)
|
subscriptions.should.have.length_of(0)
|
||||||
subscription = subscriptions[0]
|
|
||||||
subscription["SubscriptionArn"].should.equal(subscription_arn)
|
|
||||||
|
|
||||||
# Now delete hanging subscription
|
# Now delete hanging subscription
|
||||||
conn.unsubscribe(subscription_arn)
|
conn.unsubscribe(subscription_arn)
|
||||||
|
@ -7,7 +7,7 @@ import sure # noqa
|
|||||||
from botocore.exceptions import ClientError
|
from botocore.exceptions import ClientError
|
||||||
from nose.tools import assert_raises
|
from nose.tools import assert_raises
|
||||||
|
|
||||||
from moto import mock_sns
|
from moto import mock_sns, mock_sqs
|
||||||
from moto.sns.models import (
|
from moto.sns.models import (
|
||||||
DEFAULT_PAGE_SIZE,
|
DEFAULT_PAGE_SIZE,
|
||||||
DEFAULT_EFFECTIVE_DELIVERY_POLICY,
|
DEFAULT_EFFECTIVE_DELIVERY_POLICY,
|
||||||
@ -124,11 +124,9 @@ def test_unsubscribe_from_deleted_topic():
|
|||||||
topics = topics_json["Topics"]
|
topics = topics_json["Topics"]
|
||||||
topics.should.have.length_of(0)
|
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 = client.list_subscriptions()["Subscriptions"]
|
||||||
subscriptions.should.have.length_of(1)
|
subscriptions.should.have.length_of(0)
|
||||||
subscription = subscriptions[0]
|
|
||||||
subscription["SubscriptionArn"].should.equal(subscription_arn)
|
|
||||||
|
|
||||||
# Now delete hanging subscription
|
# Now delete hanging subscription
|
||||||
client.unsubscribe(SubscriptionArn=subscription_arn)
|
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
|
@mock_sns
|
||||||
def test_set_subscription_attributes():
|
def test_set_subscription_attributes():
|
||||||
conn = boto3.client("sns", region_name="us-east-1")
|
conn = boto3.client("sns", region_name="us-east-1")
|
||||||
|
Loading…
Reference in New Issue
Block a user