2015-08-20 15:12:25 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
import boto3
|
2017-09-07 18:19:34 +00:00
|
|
|
import json
|
2015-08-20 15:12:25 +00:00
|
|
|
|
|
|
|
import sure # noqa
|
|
|
|
|
2017-09-07 18:19:34 +00:00
|
|
|
from botocore.exceptions import ClientError
|
|
|
|
from nose.tools import assert_raises
|
|
|
|
|
2020-10-29 08:52:02 +00:00
|
|
|
from moto import mock_sns, mock_sqs
|
2019-11-30 14:51:43 +00:00
|
|
|
from moto.sns.models import (
|
|
|
|
DEFAULT_PAGE_SIZE,
|
|
|
|
DEFAULT_EFFECTIVE_DELIVERY_POLICY,
|
|
|
|
DEFAULT_ACCOUNT_ID,
|
|
|
|
)
|
2015-08-20 15:12:25 +00:00
|
|
|
|
|
|
|
|
2017-09-25 23:21:07 +00:00
|
|
|
@mock_sns
|
|
|
|
def test_subscribe_sms():
|
2019-10-31 15:44:26 +00:00
|
|
|
client = boto3.client("sns", region_name="us-east-1")
|
2017-09-25 23:21:07 +00:00
|
|
|
client.create_topic(Name="some-topic")
|
|
|
|
resp = client.create_topic(Name="some-topic")
|
2019-10-31 15:44:26 +00:00
|
|
|
arn = resp["TopicArn"]
|
|
|
|
|
|
|
|
resp = client.subscribe(TopicArn=arn, Protocol="sms", Endpoint="+15551234567")
|
2019-11-04 21:57:53 +00:00
|
|
|
resp.should.have.key("SubscriptionArn")
|
|
|
|
|
|
|
|
resp = client.subscribe(TopicArn=arn, Protocol="sms", Endpoint="+15/55-123.4567")
|
|
|
|
resp.should.have.key("SubscriptionArn")
|
2017-09-25 23:21:07 +00:00
|
|
|
|
|
|
|
|
2018-01-02 00:30:39 +00:00
|
|
|
@mock_sns
|
|
|
|
def test_double_subscription():
|
2019-10-31 15:44:26 +00:00
|
|
|
client = boto3.client("sns", region_name="us-east-1")
|
2018-01-02 00:30:39 +00:00
|
|
|
client.create_topic(Name="some-topic")
|
|
|
|
resp = client.create_topic(Name="some-topic")
|
2019-10-31 15:44:26 +00:00
|
|
|
arn = resp["TopicArn"]
|
2018-01-02 00:30:39 +00:00
|
|
|
|
|
|
|
do_subscribe_sqs = lambda sqs_arn: client.subscribe(
|
2019-10-31 15:44:26 +00:00
|
|
|
TopicArn=arn, Protocol="sqs", Endpoint=sqs_arn
|
2018-01-02 00:30:39 +00:00
|
|
|
)
|
2019-10-31 15:44:26 +00:00
|
|
|
resp1 = do_subscribe_sqs("arn:aws:sqs:elasticmq:000000000000:foo")
|
|
|
|
resp2 = do_subscribe_sqs("arn:aws:sqs:elasticmq:000000000000:foo")
|
2018-01-02 00:30:39 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
resp1["SubscriptionArn"].should.equal(resp2["SubscriptionArn"])
|
2018-01-02 00:30:39 +00:00
|
|
|
|
2017-09-25 23:21:07 +00:00
|
|
|
|
|
|
|
@mock_sns
|
|
|
|
def test_subscribe_bad_sms():
|
2019-10-31 15:44:26 +00:00
|
|
|
client = boto3.client("sns", region_name="us-east-1")
|
2017-09-25 23:21:07 +00:00
|
|
|
client.create_topic(Name="some-topic")
|
|
|
|
resp = client.create_topic(Name="some-topic")
|
2019-10-31 15:44:26 +00:00
|
|
|
arn = resp["TopicArn"]
|
2017-09-25 23:21:07 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# Test invalid number
|
2019-10-31 15:44:26 +00:00
|
|
|
client.subscribe(TopicArn=arn, Protocol="sms", Endpoint="NAA+15551234567")
|
2017-09-25 23:21:07 +00:00
|
|
|
except ClientError as err:
|
2019-10-31 15:44:26 +00:00
|
|
|
err.response["Error"]["Code"].should.equal("InvalidParameter")
|
2017-09-25 23:21:07 +00:00
|
|
|
|
2019-11-04 21:57:53 +00:00
|
|
|
client.subscribe.when.called_with(
|
|
|
|
TopicArn=arn, Protocol="sms", Endpoint="+15--551234567"
|
|
|
|
).should.throw(ClientError, "Invalid SMS endpoint: +15--551234567")
|
|
|
|
|
|
|
|
client.subscribe.when.called_with(
|
|
|
|
TopicArn=arn, Protocol="sms", Endpoint="+15551234567."
|
|
|
|
).should.throw(ClientError, "Invalid SMS endpoint: +15551234567.")
|
|
|
|
|
|
|
|
client.subscribe.when.called_with(
|
|
|
|
TopicArn=arn, Protocol="sms", Endpoint="/+15551234567"
|
|
|
|
).should.throw(ClientError, "Invalid SMS endpoint: /+15551234567")
|
|
|
|
|
2017-09-25 23:21:07 +00:00
|
|
|
|
2015-08-20 15:12:25 +00:00
|
|
|
@mock_sns
|
|
|
|
def test_creating_subscription():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
2015-08-20 15:12:25 +00:00
|
|
|
conn.create_topic(Name="some-topic")
|
|
|
|
response = conn.list_topics()
|
2019-10-31 15:44:26 +00:00
|
|
|
topic_arn = response["Topics"][0]["TopicArn"]
|
2015-08-20 15:12:25 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.subscribe(TopicArn=topic_arn, Protocol="http", Endpoint="http://example.com/")
|
2015-08-20 15:12:25 +00:00
|
|
|
|
|
|
|
subscriptions = conn.list_subscriptions()["Subscriptions"]
|
|
|
|
subscriptions.should.have.length_of(1)
|
|
|
|
subscription = subscriptions[0]
|
|
|
|
subscription["TopicArn"].should.equal(topic_arn)
|
|
|
|
subscription["Protocol"].should.equal("http")
|
|
|
|
subscription["SubscriptionArn"].should.contain(topic_arn)
|
|
|
|
subscription["Endpoint"].should.equal("http://example.com/")
|
|
|
|
|
|
|
|
# Now unsubscribe the subscription
|
|
|
|
conn.unsubscribe(SubscriptionArn=subscription["SubscriptionArn"])
|
|
|
|
|
|
|
|
# And there should be zero subscriptions left
|
|
|
|
subscriptions = conn.list_subscriptions()["Subscriptions"]
|
|
|
|
subscriptions.should.have.length_of(0)
|
|
|
|
|
2017-09-21 20:16:00 +00:00
|
|
|
|
2017-07-31 11:37:29 +00:00
|
|
|
@mock_sns
|
2019-12-27 15:04:12 +00: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 15:44:26 +00:00
|
|
|
topic_arn = response["Topics"][0]["TopicArn"]
|
2017-07-31 11:37:29 +00:00
|
|
|
|
2019-12-27 15:04:12 +00:00
|
|
|
client.subscribe(
|
|
|
|
TopicArn=topic_arn, Protocol="http", Endpoint="http://example.com/"
|
|
|
|
)
|
2017-07-31 11:37:29 +00:00
|
|
|
|
2019-12-27 15:04:12 +00:00
|
|
|
subscriptions = client.list_subscriptions()["Subscriptions"]
|
2017-07-31 11:37:29 +00:00
|
|
|
subscriptions.should.have.length_of(1)
|
|
|
|
subscription = subscriptions[0]
|
2019-12-27 15:04:12 +00:00
|
|
|
subscription_arn = subscription["SubscriptionArn"]
|
2017-07-31 11:37:29 +00:00
|
|
|
subscription["TopicArn"].should.equal(topic_arn)
|
|
|
|
subscription["Protocol"].should.equal("http")
|
2019-12-27 15:04:12 +00:00
|
|
|
subscription_arn.should.contain(topic_arn)
|
2017-07-31 11:37:29 +00:00
|
|
|
subscription["Endpoint"].should.equal("http://example.com/")
|
|
|
|
|
|
|
|
# Now delete the topic
|
2019-12-27 15:04:12 +00:00
|
|
|
client.delete_topic(TopicArn=topic_arn)
|
2017-07-31 11:37:29 +00:00
|
|
|
|
|
|
|
# And there should now be 0 topics
|
2019-12-27 15:04:12 +00:00
|
|
|
topics_json = client.list_topics()
|
2017-07-31 11:37:29 +00:00
|
|
|
topics = topics_json["Topics"]
|
|
|
|
topics.should.have.length_of(0)
|
|
|
|
|
2020-10-29 08:52:02 +00:00
|
|
|
# as per the documentation deleting a topic deletes all the subscriptions
|
2019-12-27 15:04:12 +00:00
|
|
|
subscriptions = client.list_subscriptions()["Subscriptions"]
|
2020-10-29 08:52:02 +00:00
|
|
|
subscriptions.should.have.length_of(0)
|
2019-12-27 15:04:12 +00:00
|
|
|
|
|
|
|
# Now delete hanging subscription
|
|
|
|
client.unsubscribe(SubscriptionArn=subscription_arn)
|
|
|
|
|
|
|
|
subscriptions = client.list_subscriptions()["Subscriptions"]
|
2017-07-31 11:37:29 +00:00
|
|
|
subscriptions.should.have.length_of(0)
|
2015-08-20 15:12:25 +00:00
|
|
|
|
2019-12-27 15:04:12 +00:00
|
|
|
# Deleting it again should not result in any error
|
|
|
|
client.unsubscribe(SubscriptionArn=subscription_arn)
|
|
|
|
|
2017-09-21 20:16:00 +00:00
|
|
|
|
2015-08-20 15:12:25 +00:00
|
|
|
@mock_sns
|
|
|
|
def test_getting_subscriptions_by_topic():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
2015-08-20 15:12:25 +00:00
|
|
|
conn.create_topic(Name="topic1")
|
|
|
|
conn.create_topic(Name="topic2")
|
|
|
|
|
|
|
|
response = conn.list_topics()
|
|
|
|
topics = response["Topics"]
|
2019-10-31 15:44:26 +00:00
|
|
|
topic1_arn = topics[0]["TopicArn"]
|
|
|
|
topic2_arn = topics[1]["TopicArn"]
|
2015-08-20 15:12:25 +00:00
|
|
|
|
2019-10-31 15:44:26 +00: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 15:12:25 +00:00
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
topic1_subscriptions = conn.list_subscriptions_by_topic(TopicArn=topic1_arn)[
|
2019-10-31 15:44:26 +00:00
|
|
|
"Subscriptions"
|
|
|
|
]
|
2015-08-20 15:12:25 +00:00
|
|
|
topic1_subscriptions.should.have.length_of(1)
|
2019-10-31 15:44:26 +00:00
|
|
|
topic1_subscriptions[0]["Endpoint"].should.equal("http://example1.com/")
|
2015-08-20 15:12:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sns
|
|
|
|
def test_subscription_paging():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
2015-08-20 15:12:25 +00:00
|
|
|
conn.create_topic(Name="topic1")
|
|
|
|
|
|
|
|
response = conn.list_topics()
|
|
|
|
topics = response["Topics"]
|
2019-10-31 15:44:26 +00:00
|
|
|
topic1_arn = topics[0]["TopicArn"]
|
2015-08-20 15:12:25 +00:00
|
|
|
|
|
|
|
for index in range(DEFAULT_PAGE_SIZE + int(DEFAULT_PAGE_SIZE / 3)):
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.subscribe(
|
|
|
|
TopicArn=topic1_arn,
|
|
|
|
Protocol="email",
|
|
|
|
Endpoint="email_" + str(index) + "@test.com",
|
|
|
|
)
|
2015-08-20 15:12:25 +00:00
|
|
|
|
|
|
|
all_subscriptions = conn.list_subscriptions()
|
|
|
|
all_subscriptions["Subscriptions"].should.have.length_of(DEFAULT_PAGE_SIZE)
|
|
|
|
next_token = all_subscriptions["NextToken"]
|
|
|
|
next_token.should.equal(str(DEFAULT_PAGE_SIZE))
|
|
|
|
|
|
|
|
all_subscriptions = conn.list_subscriptions(NextToken=next_token)
|
2019-10-31 15:44:26 +00:00
|
|
|
all_subscriptions["Subscriptions"].should.have.length_of(int(DEFAULT_PAGE_SIZE / 3))
|
2015-08-20 15:12:25 +00:00
|
|
|
all_subscriptions.shouldnt.have("NextToken")
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
topic1_subscriptions = conn.list_subscriptions_by_topic(TopicArn=topic1_arn)
|
|
|
|
topic1_subscriptions["Subscriptions"].should.have.length_of(DEFAULT_PAGE_SIZE)
|
2015-08-20 15:12:25 +00:00
|
|
|
next_token = topic1_subscriptions["NextToken"]
|
|
|
|
next_token.should.equal(str(DEFAULT_PAGE_SIZE))
|
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
topic1_subscriptions = conn.list_subscriptions_by_topic(
|
2019-10-31 15:44:26 +00:00
|
|
|
TopicArn=topic1_arn, NextToken=next_token
|
|
|
|
)
|
2017-02-24 02:37:43 +00:00
|
|
|
topic1_subscriptions["Subscriptions"].should.have.length_of(
|
2019-10-31 15:44:26 +00:00
|
|
|
int(DEFAULT_PAGE_SIZE / 3)
|
|
|
|
)
|
2015-08-20 15:12:25 +00:00
|
|
|
topic1_subscriptions.shouldnt.have("NextToken")
|
2017-09-07 18:19:34 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2019-10-14 16:03:01 +00:00
|
|
|
@mock_sns
|
|
|
|
def test_subscribe_attributes():
|
2019-10-31 15:44:26 +00:00
|
|
|
client = boto3.client("sns", region_name="us-east-1")
|
2019-10-14 16:03:01 +00:00
|
|
|
client.create_topic(Name="some-topic")
|
|
|
|
resp = client.create_topic(Name="some-topic")
|
2019-10-31 15:44:26 +00:00
|
|
|
arn = resp["TopicArn"]
|
2019-10-14 16:03:01 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
resp = client.subscribe(TopicArn=arn, Protocol="http", Endpoint="http://test.com")
|
2019-10-14 16:03:01 +00:00
|
|
|
|
2019-11-30 14:51:43 +00:00
|
|
|
response = client.get_subscription_attributes(
|
2019-10-31 15:44:26 +00:00
|
|
|
SubscriptionArn=resp["SubscriptionArn"]
|
2019-10-14 16:03:01 +00:00
|
|
|
)
|
|
|
|
|
2019-11-30 14:51:43 +00:00
|
|
|
response.should.contain("Attributes")
|
|
|
|
attributes = response["Attributes"]
|
|
|
|
attributes["PendingConfirmation"].should.equal("false")
|
|
|
|
attributes["ConfirmationWasAuthenticated"].should.equal("true")
|
|
|
|
attributes["Endpoint"].should.equal("http://test.com")
|
|
|
|
attributes["TopicArn"].should.equal(arn)
|
|
|
|
attributes["Protocol"].should.equal("http")
|
|
|
|
attributes["SubscriptionArn"].should.equal(resp["SubscriptionArn"])
|
|
|
|
attributes["Owner"].should.equal(str(DEFAULT_ACCOUNT_ID))
|
|
|
|
attributes["RawMessageDelivery"].should.equal("false")
|
|
|
|
json.loads(attributes["EffectiveDeliveryPolicy"]).should.equal(
|
|
|
|
DEFAULT_EFFECTIVE_DELIVERY_POLICY
|
|
|
|
)
|
2019-10-14 16:03:01 +00:00
|
|
|
|
2017-09-07 18:19:34 +00:00
|
|
|
|
2018-07-13 09:21:33 +00:00
|
|
|
@mock_sns
|
|
|
|
def test_creating_subscription_with_attributes():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
2018-07-13 09:21:33 +00:00
|
|
|
conn.create_topic(Name="some-topic")
|
|
|
|
response = conn.list_topics()
|
2019-10-31 15:44:26 +00:00
|
|
|
topic_arn = response["Topics"][0]["TopicArn"]
|
|
|
|
|
|
|
|
delivery_policy = json.dumps(
|
|
|
|
{
|
|
|
|
"healthyRetryPolicy": {
|
|
|
|
"numRetries": 10,
|
|
|
|
"minDelayTarget": 1,
|
|
|
|
"maxDelayTarget": 2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2018-07-13 09:21:33 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
filter_policy = json.dumps(
|
|
|
|
{
|
|
|
|
"store": ["example_corp"],
|
|
|
|
"event": ["order_cancelled"],
|
|
|
|
"encrypted": [False],
|
|
|
|
"customer_interests": ["basketball", "baseball"],
|
|
|
|
"price": [100, 100.12],
|
|
|
|
"error": [None],
|
2018-07-13 09:21:33 +00:00
|
|
|
}
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
conn.subscribe(
|
|
|
|
TopicArn=topic_arn,
|
|
|
|
Protocol="http",
|
|
|
|
Endpoint="http://example.com/",
|
|
|
|
Attributes={
|
|
|
|
"RawMessageDelivery": "true",
|
|
|
|
"DeliveryPolicy": delivery_policy,
|
|
|
|
"FilterPolicy": filter_policy,
|
|
|
|
},
|
|
|
|
)
|
2018-07-13 09:21:33 +00:00
|
|
|
|
|
|
|
subscriptions = conn.list_subscriptions()["Subscriptions"]
|
|
|
|
subscriptions.should.have.length_of(1)
|
|
|
|
subscription = subscriptions[0]
|
|
|
|
subscription["TopicArn"].should.equal(topic_arn)
|
|
|
|
subscription["Protocol"].should.equal("http")
|
|
|
|
subscription["SubscriptionArn"].should.contain(topic_arn)
|
|
|
|
subscription["Endpoint"].should.equal("http://example.com/")
|
|
|
|
|
|
|
|
# Test the subscription attributes have been set
|
|
|
|
subscription_arn = subscription["SubscriptionArn"]
|
2019-10-31 15:44:26 +00:00
|
|
|
attrs = conn.get_subscription_attributes(SubscriptionArn=subscription_arn)
|
2018-07-13 09:21:33 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
attrs["Attributes"]["RawMessageDelivery"].should.equal("true")
|
|
|
|
attrs["Attributes"]["DeliveryPolicy"].should.equal(delivery_policy)
|
|
|
|
attrs["Attributes"]["FilterPolicy"].should.equal(filter_policy)
|
2018-07-13 09:21:33 +00:00
|
|
|
|
|
|
|
# Now unsubscribe the subscription
|
|
|
|
conn.unsubscribe(SubscriptionArn=subscription["SubscriptionArn"])
|
|
|
|
|
|
|
|
# And there should be zero subscriptions left
|
|
|
|
subscriptions = conn.list_subscriptions()["Subscriptions"]
|
|
|
|
subscriptions.should.have.length_of(0)
|
|
|
|
|
|
|
|
# invalid attr name
|
|
|
|
with assert_raises(ClientError):
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.subscribe(
|
|
|
|
TopicArn=topic_arn,
|
|
|
|
Protocol="http",
|
|
|
|
Endpoint="http://example.com/",
|
|
|
|
Attributes={"InvalidName": "true"},
|
|
|
|
)
|
2018-07-13 09:21:33 +00:00
|
|
|
|
|
|
|
|
2020-10-29 08:52:02 +00:00
|
|
|
@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"]
|
|
|
|
|
|
|
|
subscriptions.should.have.length_of(1)
|
|
|
|
|
|
|
|
conn.delete_topic(TopicArn=topic.get("TopicArn"))
|
|
|
|
|
|
|
|
subscriptions = conn.list_subscriptions()["Subscriptions"]
|
|
|
|
subscriptions.should.have.length_of(0)
|
|
|
|
|
|
|
|
|
2017-09-07 18:19:34 +00:00
|
|
|
@mock_sns
|
|
|
|
def test_set_subscription_attributes():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
2017-09-07 18:19:34 +00:00
|
|
|
conn.create_topic(Name="some-topic")
|
|
|
|
response = conn.list_topics()
|
2019-10-31 15:44:26 +00:00
|
|
|
topic_arn = response["Topics"][0]["TopicArn"]
|
2017-09-07 18:19:34 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.subscribe(TopicArn=topic_arn, Protocol="http", Endpoint="http://example.com/")
|
2017-09-07 18:19:34 +00:00
|
|
|
|
|
|
|
subscriptions = conn.list_subscriptions()["Subscriptions"]
|
|
|
|
subscriptions.should.have.length_of(1)
|
|
|
|
subscription = subscriptions[0]
|
|
|
|
subscription["TopicArn"].should.equal(topic_arn)
|
|
|
|
subscription["Protocol"].should.equal("http")
|
|
|
|
subscription["SubscriptionArn"].should.contain(topic_arn)
|
|
|
|
subscription["Endpoint"].should.equal("http://example.com/")
|
|
|
|
|
|
|
|
subscription_arn = subscription["SubscriptionArn"]
|
2019-10-31 15:44:26 +00:00
|
|
|
attrs = conn.get_subscription_attributes(SubscriptionArn=subscription_arn)
|
|
|
|
attrs.should.have.key("Attributes")
|
2017-09-07 18:19:34 +00:00
|
|
|
conn.set_subscription_attributes(
|
|
|
|
SubscriptionArn=subscription_arn,
|
2019-10-31 15:44:26 +00:00
|
|
|
AttributeName="RawMessageDelivery",
|
|
|
|
AttributeValue="true",
|
2017-09-07 18:19:34 +00:00
|
|
|
)
|
2019-10-31 15:44:26 +00:00
|
|
|
delivery_policy = json.dumps(
|
|
|
|
{
|
|
|
|
"healthyRetryPolicy": {
|
|
|
|
"numRetries": 10,
|
|
|
|
"minDelayTarget": 1,
|
|
|
|
"maxDelayTarget": 2,
|
|
|
|
}
|
2017-09-07 18:19:34 +00:00
|
|
|
}
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2017-09-07 18:19:34 +00:00
|
|
|
conn.set_subscription_attributes(
|
|
|
|
SubscriptionArn=subscription_arn,
|
2019-10-31 15:44:26 +00:00
|
|
|
AttributeName="DeliveryPolicy",
|
|
|
|
AttributeValue=delivery_policy,
|
2017-09-07 18:19:34 +00:00
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
filter_policy = json.dumps(
|
|
|
|
{
|
|
|
|
"store": ["example_corp"],
|
|
|
|
"event": ["order_cancelled"],
|
|
|
|
"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 15:44:26 +00:00
|
|
|
AttributeName="FilterPolicy",
|
|
|
|
AttributeValue=filter_policy,
|
2018-03-21 15:49:11 +00:00
|
|
|
)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
attrs = conn.get_subscription_attributes(SubscriptionArn=subscription_arn)
|
2018-03-21 15:49:11 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
attrs["Attributes"]["RawMessageDelivery"].should.equal("true")
|
|
|
|
attrs["Attributes"]["DeliveryPolicy"].should.equal(delivery_policy)
|
|
|
|
attrs["Attributes"]["FilterPolicy"].should.equal(filter_policy)
|
2017-09-07 18:19:34 +00:00
|
|
|
|
|
|
|
# not existing subscription
|
|
|
|
with assert_raises(ClientError):
|
|
|
|
conn.set_subscription_attributes(
|
2019-10-31 15:44:26 +00:00
|
|
|
SubscriptionArn="invalid",
|
|
|
|
AttributeName="RawMessageDelivery",
|
|
|
|
AttributeValue="true",
|
2017-09-07 18:19:34 +00:00
|
|
|
)
|
|
|
|
with assert_raises(ClientError):
|
2019-10-31 15:44:26 +00:00
|
|
|
attrs = conn.get_subscription_attributes(SubscriptionArn="invalid")
|
2017-09-07 18:19:34 +00:00
|
|
|
|
|
|
|
# invalid attr name
|
|
|
|
with assert_raises(ClientError):
|
|
|
|
conn.set_subscription_attributes(
|
|
|
|
SubscriptionArn=subscription_arn,
|
2019-10-31 15:44:26 +00:00
|
|
|
AttributeName="InvalidName",
|
|
|
|
AttributeValue="true",
|
2017-09-07 18:19:34 +00:00
|
|
|
)
|
2017-09-21 20:16:00 +00:00
|
|
|
|
|
|
|
|
2019-08-25 14:48:14 +00:00
|
|
|
@mock_sns
|
|
|
|
def test_subscribe_invalid_filter_policy():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
|
|
|
conn.create_topic(Name="some-topic")
|
2019-08-25 14:48:14 +00:00
|
|
|
response = conn.list_topics()
|
2019-10-31 15:44:26 +00:00
|
|
|
topic_arn = response["Topics"][0]["TopicArn"]
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
try:
|
2019-10-31 15:44:26 +00: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 14:48:14 +00:00
|
|
|
except ClientError as err:
|
2019-10-31 15:44:26 +00:00
|
|
|
err.response["Error"]["Code"].should.equal("InvalidParameter")
|
|
|
|
err.response["Error"]["Message"].should.equal(
|
|
|
|
"Invalid parameter: FilterPolicy: Filter policy is too complex"
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
try:
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.subscribe(
|
|
|
|
TopicArn=topic_arn,
|
|
|
|
Protocol="http",
|
|
|
|
Endpoint="http://example.com/",
|
|
|
|
Attributes={"FilterPolicy": json.dumps({"store": [["example_corp"]]})},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
except ClientError as err:
|
2019-10-31 15:44:26 +00:00
|
|
|
err.response["Error"]["Code"].should.equal("InvalidParameter")
|
|
|
|
err.response["Error"]["Message"].should.equal(
|
|
|
|
"Invalid parameter: FilterPolicy: Match value must be String, number, true, false, or null"
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
try:
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.subscribe(
|
|
|
|
TopicArn=topic_arn,
|
|
|
|
Protocol="http",
|
|
|
|
Endpoint="http://example.com/",
|
|
|
|
Attributes={"FilterPolicy": json.dumps({"store": [{"exists": None}]})},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
except ClientError as err:
|
2019-10-31 15:44:26 +00:00
|
|
|
err.response["Error"]["Code"].should.equal("InvalidParameter")
|
|
|
|
err.response["Error"]["Message"].should.equal(
|
|
|
|
"Invalid parameter: FilterPolicy: exists match pattern must be either true or false."
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
try:
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.subscribe(
|
|
|
|
TopicArn=topic_arn,
|
|
|
|
Protocol="http",
|
|
|
|
Endpoint="http://example.com/",
|
|
|
|
Attributes={"FilterPolicy": json.dumps({"store": [{"error": True}]})},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
except ClientError as err:
|
2019-10-31 15:44:26 +00:00
|
|
|
err.response["Error"]["Code"].should.equal("InvalidParameter")
|
|
|
|
err.response["Error"]["Message"].should.equal(
|
|
|
|
"Invalid parameter: FilterPolicy: Unrecognized match type error"
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
try:
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.subscribe(
|
|
|
|
TopicArn=topic_arn,
|
|
|
|
Protocol="http",
|
|
|
|
Endpoint="http://example.com/",
|
|
|
|
Attributes={"FilterPolicy": json.dumps({"store": [1000000001]})},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
except ClientError as err:
|
2019-10-31 15:44:26 +00:00
|
|
|
err.response["Error"]["Code"].should.equal("InternalFailure")
|
|
|
|
|
2019-08-25 14:48:14 +00:00
|
|
|
|
2017-09-21 20:16:00 +00:00
|
|
|
@mock_sns
|
|
|
|
def test_check_not_opted_out():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
|
|
|
response = conn.check_if_phone_number_is_opted_out(phoneNumber="+447428545375")
|
2017-09-21 20:16:00 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
response.should.contain("isOptedOut")
|
|
|
|
response["isOptedOut"].should.be(False)
|
2017-09-21 20:16:00 +00: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 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
|
|
|
response = conn.check_if_phone_number_is_opted_out(phoneNumber="+447428545399")
|
2017-09-21 20:16:00 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
response.should.contain("isOptedOut")
|
|
|
|
response["isOptedOut"].should.be(True)
|
2017-09-21 20:16:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sns
|
|
|
|
def test_check_opted_out_invalid():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
2017-09-21 20:16:00 +00:00
|
|
|
|
|
|
|
# Invalid phone number
|
|
|
|
with assert_raises(ClientError):
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.check_if_phone_number_is_opted_out(phoneNumber="+44742LALALA")
|
2017-09-21 20:16:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sns
|
|
|
|
def test_list_opted_out():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
2017-09-21 20:16:00 +00:00
|
|
|
response = conn.list_phone_numbers_opted_out()
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
response.should.contain("phoneNumbers")
|
|
|
|
len(response["phoneNumbers"]).should.be.greater_than(0)
|
2017-09-21 20:16:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sns
|
|
|
|
def test_opt_in():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
2017-09-21 20:16:00 +00:00
|
|
|
response = conn.list_phone_numbers_opted_out()
|
2019-10-31 15:44:26 +00:00
|
|
|
current_len = len(response["phoneNumbers"])
|
2017-09-21 20:16:00 +00:00
|
|
|
assert current_len > 0
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.opt_in_phone_number(phoneNumber=response["phoneNumbers"][0])
|
2017-09-21 20:16:00 +00:00
|
|
|
|
|
|
|
response = conn.list_phone_numbers_opted_out()
|
2019-10-31 15:44:26 +00:00
|
|
|
len(response["phoneNumbers"]).should.be.greater_than(0)
|
|
|
|
len(response["phoneNumbers"]).should.be.lower_than(current_len)
|
2017-09-21 20:16:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sns
|
|
|
|
def test_confirm_subscription():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
|
|
|
response = conn.create_topic(Name="testconfirm")
|
2017-09-21 20:16:00 +00:00
|
|
|
|
|
|
|
conn.confirm_subscription(
|
2019-10-31 15:44:26 +00:00
|
|
|
TopicArn=response["TopicArn"],
|
|
|
|
Token="2336412f37fb687f5d51e6e241d59b68c4e583a5cee0be6f95bbf97ab8d2441cf47b99e848408adaadf4c197e65f03473d53c4ba398f6abbf38ce2e8ebf7b4ceceb2cd817959bcde1357e58a2861b05288c535822eb88cac3db04f592285249971efc6484194fc4a4586147f16916692",
|
|
|
|
AuthenticateOnUnsubscribe="true",
|
2017-09-21 20:16:00 +00:00
|
|
|
)
|