moto/tests/test_ses/test_ses.py

180 lines
5.0 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
2013-02-25 04:30:51 +00:00
import email
import boto
from boto.exception import BotoServerError
2013-08-03 21:21:25 +00:00
import sure # noqa
2013-02-25 04:30:51 +00:00
2017-02-16 03:35:45 +00:00
from moto import mock_ses_deprecated
2013-02-25 04:30:51 +00:00
2017-02-16 03:35:45 +00:00
@mock_ses_deprecated
2013-02-25 04:30:51 +00:00
def test_verify_email_identity():
2019-10-31 15:44:26 +00:00
conn = boto.connect_ses("the_key", "the_secret")
2013-02-25 04:30:51 +00:00
conn.verify_email_identity("test@example.com")
identities = conn.list_identities()
2019-10-31 15:44:26 +00:00
address = identities["ListIdentitiesResponse"]["ListIdentitiesResult"][
"Identities"
][0]
address.should.equal("test@example.com")
2013-02-25 04:30:51 +00:00
2017-02-16 03:35:45 +00:00
@mock_ses_deprecated
2013-02-25 04:30:51 +00:00
def test_domain_verify():
2019-10-31 15:44:26 +00:00
conn = boto.connect_ses("the_key", "the_secret")
2013-02-25 04:30:51 +00:00
conn.verify_domain_dkim("domain1.com")
conn.verify_domain_identity("domain2.com")
identities = conn.list_identities()
2019-10-31 15:44:26 +00:00
domains = list(
identities["ListIdentitiesResponse"]["ListIdentitiesResult"]["Identities"]
)
domains.should.equal(["domain1.com", "domain2.com"])
2013-02-25 04:30:51 +00:00
2017-02-16 03:35:45 +00:00
@mock_ses_deprecated
2013-02-25 04:30:51 +00:00
def test_delete_identity():
2019-10-31 15:44:26 +00:00
conn = boto.connect_ses("the_key", "the_secret")
2013-02-25 04:30:51 +00:00
conn.verify_email_identity("test@example.com")
2019-10-31 15:44:26 +00:00
conn.list_identities()["ListIdentitiesResponse"]["ListIdentitiesResult"][
"Identities"
].should.have.length_of(1)
2013-02-25 04:30:51 +00:00
conn.delete_identity("test@example.com")
2019-10-31 15:44:26 +00:00
conn.list_identities()["ListIdentitiesResponse"]["ListIdentitiesResult"][
"Identities"
].should.have.length_of(0)
2013-02-25 04:30:51 +00:00
2017-02-16 03:35:45 +00:00
@mock_ses_deprecated
2013-02-25 04:30:51 +00:00
def test_send_email():
2019-10-31 15:44:26 +00:00
conn = boto.connect_ses("the_key", "the_secret")
2013-02-25 04:30:51 +00:00
2013-08-03 21:21:25 +00:00
conn.send_email.when.called_with(
2019-10-31 15:44:26 +00:00
"test@example.com", "test subject", "test body", "test_to@example.com"
).should.throw(BotoServerError)
2013-02-25 04:30:51 +00:00
conn.verify_email_identity("test@example.com")
2019-10-31 15:44:26 +00:00
conn.send_email(
"test@example.com", "test subject", "test body", "test_to@example.com"
)
2013-02-25 04:30:51 +00:00
send_quota = conn.get_send_quota()
2019-10-31 15:44:26 +00:00
sent_count = int(
send_quota["GetSendQuotaResponse"]["GetSendQuotaResult"]["SentLast24Hours"]
)
2013-02-25 04:30:51 +00:00
sent_count.should.equal(1)
2017-02-24 02:37:43 +00:00
2017-02-16 03:35:45 +00:00
@mock_ses_deprecated
2013-09-26 13:17:38 +00:00
def test_send_html_email():
2019-10-31 15:44:26 +00:00
conn = boto.connect_ses("the_key", "the_secret")
2013-09-26 13:17:38 +00:00
conn.send_email.when.called_with(
2019-10-31 15:44:26 +00:00
"test@example.com",
"test subject",
"<span>test body</span>",
"test_to@example.com",
format="html",
).should.throw(BotoServerError)
2013-02-25 04:30:51 +00:00
2013-09-26 13:17:38 +00:00
conn.verify_email_identity("test@example.com")
2019-10-31 15:44:26 +00:00
conn.send_email(
"test@example.com",
"test subject",
"<span>test body</span>",
"test_to@example.com",
format="html",
)
2013-09-26 13:17:38 +00:00
send_quota = conn.get_send_quota()
2019-10-31 15:44:26 +00:00
sent_count = int(
send_quota["GetSendQuotaResponse"]["GetSendQuotaResult"]["SentLast24Hours"]
)
2013-09-26 13:17:38 +00:00
sent_count.should.equal(1)
2013-02-25 04:30:51 +00:00
2017-02-24 02:37:43 +00:00
2017-02-16 03:35:45 +00:00
@mock_ses_deprecated
2013-02-25 04:30:51 +00:00
def test_send_raw_email():
2019-10-31 15:44:26 +00:00
conn = boto.connect_ses("the_key", "the_secret")
2013-02-25 04:30:51 +00:00
message = email.mime.multipart.MIMEMultipart()
2019-10-31 15:44:26 +00:00
message["Subject"] = "Test"
message["From"] = "test@example.com"
message["To"] = "to@example.com"
2013-02-25 04:30:51 +00:00
# Message body
2019-10-31 15:44:26 +00:00
part = email.mime.text.MIMEText("test file attached")
2013-02-25 04:30:51 +00:00
message.attach(part)
# Attachment
2019-10-31 15:44:26 +00:00
part = email.mime.text.MIMEText("contents of test file here")
part.add_header("Content-Disposition", "attachment; filename=test.txt")
2013-02-25 04:30:51 +00:00
message.attach(part)
2013-08-03 21:21:25 +00:00
conn.send_raw_email.when.called_with(
2019-10-31 15:44:26 +00:00
source=message["From"], raw_message=message.as_string()
2013-08-03 21:21:25 +00:00
).should.throw(BotoServerError)
2013-02-25 04:30:51 +00:00
conn.verify_email_identity("test@example.com")
2019-10-31 15:44:26 +00:00
conn.send_raw_email(source=message["From"], raw_message=message.as_string())
2013-02-25 04:30:51 +00:00
send_quota = conn.get_send_quota()
2019-10-31 15:44:26 +00:00
sent_count = int(
send_quota["GetSendQuotaResponse"]["GetSendQuotaResult"]["SentLast24Hours"]
)
2013-02-25 04:30:51 +00:00
sent_count.should.equal(1)
@mock_ses_deprecated
def test_get_send_statistics():
conn = boto.connect_ses("the_key", "the_secret")
conn.send_email.when.called_with(
"test@example.com",
"test subject",
"<span>test body</span>",
"test_to@example.com",
format="html",
).should.throw(BotoServerError)
# tests to verify rejects in get_send_statistics
result = conn.get_send_statistics()
2020-05-04 08:24:46 +00:00
reject_count = int(
2020-05-07 08:53:07 +00:00
result["GetSendStatisticsResponse"]["GetSendStatisticsResult"][
"SendDataPoints"
][0]["Rejects"]
2020-05-04 08:24:46 +00:00
)
delivery_count = int(
2020-05-07 08:53:07 +00:00
result["GetSendStatisticsResponse"]["GetSendStatisticsResult"][
"SendDataPoints"
][0]["DeliveryAttempts"]
2020-05-04 08:24:46 +00:00
)
reject_count.should.equal(1)
delivery_count.should.equal(0)
conn.verify_email_identity("test@example.com")
conn.send_email(
"test@example.com", "test subject", "test body", "test_to@example.com"
)
# tests to delivery attempts in get_send_statistics
result = conn.get_send_statistics()
2020-05-04 08:24:46 +00:00
reject_count = int(
2020-05-07 08:53:07 +00:00
result["GetSendStatisticsResponse"]["GetSendStatisticsResult"][
"SendDataPoints"
][0]["Rejects"]
2020-05-04 08:24:46 +00:00
)
delivery_count = int(
2020-05-07 08:53:07 +00:00
result["GetSendStatisticsResponse"]["GetSendStatisticsResult"][
"SendDataPoints"
][0]["DeliveryAttempts"]
2020-05-04 08:24:46 +00:00
)
reject_count.should.equal(1)
delivery_count.should.equal(1)