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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
|
from uuid import uuid4
import boto3
import pytest
from botocore.exceptions import ClientError
from moto import mock_aws
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
from . import sns_aws_verified
@mock_aws
def test_create_platform_application():
conn = boto3.client("sns", region_name="us-east-1")
response = conn.create_platform_application(
Name="my-application",
Platform="APNS",
Attributes={
"PlatformCredential": "platform_credential",
"PlatformPrincipal": "platform_principal",
},
)
application_arn = response["PlatformApplicationArn"]
assert application_arn == (
f"arn:aws:sns:us-east-1:{ACCOUNT_ID}:app/APNS/my-application"
)
@mock_aws
def test_get_platform_application_attributes():
conn = boto3.client("sns", region_name="us-east-1")
platform_application = conn.create_platform_application(
Name="my-application",
Platform="APNS",
Attributes={
"PlatformCredential": "platform_credential",
"PlatformPrincipal": "platform_principal",
},
)
arn = platform_application["PlatformApplicationArn"]
attributes = conn.get_platform_application_attributes(PlatformApplicationArn=arn)[
"Attributes"
]
assert attributes == {
"PlatformCredential": "platform_credential",
"PlatformPrincipal": "platform_principal",
}
@mock_aws
def test_get_missing_platform_application_attributes():
conn = boto3.client("sns", region_name="us-east-1")
with pytest.raises(ClientError):
conn.get_platform_application_attributes(PlatformApplicationArn="a-fake-arn")
@mock_aws
def test_set_platform_application_attributes():
conn = boto3.client("sns", region_name="us-east-1")
platform_application = conn.create_platform_application(
Name="my-application",
Platform="APNS",
Attributes={
"PlatformCredential": "platform_credential",
"PlatformPrincipal": "platform_principal",
},
)
arn = platform_application["PlatformApplicationArn"]
conn.set_platform_application_attributes(
PlatformApplicationArn=arn, Attributes={"PlatformPrincipal": "other"}
)
attributes = conn.get_platform_application_attributes(PlatformApplicationArn=arn)[
"Attributes"
]
assert attributes == (
{"PlatformCredential": "platform_credential", "PlatformPrincipal": "other"}
)
@mock_aws
def test_list_platform_applications():
conn = boto3.client("sns", region_name="us-east-1")
conn.create_platform_application(
Name="application1", Platform="APNS", Attributes={}
)
conn.create_platform_application(
Name="application2", Platform="APNS", Attributes={}
)
applications_response = conn.list_platform_applications()
applications = applications_response["PlatformApplications"]
assert len(applications) == 2
@mock_aws
def test_delete_platform_application():
conn = boto3.client("sns", region_name="us-east-1")
conn.create_platform_application(
Name="application1", Platform="APNS", Attributes={}
)
conn.create_platform_application(
Name="application2", Platform="APNS", Attributes={}
)
applications_response = conn.list_platform_applications()
applications = applications_response["PlatformApplications"]
assert len(applications) == 2
application_arn = applications[0]["PlatformApplicationArn"]
conn.delete_platform_application(PlatformApplicationArn=application_arn)
applications_response = conn.list_platform_applications()
applications = applications_response["PlatformApplications"]
assert len(applications) == 1
@mock_aws
def test_create_platform_endpoint():
conn = boto3.client("sns", region_name="us-east-1")
platform_application = conn.create_platform_application(
Name="my-application", Platform="APNS", Attributes={}
)
application_arn = platform_application["PlatformApplicationArn"]
endpoint = conn.create_platform_endpoint(
PlatformApplicationArn=application_arn,
Token="some_unique_id",
CustomUserData="some user data",
Attributes={"Enabled": "false"},
)
endpoint_arn = endpoint["EndpointArn"]
assert (
f"arn:aws:sns:us-east-1:{ACCOUNT_ID}:endpoint/APNS/my-application/"
in endpoint_arn
)
@pytest.mark.aws_verified
@sns_aws_verified
def test_create_duplicate_default_platform_endpoint(api_key=None):
conn = boto3.client("sns", region_name="us-east-1")
platform_name = str(uuid4())[0:6]
app_arn = None
try:
platform_application = conn.create_platform_application(
Name=platform_name,
Platform="GCM",
Attributes={"PlatformCredential": api_key},
)
app_arn = platform_application["PlatformApplicationArn"]
# Create duplicate endpoints
arn1 = conn.create_platform_endpoint(
PlatformApplicationArn=app_arn, Token="token"
)["EndpointArn"]
arn2 = conn.create_platform_endpoint(
PlatformApplicationArn=app_arn, Token="token"
)["EndpointArn"]
# Create another duplicate endpoint, just specify the default value
arn3 = conn.create_platform_endpoint(
PlatformApplicationArn=app_arn,
Token="token",
Attributes={"Enabled": "true"},
)["EndpointArn"]
assert arn1 == arn2
assert arn2 == arn3
finally:
if app_arn is not None:
conn.delete_platform_application(PlatformApplicationArn=app_arn)
@pytest.mark.aws_verified
@sns_aws_verified
def test_create_duplicate_platform_endpoint_with_attrs(api_key=None):
identity = boto3.client("sts", region_name="us-east-1").get_caller_identity()
account_id = identity["Account"]
conn = boto3.client("sns", region_name="us-east-1")
platform_name = str(uuid4())[0:6]
application_arn = None
try:
platform_application = conn.create_platform_application(
Name=platform_name,
Platform="GCM",
Attributes={"PlatformCredential": api_key},
)
application_arn = platform_application["PlatformApplicationArn"]
assert (
application_arn
== f"arn:aws:sns:us-east-1:{account_id}:app/GCM/{platform_name}"
)
# WITHOUT custom user data
resp = conn.create_platform_endpoint(
PlatformApplicationArn=application_arn,
Token="token_without_userdata",
Attributes={"Enabled": "false"},
)
endpoint1_arn = resp["EndpointArn"]
# This operation is idempotent
resp = conn.create_platform_endpoint(
PlatformApplicationArn=application_arn,
Token="token_without_userdata",
Attributes={"Enabled": "false"},
)
assert resp["EndpointArn"] == endpoint1_arn
# Same token, but with CustomUserData
with pytest.raises(ClientError) as exc:
conn.create_platform_endpoint(
PlatformApplicationArn=application_arn,
Token="token_without_userdata",
CustomUserData="some user data",
Attributes={"Enabled": "false"},
)
err = exc.value.response["Error"]
assert err["Code"] == "InvalidParameter"
assert (
err["Message"]
== f"Invalid parameter: Token Reason: Endpoint {endpoint1_arn} already exists with the same Token, but different attributes."
)
# WITH custom user data
resp = conn.create_platform_endpoint(
PlatformApplicationArn=application_arn,
Token="token_with_userdata",
CustomUserData="some user data",
Attributes={"Enabled": "false"},
)
endpoint2_arn = resp["EndpointArn"]
assert endpoint2_arn.startswith(
f"arn:aws:sns:us-east-1:{account_id}:endpoint/GCM/{platform_name}/"
)
# Still idempotent
resp = conn.create_platform_endpoint(
PlatformApplicationArn=application_arn,
Token="token_with_userdata",
CustomUserData="some user data",
Attributes={"Enabled": "false"},
)
assert resp["EndpointArn"] == endpoint2_arn
# Creating a platform endpoint with different attributes fails
with pytest.raises(ClientError) as exc:
conn.create_platform_endpoint(
PlatformApplicationArn=application_arn,
Token="token_with_userdata",
CustomUserData="some user data",
Attributes={"Enabled": "true"},
)
err = exc.value.response["Error"]
assert err["Code"] == "InvalidParameter"
assert (
err["Message"]
== f"Invalid parameter: Token Reason: Endpoint {endpoint2_arn} already exists with the same Token, but different attributes."
)
with pytest.raises(ClientError) as exc:
conn.create_platform_endpoint(
PlatformApplicationArn=application_arn + "2",
Token="unknown_arn",
)
err = exc.value.response["Error"]
assert err["Code"] == "NotFound"
assert err["Message"] == "PlatformApplication does not exist"
finally:
if application_arn is not None:
conn.delete_platform_application(PlatformApplicationArn=application_arn)
@pytest.mark.aws_verified
@sns_aws_verified
def test_get_list_endpoints_by_platform_application(api_key=None):
conn = boto3.client("sns", region_name="us-east-1")
platform_name = str(uuid4())[0:6]
application_arn = None
try:
platform_application = conn.create_platform_application(
Name=platform_name,
Platform="GCM",
Attributes={"PlatformCredential": api_key},
)
application_arn = platform_application["PlatformApplicationArn"]
endpoint = conn.create_platform_endpoint(
PlatformApplicationArn=application_arn,
Token="some_unique_id",
Attributes={"CustomUserData": "some data"},
)
endpoint_arn = endpoint["EndpointArn"]
endpoint_list = conn.list_endpoints_by_platform_application(
PlatformApplicationArn=application_arn
)["Endpoints"]
assert len(endpoint_list) == 1
assert endpoint_list[0]["Attributes"]["CustomUserData"] == "some data"
assert endpoint_list[0]["EndpointArn"] == endpoint_arn
resp = conn.delete_endpoint(EndpointArn=endpoint_arn)
assert resp["ResponseMetadata"]["HTTPStatusCode"] == 200
# Idempotent operation
resp = conn.delete_endpoint(EndpointArn=endpoint_arn)
assert resp["ResponseMetadata"]["HTTPStatusCode"] == 200
finally:
if application_arn is not None:
conn.delete_platform_application(PlatformApplicationArn=application_arn)
with pytest.raises(ClientError) as exc:
conn.list_endpoints_by_platform_application(
PlatformApplicationArn=application_arn + "2"
)
err = exc.value.response["Error"]
assert err["Code"] == "NotFound"
assert err["Message"] == "PlatformApplication does not exist"
@mock_aws
def test_get_endpoint_attributes():
conn = boto3.client("sns", region_name="us-east-1")
platform_application = conn.create_platform_application(
Name="my-application", Platform="APNS", Attributes={}
)
application_arn = platform_application["PlatformApplicationArn"]
endpoint = conn.create_platform_endpoint(
PlatformApplicationArn=application_arn,
Token="some_unique_id",
CustomUserData="some user data",
Attributes={"Enabled": "false", "CustomUserData": "some data"},
)
endpoint_arn = endpoint["EndpointArn"]
attributes = conn.get_endpoint_attributes(EndpointArn=endpoint_arn)["Attributes"]
assert attributes == (
{"Token": "some_unique_id", "Enabled": "false", "CustomUserData": "some data"}
)
@pytest.mark.aws_verified
@sns_aws_verified
def test_get_non_existent_endpoint_attributes(
api_key=None,
):
identity = boto3.client("sts", region_name="us-east-1").get_caller_identity()
account_id = identity["Account"]
conn = boto3.client("sns", region_name="us-east-1")
endpoint_arn = f"arn:aws:sns:us-east-1:{account_id}:endpoint/APNS/my-application/c1f76c42-192a-4e75-b04f-a9268ce2abf3"
with pytest.raises(conn.exceptions.NotFoundException) as excinfo:
conn.get_endpoint_attributes(EndpointArn=endpoint_arn)
error = excinfo.value.response["Error"]
assert error["Type"] == "Sender"
assert error["Code"] == "NotFound"
assert error["Message"] == "Endpoint does not exist"
@mock_aws
def test_get_missing_endpoint_attributes():
conn = boto3.client("sns", region_name="us-east-1")
with pytest.raises(ClientError):
conn.get_endpoint_attributes(EndpointArn="a-fake-arn")
@mock_aws
def test_set_endpoint_attributes():
conn = boto3.client("sns", region_name="us-east-1")
platform_application = conn.create_platform_application(
Name="my-application", Platform="APNS", Attributes={}
)
application_arn = platform_application["PlatformApplicationArn"]
endpoint = conn.create_platform_endpoint(
PlatformApplicationArn=application_arn,
Token="some_unique_id",
CustomUserData="some user data",
Attributes={"Enabled": "false", "CustomUserData": "some data"},
)
endpoint_arn = endpoint["EndpointArn"]
conn.set_endpoint_attributes(
EndpointArn=endpoint_arn, Attributes={"CustomUserData": "other data"}
)
attributes = conn.get_endpoint_attributes(EndpointArn=endpoint_arn)["Attributes"]
assert attributes == (
{"Token": "some_unique_id", "Enabled": "false", "CustomUserData": "other data"}
)
@mock_aws
def test_delete_endpoint():
conn = boto3.client("sns", region_name="us-east-1")
platform_application = conn.create_platform_application(
Name="my-application", Platform="APNS", Attributes={}
)
app_arn = platform_application["PlatformApplicationArn"]
endpoint = conn.create_platform_endpoint(
PlatformApplicationArn=app_arn,
Token="some_unique_id",
CustomUserData="some user data",
Attributes={"Enabled": "true"},
)
endpoints = conn.list_endpoints_by_platform_application(
PlatformApplicationArn=app_arn
)["Endpoints"]
assert len(endpoints) == 1
conn.delete_endpoint(EndpointArn=endpoint["EndpointArn"])
endpoints = conn.list_endpoints_by_platform_application(
PlatformApplicationArn=app_arn
)["Endpoints"]
assert len(endpoints) == 0
@mock_aws
def test_publish_to_platform_endpoint():
conn = boto3.client("sns", region_name="us-east-1")
platform_application = conn.create_platform_application(
Name="my-application", Platform="APNS", Attributes={}
)
application_arn = platform_application["PlatformApplicationArn"]
endpoint = conn.create_platform_endpoint(
PlatformApplicationArn=application_arn,
Token="some_unique_id",
CustomUserData="some user data",
Attributes={"Enabled": "true"},
)
endpoint_arn = endpoint["EndpointArn"]
conn.publish(
Message="some message", MessageStructure="json", TargetArn=endpoint_arn
)
@pytest.mark.aws_verified
@sns_aws_verified
def test_publish_to_disabled_platform_endpoint(api_key=None):
conn = boto3.client("sns", region_name="us-east-1")
platform_name = str(uuid4())[0:6]
application_arn = None
try:
platform_application = conn.create_platform_application(
Name=platform_name,
Platform="GCM",
Attributes={"PlatformCredential": api_key},
)
application_arn = platform_application["PlatformApplicationArn"]
endpoint = conn.create_platform_endpoint(
PlatformApplicationArn=application_arn,
Token="some_unique_id",
Attributes={"Enabled": "false"},
)
endpoint_arn = endpoint["EndpointArn"]
with pytest.raises(ClientError) as exc:
conn.publish(Message="msg", MessageStructure="json", TargetArn=endpoint_arn)
err = exc.value.response["Error"]
assert err["Code"] == "EndpointDisabled"
assert err["Message"] == "Endpoint is disabled"
finally:
if application_arn is not None:
conn.delete_platform_application(PlatformApplicationArn=application_arn)
@sns_aws_verified
def test_publish_to_deleted_platform_endpoint(api_key=None):
"""
This used to run against AWS, but they have changed the API, and this currently throws an exception:
Invalid parameter: Attributes Reason: Platform credentials are invalid
Need to change this test accordingly
https://docs.aws.amazon.com/sns/latest/dg/sns-send-custom-platform-specific-payloads-mobile-devices.html
> Amazon SNS now supports Firebase Cloud Messaging (FCM) HTTP v1 API for sending mobile push notifications to Android devices.
>
> March 26, 2024 – Amazon SNS supports FCM HTTP v1 API for Apple devices and Webpush destinations.
> We recommend that you migrate your existing mobile push applications to the latest FCM HTTP v1 API on or before June 1, 2024 to avoid application disruption.
"""
conn = boto3.client("sns", region_name="us-east-1")
platform_name = str(uuid4())[0:6]
topic_name = "topic_" + str(uuid4())[0:6]
application_arn = None
try:
platform_application = conn.create_platform_application(
Name=platform_name,
Platform="GCM",
Attributes={"PlatformCredential": api_key},
)
application_arn = platform_application["PlatformApplicationArn"]
endpoint_arn = conn.create_platform_endpoint(
PlatformApplicationArn=application_arn,
Token="some_unique_id",
Attributes={"Enabled": "false"},
)["EndpointArn"]
topic_arn = conn.create_topic(Name=topic_name)["TopicArn"]
conn.delete_endpoint(EndpointArn=endpoint_arn)
with pytest.raises(ClientError) as exc:
conn.subscribe(
TopicArn=topic_arn,
Endpoint=endpoint_arn,
Protocol="application",
)
err = exc.value.response["Error"]
assert err["Code"] == "InvalidParameter"
assert (
err["Message"]
== f"Invalid parameter: Endpoint Reason: Endpoint does not exist for endpoint arn{endpoint_arn}"
)
finally:
if topic_arn is not None:
conn.delete_topic(TopicArn=topic_arn)
if application_arn is not None:
conn.delete_platform_application(PlatformApplicationArn=application_arn)
@mock_aws
def test_set_sms_attributes():
conn = boto3.client("sns", region_name="us-east-1")
conn.set_sms_attributes(
attributes={"DefaultSMSType": "Transactional", "test": "test"}
)
response = conn.get_sms_attributes()
assert "attributes" in response
assert "DefaultSMSType" in response["attributes"]
assert "test" in response["attributes"]
assert response["attributes"]["DefaultSMSType"] == "Transactional"
assert response["attributes"]["test"] == "test"
@mock_aws
def test_get_sms_attributes_filtered():
conn = boto3.client("sns", region_name="us-east-1")
conn.set_sms_attributes(
attributes={"DefaultSMSType": "Transactional", "test": "test"}
)
response = conn.get_sms_attributes(attributes=["DefaultSMSType"])
assert "attributes" in response
assert "DefaultSMSType" in response["attributes"]
assert "test" not in response["attributes"]
assert response["attributes"]["DefaultSMSType"] == "Transactional"
@mock_aws
def test_delete_endpoints_of_delete_app():
conn = boto3.client("sns", region_name="us-east-1")
platform_app_arn = conn.create_platform_application(
Name="app-test-kevs", Platform="GCM", Attributes={"PlatformCredential": "test"}
)["PlatformApplicationArn"]
endpoint_arn = conn.create_platform_endpoint(
PlatformApplicationArn=platform_app_arn,
Token="test",
)["EndpointArn"]
conn.delete_platform_application(PlatformApplicationArn=platform_app_arn)
with pytest.raises(conn.exceptions.NotFoundException) as excinfo:
conn.get_endpoint_attributes(EndpointArn=endpoint_arn)
error = excinfo.value.response["Error"]
assert error["Type"] == "Sender"
assert error["Code"] == "NotFound"
assert error["Message"] == "Endpoint does not exist"
|