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 <usman@okcredit.in>
This commit is contained in:
usmangani1 2021-01-26 20:34:52 +05:30 committed by GitHub
parent 98d79dca97
commit 651998853b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 5 deletions

View File

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

View File

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