| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  | import boto3 | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  | from botocore.exceptions import ClientError | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 14:22:02 +05:30
										 |  |  | from moto import mock_sns, mock_sqs | 
					
						
							| 
									
										
										
										
											2022-08-13 09:49:43 +00:00
										 |  |  | from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID | 
					
						
							| 
									
										
										
										
											2019-11-30 15:51:43 +01:00
										 |  |  | from moto.sns.models import ( | 
					
						
							|  |  |  |     DEFAULT_PAGE_SIZE, | 
					
						
							|  |  |  |     DEFAULT_EFFECTIVE_DELIVERY_POLICY, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-26 00:21:07 +01:00
										 |  |  | @mock_sns | 
					
						
							|  |  |  | def test_subscribe_sms(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client = boto3.client("sns", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2017-09-26 00:21:07 +01:00
										 |  |  |     client.create_topic(Name="some-topic") | 
					
						
							|  |  |  |     resp = client.create_topic(Name="some-topic") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     arn = resp["TopicArn"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.subscribe(TopicArn=arn, Protocol="sms", Endpoint="+15551234567") | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert "SubscriptionArn" in resp | 
					
						
							| 
									
										
										
										
											2019-11-04 22:57:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = client.subscribe(TopicArn=arn, Protocol="sms", Endpoint="+15/55-123.4567") | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert "SubscriptionArn" in resp | 
					
						
							| 
									
										
										
										
											2017-09-26 00:21:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-02 11:30:39 +11:00
										 |  |  | @mock_sns | 
					
						
							|  |  |  | def test_double_subscription(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client = boto3.client("sns", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2018-01-02 11:30:39 +11:00
										 |  |  |     client.create_topic(Name="some-topic") | 
					
						
							|  |  |  |     resp = client.create_topic(Name="some-topic") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     arn = resp["TopicArn"] | 
					
						
							| 
									
										
										
										
											2018-01-02 11:30:39 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-11 20:28:45 -01:00
										 |  |  |     resp1 = client.subscribe( | 
					
						
							|  |  |  |         TopicArn=arn, Protocol="sqs", Endpoint="arn:aws:sqs:elasticmq:000000000000:foo" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     resp2 = client.subscribe( | 
					
						
							|  |  |  |         TopicArn=arn, Protocol="sqs", Endpoint="arn:aws:sqs:elasticmq:000000000000:foo" | 
					
						
							| 
									
										
										
										
											2018-01-02 11:30:39 +11:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert resp1["SubscriptionArn"] == resp2["SubscriptionArn"] | 
					
						
							| 
									
										
										
										
											2018-01-02 11:30:39 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-26 00:21:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | @mock_sns | 
					
						
							|  |  |  | def test_subscribe_bad_sms(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client = boto3.client("sns", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2017-09-26 00:21:07 +01:00
										 |  |  |     client.create_topic(Name="some-topic") | 
					
						
							|  |  |  |     resp = client.create_topic(Name="some-topic") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     arn = resp["TopicArn"] | 
					
						
							| 
									
										
										
										
											2017-09-26 00:21:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         # Test invalid number | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         client.subscribe(TopicArn=arn, Protocol="sms", Endpoint="NAA+15551234567") | 
					
						
							| 
									
										
										
										
											2017-09-26 00:21:07 +01:00
										 |  |  |     except ClientError as err: | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |         assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							| 
									
										
										
										
											2017-09-26 00:21:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     with pytest.raises(ClientError) as client_err: | 
					
						
							|  |  |  |         client.subscribe(TopicArn=arn, Protocol="sms", Endpoint="+15--551234567") | 
					
						
							|  |  |  |     assert ( | 
					
						
							|  |  |  |         client_err.value.response["Error"]["Message"] | 
					
						
							|  |  |  |         == "Invalid SMS endpoint: +15--551234567" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-11-04 22:57:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     with pytest.raises(ClientError) as client_err: | 
					
						
							|  |  |  |         client.subscribe(TopicArn=arn, Protocol="sms", Endpoint="+15551234567.") | 
					
						
							|  |  |  |     assert ( | 
					
						
							|  |  |  |         client_err.value.response["Error"]["Message"] | 
					
						
							|  |  |  |         == "Invalid SMS endpoint: +15551234567." | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-11-04 22:57:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     with pytest.raises(ClientError) as client_err: | 
					
						
							|  |  |  |         assert client.subscribe(TopicArn=arn, Protocol="sms", Endpoint="/+15551234567") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert ( | 
					
						
							|  |  |  |         client_err.value.response["Error"]["Message"] | 
					
						
							|  |  |  |         == "Invalid SMS endpoint: /+15551234567" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-11-04 22:57:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-26 00:21:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | @mock_sns | 
					
						
							|  |  |  | def test_creating_subscription(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto3.client("sns", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  |     conn.create_topic(Name="some-topic") | 
					
						
							|  |  |  |     response = conn.list_topics() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     topic_arn = response["Topics"][0]["TopicArn"] | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.subscribe(TopicArn=topic_arn, Protocol="http", Endpoint="http://example.com/") | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     subscriptions = conn.list_subscriptions()["Subscriptions"] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(subscriptions) == 1 | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  |     subscription = subscriptions[0] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert subscription["TopicArn"] == topic_arn | 
					
						
							|  |  |  |     assert subscription["Protocol"] == "http" | 
					
						
							|  |  |  |     assert topic_arn in subscription["SubscriptionArn"] | 
					
						
							|  |  |  |     assert subscription["Endpoint"] == "http://example.com/" | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Now unsubscribe the subscription | 
					
						
							|  |  |  |     conn.unsubscribe(SubscriptionArn=subscription["SubscriptionArn"]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # And there should be zero subscriptions left | 
					
						
							|  |  |  |     subscriptions = conn.list_subscriptions()["Subscriptions"] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(subscriptions) == 0 | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-31 13:37:29 +02:00
										 |  |  | @mock_sns | 
					
						
							| 
									
										
										
										
											2019-12-27 16:04:12 +01:00
										 |  |  | def test_unsubscribe_from_deleted_topic(): | 
					
						
							|  |  |  |     client = boto3.client("sns", region_name="us-east-1") | 
					
						
							|  |  |  |     client.create_topic(Name="some-topic") | 
					
						
							|  |  |  |     response = client.list_topics() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     topic_arn = response["Topics"][0]["TopicArn"] | 
					
						
							| 
									
										
										
										
											2017-07-31 13:37:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-27 16:04:12 +01:00
										 |  |  |     client.subscribe( | 
					
						
							|  |  |  |         TopicArn=topic_arn, Protocol="http", Endpoint="http://example.com/" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-07-31 13:37:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-27 16:04:12 +01:00
										 |  |  |     subscriptions = client.list_subscriptions()["Subscriptions"] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(subscriptions) == 1 | 
					
						
							| 
									
										
										
										
											2017-07-31 13:37:29 +02:00
										 |  |  |     subscription = subscriptions[0] | 
					
						
							| 
									
										
										
										
											2019-12-27 16:04:12 +01:00
										 |  |  |     subscription_arn = subscription["SubscriptionArn"] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert subscription["TopicArn"] == topic_arn | 
					
						
							|  |  |  |     assert subscription["Protocol"] == "http" | 
					
						
							|  |  |  |     assert topic_arn in subscription_arn | 
					
						
							|  |  |  |     assert subscription["Endpoint"] == "http://example.com/" | 
					
						
							| 
									
										
										
										
											2017-07-31 13:37:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Now delete the topic | 
					
						
							| 
									
										
										
										
											2019-12-27 16:04:12 +01:00
										 |  |  |     client.delete_topic(TopicArn=topic_arn) | 
					
						
							| 
									
										
										
										
											2017-07-31 13:37:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # And there should now be 0 topics | 
					
						
							| 
									
										
										
										
											2019-12-27 16:04:12 +01:00
										 |  |  |     topics_json = client.list_topics() | 
					
						
							| 
									
										
										
										
											2017-07-31 13:37:29 +02:00
										 |  |  |     topics = topics_json["Topics"] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(topics) == 0 | 
					
						
							| 
									
										
										
										
											2017-07-31 13:37:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 14:22:02 +05:30
										 |  |  |     # as per the documentation deleting a topic deletes all the subscriptions | 
					
						
							| 
									
										
										
										
											2019-12-27 16:04:12 +01:00
										 |  |  |     subscriptions = client.list_subscriptions()["Subscriptions"] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(subscriptions) == 0 | 
					
						
							| 
									
										
										
										
											2019-12-27 16:04:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Now delete hanging subscription | 
					
						
							|  |  |  |     client.unsubscribe(SubscriptionArn=subscription_arn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     subscriptions = client.list_subscriptions()["Subscriptions"] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(subscriptions) == 0 | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-27 16:04:12 +01:00
										 |  |  |     # Deleting it again should not result in any error | 
					
						
							|  |  |  |     client.unsubscribe(SubscriptionArn=subscription_arn) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | @mock_sns | 
					
						
							|  |  |  | def test_getting_subscriptions_by_topic(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto3.client("sns", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  |     conn.create_topic(Name="topic1") | 
					
						
							|  |  |  |     conn.create_topic(Name="topic2") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response = conn.list_topics() | 
					
						
							|  |  |  |     topics = response["Topics"] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     topic1_arn = topics[0]["TopicArn"] | 
					
						
							|  |  |  |     topic2_arn = topics[1]["TopicArn"] | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.subscribe( | 
					
						
							|  |  |  |         TopicArn=topic1_arn, Protocol="http", Endpoint="http://example1.com/" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     conn.subscribe( | 
					
						
							|  |  |  |         TopicArn=topic2_arn, Protocol="http", Endpoint="http://example2.com/" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     topic1_subscriptions = conn.list_subscriptions_by_topic(TopicArn=topic1_arn)[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         "Subscriptions" | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(topic1_subscriptions) == 1 | 
					
						
							|  |  |  |     assert topic1_subscriptions[0]["Endpoint"] == "http://example1.com/" | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_sns | 
					
						
							|  |  |  | def test_subscription_paging(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto3.client("sns", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  |     conn.create_topic(Name="topic1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response = conn.list_topics() | 
					
						
							|  |  |  |     topics = response["Topics"] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     topic1_arn = topics[0]["TopicArn"] | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for index in range(DEFAULT_PAGE_SIZE + int(DEFAULT_PAGE_SIZE / 3)): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic1_arn, | 
					
						
							|  |  |  |             Protocol="email", | 
					
						
							|  |  |  |             Endpoint="email_" + str(index) + "@test.com", | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     all_subscriptions = conn.list_subscriptions() | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(all_subscriptions["Subscriptions"]) == DEFAULT_PAGE_SIZE | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  |     next_token = all_subscriptions["NextToken"] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert next_token == str(DEFAULT_PAGE_SIZE) | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     all_subscriptions = conn.list_subscriptions(NextToken=next_token) | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(all_subscriptions["Subscriptions"]) == int(DEFAULT_PAGE_SIZE / 3) | 
					
						
							|  |  |  |     assert "NextToken" not in all_subscriptions | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     topic1_subscriptions = conn.list_subscriptions_by_topic(TopicArn=topic1_arn) | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(topic1_subscriptions["Subscriptions"]) == DEFAULT_PAGE_SIZE | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  |     next_token = topic1_subscriptions["NextToken"] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert next_token == str(DEFAULT_PAGE_SIZE) | 
					
						
							| 
									
										
										
										
											2015-08-20 11:12:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     topic1_subscriptions = conn.list_subscriptions_by_topic( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         TopicArn=topic1_arn, NextToken=next_token | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(topic1_subscriptions["Subscriptions"]) == int(DEFAULT_PAGE_SIZE / 3) | 
					
						
							|  |  |  |     assert "NextToken" not in topic1_subscriptions | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-14 18:03:01 +02:00
										 |  |  | @mock_sns | 
					
						
							|  |  |  | def test_subscribe_attributes(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client = boto3.client("sns", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2019-10-14 18:03:01 +02:00
										 |  |  |     client.create_topic(Name="some-topic") | 
					
						
							|  |  |  |     resp = client.create_topic(Name="some-topic") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     arn = resp["TopicArn"] | 
					
						
							| 
									
										
										
										
											2019-10-14 18:03:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp = client.subscribe(TopicArn=arn, Protocol="http", Endpoint="http://test.com") | 
					
						
							| 
									
										
										
										
											2019-10-14 18:03:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-30 15:51:43 +01:00
										 |  |  |     response = client.get_subscription_attributes( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         SubscriptionArn=resp["SubscriptionArn"] | 
					
						
							| 
									
										
										
										
											2019-10-14 18:03:01 +02:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert "Attributes" in response | 
					
						
							| 
									
										
										
										
											2019-11-30 15:51:43 +01:00
										 |  |  |     attributes = response["Attributes"] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert attributes["PendingConfirmation"] == "false" | 
					
						
							|  |  |  |     assert attributes["ConfirmationWasAuthenticated"] == "true" | 
					
						
							|  |  |  |     assert attributes["Endpoint"] == "http://test.com" | 
					
						
							|  |  |  |     assert attributes["TopicArn"] == arn | 
					
						
							|  |  |  |     assert attributes["Protocol"] == "http" | 
					
						
							|  |  |  |     assert attributes["SubscriptionArn"] == resp["SubscriptionArn"] | 
					
						
							|  |  |  |     assert attributes["Owner"] == str(ACCOUNT_ID) | 
					
						
							|  |  |  |     assert attributes["RawMessageDelivery"] == "false" | 
					
						
							|  |  |  |     assert json.loads(attributes["EffectiveDeliveryPolicy"]) == ( | 
					
						
							| 
									
										
										
										
											2019-11-30 15:51:43 +01:00
										 |  |  |         DEFAULT_EFFECTIVE_DELIVERY_POLICY | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-14 18:03:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-13 19:21:33 +10:00
										 |  |  | @mock_sns | 
					
						
							|  |  |  | def test_creating_subscription_with_attributes(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto3.client("sns", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2018-07-13 19:21:33 +10:00
										 |  |  |     conn.create_topic(Name="some-topic") | 
					
						
							|  |  |  |     response = conn.list_topics() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     topic_arn = response["Topics"][0]["TopicArn"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     delivery_policy = json.dumps( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "healthyRetryPolicy": { | 
					
						
							|  |  |  |                 "numRetries": 10, | 
					
						
							|  |  |  |                 "minDelayTarget": 1, | 
					
						
							|  |  |  |                 "maxDelayTarget": 2, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-07-13 19:21:33 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filter_policy = json.dumps( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "store": ["example_corp"], | 
					
						
							|  |  |  |             "encrypted": [False], | 
					
						
							|  |  |  |             "customer_interests": ["basketball", "baseball"], | 
					
						
							|  |  |  |             "price": [100, 100.12], | 
					
						
							|  |  |  |             "error": [None], | 
					
						
							| 
									
										
										
										
											2018-07-13 19:21:33 +10:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-26 07:57:44 -05:00
										 |  |  |     subscription_role_arn = "arn:aws:iam:000000000:role/test-role" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.subscribe( | 
					
						
							|  |  |  |         TopicArn=topic_arn, | 
					
						
							|  |  |  |         Protocol="http", | 
					
						
							|  |  |  |         Endpoint="http://example.com/", | 
					
						
							|  |  |  |         Attributes={ | 
					
						
							|  |  |  |             "RawMessageDelivery": "true", | 
					
						
							|  |  |  |             "DeliveryPolicy": delivery_policy, | 
					
						
							|  |  |  |             "FilterPolicy": filter_policy, | 
					
						
							| 
									
										
										
										
											2022-07-26 07:57:44 -05:00
										 |  |  |             "SubscriptionRoleArn": subscription_role_arn, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-07-13 19:21:33 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     subscriptions = conn.list_subscriptions()["Subscriptions"] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(subscriptions) == 1 | 
					
						
							| 
									
										
										
										
											2018-07-13 19:21:33 +10:00
										 |  |  |     subscription = subscriptions[0] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert subscription["TopicArn"] == topic_arn | 
					
						
							|  |  |  |     assert subscription["Protocol"] == "http" | 
					
						
							|  |  |  |     assert topic_arn in subscription["SubscriptionArn"] | 
					
						
							|  |  |  |     assert subscription["Endpoint"] == "http://example.com/" | 
					
						
							| 
									
										
										
										
											2018-07-13 19:21:33 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Test the subscription attributes have been set | 
					
						
							|  |  |  |     subscription_arn = subscription["SubscriptionArn"] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     attrs = conn.get_subscription_attributes(SubscriptionArn=subscription_arn) | 
					
						
							| 
									
										
										
										
											2018-07-13 19:21:33 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert attrs["Attributes"]["RawMessageDelivery"] == "true" | 
					
						
							|  |  |  |     assert attrs["Attributes"]["DeliveryPolicy"] == delivery_policy | 
					
						
							|  |  |  |     assert attrs["Attributes"]["FilterPolicy"] == filter_policy | 
					
						
							|  |  |  |     assert attrs["Attributes"]["SubscriptionRoleArn"] == subscription_role_arn | 
					
						
							| 
									
										
										
										
											2018-07-13 19:21:33 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Now unsubscribe the subscription | 
					
						
							|  |  |  |     conn.unsubscribe(SubscriptionArn=subscription["SubscriptionArn"]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # And there should be zero subscriptions left | 
					
						
							|  |  |  |     subscriptions = conn.list_subscriptions()["Subscriptions"] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(subscriptions) == 0 | 
					
						
							| 
									
										
										
										
											2018-07-13 19:21:33 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # invalid attr name | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={"InvalidName": "true"}, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-07-13 19:21:33 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 14:22:02 +05:30
										 |  |  | @mock_sns | 
					
						
							|  |  |  | @mock_sqs | 
					
						
							|  |  |  | def test_delete_subscriptions_on_delete_topic(): | 
					
						
							|  |  |  |     sqs = boto3.client("sqs", region_name="us-east-1") | 
					
						
							|  |  |  |     conn = boto3.client("sns", region_name="us-east-1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     queue = sqs.create_queue(QueueName="test-queue") | 
					
						
							|  |  |  |     topic = conn.create_topic(Name="some-topic") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     conn.subscribe( | 
					
						
							|  |  |  |         TopicArn=topic.get("TopicArn"), Protocol="sqs", Endpoint=queue.get("QueueUrl") | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     subscriptions = conn.list_subscriptions()["Subscriptions"] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(subscriptions) == 1 | 
					
						
							| 
									
										
										
										
											2020-10-29 14:22:02 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     conn.delete_topic(TopicArn=topic.get("TopicArn")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     subscriptions = conn.list_subscriptions()["Subscriptions"] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(subscriptions) == 0 | 
					
						
							| 
									
										
										
										
											2020-10-29 14:22:02 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  | @mock_sns | 
					
						
							|  |  |  | def test_set_subscription_attributes(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto3.client("sns", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  |     conn.create_topic(Name="some-topic") | 
					
						
							|  |  |  |     response = conn.list_topics() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     topic_arn = response["Topics"][0]["TopicArn"] | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.subscribe(TopicArn=topic_arn, Protocol="http", Endpoint="http://example.com/") | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     subscriptions = conn.list_subscriptions()["Subscriptions"] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(subscriptions) == 1 | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  |     subscription = subscriptions[0] | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert subscription["TopicArn"] == topic_arn | 
					
						
							|  |  |  |     assert subscription["Protocol"] == "http" | 
					
						
							|  |  |  |     assert topic_arn in subscription["SubscriptionArn"] | 
					
						
							|  |  |  |     assert subscription["Endpoint"] == "http://example.com/" | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     subscription_arn = subscription["SubscriptionArn"] | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     attrs = conn.get_subscription_attributes(SubscriptionArn=subscription_arn) | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert "Attributes" in attrs | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  |     conn.set_subscription_attributes( | 
					
						
							|  |  |  |         SubscriptionArn=subscription_arn, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         AttributeName="RawMessageDelivery", | 
					
						
							|  |  |  |         AttributeValue="true", | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     delivery_policy = json.dumps( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "healthyRetryPolicy": { | 
					
						
							|  |  |  |                 "numRetries": 10, | 
					
						
							|  |  |  |                 "minDelayTarget": 1, | 
					
						
							|  |  |  |                 "maxDelayTarget": 2, | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  |     conn.set_subscription_attributes( | 
					
						
							|  |  |  |         SubscriptionArn=subscription_arn, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         AttributeName="DeliveryPolicy", | 
					
						
							|  |  |  |         AttributeValue=delivery_policy, | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-03-21 15:49:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     filter_policy = json.dumps( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "store": ["example_corp"], | 
					
						
							|  |  |  |             "encrypted": [False], | 
					
						
							|  |  |  |             "customer_interests": ["basketball", "baseball"], | 
					
						
							|  |  |  |             "price": [100, 100.12], | 
					
						
							|  |  |  |             "error": [None], | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-03-21 15:49:11 +00:00
										 |  |  |     conn.set_subscription_attributes( | 
					
						
							|  |  |  |         SubscriptionArn=subscription_arn, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         AttributeName="FilterPolicy", | 
					
						
							|  |  |  |         AttributeValue=filter_policy, | 
					
						
							| 
									
										
										
										
											2018-03-21 15:49:11 +00:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     attrs = conn.get_subscription_attributes(SubscriptionArn=subscription_arn) | 
					
						
							| 
									
										
										
										
											2018-03-21 15:49:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert attrs["Attributes"]["RawMessageDelivery"] == "true" | 
					
						
							|  |  |  |     assert attrs["Attributes"]["DeliveryPolicy"] == delivery_policy | 
					
						
							|  |  |  |     assert attrs["Attributes"]["FilterPolicy"] == filter_policy | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 19:24:44 +02:00
										 |  |  |     filter_policy_scope = "MessageBody" | 
					
						
							|  |  |  |     conn.set_subscription_attributes( | 
					
						
							|  |  |  |         SubscriptionArn=subscription_arn, | 
					
						
							|  |  |  |         AttributeName="FilterPolicyScope", | 
					
						
							|  |  |  |         AttributeValue=filter_policy_scope, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     attrs = conn.get_subscription_attributes(SubscriptionArn=subscription_arn) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert attrs["Attributes"]["FilterPolicyScope"] == filter_policy_scope | 
					
						
							| 
									
										
										
										
											2023-05-15 19:24:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  |     # not existing subscription | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError): | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  |         conn.set_subscription_attributes( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             SubscriptionArn="invalid", | 
					
						
							|  |  |  |             AttributeName="RawMessageDelivery", | 
					
						
							|  |  |  |             AttributeValue="true", | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         attrs = conn.get_subscription_attributes(SubscriptionArn="invalid") | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # invalid attr name | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError): | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  |         conn.set_subscription_attributes( | 
					
						
							|  |  |  |             SubscriptionArn=subscription_arn, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             AttributeName="InvalidName", | 
					
						
							|  |  |  |             AttributeValue="true", | 
					
						
							| 
									
										
										
										
											2017-09-08 03:19:34 +09:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 16:48:14 +02:00
										 |  |  | @mock_sns | 
					
						
							|  |  |  | def test_subscribe_invalid_filter_policy(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto3.client("sns", region_name="us-east-1") | 
					
						
							|  |  |  |     conn.create_topic(Name="some-topic") | 
					
						
							| 
									
										
										
										
											2019-08-25 16:48:14 +02:00
										 |  |  |     response = conn.list_topics() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     topic_arn = response["Topics"][0]["TopicArn"] | 
					
						
							| 
									
										
										
										
											2019-08-25 16:48:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |     with pytest.raises(ClientError) as err_info: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={ | 
					
						
							|  |  |  |                 "FilterPolicy": json.dumps({"store": [str(i) for i in range(151)]}) | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2019-08-25 16:48:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |     err = err_info.value | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |     assert err.response["Error"]["Message"] == ( | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |         "Invalid parameter: FilterPolicy: Filter policy is too complex" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as err_info: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={"FilterPolicy": json.dumps({"store": [["example_corp"]]})}, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2019-08-25 16:48:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |     err = err_info.value | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |     assert err.response["Error"]["Message"] == ( | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |         "Invalid parameter: FilterPolicy: Match value must be String, number, true, false, or null" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as err_info: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={"FilterPolicy": json.dumps({"store": [{"exists": None}]})}, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2019-08-25 16:48:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |     err = err_info.value | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |     assert err.response["Error"]["Message"] == ( | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |         "Invalid parameter: FilterPolicy: exists match pattern must be either true or false." | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as err_info: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={"FilterPolicy": json.dumps({"store": [{"error": True}]})}, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2019-08-25 16:48:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |     err = err_info.value | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |     assert err.response["Error"]["Message"] == ( | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |         "Invalid parameter: FilterPolicy: Unrecognized match type error" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as err_info: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={"FilterPolicy": json.dumps({"store": [1000000001]})}, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     err = err_info.value | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert err.response["Error"]["Code"] == "InternalFailure" | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as err_info: | 
					
						
							|  |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={ | 
					
						
							|  |  |  |                 "FilterPolicy": json.dumps({"price": [{"numeric": ["<", "100"]}]}) | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     err = err_info.value | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |     assert err.response["Error"]["Message"] == ( | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |         "Invalid parameter: Attributes Reason: FilterPolicy: Value of < must be numeric\n at ..." | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as err_info: | 
					
						
							|  |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={ | 
					
						
							|  |  |  |                 "FilterPolicy": json.dumps( | 
					
						
							|  |  |  |                     {"price": [{"numeric": [">", 50, "<=", "100"]}]} | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     err = err_info.value | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |     assert err.response["Error"]["Message"] == ( | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |         "Invalid parameter: Attributes Reason: FilterPolicy: Value of <= must be numeric\n at ..." | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as err_info: | 
					
						
							|  |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={"FilterPolicy": json.dumps({"price": [{"numeric": []}]})}, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     err = err_info.value | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |     assert err.response["Error"]["Message"] == ( | 
					
						
							|  |  |  |         "Invalid parameter: Attributes Reason: FilterPolicy: " | 
					
						
							|  |  |  |         "Invalid member in numeric match: ]\n at ..." | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as err_info: | 
					
						
							|  |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={ | 
					
						
							|  |  |  |                 "FilterPolicy": json.dumps({"price": [{"numeric": [50, "<=", "100"]}]}) | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     err = err_info.value | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |     assert err.response["Error"]["Message"] == ( | 
					
						
							|  |  |  |         "Invalid parameter: Attributes Reason: FilterPolicy: Invalid " | 
					
						
							|  |  |  |         "member in numeric match: 50\n at ..." | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as err_info: | 
					
						
							|  |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={"FilterPolicy": json.dumps({"price": [{"numeric": ["<"]}]})}, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     err = err_info.value | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |     assert err.response["Error"]["Message"] == ( | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |         "Invalid parameter: Attributes Reason: FilterPolicy: Value of < must be numeric\n at ..." | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as err_info: | 
					
						
							|  |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={"FilterPolicy": json.dumps({"price": [{"numeric": ["0"]}]})}, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     err = err_info.value | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |     assert err.response["Error"]["Message"] == ( | 
					
						
							|  |  |  |         "Invalid parameter: Attributes Reason: FilterPolicy: " | 
					
						
							|  |  |  |         "Unrecognized numeric range operator: 0\n at ..." | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as err_info: | 
					
						
							|  |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={ | 
					
						
							|  |  |  |                 "FilterPolicy": json.dumps({"price": [{"numeric": ["<", 20, ">", 1]}]}) | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     err = err_info.value | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |     assert err.response["Error"]["Message"] == ( | 
					
						
							|  |  |  |         "Invalid parameter: Attributes Reason: FilterPolicy: Too many " | 
					
						
							|  |  |  |         "elements in numeric expression\n at ..." | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as err_info: | 
					
						
							|  |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={ | 
					
						
							|  |  |  |                 "FilterPolicy": json.dumps({"price": [{"numeric": [">", 20, ">", 1]}]}) | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     err = err_info.value | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |     assert err.response["Error"]["Message"] == ( | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |         "Invalid parameter: Attributes Reason: FilterPolicy: Bad numeric range operator: >\n at ..." | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as err_info: | 
					
						
							|  |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={ | 
					
						
							|  |  |  |                 "FilterPolicy": json.dumps({"price": [{"numeric": [">", 20, "<", 1]}]}) | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     err = err_info.value | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |     assert err.response["Error"]["Message"] == ( | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |         "Invalid parameter: Attributes Reason: FilterPolicy: Bottom must be less than top\n at ..." | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as err_info: | 
					
						
							|  |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={ | 
					
						
							|  |  |  |                 "FilterPolicy": json.dumps({"price": [{"numeric": [">", 20, "<"]}]}) | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     err = err_info.value | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |     assert err.response["Error"]["Message"] == ( | 
					
						
							| 
									
										
										
										
											2023-04-24 12:05:21 +02:00
										 |  |  |         "Invalid parameter: Attributes Reason: FilterPolicy: Value of < must be numeric\n at ..." | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 19:24:44 +02:00
										 |  |  |     try: | 
					
						
							|  |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={ | 
					
						
							|  |  |  |                 "FilterPolicy": json.dumps({"store": {"key": [{"exists": None}]}}) | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     except ClientError as err: | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |         assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |         assert err.response["Error"]["Message"] == ( | 
					
						
							|  |  |  |             "Invalid parameter: Filter policy scope MessageAttributes does " | 
					
						
							|  |  |  |             "not support nested filter policy" | 
					
						
							| 
									
										
										
										
											2023-05-15 19:24:44 +02:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         filter_policy = { | 
					
						
							|  |  |  |             "key_a": ["value_one"], | 
					
						
							|  |  |  |             "key_b": ["value_two"], | 
					
						
							|  |  |  |             "key_c": ["value_three"], | 
					
						
							|  |  |  |             "key_d": ["value_four"], | 
					
						
							|  |  |  |             "key_e": ["value_five"], | 
					
						
							|  |  |  |             "key_f": ["value_six"], | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={"FilterPolicy": json.dumps(filter_policy)}, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     except ClientError as err: | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |         assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |         assert err.response["Error"]["Message"] == ( | 
					
						
							| 
									
										
										
										
											2023-05-15 19:24:44 +02:00
										 |  |  |             "Invalid parameter: FilterPolicy: Filter policy can not have more than 5 keys" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         nested_filter_policy = { | 
					
						
							|  |  |  |             "key_a": { | 
					
						
							|  |  |  |                 "key_b": { | 
					
						
							|  |  |  |                     "key_c": ["value_one", "value_two", "value_three", "value_four"] | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             "key_d": {"key_e": ["value_one", "value_two", "value_three"]}, | 
					
						
							|  |  |  |             "key_f": ["value_one", "value_two", "value_three"], | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |         # The first array has four values in a three-level nested key, | 
					
						
							|  |  |  |         # and the second has three values in a two-level nested key. The | 
					
						
							|  |  |  |         # total combination is calculated as follows: | 
					
						
							| 
									
										
										
										
											2023-05-15 19:24:44 +02:00
										 |  |  |         # 3 x 4 x 2 x 3 x 1 x 3 = 216 | 
					
						
							|  |  |  |         conn.subscribe( | 
					
						
							|  |  |  |             TopicArn=topic_arn, | 
					
						
							|  |  |  |             Protocol="http", | 
					
						
							|  |  |  |             Endpoint="http://example.com/", | 
					
						
							|  |  |  |             Attributes={ | 
					
						
							|  |  |  |                 "FilterPolicyScope": "MessageBody", | 
					
						
							|  |  |  |                 "FilterPolicy": json.dumps(nested_filter_policy), | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     except ClientError as err: | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |         assert err.response["Error"]["Code"] == "InvalidParameter" | 
					
						
							|  |  |  |         assert err.response["Error"]["Message"] == ( | 
					
						
							| 
									
										
										
										
											2023-05-15 19:24:44 +02:00
										 |  |  |             "Invalid parameter: FilterPolicy: Filter policy is too complex" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 16:48:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  | @mock_sns | 
					
						
							|  |  |  | def test_check_not_opted_out(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto3.client("sns", region_name="us-east-1") | 
					
						
							|  |  |  |     response = conn.check_if_phone_number_is_opted_out(phoneNumber="+447428545375") | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert "isOptedOut" in response | 
					
						
							|  |  |  |     assert response["isOptedOut"] is False | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_sns | 
					
						
							|  |  |  | def test_check_opted_out(): | 
					
						
							|  |  |  |     # Phone number ends in 99 so is hardcoded in the endpoint to return opted | 
					
						
							|  |  |  |     # out status | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto3.client("sns", region_name="us-east-1") | 
					
						
							|  |  |  |     response = conn.check_if_phone_number_is_opted_out(phoneNumber="+447428545399") | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert "isOptedOut" in response | 
					
						
							|  |  |  |     assert response["isOptedOut"] is True | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_sns | 
					
						
							|  |  |  | def test_check_opted_out_invalid(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto3.client("sns", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Invalid phone number | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         conn.check_if_phone_number_is_opted_out(phoneNumber="+44742LALALA") | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_sns | 
					
						
							|  |  |  | def test_list_opted_out(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto3.client("sns", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  |     response = conn.list_phone_numbers_opted_out() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert "phoneNumbers" in response | 
					
						
							|  |  |  |     assert len(response["phoneNumbers"]) > 0 | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_sns | 
					
						
							|  |  |  | def test_opt_in(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto3.client("sns", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  |     response = conn.list_phone_numbers_opted_out() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     current_len = len(response["phoneNumbers"]) | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  |     assert current_len > 0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn.opt_in_phone_number(phoneNumber=response["phoneNumbers"][0]) | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     response = conn.list_phone_numbers_opted_out() | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     assert len(response["phoneNumbers"]) > 0 | 
					
						
							|  |  |  |     assert len(response["phoneNumbers"]) < current_len | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_sns | 
					
						
							|  |  |  | def test_confirm_subscription(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     conn = boto3.client("sns", region_name="us-east-1") | 
					
						
							|  |  |  |     response = conn.create_topic(Name="testconfirm") | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     conn.confirm_subscription( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         TopicArn=response["TopicArn"], | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |         Token=( | 
					
						
							|  |  |  |             "2336412f37fb687f5d51e6e241d59b68c4e583a5cee0be6f95bbf97ab8d" | 
					
						
							|  |  |  |             "2441cf47b99e848408adaadf4c197e65f03473d53c4ba398f6abbf38ce2" | 
					
						
							|  |  |  |             "e8ebf7b4ceceb2cd817959bcde1357e58a2861b05288c535822eb88cac3" | 
					
						
							|  |  |  |             "db04f592285249971efc6484194fc4a4586147f16916692" | 
					
						
							|  |  |  |         ), | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         AuthenticateOnUnsubscribe="true", | 
					
						
							| 
									
										
										
										
											2017-09-21 21:16:00 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2021-08-27 00:23:17 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_sns | 
					
						
							|  |  |  | def test_get_subscription_attributes_error_not_exists(): | 
					
						
							|  |  |  |     # given | 
					
						
							|  |  |  |     client = boto3.client("sns", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     sub_arn = ( | 
					
						
							|  |  |  |         f"arn:aws:sqs:us-east-1:{ACCOUNT_ID}:test-queue" | 
					
						
							|  |  |  |         ":66d97e76-31e5-444f-8fa7-b60b680d0d39" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2021-08-27 00:23:17 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # when | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     with pytest.raises(ClientError) as exc: | 
					
						
							| 
									
										
										
										
											2021-08-27 00:23:17 +09:00
										 |  |  |         client.get_subscription_attributes(SubscriptionArn=sub_arn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # then | 
					
						
							| 
									
										
										
										
											2023-08-10 14:07:41 -04:00
										 |  |  |     exc_value = exc.value | 
					
						
							|  |  |  |     assert exc_value.operation_name == "GetSubscriptionAttributes" | 
					
						
							|  |  |  |     assert exc_value.response["ResponseMetadata"]["HTTPStatusCode"] == 404 | 
					
						
							|  |  |  |     assert "NotFound" in exc_value.response["Error"]["Code"] | 
					
						
							|  |  |  |     assert exc_value.response["Error"]["Message"] == "Subscription does not exist" |