2015-08-20 15:12:25 +00:00
|
|
|
from __future__ import unicode_literals
|
2017-07-30 12:44:06 +00:00
|
|
|
|
2018-04-27 22:10:30 +00:00
|
|
|
import base64
|
2017-07-30 12:44:06 +00:00
|
|
|
import json
|
|
|
|
|
2015-08-20 15:12:25 +00:00
|
|
|
import boto3
|
2017-08-21 19:29:34 +00:00
|
|
|
import re
|
2015-08-20 15:12:25 +00:00
|
|
|
from freezegun import freeze_time
|
|
|
|
import sure # noqa
|
|
|
|
|
2018-04-05 20:57:43 +00:00
|
|
|
import responses
|
2017-09-25 23:21:07 +00:00
|
|
|
from botocore.exceptions import ClientError
|
2018-06-04 12:53:24 +00:00
|
|
|
from nose.tools import assert_raises
|
2015-08-20 15:12:25 +00:00
|
|
|
from moto import mock_sns, mock_sqs
|
2017-08-21 19:29:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
MESSAGE_FROM_SQS_TEMPLATE = '{\n "Message": "%s",\n "MessageId": "%s",\n "Signature": "EXAMPLElDMXvB8r9R83tGoNn0ecwd5UjllzsvSvbItzfaMpN2nk5HVSw7XnOn/49IkxDKz8YrlH2qJXj2iZB0Zo2O71c4qQk1fMUDi3LGpij7RCW7AW9vYYsSqIKRnFS94ilu7NFhUzLiieYr4BKHpdTmdD6c0esKEYBpabxDSc=",\n "SignatureVersion": "1",\n "SigningCertURL": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem",\n "Subject": "my subject",\n "Timestamp": "2015-01-01T12:00:00.000Z",\n "TopicArn": "arn:aws:sns:%s:123456789012:some-topic",\n "Type": "Notification",\n "UnsubscribeURL": "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:123456789012:some-topic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55"\n}'
|
2015-08-20 15:12:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_publish_to_sqs():
|
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
|
|
|
sqs_conn = boto3.resource("sqs", region_name="us-east-1")
|
2015-08-20 15:12:25 +00:00
|
|
|
sqs_conn.create_queue(QueueName="test-queue")
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.subscribe(
|
|
|
|
TopicArn=topic_arn,
|
|
|
|
Protocol="sqs",
|
|
|
|
Endpoint="arn:aws:sqs:us-east-1:123456789012:test-queue",
|
|
|
|
)
|
|
|
|
message = "my message"
|
2017-08-21 19:29:34 +00:00
|
|
|
with freeze_time("2015-01-01 12:00:00"):
|
|
|
|
published_message = conn.publish(TopicArn=topic_arn, Message=message)
|
2019-10-31 15:44:26 +00:00
|
|
|
published_message_id = published_message["MessageId"]
|
2015-08-20 15:12:25 +00:00
|
|
|
|
|
|
|
queue = sqs_conn.get_queue_by_name(QueueName="test-queue")
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=1)
|
2019-10-31 15:44:26 +00:00
|
|
|
expected = MESSAGE_FROM_SQS_TEMPLATE % (message, published_message_id, "us-east-1")
|
|
|
|
acquired_message = re.sub(
|
|
|
|
"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z",
|
|
|
|
"2015-01-01T12:00:00.000Z",
|
|
|
|
messages[0].body,
|
|
|
|
)
|
2017-08-21 19:29:34 +00:00
|
|
|
acquired_message.should.equal(expected)
|
2015-08-20 15:12:25 +00:00
|
|
|
|
|
|
|
|
2018-05-29 14:06:25 +00:00
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_publish_to_sqs_raw():
|
2019-10-31 15:44:26 +00:00
|
|
|
sns = boto3.resource("sns", region_name="us-east-1")
|
|
|
|
topic = sns.create_topic(Name="some-topic")
|
2018-05-29 14:06:25 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
sqs = boto3.resource("sqs", region_name="us-east-1")
|
|
|
|
queue = sqs.create_queue(QueueName="test-queue")
|
2018-05-29 14:06:25 +00:00
|
|
|
|
|
|
|
subscription = topic.subscribe(
|
2019-10-31 15:44:26 +00:00
|
|
|
Protocol="sqs", Endpoint=queue.attributes["QueueArn"]
|
|
|
|
)
|
2018-05-29 14:06:25 +00:00
|
|
|
|
|
|
|
subscription.set_attributes(
|
2019-10-31 15:44:26 +00:00
|
|
|
AttributeName="RawMessageDelivery", AttributeValue="true"
|
|
|
|
)
|
2018-05-29 14:06:25 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
message = "my message"
|
2018-05-29 14:06:25 +00:00
|
|
|
with freeze_time("2015-01-01 12:00:00"):
|
|
|
|
topic.publish(Message=message)
|
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=1)
|
|
|
|
messages[0].body.should.equal(message)
|
|
|
|
|
|
|
|
|
2018-04-27 22:10:30 +00:00
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_publish_to_sqs_bad():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
2018-04-27 22:10:30 +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"]
|
2018-04-27 22:10:30 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
sqs_conn = boto3.resource("sqs", region_name="us-east-1")
|
2018-04-27 22:10:30 +00:00
|
|
|
sqs_conn.create_queue(QueueName="test-queue")
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.subscribe(
|
|
|
|
TopicArn=topic_arn,
|
|
|
|
Protocol="sqs",
|
|
|
|
Endpoint="arn:aws:sqs:us-east-1:123456789012:test-queue",
|
|
|
|
)
|
|
|
|
message = "my message"
|
2018-04-27 22:10:30 +00:00
|
|
|
try:
|
|
|
|
# Test missing Value
|
|
|
|
conn.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
TopicArn=topic_arn,
|
|
|
|
Message=message,
|
|
|
|
MessageAttributes={"store": {"DataType": "String"}},
|
|
|
|
)
|
2018-04-27 22:10:30 +00:00
|
|
|
except ClientError as err:
|
2019-10-31 15:44:26 +00:00
|
|
|
err.response["Error"]["Code"].should.equal("InvalidParameterValue")
|
2018-04-27 22:10:30 +00:00
|
|
|
try:
|
|
|
|
# Test empty DataType (if the DataType field is missing entirely
|
|
|
|
# botocore throws an exception during validation)
|
|
|
|
conn.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
TopicArn=topic_arn,
|
|
|
|
Message=message,
|
|
|
|
MessageAttributes={
|
|
|
|
"store": {"DataType": "", "StringValue": "example_corp"}
|
|
|
|
},
|
|
|
|
)
|
2018-04-27 22:10:30 +00:00
|
|
|
except ClientError as err:
|
2019-10-31 15:44:26 +00:00
|
|
|
err.response["Error"]["Code"].should.equal("InvalidParameterValue")
|
2018-04-27 22:10:30 +00:00
|
|
|
try:
|
|
|
|
# Test empty Value
|
|
|
|
conn.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
TopicArn=topic_arn,
|
|
|
|
Message=message,
|
|
|
|
MessageAttributes={"store": {"DataType": "String", "StringValue": ""}},
|
|
|
|
)
|
2018-04-27 22:10:30 +00:00
|
|
|
except ClientError as err:
|
2019-10-31 15:44:26 +00:00
|
|
|
err.response["Error"]["Code"].should.equal("InvalidParameterValue")
|
2019-08-25 14:48:14 +00:00
|
|
|
try:
|
|
|
|
# Test Number DataType, with a non numeric value
|
|
|
|
conn.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
TopicArn=topic_arn,
|
|
|
|
Message=message,
|
|
|
|
MessageAttributes={"price": {"DataType": "Number", "StringValue": "error"}},
|
|
|
|
)
|
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("InvalidParameterValue")
|
|
|
|
err.response["Error"]["Message"].should.equal(
|
|
|
|
"An error occurred (ParameterValueInvalid) when calling the Publish operation: Could not cast message attribute 'price' value to number."
|
|
|
|
)
|
2018-04-27 22:10:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_publish_to_sqs_msg_attr_byte_value():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
2018-04-27 22:10:30 +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"]
|
2018-04-27 22:10:30 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
sqs_conn = boto3.resource("sqs", region_name="us-east-1")
|
2018-04-27 22:10:30 +00:00
|
|
|
queue = sqs_conn.create_queue(QueueName="test-queue")
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.subscribe(
|
|
|
|
TopicArn=topic_arn,
|
|
|
|
Protocol="sqs",
|
|
|
|
Endpoint="arn:aws:sqs:us-east-1:123456789012:test-queue",
|
|
|
|
)
|
|
|
|
message = "my message"
|
2018-04-27 22:10:30 +00:00
|
|
|
conn.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
TopicArn=topic_arn,
|
|
|
|
Message=message,
|
|
|
|
MessageAttributes={
|
|
|
|
"store": {"DataType": "Binary", "BinaryValue": b"\x02\x03\x04"}
|
|
|
|
},
|
|
|
|
)
|
2018-04-27 22:10:30 +00:00
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
|
|
|
message_attributes.should.equal(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"store": {
|
|
|
|
"Type": "Binary",
|
|
|
|
"Value": base64.b64encode(b"\x02\x03\x04").decode(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
2018-04-27 22:10:30 +00:00
|
|
|
|
|
|
|
|
2017-09-25 23:21:07 +00:00
|
|
|
@mock_sns
|
|
|
|
def test_publish_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
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
client.subscribe(TopicArn=arn, Protocol="sms", Endpoint="+15551234567")
|
2017-09-25 23:21:07 +00:00
|
|
|
|
|
|
|
result = client.publish(PhoneNumber="+15551234567", Message="my message")
|
2019-10-31 15:44:26 +00:00
|
|
|
result.should.contain("MessageId")
|
2017-09-25 23:21:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sns
|
|
|
|
def test_publish_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
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
client.subscribe(TopicArn=arn, Protocol="sms", Endpoint="+15551234567")
|
2017-09-25 23:21:07 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# Test invalid number
|
|
|
|
client.publish(PhoneNumber="NAA+15551234567", Message="my message")
|
|
|
|
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
|
|
|
|
|
|
|
try:
|
|
|
|
# Test not found number
|
|
|
|
client.publish(PhoneNumber="+44001234567", Message="my message")
|
|
|
|
except ClientError as err:
|
2019-10-31 15:44:26 +00:00
|
|
|
err.response["Error"]["Code"].should.equal("ParameterValueInvalid")
|
2017-09-25 23:21:07 +00:00
|
|
|
|
|
|
|
|
2017-08-17 13:40:39 +00:00
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_publish_to_sqs_dump_json():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
2017-08-17 13:40:39 +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-08-17 13:40:39 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
sqs_conn = boto3.resource("sqs", region_name="us-east-1")
|
2017-08-17 13:40:39 +00:00
|
|
|
sqs_conn.create_queue(QueueName="test-queue")
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.subscribe(
|
|
|
|
TopicArn=topic_arn,
|
|
|
|
Protocol="sqs",
|
|
|
|
Endpoint="arn:aws:sqs:us-east-1:123456789012:test-queue",
|
|
|
|
)
|
2017-08-17 13:40:39 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
message = json.dumps(
|
|
|
|
{
|
|
|
|
"Records": [
|
|
|
|
{
|
|
|
|
"eventVersion": "2.0",
|
|
|
|
"eventSource": "aws:s3",
|
|
|
|
"s3": {"s3SchemaVersion": "1.0"},
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
sort_keys=True,
|
|
|
|
)
|
2017-08-21 19:29:34 +00:00
|
|
|
with freeze_time("2015-01-01 12:00:00"):
|
|
|
|
published_message = conn.publish(TopicArn=topic_arn, Message=message)
|
2019-10-31 15:44:26 +00:00
|
|
|
published_message_id = published_message["MessageId"]
|
2017-08-21 19:29:34 +00:00
|
|
|
|
2017-08-17 13:40:39 +00:00
|
|
|
queue = sqs_conn.get_queue_by_name(QueueName="test-queue")
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=1)
|
2017-08-21 19:29:34 +00:00
|
|
|
|
|
|
|
escaped = message.replace('"', '\\"')
|
2019-10-31 15:44:26 +00:00
|
|
|
expected = MESSAGE_FROM_SQS_TEMPLATE % (escaped, published_message_id, "us-east-1")
|
|
|
|
acquired_message = re.sub(
|
|
|
|
"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z",
|
|
|
|
"2015-01-01T12:00:00.000Z",
|
|
|
|
messages[0].body,
|
|
|
|
)
|
2017-08-21 19:29:34 +00:00
|
|
|
acquired_message.should.equal(expected)
|
2017-08-17 13:40:39 +00:00
|
|
|
|
|
|
|
|
2015-08-20 15:12:25 +00:00
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_publish_to_sqs_in_different_region():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-west-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
|
|
|
sqs_conn = boto3.resource("sqs", region_name="us-west-2")
|
2015-08-20 15:12:25 +00:00
|
|
|
sqs_conn.create_queue(QueueName="test-queue")
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.subscribe(
|
|
|
|
TopicArn=topic_arn,
|
|
|
|
Protocol="sqs",
|
|
|
|
Endpoint="arn:aws:sqs:us-west-2:123456789012:test-queue",
|
|
|
|
)
|
2015-08-20 15:12:25 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
message = "my message"
|
2017-08-21 19:29:34 +00:00
|
|
|
with freeze_time("2015-01-01 12:00:00"):
|
|
|
|
published_message = conn.publish(TopicArn=topic_arn, Message=message)
|
2019-10-31 15:44:26 +00:00
|
|
|
published_message_id = published_message["MessageId"]
|
2015-08-20 15:12:25 +00:00
|
|
|
|
|
|
|
queue = sqs_conn.get_queue_by_name(QueueName="test-queue")
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=1)
|
2019-10-31 15:44:26 +00:00
|
|
|
expected = MESSAGE_FROM_SQS_TEMPLATE % (message, published_message_id, "us-west-1")
|
|
|
|
acquired_message = re.sub(
|
|
|
|
"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z",
|
|
|
|
"2015-01-01T12:00:00.000Z",
|
|
|
|
messages[0].body,
|
|
|
|
)
|
2017-08-21 19:29:34 +00:00
|
|
|
acquired_message.should.equal(expected)
|
2015-08-20 15:12:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2013-01-01")
|
|
|
|
@mock_sns
|
|
|
|
def test_publish_to_http():
|
2017-07-30 12:44:06 +00:00
|
|
|
def callback(request):
|
2018-06-01 22:18:27 +00:00
|
|
|
request.headers["Content-Type"].should.equal("text/plain; charset=UTF-8")
|
2019-10-31 15:44:26 +00:00
|
|
|
json.loads.when.called_with(request.body.decode()).should_not.throw(Exception)
|
2017-07-30 12:44:06 +00:00
|
|
|
return 200, {}, ""
|
|
|
|
|
|
|
|
responses.add_callback(
|
2019-10-31 15:44:26 +00:00
|
|
|
method="POST", url="http://example.com/foobar", callback=callback
|
2015-08-20 15:12:25 +00:00
|
|
|
)
|
|
|
|
|
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/foobar"
|
|
|
|
)
|
2015-08-20 15:12:25 +00:00
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
response = conn.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
TopicArn=topic_arn, Message="my message", Subject="my subject"
|
|
|
|
)
|
2017-10-20 12:19:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_publish_subject():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("sns", region_name="us-east-1")
|
2017-10-20 12:19:55 +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-10-20 12:19:55 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
sqs_conn = boto3.resource("sqs", region_name="us-east-1")
|
2017-10-20 12:19:55 +00:00
|
|
|
sqs_conn.create_queue(QueueName="test-queue")
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.subscribe(
|
|
|
|
TopicArn=topic_arn,
|
|
|
|
Protocol="sqs",
|
|
|
|
Endpoint="arn:aws:sqs:us-east-1:123456789012:test-queue",
|
|
|
|
)
|
|
|
|
message = "my message"
|
|
|
|
subject1 = "test subject"
|
|
|
|
subject2 = "test subject" * 20
|
2017-10-20 12:19:55 +00:00
|
|
|
with freeze_time("2015-01-01 12:00:00"):
|
2017-10-20 13:52:24 +00:00
|
|
|
conn.publish(TopicArn=topic_arn, Message=message, Subject=subject1)
|
2017-10-20 12:19:55 +00:00
|
|
|
|
|
|
|
# Just that it doesnt error is a pass
|
|
|
|
try:
|
|
|
|
with freeze_time("2015-01-01 12:00:00"):
|
2017-10-20 13:52:24 +00:00
|
|
|
conn.publish(TopicArn=topic_arn, Message=message, Subject=subject2)
|
2017-10-20 12:19:55 +00:00
|
|
|
except ClientError as err:
|
2019-10-31 15:44:26 +00:00
|
|
|
err.response["Error"]["Code"].should.equal("InvalidParameter")
|
2017-10-20 12:19:55 +00:00
|
|
|
else:
|
2019-10-31 15:44:26 +00:00
|
|
|
raise RuntimeError("Should have raised an InvalidParameter exception")
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
|
2018-06-04 12:53:24 +00:00
|
|
|
@mock_sns
|
|
|
|
def test_publish_message_too_long():
|
2019-10-31 15:44:26 +00:00
|
|
|
sns = boto3.resource("sns", region_name="us-east-1")
|
|
|
|
topic = sns.create_topic(Name="some-topic")
|
2018-06-04 12:53:24 +00:00
|
|
|
|
|
|
|
with assert_raises(ClientError):
|
2019-10-31 15:44:26 +00:00
|
|
|
topic.publish(Message="".join(["." for i in range(0, 262145)]))
|
2018-06-04 12:53:24 +00:00
|
|
|
|
|
|
|
# message short enough - does not raise an error
|
2019-10-31 15:44:26 +00:00
|
|
|
topic.publish(Message="".join(["." for i in range(0, 262144)]))
|
2018-06-04 12:53:24 +00:00
|
|
|
|
|
|
|
|
2018-03-21 15:49:11 +00:00
|
|
|
def _setup_filter_policy_test(filter_policy):
|
2019-10-31 15:44:26 +00:00
|
|
|
sns = boto3.resource("sns", region_name="us-east-1")
|
|
|
|
topic = sns.create_topic(Name="some-topic")
|
2018-03-21 15:49:11 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
sqs = boto3.resource("sqs", region_name="us-east-1")
|
|
|
|
queue = sqs.create_queue(QueueName="test-queue")
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
subscription = topic.subscribe(
|
2019-10-31 15:44:26 +00:00
|
|
|
Protocol="sqs", Endpoint=queue.attributes["QueueArn"]
|
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
subscription.set_attributes(
|
2019-10-31 15:44:26 +00:00
|
|
|
AttributeName="FilterPolicy", AttributeValue=json.dumps(filter_policy)
|
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
return topic, subscription, queue
|
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_exact_string():
|
2019-10-31 15:44:26 +00:00
|
|
|
topic, subscription, queue = _setup_filter_policy_test({"store": ["example_corp"]})
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match",
|
|
|
|
MessageAttributes={
|
|
|
|
"store": {"DataType": "String", "StringValue": "example_corp"}
|
|
|
|
},
|
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
|
|
|
message_bodies.should.equal(["match"])
|
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2018-04-17 16:27:48 +00:00
|
|
|
message_attributes.should.equal(
|
2019-10-31 15:44:26 +00:00
|
|
|
[{"store": {"Type": "String", "Value": "example_corp"}}]
|
|
|
|
)
|
2018-04-17 16:27:48 +00:00
|
|
|
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_exact_string_multiple_message_attributes():
|
2019-10-31 15:44:26 +00:00
|
|
|
topic, subscription, queue = _setup_filter_policy_test({"store": ["example_corp"]})
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match",
|
|
|
|
MessageAttributes={
|
|
|
|
"store": {"DataType": "String", "StringValue": "example_corp"},
|
|
|
|
"event": {"DataType": "String", "StringValue": "order_cancelled"},
|
|
|
|
},
|
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
|
|
|
message_bodies.should.equal(["match"])
|
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
|
|
|
message_attributes.should.equal(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"store": {"Type": "String", "Value": "example_corp"},
|
|
|
|
"event": {"Type": "String", "Value": "order_cancelled"},
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
|
2018-04-27 22:10:30 +00:00
|
|
|
|
2018-03-21 15:49:11 +00:00
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_exact_string_OR_matching():
|
|
|
|
topic, subscription, queue = _setup_filter_policy_test(
|
2019-10-31 15:44:26 +00:00
|
|
|
{"store": ["example_corp", "different_corp"]}
|
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match example_corp",
|
|
|
|
MessageAttributes={
|
|
|
|
"store": {"DataType": "String", "StringValue": "example_corp"}
|
|
|
|
},
|
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match different_corp",
|
|
|
|
MessageAttributes={
|
|
|
|
"store": {"DataType": "String", "StringValue": "different_corp"}
|
|
|
|
},
|
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
|
|
|
message_bodies.should.equal(["match example_corp", "match different_corp"])
|
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
|
|
|
message_attributes.should.equal(
|
|
|
|
[
|
|
|
|
{"store": {"Type": "String", "Value": "example_corp"}},
|
|
|
|
{"store": {"Type": "String", "Value": "different_corp"}},
|
|
|
|
]
|
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
|
2018-04-27 22:10:30 +00:00
|
|
|
|
2018-03-21 15:49:11 +00:00
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_exact_string_AND_matching_positive():
|
|
|
|
topic, subscription, queue = _setup_filter_policy_test(
|
2019-10-31 15:44:26 +00:00
|
|
|
{"store": ["example_corp"], "event": ["order_cancelled"]}
|
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match example_corp order_cancelled",
|
|
|
|
MessageAttributes={
|
|
|
|
"store": {"DataType": "String", "StringValue": "example_corp"},
|
|
|
|
"event": {"DataType": "String", "StringValue": "order_cancelled"},
|
|
|
|
},
|
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
|
|
|
message_bodies.should.equal(["match example_corp order_cancelled"])
|
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
|
|
|
message_attributes.should.equal(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"store": {"Type": "String", "Value": "example_corp"},
|
|
|
|
"event": {"Type": "String", "Value": "order_cancelled"},
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
|
2018-04-27 22:10:30 +00:00
|
|
|
|
2018-03-21 15:49:11 +00:00
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_exact_string_AND_matching_no_match():
|
|
|
|
topic, subscription, queue = _setup_filter_policy_test(
|
2019-10-31 15:44:26 +00:00
|
|
|
{"store": ["example_corp"], "event": ["order_cancelled"]}
|
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match example_corp order_accepted",
|
|
|
|
MessageAttributes={
|
|
|
|
"store": {"DataType": "String", "StringValue": "example_corp"},
|
|
|
|
"event": {"DataType": "String", "StringValue": "order_accepted"},
|
|
|
|
},
|
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
2018-03-21 15:49:11 +00:00
|
|
|
message_bodies.should.equal([])
|
2019-10-31 15:44:26 +00:00
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2018-04-17 16:27:48 +00:00
|
|
|
message_attributes.should.equal([])
|
2018-03-21 15:49:11 +00:00
|
|
|
|
2018-04-27 22:10:30 +00:00
|
|
|
|
2018-03-21 15:49:11 +00:00
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_exact_string_no_match():
|
2019-10-31 15:44:26 +00:00
|
|
|
topic, subscription, queue = _setup_filter_policy_test({"store": ["example_corp"]})
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="no match",
|
|
|
|
MessageAttributes={
|
|
|
|
"store": {"DataType": "String", "StringValue": "different_corp"}
|
|
|
|
},
|
|
|
|
)
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
2018-03-21 15:49:11 +00:00
|
|
|
message_bodies.should.equal([])
|
2019-10-31 15:44:26 +00:00
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2018-04-17 16:27:48 +00:00
|
|
|
message_attributes.should.equal([])
|
2018-03-21 15:49:11 +00:00
|
|
|
|
2018-04-27 22:10:30 +00:00
|
|
|
|
2018-03-21 15:49:11 +00:00
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_exact_string_no_attributes_no_match():
|
2019-10-31 15:44:26 +00:00
|
|
|
topic, subscription, queue = _setup_filter_policy_test({"store": ["example_corp"]})
|
2018-03-21 15:49:11 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
topic.publish(Message="no match")
|
2018-03-21 15:49:11 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
2018-03-21 15:49:11 +00:00
|
|
|
message_bodies.should.equal([])
|
2019-10-31 15:44:26 +00:00
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2018-04-17 16:27:48 +00:00
|
|
|
message_attributes.should.equal([])
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_exact_number_int():
|
2019-10-31 15:44:26 +00:00
|
|
|
topic, subscription, queue = _setup_filter_policy_test({"price": [100]})
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match",
|
|
|
|
MessageAttributes={"price": {"DataType": "Number", "StringValue": "100"}},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
|
|
|
message_bodies.should.equal(["match"])
|
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
|
|
|
message_attributes.should.equal([{"price": {"Type": "Number", "Value": 100}}])
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_exact_number_float():
|
2019-10-31 15:44:26 +00:00
|
|
|
topic, subscription, queue = _setup_filter_policy_test({"price": [100.1]})
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match",
|
|
|
|
MessageAttributes={"price": {"DataType": "Number", "StringValue": "100.1"}},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
|
|
|
message_bodies.should.equal(["match"])
|
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
|
|
|
message_attributes.should.equal([{"price": {"Type": "Number", "Value": 100.1}}])
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_exact_number_float_accuracy():
|
2019-10-31 15:44:26 +00:00
|
|
|
topic, subscription, queue = _setup_filter_policy_test({"price": [100.123456789]})
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match",
|
|
|
|
MessageAttributes={
|
|
|
|
"price": {"DataType": "Number", "StringValue": "100.1234561"}
|
|
|
|
},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
|
|
|
message_bodies.should.equal(["match"])
|
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_attributes.should.equal(
|
2019-10-31 15:44:26 +00:00
|
|
|
[{"price": {"Type": "Number", "Value": 100.1234561}}]
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_exact_number_no_match():
|
2019-10-31 15:44:26 +00:00
|
|
|
topic, subscription, queue = _setup_filter_policy_test({"price": [100]})
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="no match",
|
|
|
|
MessageAttributes={"price": {"DataType": "Number", "StringValue": "101"}},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_bodies.should.equal([])
|
2019-10-31 15:44:26 +00:00
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_attributes.should.equal([])
|
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_exact_number_with_string_no_match():
|
2019-10-31 15:44:26 +00:00
|
|
|
topic, subscription, queue = _setup_filter_policy_test({"price": [100]})
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="no match",
|
|
|
|
MessageAttributes={"price": {"DataType": "String", "StringValue": "100"}},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_bodies.should.equal([])
|
2019-10-31 15:44:26 +00:00
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_attributes.should.equal([])
|
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_string_array_match():
|
|
|
|
topic, subscription, queue = _setup_filter_policy_test(
|
2019-10-31 15:44:26 +00:00
|
|
|
{"customer_interests": ["basketball", "baseball"]}
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match",
|
|
|
|
MessageAttributes={
|
|
|
|
"customer_interests": {
|
|
|
|
"DataType": "String.Array",
|
|
|
|
"StringValue": json.dumps(["basketball", "rugby"]),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
|
|
|
message_bodies.should.equal(["match"])
|
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_attributes.should.equal(
|
2019-10-31 15:44:26 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"customer_interests": {
|
|
|
|
"Type": "String.Array",
|
|
|
|
"Value": json.dumps(["basketball", "rugby"]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_string_array_no_match():
|
|
|
|
topic, subscription, queue = _setup_filter_policy_test(
|
2019-10-31 15:44:26 +00:00
|
|
|
{"customer_interests": ["baseball"]}
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="no_match",
|
|
|
|
MessageAttributes={
|
|
|
|
"customer_interests": {
|
|
|
|
"DataType": "String.Array",
|
|
|
|
"StringValue": json.dumps(["basketball", "rugby"]),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_bodies.should.equal([])
|
2019-10-31 15:44:26 +00:00
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_attributes.should.equal([])
|
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_string_array_with_number_match():
|
2019-10-31 15:44:26 +00:00
|
|
|
topic, subscription, queue = _setup_filter_policy_test({"price": [100, 500]})
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match",
|
|
|
|
MessageAttributes={
|
|
|
|
"price": {"DataType": "String.Array", "StringValue": json.dumps([100, 50])}
|
|
|
|
},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
|
|
|
message_bodies.should.equal(["match"])
|
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_attributes.should.equal(
|
2019-10-31 15:44:26 +00:00
|
|
|
[{"price": {"Type": "String.Array", "Value": json.dumps([100, 50])}}]
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_string_array_with_number_float_accuracy_match():
|
|
|
|
topic, subscription, queue = _setup_filter_policy_test(
|
2019-10-31 15:44:26 +00:00
|
|
|
{"price": [100.123456789, 500]}
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match",
|
|
|
|
MessageAttributes={
|
|
|
|
"price": {
|
|
|
|
"DataType": "String.Array",
|
|
|
|
"StringValue": json.dumps([100.1234561, 50]),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
|
|
|
message_bodies.should.equal(["match"])
|
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_attributes.should.equal(
|
2019-10-31 15:44:26 +00:00
|
|
|
[{"price": {"Type": "String.Array", "Value": json.dumps([100.1234561, 50])}}]
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
# this is the correct behavior from SNS
|
|
|
|
def test_filtering_string_array_with_number_no_array_match():
|
2019-10-31 15:44:26 +00:00
|
|
|
topic, subscription, queue = _setup_filter_policy_test({"price": [100, 500]})
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match",
|
|
|
|
MessageAttributes={"price": {"DataType": "String.Array", "StringValue": "100"}},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
|
|
|
message_bodies.should.equal(["match"])
|
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_attributes.should.equal(
|
2019-10-31 15:44:26 +00:00
|
|
|
[{"price": {"Type": "String.Array", "Value": "100"}}]
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_string_array_with_number_no_match():
|
2019-10-31 15:44:26 +00:00
|
|
|
topic, subscription, queue = _setup_filter_policy_test({"price": [500]})
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="no_match",
|
|
|
|
MessageAttributes={
|
|
|
|
"price": {"DataType": "String.Array", "StringValue": json.dumps([100, 50])}
|
|
|
|
},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_bodies.should.equal([])
|
2019-10-31 15:44:26 +00:00
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_attributes.should.equal([])
|
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
# this is the correct behavior from SNS
|
|
|
|
def test_filtering_string_array_with_string_no_array_no_match():
|
2019-10-31 15:44:26 +00:00
|
|
|
topic, subscription, queue = _setup_filter_policy_test({"price": [100]})
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="no_match",
|
|
|
|
MessageAttributes={
|
|
|
|
"price": {"DataType": "String.Array", "StringValue": "one hundread"}
|
|
|
|
},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_bodies.should.equal([])
|
2019-10-31 15:44:26 +00:00
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_attributes.should.equal([])
|
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_attribute_key_exists_match():
|
|
|
|
topic, subscription, queue = _setup_filter_policy_test(
|
2019-10-31 15:44:26 +00:00
|
|
|
{"store": [{"exists": True}]}
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match",
|
|
|
|
MessageAttributes={
|
|
|
|
"store": {"DataType": "String", "StringValue": "example_corp"}
|
|
|
|
},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
|
|
|
message_bodies.should.equal(["match"])
|
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_attributes.should.equal(
|
2019-10-31 15:44:26 +00:00
|
|
|
[{"store": {"Type": "String", "Value": "example_corp"}}]
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_attribute_key_exists_no_match():
|
|
|
|
topic, subscription, queue = _setup_filter_policy_test(
|
2019-10-31 15:44:26 +00:00
|
|
|
{"store": [{"exists": True}]}
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="no match",
|
|
|
|
MessageAttributes={
|
|
|
|
"event": {"DataType": "String", "StringValue": "order_cancelled"}
|
|
|
|
},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_bodies.should.equal([])
|
2019-10-31 15:44:26 +00:00
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_attributes.should.equal([])
|
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_attribute_key_not_exists_match():
|
|
|
|
topic, subscription, queue = _setup_filter_policy_test(
|
2019-10-31 15:44:26 +00:00
|
|
|
{"store": [{"exists": False}]}
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match",
|
|
|
|
MessageAttributes={
|
|
|
|
"event": {"DataType": "String", "StringValue": "order_cancelled"}
|
|
|
|
},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
|
|
|
message_bodies.should.equal(["match"])
|
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_attributes.should.equal(
|
2019-10-31 15:44:26 +00:00
|
|
|
[{"event": {"Type": "String", "Value": "order_cancelled"}}]
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_attribute_key_not_exists_no_match():
|
|
|
|
topic, subscription, queue = _setup_filter_policy_test(
|
2019-10-31 15:44:26 +00:00
|
|
|
{"store": [{"exists": False}]}
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="no match",
|
|
|
|
MessageAttributes={
|
|
|
|
"store": {"DataType": "String", "StringValue": "example_corp"}
|
|
|
|
},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_bodies.should.equal([])
|
2019-10-31 15:44:26 +00:00
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_attributes.should.equal([])
|
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_all_AND_matching_match():
|
|
|
|
topic, subscription, queue = _setup_filter_policy_test(
|
2019-10-31 15:44:26 +00:00
|
|
|
{
|
|
|
|
"store": [{"exists": True}],
|
|
|
|
"event": ["order_cancelled"],
|
|
|
|
"customer_interests": ["basketball", "baseball"],
|
|
|
|
"price": [100],
|
|
|
|
}
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="match",
|
|
|
|
MessageAttributes={
|
|
|
|
"store": {"DataType": "String", "StringValue": "example_corp"},
|
|
|
|
"event": {"DataType": "String", "StringValue": "order_cancelled"},
|
|
|
|
"customer_interests": {
|
|
|
|
"DataType": "String.Array",
|
|
|
|
"StringValue": json.dumps(["basketball", "rugby"]),
|
|
|
|
},
|
|
|
|
"price": {"DataType": "Number", "StringValue": "100"},
|
|
|
|
},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
|
|
|
message_bodies.should.equal(["match"])
|
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
|
|
|
message_attributes.should.equal(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"store": {"Type": "String", "Value": "example_corp"},
|
|
|
|
"event": {"Type": "String", "Value": "order_cancelled"},
|
|
|
|
"customer_interests": {
|
|
|
|
"Type": "String.Array",
|
|
|
|
"Value": json.dumps(["basketball", "rugby"]),
|
|
|
|
},
|
|
|
|
"price": {"Type": "Number", "Value": 100},
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
@mock_sns
|
|
|
|
def test_filtering_all_AND_matching_no_match():
|
|
|
|
topic, subscription, queue = _setup_filter_policy_test(
|
2019-10-31 15:44:26 +00:00
|
|
|
{
|
|
|
|
"store": [{"exists": True}],
|
|
|
|
"event": ["order_cancelled"],
|
|
|
|
"customer_interests": ["basketball", "baseball"],
|
|
|
|
"price": [100],
|
|
|
|
"encrypted": [False],
|
|
|
|
}
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
topic.publish(
|
2019-10-31 15:44:26 +00:00
|
|
|
Message="no match",
|
|
|
|
MessageAttributes={
|
|
|
|
"store": {"DataType": "String", "StringValue": "example_corp"},
|
|
|
|
"event": {"DataType": "String", "StringValue": "order_cancelled"},
|
|
|
|
"customer_interests": {
|
|
|
|
"DataType": "String.Array",
|
|
|
|
"StringValue": json.dumps(["basketball", "rugby"]),
|
|
|
|
},
|
|
|
|
"price": {"DataType": "Number", "StringValue": "100"},
|
|
|
|
},
|
|
|
|
)
|
2019-08-25 14:48:14 +00:00
|
|
|
|
|
|
|
messages = queue.receive_messages(MaxNumberOfMessages=5)
|
2019-10-31 15:44:26 +00:00
|
|
|
message_bodies = [json.loads(m.body)["Message"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_bodies.should.equal([])
|
2019-10-31 15:44:26 +00:00
|
|
|
message_attributes = [json.loads(m.body)["MessageAttributes"] for m in messages]
|
2019-08-25 14:48:14 +00:00
|
|
|
message_attributes.should.equal([])
|