diff --git a/moto/sns/models.py b/moto/sns/models.py index 32514fc0e..b6877e69e 100644 --- a/moto/sns/models.py +++ b/moto/sns/models.py @@ -27,7 +27,7 @@ class Topic(object): self.policy = DEFAULT_TOPIC_POLICY self.delivery_policy = "" self.effective_delivery_policy = DEFAULT_EFFECTIVE_DELIVERY_POLICY - self.arn = make_arn_for_topic(self.account_id, name) + self.arn = make_arn_for_topic(self.account_id, name, sns_backend.region_name) self.subscriptions_pending = 0 self.subscriptions_confimed = 0 @@ -135,11 +135,18 @@ class PlatformEndpoint(object): class SNSBackend(BaseBackend): - def __init__(self): + def __init__(self, region_name): + super(SNSBackend, self).__init__() self.topics = OrderedDict() self.subscriptions = OrderedDict() self.applications = {} self.platform_endpoints = {} + self.region_name = region_name + + def reset(self): + region_name = self.region_name + self.__dict__ = {} + self.__init__(region_name) def create_topic(self, name): topic = Topic(name, self) @@ -247,7 +254,7 @@ class SNSBackend(BaseBackend): sns_backends = {} for region in boto.sns.regions(): - sns_backends[region.name] = SNSBackend() + sns_backends[region.name] = SNSBackend(region.name) DEFAULT_TOPIC_POLICY = { diff --git a/moto/sns/utils.py b/moto/sns/utils.py index 92b76345a..864c3af6b 100644 --- a/moto/sns/utils.py +++ b/moto/sns/utils.py @@ -2,8 +2,8 @@ from __future__ import unicode_literals import uuid -def make_arn_for_topic(account_id, name): - return "arn:aws:sns:us-east-1:{0}:{1}".format(account_id, name) +def make_arn_for_topic(account_id, name, region_name): + return "arn:aws:sns:{0}:{1}:{2}".format(region_name, account_id, name) def make_arn_for_subscription(topic_arn): diff --git a/tests/test_sns/test_publishing.py b/tests/test_sns/test_publishing.py index a7d051b63..3805d9e5e 100644 --- a/tests/test_sns/test_publishing.py +++ b/tests/test_sns/test_publishing.py @@ -72,7 +72,7 @@ def test_publish_to_http(): parse_qs(last_request.body.decode('utf-8')).should.equal({ "Type": ["Notification"], "MessageId": [message_id], - "TopicArn": ["arn:aws:sns:us-east-1:123456789012:some-topic"], + "TopicArn": ["arn:aws:sns:{0}:123456789012:some-topic".format(conn.region.name)], "Subject": ["my subject"], "Message": ["my message"], "Timestamp": ["2013-01-01T00:00:00.000Z"], diff --git a/tests/test_sns/test_topics.py b/tests/test_sns/test_topics.py index e2488f3d2..59066ac0d 100644 --- a/tests/test_sns/test_topics.py +++ b/tests/test_sns/test_topics.py @@ -17,7 +17,10 @@ def test_create_and_delete_topic(): topics_json = conn.get_all_topics() topics = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"] topics.should.have.length_of(1) - topics[0]['TopicArn'].should.equal("arn:aws:sns:us-east-1:123456789012:some-topic") + topics[0]['TopicArn'].should.equal( + "arn:aws:sns:{}:123456789012:some-topic" + .format(conn.region.name) + ) # Delete the topic conn.delete_topic(topics[0]['TopicArn']) @@ -36,15 +39,19 @@ def test_get_missing_topic(): @mock_sns def test_create_topic_in_multiple_regions(): - west1_conn = boto.sns.connect_to_region("us-west-1") - west1_conn.create_topic("some-topic") - - west2_conn = boto.sns.connect_to_region("us-west-2") - west2_conn.create_topic("some-topic") - - list(west1_conn.get_all_topics()["ListTopicsResponse"]["ListTopicsResult"]["Topics"]).should.have.length_of(1) - list(west2_conn.get_all_topics()["ListTopicsResponse"]["ListTopicsResult"]["Topics"]).should.have.length_of(1) + for region in ['us-west-1', 'us-west-2']: + conn = boto.sns.connect_to_region(region) + conn.create_topic("some-topic") + list(conn.get_all_topics()["ListTopicsResponse"]["ListTopicsResult"]["Topics"]).should.have.length_of(1) +@mock_sns +def test_topic_corresponds_to_region(): + for region in ['us-east-1', 'us-west-2']: + conn = boto.sns.connect_to_region(region) + conn.create_topic("some-topic") + topics_json = conn.get_all_topics() + topic_arn = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"][0]['TopicArn'] + topic_arn.should.equal("arn:aws:sns:{0}:123456789012:some-topic".format(region)) @mock_sns def test_topic_attributes(): @@ -55,7 +62,10 @@ def test_topic_attributes(): topic_arn = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"][0]['TopicArn'] attributes = conn.get_topic_attributes(topic_arn)['GetTopicAttributesResponse']['GetTopicAttributesResult']['Attributes'] - attributes["TopicArn"].should.equal("arn:aws:sns:us-east-1:123456789012:some-topic") + attributes["TopicArn"].should.equal( + "arn:aws:sns:{0}:123456789012:some-topic" + .format(conn.region.name) + ) attributes["Owner"].should.equal(123456789012) attributes["Policy"].should.equal(DEFAULT_TOPIC_POLICY) attributes["DisplayName"].should.equal("") @@ -85,7 +95,6 @@ def test_topic_attributes(): attributes["DisplayName"].should.equal("My display name") attributes["DeliveryPolicy"].should.equal("{'http': {'defaultHealthyRetryPolicy': {'numRetries': 5}}}") - @mock_sns def test_topic_paging(): conn = boto.connect_sns()