From b08fc8beded178f3de6580e471faf6be380c42bf Mon Sep 17 00:00:00 2001 From: Ben Jolitz Date: Fri, 4 May 2018 16:30:47 -0700 Subject: [PATCH 1/4] allow topic names to start/end with `_`, `-` --- moto/sns/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moto/sns/models.py b/moto/sns/models.py index 1c1be668..6ee6c79e 100644 --- a/moto/sns/models.py +++ b/moto/sns/models.py @@ -240,7 +240,7 @@ class SNSBackend(BaseBackend): self.sms_attributes.update(attrs) def create_topic(self, name): - fails_constraints = not re.match(r'^[a-zA-Z0-9](?:[A-Za-z0-9_-]{0,253}[a-zA-Z0-9])?$', name) + fails_constraints = not re.match(r'^[a-zA-Z0-9\_\-]{0,256}$', name) 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.") candidate_topic = Topic(name, self) From 0ed18c8b8a23193d6d5989e3869abbf3cb699e6b Mon Sep 17 00:00:00 2001 From: Ben Jolitz Date: Fri, 4 May 2018 16:33:43 -0700 Subject: [PATCH 2/4] remove extraneous backslash --- moto/sns/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moto/sns/models.py b/moto/sns/models.py index 6ee6c79e..65dcc6cf 100644 --- a/moto/sns/models.py +++ b/moto/sns/models.py @@ -240,7 +240,7 @@ class SNSBackend(BaseBackend): self.sms_attributes.update(attrs) def create_topic(self, name): - fails_constraints = not re.match(r'^[a-zA-Z0-9\_\-]{0,256}$', name) + fails_constraints = not re.match(r'^[a-zA-Z0-9_-]{0,256}$', name) 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.") candidate_topic = Topic(name, self) From 289d3614633f54d1a4a34bdfb85baf6e8c0bb1fd Mon Sep 17 00:00:00 2001 From: Ben Jolitz Date: Fri, 4 May 2018 19:16:12 -0700 Subject: [PATCH 3/4] add check for at least 1 character as the minumum length REF: http://boto3.readthedocs.io/en/latest/reference/services/sns.html#SNS.Client.create_topic --- moto/sns/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moto/sns/models.py b/moto/sns/models.py index 65dcc6cf..d6105c1d 100644 --- a/moto/sns/models.py +++ b/moto/sns/models.py @@ -240,7 +240,7 @@ class SNSBackend(BaseBackend): self.sms_attributes.update(attrs) def create_topic(self, name): - fails_constraints = not re.match(r'^[a-zA-Z0-9_-]{0,256}$', name) + fails_constraints = not re.match(r'^[a-zA-Z0-9_-]{1,256}$', name) 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.") candidate_topic = Topic(name, self) From 45b529fef4c15e26e7850fc0777c7b8debc78f2d Mon Sep 17 00:00:00 2001 From: Ben Jolitz Date: Fri, 4 May 2018 19:17:56 -0700 Subject: [PATCH 4/4] parameterize topic name create/delete --- tests/test_sns/test_topics_boto3.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/tests/test_sns/test_topics_boto3.py b/tests/test_sns/test_topics_boto3.py index 95dd41f8..7d9a27b1 100644 --- a/tests/test_sns/test_topics_boto3.py +++ b/tests/test_sns/test_topics_boto3.py @@ -13,23 +13,24 @@ from moto.sns.models import DEFAULT_TOPIC_POLICY, DEFAULT_EFFECTIVE_DELIVERY_POL @mock_sns def test_create_and_delete_topic(): conn = boto3.client("sns", region_name="us-east-1") - conn.create_topic(Name="some-topic") + for topic_name in ('some-topic', '-some-topic-', '_some-topic_', 'a' * 256): + conn.create_topic(Name=topic_name) - topics_json = conn.list_topics() - topics = topics_json["Topics"] - topics.should.have.length_of(1) - topics[0]['TopicArn'].should.equal( - "arn:aws:sns:{0}:123456789012:some-topic" - .format(conn._client_config.region_name) - ) + topics_json = conn.list_topics() + topics = topics_json["Topics"] + topics.should.have.length_of(1) + topics[0]['TopicArn'].should.equal( + "arn:aws:sns:{0}:123456789012:{1}" + .format(conn._client_config.region_name, topic_name) + ) - # Delete the topic - conn.delete_topic(TopicArn=topics[0]['TopicArn']) + # Delete the topic + conn.delete_topic(TopicArn=topics[0]['TopicArn']) - # And there should now be 0 topics - topics_json = conn.list_topics() - topics = topics_json["Topics"] - topics.should.have.length_of(0) + # And there should now be 0 topics + topics_json = conn.list_topics() + topics = topics_json["Topics"] + topics.should.have.length_of(0) @mock_sns def test_create_topic_should_be_indempodent():