From 651998853b0471d5e382e8ebdaa97551b289730d Mon Sep 17 00:00:00 2001 From: usmangani1 Date: Tue, 26 Jan 2021 20:34:52 +0530 Subject: [PATCH] Fix:SNS Create FIFO Topic (#3533) * Fix:SNS Create FIFO Topic * Added more tests * change regular expression and added tests * Handling NPE Co-authored-by: usmanokc --- moto/sns/models.py | 20 ++++++++++++--- tests/test_sns/test_topics_boto3.py | 39 ++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/moto/sns/models.py b/moto/sns/models.py index ea393e845..f1091bf74 100644 --- a/moto/sns/models.py +++ b/moto/sns/models.py @@ -391,11 +391,23 @@ class SNSBackend(BaseBackend): self.sms_attributes.update(attrs) def create_topic(self, name, attributes=None, tags=None): - fails_constraints = not re.match(r"^[a-zA-Z0-9_-]{1,256}$", name) + + if attributes is None: + attributes = {} + if ( + attributes.get("FifoTopic") + and attributes.get("FifoTopic").lower() == "true" + ): + fails_constraints = not re.match(r"^[a-zA-Z0-9_-]{1,256}\.fifo$", name) + msg = "Fifo Topic names must end with .fifo and must be made up of only uppercase and lowercase ASCII letters, numbers, underscores, and hyphens, and must be between 1 and 256 characters long." + + else: + fails_constraints = not re.match(r"^[a-zA-Z0-9_-]{1,256}$", name) + msg = "Topic names must be made up of only uppercase and lowercase ASCII letters, numbers, underscores, and hyphens, and must be between 1 and 256 characters long." + if fails_constraints: - raise InvalidParameterValue( - "Topic names must be made up of only uppercase and lowercase ASCII letters, numbers, underscores, and hyphens, and must be between 1 and 256 characters long." - ) + raise InvalidParameterValue(msg) + candidate_topic = Topic(name, self) if attributes: for attribute in attributes: diff --git a/tests/test_sns/test_topics_boto3.py b/tests/test_sns/test_topics_boto3.py index 6b1e52df6..e6475125f 100644 --- a/tests/test_sns/test_topics_boto3.py +++ b/tests/test_sns/test_topics_boto3.py @@ -3,7 +3,7 @@ import boto3 import six import json -import sure # noqa +# import sure # noqa from botocore.exceptions import ClientError from moto import mock_sns @@ -522,6 +522,43 @@ def test_untag_resource_error(): ).should.throw(ClientError, "Resource does not exist") +@mock_sns +def test_create_fifo_topic(): + conn = boto3.client("sns", region_name="us-east-1") + response = conn.create_topic( + Name="test_topic.fifo", Attributes={"FifoTopic": "true"} + ) + + assert "TopicArn" in response + + try: + conn.create_topic(Name="test_topic", Attributes={"FifoTopic": "true"}) + except ClientError as err: + err.response["Error"]["Code"].should.equal("InvalidParameterValue") + err.response["Error"]["Message"].should.equal( + "Fifo Topic names must end with .fifo and must be made up of only uppercase and lowercase ASCII letters, " + "numbers, underscores, and hyphens, and must be between 1 and 256 characters long." + ) + + try: + conn.create_topic(Name="test_topic.fifo") + except ClientError as err: + err.response["Error"]["Code"].should.equal("InvalidParameterValue") + err.response["Error"]["Message"].should.equal( + "Topic names must be made up of only uppercase and lowercase ASCII letters, numbers, underscores, " + "and hyphens, and must be between 1 and 256 characters long." + ) + + try: + conn.create_topic(Name="topic.name.fifo", Attributes={"FifoTopic": "true"}) + except ClientError as err: + err.response["Error"]["Code"].should.equal("InvalidParameterValue") + err.response["Error"]["Message"].should.equal( + "Fifo Topic names must end with .fifo and must be made up of only uppercase and lowercase ASCII letters, " + "numbers, underscores, and hyphens, and must be between 1 and 256 characters long." + ) + + @mock_sns def test_topic_kms_master_key_id_attribute(): client = boto3.client("sns", region_name="us-west-2")