[SNS] Mock sending directly SMS (#3253)

* [SNS] Mock sending directly SMS

Proper behaviour when publishing to PhoneNumber is sending
message directly to this number, without any topic or
previous confirmation.

https://docs.aws.amazon.com/sns/latest/dg/sns-mobile-phone-number-as-subscriber.html

* Fix arguments order

* Omit checking local backend when tests in server mode
This commit is contained in:
Kamil Mańkowski 2020-08-25 14:05:49 +02:00 committed by GitHub
commit 8a551a9754
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 43 deletions

View file

@ -11,8 +11,9 @@ import sure # noqa
import responses
from botocore.exceptions import ClientError
from nose.tools import assert_raises
from moto import mock_sns, mock_sqs
from moto import mock_sns, mock_sqs, settings
from moto.core import ACCOUNT_ID
from moto.sns import sns_backend
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:'
@ -223,36 +224,31 @@ def test_publish_to_sqs_msg_attr_number_type():
@mock_sns
def test_publish_sms():
client = boto3.client("sns", region_name="us-east-1")
client.create_topic(Name="some-topic")
resp = client.create_topic(Name="some-topic")
arn = resp["TopicArn"]
client.subscribe(TopicArn=arn, Protocol="sms", Endpoint="+15551234567")
result = client.publish(PhoneNumber="+15551234567", Message="my message")
result.should.contain("MessageId")
if not settings.TEST_SERVER_MODE:
sns_backend.sms_messages.should.have.key(result["MessageId"]).being.equal(
("+15551234567", "my message")
)
@mock_sns
def test_publish_bad_sms():
client = boto3.client("sns", region_name="us-east-1")
client.create_topic(Name="some-topic")
resp = client.create_topic(Name="some-topic")
arn = resp["TopicArn"]
client.subscribe(TopicArn=arn, Protocol="sms", Endpoint="+15551234567")
try:
# Test invalid number
# Test invalid number
with assert_raises(ClientError) as cm:
client.publish(PhoneNumber="NAA+15551234567", Message="my message")
except ClientError as err:
err.response["Error"]["Code"].should.equal("InvalidParameter")
cm.exception.response["Error"]["Code"].should.equal("InvalidParameter")
cm.exception.response["Error"]["Message"].should.contain("not meet the E164")
try:
# Test not found number
client.publish(PhoneNumber="+44001234567", Message="my message")
except ClientError as err:
err.response["Error"]["Code"].should.equal("ParameterValueInvalid")
# Test to long ASCII message
with assert_raises(ClientError) as cm:
client.publish(PhoneNumber="+15551234567", Message="a" * 1601)
cm.exception.response["Error"]["Code"].should.equal("InvalidParameter")
cm.exception.response["Error"]["Message"].should.contain("must be less than 1600")
@mock_sqs