1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
import json
import boto3
import pytest
from botocore.exceptions import ClientError
from moto import mock_aws
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
@mock_aws
def test_publish_batch_unknown_topic():
client = boto3.client("sns", region_name="us-east-1")
with pytest.raises(ClientError) as exc:
client.publish_batch(
TopicArn=f"arn:aws:sns:us-east-1:{ACCOUNT_ID}:unknown",
PublishBatchRequestEntries=[{"Id": "id_1", "Message": "1"}],
)
err = exc.value.response["Error"]
assert err["Code"] == "NotFound"
assert err["Message"] == "Topic does not exist"
@mock_aws
def test_publish_batch_too_many_items():
client = boto3.client("sns", region_name="eu-north-1")
topic = client.create_topic(Name="some-topic")
with pytest.raises(ClientError) as exc:
client.publish_batch(
TopicArn=topic["TopicArn"],
PublishBatchRequestEntries=[
{"Id": f"id_{idx}", "Message": f"{idx}"} for idx in range(11)
],
)
err = exc.value.response["Error"]
assert err["Code"] == "TooManyEntriesInBatchRequest"
assert err["Message"] == "The batch request contains more entries than permissible."
@mock_aws
def test_publish_batch_non_unique_ids():
client = boto3.client("sns", region_name="us-west-2")
topic = client.create_topic(Name="some-topic")
with pytest.raises(ClientError) as exc:
client.publish_batch(
TopicArn=topic["TopicArn"],
PublishBatchRequestEntries=[
{"Id": "id", "Message": f"{idx}"} for idx in range(5)
],
)
err = exc.value.response["Error"]
assert err["Code"] == "BatchEntryIdsNotDistinct"
assert (
err["Message"] == "Two or more batch entries in the request have the same Id."
)
@mock_aws
def test_publish_batch_fifo_without_message_group_id():
client = boto3.client("sns", region_name="us-east-1")
topic = client.create_topic(
Name="fifo_without_msg.fifo",
Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "true"},
)
with pytest.raises(ClientError) as exc:
client.publish_batch(
TopicArn=topic["TopicArn"],
PublishBatchRequestEntries=[{"Id": "id_2", "Message": "2"}],
)
err = exc.value.response["Error"]
assert err["Code"] == "InvalidParameter"
assert err["Message"] == (
"Invalid parameter: The MessageGroupId parameter is required for FIFO topics"
)
@mock_aws
def test_publish_batch_standard_with_message_group_id():
client = boto3.client("sns", region_name="us-east-1")
topic_arn = client.create_topic(Name="standard_topic")["TopicArn"]
entries = [
{"Id": "id_1", "Message": "1"},
{"Id": "id_2", "Message": "2", "MessageGroupId": "mgid"},
{"Id": "id_3", "Message": "3"},
]
resp = client.publish_batch(TopicArn=topic_arn, PublishBatchRequestEntries=entries)
assert len(resp["Successful"]) == 2
for message_status in resp["Successful"]:
assert "MessageId" in message_status
assert [m["Id"] for m in resp["Successful"]] == ["id_1", "id_3"]
assert len(resp["Failed"]) == 1
assert resp["Failed"][0] == {
"Id": "id_2",
"Code": "InvalidParameter",
"Message": (
"Invalid parameter: MessageGroupId Reason: The request includes "
"MessageGroupId parameter that is not valid for this topic type"
),
"SenderFault": True,
}
@mock_aws
def test_publish_batch_to_sqs():
client = boto3.client("sns", region_name="us-east-1")
topic_arn = client.create_topic(Name="standard_topic")["TopicArn"]
entries = [
{"Id": "id_1", "Message": "1"},
{"Id": "id_2", "Message": "2", "Subject": "subj2"},
{
"Id": "id_3",
"Message": "3",
"MessageAttributes": {"a": {"DataType": "String", "StringValue": "v"}},
},
]
sqs_conn = boto3.resource("sqs", region_name="us-east-1")
queue = sqs_conn.create_queue(QueueName="test-queue")
queue_url = f"arn:aws:sqs:us-east-1:{ACCOUNT_ID}:test-queue"
client.subscribe(TopicArn=topic_arn, Protocol="sqs", Endpoint=queue_url)
resp = client.publish_batch(TopicArn=topic_arn, PublishBatchRequestEntries=entries)
assert len(resp["Successful"]) == 3
messages = queue.receive_messages(MaxNumberOfMessages=3)
assert len(messages) == 3
messages = [json.loads(m.body) for m in messages]
for message in messages:
for key in list(message.keys()):
if key not in ["Message", "Subject", "MessageAttributes"]:
del message[key]
assert {"Message": "1"} in messages
assert {"Message": "2", "Subject": "subj2"} in messages
assert (
{"Message": "3", "MessageAttributes": {"a": {"Type": "String", "Value": "v"}}}
) in messages
@mock_aws
def test_publish_batch_to_sqs_raw():
client = boto3.client("sns", region_name="us-east-1")
topic_arn = client.create_topic(Name="standard_topic")["TopicArn"]
sqs = boto3.resource("sqs", region_name="us-east-1")
queue = sqs.create_queue(QueueName="test-queue")
queue_url = f"arn:aws:sqs:us-east-1:{ACCOUNT_ID}:test-queue"
client.subscribe(
TopicArn=topic_arn,
Protocol="sqs",
Endpoint=queue_url,
Attributes={"RawMessageDelivery": "true"},
)
entries = [
{"Id": "1", "Message": "foo"},
{
"Id": "2",
"Message": "bar",
"MessageAttributes": {"a": {"DataType": "String", "StringValue": "v"}},
},
]
resp = client.publish_batch(TopicArn=topic_arn, PublishBatchRequestEntries=entries)
assert len(resp["Successful"]) == 2
received = queue.receive_messages(
MaxNumberOfMessages=10, MessageAttributeNames=["All"]
)
messages = [(message.body, message.message_attributes) for message in received]
assert ("foo", None) in messages
assert ("bar", {"a": {"StringValue": "v", "DataType": "String"}}) in messages
@mock_aws
def test_publish_with_with_message_structure_json():
sns = boto3.client("sns", region_name="us-east-1")
topic = sns.create_topic(Name="some-topic")
sqs = boto3.resource("sqs", region_name="us-east-1")
queue = sqs.create_queue(QueueName="test-queue")
sns.subscribe(
TopicArn=topic["TopicArn"],
Protocol="sqs",
Endpoint=queue.attributes["QueueArn"],
)
entries = [
{
"Id": "no-message-structure",
"Message": "no-message-structure",
},
{
"Id": "json-message-structure",
"Message": json.dumps(
{
"default": "default-message",
"sqs": "queue-message",
}
),
"MessageStructure": "json",
},
{
"Id": "json-message-structure-fallback-to-default",
"Message": json.dumps(
{
"default": "default-message",
"email": "email-message",
}
),
"MessageStructure": "json",
},
]
sns.publish_batch(
TopicArn=topic["TopicArn"],
PublishBatchRequestEntries=entries,
)
queue_messages = queue.receive_messages(MaxNumberOfMessages=10)
assert len(queue_messages) == 3
first_queue_message = json.loads(queue_messages[0].body)
assert first_queue_message["Message"] == "no-message-structure"
second_queue_message = json.loads(queue_messages[1].body)
assert second_queue_message["Message"] == "queue-message"
third_queue_message = json.loads(queue_messages[2].body)
assert third_queue_message["Message"] == "default-message"
@mock_aws
def test_publish_with_upper_cased_message_attributes():
sns = boto3.client("sns", "us-east-1")
sqs = boto3.resource("sqs", "us-east-1")
topic_arn = sns.create_topic(Name="topic")["TopicArn"]
queue = sqs.create_queue(QueueName="queue")
queue_arn = sqs.meta.client.get_queue_attributes(
QueueUrl=queue.url, AttributeNames=["QueueArn"]
)["Attributes"]["QueueArn"]
sns.subscribe(TopicArn=topic_arn, Protocol="sqs", Endpoint=queue_arn)
sns.publish_batch(
TopicArn=topic_arn,
PublishBatchRequestEntries=[
{
"Id": "some id",
"Message": json.dumps({"type": "test"}),
"MessageAttributes": {
"SomeCamelCaseAttr": {"DataType": "String", "StringValue": "value"}
},
}
],
)
msg = queue.receive_messages()[0]
body = json.loads(msg.body)
assert body["MessageAttributes"] == {
"SomeCamelCaseAttr": {"Type": "String", "Value": "value"}
}
|