2021-12-24 21:02:45 +00:00
|
|
|
import boto3
|
2022-12-15 12:17:50 +00:00
|
|
|
from unittest import mock
|
2021-12-24 21:02:45 +00:00
|
|
|
import os
|
|
|
|
import pytest
|
2022-03-11 22:29:16 +00:00
|
|
|
from moto import mock_dynamodb, mock_sns, settings
|
2021-12-24 21:02:45 +00:00
|
|
|
from unittest import SkipTest
|
|
|
|
|
|
|
|
|
|
|
|
@mock_sns
|
|
|
|
def test_use_invalid_region():
|
|
|
|
if settings.TEST_SERVER_MODE:
|
|
|
|
raise SkipTest("ServerMode will throw different errors")
|
|
|
|
client = boto3.client("sns", region_name="any-region")
|
|
|
|
with pytest.raises(KeyError) as exc:
|
|
|
|
client.list_platform_applications()
|
2023-07-10 21:04:31 +00:00
|
|
|
assert "any-region" in str(exc.value)
|
2021-12-24 21:02:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sns
|
|
|
|
@mock.patch.dict(os.environ, {"AWS_DEFAULT_REGION": "us-east-2"})
|
|
|
|
def test_use_region_from_env():
|
|
|
|
client = boto3.client("sns")
|
2023-07-10 21:04:31 +00:00
|
|
|
assert client.list_platform_applications()["PlatformApplications"] == []
|
2021-12-24 21:02:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sns
|
|
|
|
@mock.patch.dict(os.environ, {"AWS_DEFAULT_REGION": "any-region"})
|
|
|
|
def test_use_unknown_region_from_env():
|
|
|
|
if settings.TEST_SERVER_MODE:
|
|
|
|
raise SkipTest("Cannot set environemnt variables in ServerMode")
|
|
|
|
client = boto3.client("sns")
|
|
|
|
with pytest.raises(KeyError) as exc:
|
|
|
|
client.list_platform_applications()
|
2023-07-10 21:04:31 +00:00
|
|
|
assert "any-region" in str(exc.value)
|
2021-12-24 21:02:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sns
|
|
|
|
@mock.patch.dict(os.environ, {"AWS_DEFAULT_REGION": "any-region"})
|
|
|
|
@mock.patch.dict(os.environ, {"MOTO_ALLOW_NONEXISTENT_REGION": "trUe"})
|
|
|
|
def test_use_unknown_region_from_env_but_allow_it():
|
|
|
|
if settings.TEST_SERVER_MODE:
|
|
|
|
raise SkipTest("Cannot set environemnt variables in ServerMode")
|
|
|
|
client = boto3.client("sns")
|
2023-07-10 21:04:31 +00:00
|
|
|
assert client.list_platform_applications()["PlatformApplications"] == []
|
2022-02-27 21:47:05 +00:00
|
|
|
|
|
|
|
|
2022-03-11 22:29:16 +00:00
|
|
|
@mock_dynamodb
|
2022-02-27 21:47:05 +00:00
|
|
|
@mock.patch.dict(os.environ, {"MOTO_ALLOW_NONEXISTENT_REGION": "trUe"})
|
|
|
|
def test_use_unknown_region_from_env_but_allow_it__dynamo():
|
|
|
|
if settings.TEST_SERVER_MODE:
|
|
|
|
raise SkipTest("Cannot set environemnt variables in ServerMode")
|
|
|
|
dynamo_db = boto3.resource("dynamodb", region_name="test")
|
|
|
|
dynamo_db.create_table(
|
|
|
|
TableName="test_table",
|
|
|
|
KeySchema=[{"AttributeName": "key", "KeyType": "HASH"}],
|
|
|
|
AttributeDefinitions=[{"AttributeName": "key", "AttributeType": "S"}],
|
|
|
|
BillingMode="PAY_PER_REQUEST",
|
|
|
|
)
|
|
|
|
tables = list(dynamo_db.tables.all())
|
2023-07-10 21:04:31 +00:00
|
|
|
assert [table.name for table in tables] == ["test_table"]
|