2016-03-09 05:05:17 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import boto3
|
|
|
|
from botocore.exceptions import ClientError
|
|
|
|
from six.moves.email_mime_multipart import MIMEMultipart
|
|
|
|
from six.moves.email_mime_text import MIMEText
|
2020-05-01 15:46:33 +00:00
|
|
|
from nose.tools import assert_raises
|
|
|
|
|
2016-03-09 05:05:17 +00:00
|
|
|
|
|
|
|
import sure # noqa
|
|
|
|
|
|
|
|
from moto import mock_ses
|
|
|
|
|
|
|
|
|
|
|
|
@mock_ses
|
|
|
|
def test_verify_email_identity():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("ses", region_name="us-east-1")
|
2016-03-09 05:05:17 +00:00
|
|
|
conn.verify_email_identity(EmailAddress="test@example.com")
|
|
|
|
|
|
|
|
identities = conn.list_identities()
|
2019-10-31 15:44:26 +00:00
|
|
|
address = identities["Identities"][0]
|
|
|
|
address.should.equal("test@example.com")
|
|
|
|
|
2016-03-09 05:05:17 +00:00
|
|
|
|
2017-07-11 08:02:31 +00:00
|
|
|
@mock_ses
|
|
|
|
def test_verify_email_address():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("ses", region_name="us-east-1")
|
2017-07-11 08:02:31 +00:00
|
|
|
conn.verify_email_address(EmailAddress="test@example.com")
|
|
|
|
email_addresses = conn.list_verified_email_addresses()
|
2019-10-31 15:44:26 +00:00
|
|
|
email = email_addresses["VerifiedEmailAddresses"][0]
|
|
|
|
email.should.equal("test@example.com")
|
|
|
|
|
2016-03-09 05:05:17 +00:00
|
|
|
|
|
|
|
@mock_ses
|
|
|
|
def test_domain_verify():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("ses", region_name="us-east-1")
|
2016-03-09 05:05:17 +00:00
|
|
|
|
|
|
|
conn.verify_domain_dkim(Domain="domain1.com")
|
|
|
|
conn.verify_domain_identity(Domain="domain2.com")
|
|
|
|
|
|
|
|
identities = conn.list_identities()
|
2019-10-31 15:44:26 +00:00
|
|
|
domains = list(identities["Identities"])
|
|
|
|
domains.should.equal(["domain1.com", "domain2.com"])
|
2016-03-09 05:05:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ses
|
|
|
|
def test_delete_identity():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("ses", region_name="us-east-1")
|
2016-03-09 05:05:17 +00:00
|
|
|
conn.verify_email_identity(EmailAddress="test@example.com")
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.list_identities()["Identities"].should.have.length_of(1)
|
2016-03-09 05:05:17 +00:00
|
|
|
conn.delete_identity(Identity="test@example.com")
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.list_identities()["Identities"].should.have.length_of(0)
|
2016-03-09 05:05:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ses
|
|
|
|
def test_send_email():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("ses", region_name="us-east-1")
|
2016-03-09 05:05:17 +00:00
|
|
|
|
|
|
|
kwargs = dict(
|
|
|
|
Source="test@example.com",
|
|
|
|
Destination={
|
|
|
|
"ToAddresses": ["test_to@example.com"],
|
|
|
|
"CcAddresses": ["test_cc@example.com"],
|
|
|
|
"BccAddresses": ["test_bcc@example.com"],
|
|
|
|
},
|
|
|
|
Message={
|
|
|
|
"Subject": {"Data": "test subject"},
|
2019-10-31 15:44:26 +00:00
|
|
|
"Body": {"Text": {"Data": "test body"}},
|
|
|
|
},
|
2016-03-09 05:05:17 +00:00
|
|
|
)
|
|
|
|
conn.send_email.when.called_with(**kwargs).should.throw(ClientError)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.verify_domain_identity(Domain="example.com")
|
2016-03-09 05:05:17 +00:00
|
|
|
conn.send_email(**kwargs)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
too_many_addresses = list("to%s@example.com" % i for i in range(51))
|
2016-03-09 05:05:17 +00:00
|
|
|
conn.send_email.when.called_with(
|
2019-10-31 15:44:26 +00:00
|
|
|
**dict(kwargs, Destination={"ToAddresses": too_many_addresses})
|
2016-03-09 05:05:17 +00:00
|
|
|
).should.throw(ClientError)
|
|
|
|
|
|
|
|
send_quota = conn.get_send_quota()
|
2019-10-31 15:44:26 +00:00
|
|
|
sent_count = int(send_quota["SentLast24Hours"])
|
2016-03-09 05:05:17 +00:00
|
|
|
sent_count.should.equal(3)
|
|
|
|
|
|
|
|
|
2019-10-02 07:39:35 +00:00
|
|
|
@mock_ses
|
|
|
|
def test_send_templated_email():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("ses", region_name="us-east-1")
|
2019-10-02 07:39:35 +00:00
|
|
|
|
|
|
|
kwargs = dict(
|
|
|
|
Source="test@example.com",
|
|
|
|
Destination={
|
|
|
|
"ToAddresses": ["test_to@example.com"],
|
|
|
|
"CcAddresses": ["test_cc@example.com"],
|
|
|
|
"BccAddresses": ["test_bcc@example.com"],
|
|
|
|
},
|
|
|
|
Template="test_template",
|
2019-10-31 15:44:26 +00:00
|
|
|
TemplateData='{"name": "test"}',
|
2019-10-02 07:39:35 +00:00
|
|
|
)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.send_templated_email.when.called_with(**kwargs).should.throw(ClientError)
|
2019-10-02 07:39:35 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
conn.verify_domain_identity(Domain="example.com")
|
2019-10-02 07:39:35 +00:00
|
|
|
conn.send_templated_email(**kwargs)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
too_many_addresses = list("to%s@example.com" % i for i in range(51))
|
2019-10-02 07:39:35 +00:00
|
|
|
conn.send_templated_email.when.called_with(
|
2019-10-31 15:44:26 +00:00
|
|
|
**dict(kwargs, Destination={"ToAddresses": too_many_addresses})
|
2019-10-02 07:39:35 +00:00
|
|
|
).should.throw(ClientError)
|
|
|
|
|
|
|
|
send_quota = conn.get_send_quota()
|
2019-10-31 15:44:26 +00:00
|
|
|
sent_count = int(send_quota["SentLast24Hours"])
|
2019-10-02 07:39:35 +00:00
|
|
|
sent_count.should.equal(3)
|
|
|
|
|
|
|
|
|
2016-03-09 05:05:17 +00:00
|
|
|
@mock_ses
|
|
|
|
def test_send_html_email():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("ses", region_name="us-east-1")
|
2016-03-09 05:05:17 +00:00
|
|
|
|
|
|
|
kwargs = dict(
|
|
|
|
Source="test@example.com",
|
2019-10-31 15:44:26 +00:00
|
|
|
Destination={"ToAddresses": ["test_to@example.com"]},
|
2016-03-09 05:05:17 +00:00
|
|
|
Message={
|
|
|
|
"Subject": {"Data": "test subject"},
|
2019-10-31 15:44:26 +00:00
|
|
|
"Body": {"Html": {"Data": "test body"}},
|
|
|
|
},
|
2016-03-09 05:05:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
conn.send_email.when.called_with(**kwargs).should.throw(ClientError)
|
|
|
|
|
|
|
|
conn.verify_email_identity(EmailAddress="test@example.com")
|
|
|
|
conn.send_email(**kwargs)
|
|
|
|
|
|
|
|
send_quota = conn.get_send_quota()
|
2019-10-31 15:44:26 +00:00
|
|
|
sent_count = int(send_quota["SentLast24Hours"])
|
2016-03-09 05:05:17 +00:00
|
|
|
sent_count.should.equal(1)
|
|
|
|
|
|
|
|
|
|
|
|
@mock_ses
|
|
|
|
def test_send_raw_email():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("ses", region_name="us-east-1")
|
2016-03-09 05:05:17 +00:00
|
|
|
|
|
|
|
message = MIMEMultipart()
|
2019-10-31 15:44:26 +00:00
|
|
|
message["Subject"] = "Test"
|
|
|
|
message["From"] = "test@example.com"
|
|
|
|
message["To"] = "to@example.com, foo@example.com"
|
2016-03-09 05:05:17 +00:00
|
|
|
|
|
|
|
# Message body
|
2019-10-31 15:44:26 +00:00
|
|
|
part = MIMEText("test file attached")
|
2016-03-09 05:05:17 +00:00
|
|
|
message.attach(part)
|
|
|
|
|
|
|
|
# Attachment
|
2019-10-31 15:44:26 +00:00
|
|
|
part = MIMEText("contents of test file here")
|
|
|
|
part.add_header("Content-Disposition", "attachment; filename=test.txt")
|
2016-03-09 05:05:17 +00:00
|
|
|
message.attach(part)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
kwargs = dict(Source=message["From"], RawMessage={"Data": message.as_string()})
|
2016-03-09 05:05:17 +00:00
|
|
|
|
|
|
|
conn.send_raw_email.when.called_with(**kwargs).should.throw(ClientError)
|
|
|
|
|
|
|
|
conn.verify_email_identity(EmailAddress="test@example.com")
|
|
|
|
conn.send_raw_email(**kwargs)
|
|
|
|
|
|
|
|
send_quota = conn.get_send_quota()
|
2019-10-31 15:44:26 +00:00
|
|
|
sent_count = int(send_quota["SentLast24Hours"])
|
2016-03-09 05:05:17 +00:00
|
|
|
sent_count.should.equal(2)
|
2018-05-05 01:22:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ses
|
|
|
|
def test_send_raw_email_without_source():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("ses", region_name="us-east-1")
|
2018-05-05 01:22:47 +00:00
|
|
|
|
|
|
|
message = MIMEMultipart()
|
2019-10-31 15:44:26 +00:00
|
|
|
message["Subject"] = "Test"
|
|
|
|
message["From"] = "test@example.com"
|
|
|
|
message["To"] = "to@example.com, foo@example.com"
|
2018-05-05 01:22:47 +00:00
|
|
|
|
|
|
|
# Message body
|
2019-10-31 15:44:26 +00:00
|
|
|
part = MIMEText("test file attached")
|
2018-05-05 01:22:47 +00:00
|
|
|
message.attach(part)
|
|
|
|
|
|
|
|
# Attachment
|
2019-10-31 15:44:26 +00:00
|
|
|
part = MIMEText("contents of test file here")
|
|
|
|
part.add_header("Content-Disposition", "attachment; filename=test.txt")
|
2018-05-05 01:22:47 +00:00
|
|
|
message.attach(part)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
kwargs = dict(RawMessage={"Data": message.as_string()})
|
2018-05-05 01:22:47 +00:00
|
|
|
|
|
|
|
conn.send_raw_email.when.called_with(**kwargs).should.throw(ClientError)
|
|
|
|
|
|
|
|
conn.verify_email_identity(EmailAddress="test@example.com")
|
|
|
|
conn.send_raw_email(**kwargs)
|
|
|
|
|
|
|
|
send_quota = conn.get_send_quota()
|
2019-10-31 15:44:26 +00:00
|
|
|
sent_count = int(send_quota["SentLast24Hours"])
|
2018-05-05 01:22:47 +00:00
|
|
|
sent_count.should.equal(2)
|
|
|
|
|
|
|
|
|
|
|
|
@mock_ses
|
|
|
|
def test_send_raw_email_without_source_or_from():
|
2019-10-31 15:44:26 +00:00
|
|
|
conn = boto3.client("ses", region_name="us-east-1")
|
2018-05-05 01:22:47 +00:00
|
|
|
|
|
|
|
message = MIMEMultipart()
|
2019-10-31 15:44:26 +00:00
|
|
|
message["Subject"] = "Test"
|
|
|
|
message["To"] = "to@example.com, foo@example.com"
|
2018-05-05 01:22:47 +00:00
|
|
|
|
|
|
|
# Message body
|
2019-10-31 15:44:26 +00:00
|
|
|
part = MIMEText("test file attached")
|
2018-05-05 01:22:47 +00:00
|
|
|
message.attach(part)
|
|
|
|
# Attachment
|
2019-10-31 15:44:26 +00:00
|
|
|
part = MIMEText("contents of test file here")
|
|
|
|
part.add_header("Content-Disposition", "attachment; filename=test.txt")
|
2018-05-05 01:22:47 +00:00
|
|
|
message.attach(part)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
kwargs = dict(RawMessage={"Data": message.as_string()})
|
2018-05-05 01:22:47 +00:00
|
|
|
|
|
|
|
conn.send_raw_email.when.called_with(**kwargs).should.throw(ClientError)
|
2020-03-18 11:46:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ses
|
|
|
|
def test_send_email_notification_with_encoded_sender():
|
|
|
|
sender = "Foo <foo@bar.baz>"
|
|
|
|
conn = boto3.client("ses", region_name="us-east-1")
|
|
|
|
conn.verify_email_identity(EmailAddress=sender)
|
|
|
|
response = conn.send_email(
|
|
|
|
Source=sender,
|
|
|
|
Destination={"ToAddresses": ["your.friend@hotmail.com"]},
|
|
|
|
Message={"Subject": {"Data": "hi",}, "Body": {"Text": {"Data": "there",}}},
|
|
|
|
)
|
|
|
|
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
|
2020-05-01 15:46:33 +00:00
|
|
|
|
2020-05-04 08:24:46 +00:00
|
|
|
|
2020-05-01 15:46:33 +00:00
|
|
|
@mock_ses
|
|
|
|
def test_create_configuration_set():
|
|
|
|
conn = boto3.client("ses", region_name="us-east-1")
|
|
|
|
conn.create_configuration_set(ConfigurationSet=dict({"Name": "test"}))
|
|
|
|
|
|
|
|
conn.create_configuration_set_event_destination(
|
2020-05-04 08:24:46 +00:00
|
|
|
ConfigurationSetName="test",
|
|
|
|
EventDestination={
|
|
|
|
"Name": "snsEvent",
|
|
|
|
"Enabled": True,
|
|
|
|
"MatchingEventTypes": ["send",],
|
|
|
|
"SNSDestination": {
|
|
|
|
"TopicARN": "arn:aws:sns:us-east-1:123456789012:myTopic"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2020-05-01 15:46:33 +00:00
|
|
|
|
|
|
|
with assert_raises(ClientError) as ex:
|
|
|
|
conn.create_configuration_set_event_destination(
|
2020-05-04 08:24:46 +00:00
|
|
|
ConfigurationSetName="failtest",
|
2020-05-01 15:46:33 +00:00
|
|
|
EventDestination={
|
2020-05-04 08:24:46 +00:00
|
|
|
"Name": "snsEvent",
|
|
|
|
"Enabled": True,
|
|
|
|
"MatchingEventTypes": ["send",],
|
|
|
|
"SNSDestination": {
|
|
|
|
"TopicARN": "arn:aws:sns:us-east-1:123456789012:myTopic"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2020-05-01 15:46:33 +00:00
|
|
|
|
|
|
|
ex.exception.response["Error"]["Code"].should.equal("ConfigurationSetDoesNotExist")
|
|
|
|
|
|
|
|
with assert_raises(ClientError) as ex:
|
|
|
|
conn.create_configuration_set_event_destination(
|
2020-05-04 08:24:46 +00:00
|
|
|
ConfigurationSetName="test",
|
2020-05-01 15:46:33 +00:00
|
|
|
EventDestination={
|
2020-05-04 08:24:46 +00:00
|
|
|
"Name": "snsEvent",
|
|
|
|
"Enabled": True,
|
|
|
|
"MatchingEventTypes": ["send",],
|
|
|
|
"SNSDestination": {
|
|
|
|
"TopicARN": "arn:aws:sns:us-east-1:123456789012:myTopic"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ex.exception.response["Error"]["Code"].should.equal("EventDestinationAlreadyExists")
|