SNS: Fix error message when topic is not found (#6349)

This commit is contained in:
Viren Nadkarni 2023-05-31 20:09:50 +05:30 committed by GitHub
parent 7bdea2688b
commit c457c01305
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 24 deletions

View File

@ -28,8 +28,8 @@ sns
|start-h3| Implemented features for this service |end-h3| |start-h3| Implemented features for this service |end-h3|
- [X] add_permission - [X] add_permission
- [ ] check_if_phone_number_is_opted_out - [x] check_if_phone_number_is_opted_out
- [ ] confirm_subscription - [x] confirm_subscription
- [X] create_platform_application - [X] create_platform_application
- [X] create_platform_endpoint - [X] create_platform_endpoint
- [ ] create_sms_sandbox_phone_number - [ ] create_sms_sandbox_phone_number
@ -39,22 +39,22 @@ sns
- [ ] delete_sms_sandbox_phone_number - [ ] delete_sms_sandbox_phone_number
- [X] delete_topic - [X] delete_topic
- [ ] get_data_protection_policy - [ ] get_data_protection_policy
- [ ] get_endpoint_attributes - [x] get_endpoint_attributes
- [ ] get_platform_application_attributes - [x] get_platform_application_attributes
- [ ] get_sms_attributes - [x] get_sms_attributes
- [ ] get_sms_sandbox_account_status - [ ] get_sms_sandbox_account_status
- [X] get_subscription_attributes - [X] get_subscription_attributes
- [ ] get_topic_attributes - [x] get_topic_attributes
- [X] list_endpoints_by_platform_application - [X] list_endpoints_by_platform_application
- [ ] list_origination_numbers - [ ] list_origination_numbers
- [ ] list_phone_numbers_opted_out - [x] list_phone_numbers_opted_out
- [X] list_platform_applications - [X] list_platform_applications
- [ ] list_sms_sandbox_phone_numbers - [ ] list_sms_sandbox_phone_numbers
- [X] list_subscriptions - [X] list_subscriptions
- [ ] list_subscriptions_by_topic - [x] list_subscriptions_by_topic
- [X] list_tags_for_resource - [X] list_tags_for_resource
- [X] list_topics - [X] list_topics
- [ ] opt_in_phone_number - [x] opt_in_phone_number
- [X] publish - [X] publish
- [X] publish_batch - [X] publish_batch
@ -64,8 +64,8 @@ sns
- [ ] put_data_protection_policy - [ ] put_data_protection_policy
- [X] remove_permission - [X] remove_permission
- [X] set_endpoint_attributes - [X] set_endpoint_attributes
- [ ] set_platform_application_attributes - [x] set_platform_application_attributes
- [ ] set_sms_attributes - [x] set_sms_attributes
- [X] set_subscription_attributes - [X] set_subscription_attributes
- [ ] set_topic_attributes - [ ] set_topic_attributes
- [X] subscribe - [X] subscribe

View File

@ -1,3 +1,4 @@
import contextlib
import datetime import datetime
import json import json
import requests import requests
@ -483,20 +484,18 @@ class SNSBackend(BaseBackend):
self.subscriptions.pop(key) self.subscriptions.pop(key)
def delete_topic(self, arn: str) -> None: def delete_topic(self, arn: str) -> None:
try: with contextlib.suppress(TopicNotFound):
topic = self.get_topic(arn) topic = self.get_topic(arn)
self.delete_topic_subscriptions(topic) self.delete_topic_subscriptions(topic)
parsed_arn = parse_arn(arn) parsed_arn = parse_arn(arn)
sns_backends[parsed_arn.account][parsed_arn.region].topics.pop(arn, None) sns_backends[parsed_arn.account][parsed_arn.region].topics.pop(arn, None)
except KeyError:
raise SNSNotFoundError(f"Topic with arn {arn} not found")
def get_topic(self, arn: str) -> Topic: def get_topic(self, arn: str) -> Topic:
parsed_arn = parse_arn(arn) parsed_arn = parse_arn(arn)
try: try:
return sns_backends[parsed_arn.account][parsed_arn.region].topics[arn] return sns_backends[parsed_arn.account][parsed_arn.region].topics[arn]
except KeyError: except KeyError:
raise SNSNotFoundError(f"Topic with arn {arn} not found") raise TopicNotFound
def set_topic_attribute( def set_topic_attribute(
self, topic_arn: str, attribute_name: str, attribute_value: str self, topic_arn: str, attribute_name: str, attribute_value: str
@ -1006,10 +1005,7 @@ class SNSBackend(BaseBackend):
""" """
The MessageStructure and MessageDeduplicationId-parameters have not yet been implemented. The MessageStructure and MessageDeduplicationId-parameters have not yet been implemented.
""" """
try:
topic = self.get_topic(topic_arn) topic = self.get_topic(topic_arn)
except SNSNotFoundError:
raise TopicNotFound
if len(publish_batch_request_entries) > 10: if len(publish_batch_request_entries) > 10:
raise TooManyEntriesInBatchRequest raise TooManyEntriesInBatchRequest

View File

@ -25,6 +25,9 @@ def test_create_and_delete_topic():
# Delete the topic # Delete the topic
conn.delete_topic(TopicArn=topics[0]["TopicArn"]) conn.delete_topic(TopicArn=topics[0]["TopicArn"])
# Ensure DeleteTopic is idempotent
conn.delete_topic(TopicArn=topics[0]["TopicArn"])
# And there should now be 0 topics # And there should now be 0 topics
topics_json = conn.list_topics() topics_json = conn.list_topics()
topics = topics_json["Topics"] topics = topics_json["Topics"]
@ -35,9 +38,10 @@ def test_create_and_delete_topic():
def test_delete_non_existent_topic(): def test_delete_non_existent_topic():
conn = boto3.client("sns", region_name="us-east-1") conn = boto3.client("sns", region_name="us-east-1")
conn.delete_topic.when.called_with( # Ensure DeleteTopic does not throw an error for non-existent topics
TopicArn="arn:aws:sns:us-east-1:123456789012:fake-topic" conn.delete_topic(
).should.throw(conn.exceptions.NotFoundException) TopicArn="arn:aws:sns:us-east-1:123456789012:this-topic-does-not-exist"
)
@mock_sns @mock_sns
@ -362,7 +366,7 @@ def test_add_permission_errors():
ActionName=["AddPermission"], ActionName=["AddPermission"],
).should.throw( ).should.throw(
ClientError, ClientError,
f"An error occurred (NotFound) when calling the AddPermission operation: Topic with arn {topic_arn + '-not-existing'} not found", "An error occurred (NotFound) when calling the AddPermission operation: Topic does not exist",
) )
client.add_permission.when.called_with( client.add_permission.when.called_with(
@ -388,7 +392,7 @@ def test_remove_permission_errors():
TopicArn=topic_arn + "-not-existing", Label="test" TopicArn=topic_arn + "-not-existing", Label="test"
).should.throw( ).should.throw(
ClientError, ClientError,
f"An error occurred (NotFound) when calling the RemovePermission operation: Topic with arn {topic_arn + '-not-existing'} not found", "An error occurred (NotFound) when calling the RemovePermission operation: Topic does not exist",
) )