| 
									
										
										
										
											2014-08-27 11:17:06 -04:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							| 
									
										
										
										
											2014-05-11 22:56:44 -04:00
										 |  |  | import boto | 
					
						
							| 
									
										
										
										
											2017-02-27 10:20:53 -05:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2014-08-26 13:25:50 -04:00
										 |  |  | import six | 
					
						
							| 
									
										
										
										
											2014-05-11 22:56:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | import sure  # noqa | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-14 09:06:31 -04:00
										 |  |  | from boto.exception import BotoServerError | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | from moto import mock_sns_deprecated | 
					
						
							| 
									
										
										
										
											2014-11-29 22:37:48 -05:00
										 |  |  | from moto.sns.models import DEFAULT_TOPIC_POLICY, DEFAULT_EFFECTIVE_DELIVERY_POLICY, DEFAULT_PAGE_SIZE | 
					
						
							| 
									
										
										
										
											2014-05-11 22:56:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_sns_deprecated | 
					
						
							| 
									
										
										
										
											2014-05-11 22:56:44 -04:00
										 |  |  | def test_create_and_delete_topic(): | 
					
						
							|  |  |  |     conn = boto.connect_sns() | 
					
						
							|  |  |  |     conn.create_topic("some-topic") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     topics_json = conn.get_all_topics() | 
					
						
							|  |  |  |     topics = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"] | 
					
						
							|  |  |  |     topics.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2015-07-31 16:01:07 -04:00
										 |  |  |     topics[0]['TopicArn'].should.equal( | 
					
						
							| 
									
										
										
										
											2015-08-03 10:32:33 -04:00
										 |  |  |         "arn:aws:sns:{0}:123456789012:some-topic" | 
					
						
							| 
									
										
										
										
											2015-07-31 16:01:07 -04:00
										 |  |  |         .format(conn.region.name) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-05-11 22:56:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Delete the topic | 
					
						
							|  |  |  |     conn.delete_topic(topics[0]['TopicArn']) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # And there should now be 0 topics | 
					
						
							|  |  |  |     topics_json = conn.get_all_topics() | 
					
						
							|  |  |  |     topics = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"] | 
					
						
							|  |  |  |     topics.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_sns_deprecated | 
					
						
							| 
									
										
										
										
											2015-03-14 09:06:31 -04:00
										 |  |  | def test_get_missing_topic(): | 
					
						
							|  |  |  |     conn = boto.connect_sns() | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     conn.get_topic_attributes.when.called_with( | 
					
						
							|  |  |  |         "a-fake-arn").should.throw(BotoServerError) | 
					
						
							| 
									
										
										
										
											2015-03-14 09:06:31 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_sns_deprecated | 
					
						
							| 
									
										
										
										
											2014-11-16 18:35:11 -05:00
										 |  |  | def test_create_topic_in_multiple_regions(): | 
					
						
							| 
									
										
										
										
											2015-07-31 16:01:07 -04:00
										 |  |  |     for region in ['us-west-1', 'us-west-2']: | 
					
						
							|  |  |  |         conn = boto.sns.connect_to_region(region) | 
					
						
							|  |  |  |         conn.create_topic("some-topic") | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |         list(conn.get_all_topics()["ListTopicsResponse"][ | 
					
						
							|  |  |  |              "ListTopicsResult"]["Topics"]).should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-16 18:35:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_sns_deprecated | 
					
						
							| 
									
										
										
										
											2015-07-31 16:01:07 -04:00
										 |  |  | 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() | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |         topic_arn = topics_json["ListTopicsResponse"][ | 
					
						
							|  |  |  |             "ListTopicsResult"]["Topics"][0]['TopicArn'] | 
					
						
							|  |  |  |         topic_arn.should.equal( | 
					
						
							|  |  |  |             "arn:aws:sns:{0}:123456789012:some-topic".format(region)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-16 18:35:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_sns_deprecated | 
					
						
							| 
									
										
										
										
											2014-05-11 22:56:44 -04:00
										 |  |  | def test_topic_attributes(): | 
					
						
							|  |  |  |     conn = boto.connect_sns() | 
					
						
							|  |  |  |     conn.create_topic("some-topic") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     topics_json = conn.get_all_topics() | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     topic_arn = topics_json["ListTopicsResponse"][ | 
					
						
							|  |  |  |         "ListTopicsResult"]["Topics"][0]['TopicArn'] | 
					
						
							| 
									
										
										
										
											2014-05-11 22:56:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     attributes = conn.get_topic_attributes(topic_arn)['GetTopicAttributesResponse'][ | 
					
						
							|  |  |  |         'GetTopicAttributesResult']['Attributes'] | 
					
						
							| 
									
										
										
										
											2015-07-31 16:01:07 -04:00
										 |  |  |     attributes["TopicArn"].should.equal( | 
					
						
							|  |  |  |         "arn:aws:sns:{0}:123456789012:some-topic" | 
					
						
							|  |  |  |         .format(conn.region.name) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-05-11 22:56:44 -04:00
										 |  |  |     attributes["Owner"].should.equal(123456789012) | 
					
						
							| 
									
										
										
										
											2017-02-27 10:20:53 -05:00
										 |  |  |     json.loads(attributes["Policy"]).should.equal(DEFAULT_TOPIC_POLICY) | 
					
						
							| 
									
										
										
										
											2014-05-11 22:56:44 -04:00
										 |  |  |     attributes["DisplayName"].should.equal("") | 
					
						
							|  |  |  |     attributes["SubscriptionsPending"].should.equal(0) | 
					
						
							|  |  |  |     attributes["SubscriptionsConfirmed"].should.equal(0) | 
					
						
							|  |  |  |     attributes["SubscriptionsDeleted"].should.equal(0) | 
					
						
							|  |  |  |     attributes["DeliveryPolicy"].should.equal("") | 
					
						
							| 
									
										
										
										
											2017-02-27 20:53:57 -05:00
										 |  |  |     json.loads(attributes["EffectiveDeliveryPolicy"]).should.equal( | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |         DEFAULT_EFFECTIVE_DELIVERY_POLICY) | 
					
						
							| 
									
										
										
										
											2014-05-11 22:56:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-26 13:25:50 -04:00
										 |  |  |     # boto can't handle prefix-mandatory strings: | 
					
						
							|  |  |  |     # i.e. unicode on Python 2 -- u"foobar" | 
					
						
							|  |  |  |     # and bytes on Python 3 -- b"foobar" | 
					
						
							|  |  |  |     if six.PY2: | 
					
						
							|  |  |  |         policy = {b"foo": b"bar"} | 
					
						
							|  |  |  |         displayname = b"My display name" | 
					
						
							|  |  |  |         delivery = {b"http": {b"defaultHealthyRetryPolicy": {b"numRetries": 5}}} | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         policy = {u"foo": u"bar"} | 
					
						
							|  |  |  |         displayname = u"My display name" | 
					
						
							|  |  |  |         delivery = {u"http": {u"defaultHealthyRetryPolicy": {u"numRetries": 5}}} | 
					
						
							|  |  |  |     conn.set_topic_attributes(topic_arn, "Policy", policy) | 
					
						
							|  |  |  |     conn.set_topic_attributes(topic_arn, "DisplayName", displayname) | 
					
						
							|  |  |  |     conn.set_topic_attributes(topic_arn, "DeliveryPolicy", delivery) | 
					
						
							| 
									
										
										
										
											2014-05-11 22:56:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     attributes = conn.get_topic_attributes(topic_arn)['GetTopicAttributesResponse'][ | 
					
						
							|  |  |  |         'GetTopicAttributesResult']['Attributes'] | 
					
						
							| 
									
										
										
										
											2014-05-11 22:56:44 -04:00
										 |  |  |     attributes["Policy"].should.equal("{'foo': 'bar'}") | 
					
						
							|  |  |  |     attributes["DisplayName"].should.equal("My display name") | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     attributes["DeliveryPolicy"].should.equal( | 
					
						
							|  |  |  |         "{'http': {'defaultHealthyRetryPolicy': {'numRetries': 5}}}") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-29 22:37:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 22:35:45 -05:00
										 |  |  | @mock_sns_deprecated | 
					
						
							| 
									
										
										
										
											2014-11-29 22:37:48 -05:00
										 |  |  | def test_topic_paging(): | 
					
						
							|  |  |  |     conn = boto.connect_sns() | 
					
						
							|  |  |  |     for index in range(DEFAULT_PAGE_SIZE + int(DEFAULT_PAGE_SIZE / 2)): | 
					
						
							|  |  |  |         conn.create_topic("some-topic_" + str(index)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     topics_json = conn.get_all_topics() | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     topics_list = topics_json["ListTopicsResponse"][ | 
					
						
							|  |  |  |         "ListTopicsResult"]["Topics"] | 
					
						
							|  |  |  |     next_token = topics_json["ListTopicsResponse"][ | 
					
						
							|  |  |  |         "ListTopicsResult"]["NextToken"] | 
					
						
							| 
									
										
										
										
											2014-11-29 22:37:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     len(topics_list).should.equal(DEFAULT_PAGE_SIZE) | 
					
						
							|  |  |  |     next_token.should.equal(DEFAULT_PAGE_SIZE) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     topics_json = conn.get_all_topics(next_token=next_token) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     topics_list = topics_json["ListTopicsResponse"][ | 
					
						
							|  |  |  |         "ListTopicsResult"]["Topics"] | 
					
						
							|  |  |  |     next_token = topics_json["ListTopicsResponse"][ | 
					
						
							|  |  |  |         "ListTopicsResult"]["NextToken"] | 
					
						
							| 
									
										
										
										
											2014-11-29 22:37:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     topics_list.should.have.length_of(int(DEFAULT_PAGE_SIZE / 2)) | 
					
						
							|  |  |  |     next_token.should.equal(None) |