Reformat code with automated tools

Apply standardized code style
This commit is contained in:
medmunds
2023-02-06 12:27:43 -08:00
committed by Mike Edmunds
parent 40891fcb4a
commit b4e22c63b3
94 changed files with 12936 additions and 7443 deletions

View File

@@ -7,20 +7,34 @@ from email.mime.text import MIMEText
from django.core import mail
from django.test import override_settings, tag
from django.utils.timezone import get_fixed_timezone, override as override_current_timezone
from django.utils.timezone import (
get_fixed_timezone,
override as override_current_timezone,
)
from anymail.exceptions import (
AnymailAPIError, AnymailConfigurationError, AnymailRecipientsRefused,
AnymailSerializationError, AnymailUnsupportedFeature)
AnymailAPIError,
AnymailConfigurationError,
AnymailRecipientsRefused,
AnymailSerializationError,
AnymailUnsupportedFeature,
)
from anymail.message import attach_inline_image_file
from .mock_requests_backend import RequestsBackendMockAPITestCase
from .utils import SAMPLE_IMAGE_FILENAME, decode_att, sample_image_content, sample_image_path
from .utils import (
SAMPLE_IMAGE_FILENAME,
decode_att,
sample_image_content,
sample_image_path,
)
@tag('sparkpost')
@override_settings(EMAIL_BACKEND='anymail.backends.sparkpost.EmailBackend',
ANYMAIL={'SPARKPOST_API_KEY': 'test_api_key'})
@tag("sparkpost")
@override_settings(
EMAIL_BACKEND="anymail.backends.sparkpost.EmailBackend",
ANYMAIL={"SPARKPOST_API_KEY": "test_api_key"},
)
class SparkPostBackendMockAPITestCase(RequestsBackendMockAPITestCase):
"""TestCase that uses SparkPostEmailBackend with a mocked transmissions.send API"""
@@ -35,32 +49,40 @@ class SparkPostBackendMockAPITestCase(RequestsBackendMockAPITestCase):
def setUp(self):
super().setUp()
# Simple message useful for many tests
self.message = mail.EmailMultiAlternatives('Subject', 'Text Body',
'from@example.com', ['to@example.com'])
self.message = mail.EmailMultiAlternatives(
"Subject", "Text Body", "from@example.com", ["to@example.com"]
)
def set_mock_result(self, accepted=1, rejected=0, id="12345678901234567890"):
"""Set a mock response that reflects count of accepted/rejected recipients"""
raw = json.dumps({
"results": {
"id": id,
"total_accepted_recipients": accepted,
"total_rejected_recipients": rejected,
raw = json.dumps(
{
"results": {
"id": id,
"total_accepted_recipients": accepted,
"total_rejected_recipients": rejected,
}
}
}).encode("utf-8")
).encode("utf-8")
self.set_mock_response(raw=raw)
return raw
@tag('sparkpost')
@tag("sparkpost")
class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
"""Test backend support for Django standard email features"""
def test_send_mail(self):
"""Test basic API for simple send"""
mail.send_mail('Subject here', 'Here is the message.',
'from@example.com', ['to@example.com'], fail_silently=False)
mail.send_mail(
"Subject here",
"Here is the message.",
"from@example.com",
["to@example.com"],
fail_silently=False,
)
self.assert_esp_called('/api/v1/transmissions/')
self.assert_esp_called("/api/v1/transmissions/")
headers = self.get_api_call_headers()
self.assertEqual("test_api_key", headers["Authorization"])
@@ -69,9 +91,10 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
self.assertEqual(data["content"]["subject"], "Subject here")
self.assertEqual(data["content"]["text"], "Here is the message.")
self.assertEqual(data["content"]["from"], "from@example.com")
self.assertEqual(data['recipients'], [{
"address": {"email": "to@example.com", "header_to": "to@example.com"}
}])
self.assertEqual(
data["recipients"],
[{"address": {"email": "to@example.com", "header_to": "to@example.com"}}],
)
def test_name_addr(self):
"""Make sure RFC2822 name-addr format (with display-name) is allowed
@@ -80,73 +103,102 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
"""
self.set_mock_result(accepted=6)
msg = mail.EmailMessage(
'Subject', 'Message', 'From Name <from@example.com>',
['Recipient #1 <to1@example.com>', 'to2@example.com'],
cc=['Carbon Copy <cc1@example.com>', 'cc2@example.com'],
bcc=['Blind Copy <bcc1@example.com>', 'bcc2@example.com'])
"Subject",
"Message",
"From Name <from@example.com>",
["Recipient #1 <to1@example.com>", "to2@example.com"],
cc=["Carbon Copy <cc1@example.com>", "cc2@example.com"],
bcc=["Blind Copy <bcc1@example.com>", "bcc2@example.com"],
)
msg.send()
data = self.get_api_call_json()
self.assertEqual(data["content"]["from"], "From Name <from@example.com>")
# This also checks recipient generation for cc and bcc. Because it's *not*
# a batch send, each recipient should see a To header reflecting all To addresses.
self.assertCountEqual(data["recipients"], [
{"address": {
"email": "to1@example.com",
"header_to": "Recipient #1 <to1@example.com>, to2@example.com",
}},
{"address": {
"email": "to2@example.com",
"header_to": "Recipient #1 <to1@example.com>, to2@example.com",
}},
# cc and bcc must be explicitly specified as recipients
{"address": {
"email": "cc1@example.com",
"header_to": "Recipient #1 <to1@example.com>, to2@example.com",
}},
{"address": {
"email": "cc2@example.com",
"header_to": "Recipient #1 <to1@example.com>, to2@example.com",
}},
{"address": {
"email": "bcc1@example.com",
"header_to": "Recipient #1 <to1@example.com>, to2@example.com",
}},
{"address": {
"email": "bcc2@example.com",
"header_to": "Recipient #1 <to1@example.com>, to2@example.com",
}},
])
# This also checks recipient generation for cc and bcc. Because it's *not* a
# batch send, each recipient should see a To header reflecting all To addresses.
self.assertCountEqual(
data["recipients"],
[
{
"address": {
"email": "to1@example.com",
"header_to": "Recipient #1 <to1@example.com>, to2@example.com",
}
},
{
"address": {
"email": "to2@example.com",
"header_to": "Recipient #1 <to1@example.com>, to2@example.com",
}
},
# cc and bcc must be explicitly specified as recipients
{
"address": {
"email": "cc1@example.com",
"header_to": "Recipient #1 <to1@example.com>, to2@example.com",
}
},
{
"address": {
"email": "cc2@example.com",
"header_to": "Recipient #1 <to1@example.com>, to2@example.com",
}
},
{
"address": {
"email": "bcc1@example.com",
"header_to": "Recipient #1 <to1@example.com>, to2@example.com",
}
},
{
"address": {
"email": "bcc2@example.com",
"header_to": "Recipient #1 <to1@example.com>, to2@example.com",
}
},
],
)
# Make sure we added a formatted Cc header visible to recipients
# (and not a Bcc header)
self.assertEqual(data["content"]["headers"], {
"Cc": "Carbon Copy <cc1@example.com>, cc2@example.com"
})
self.assertEqual(
data["content"]["headers"],
{"Cc": "Carbon Copy <cc1@example.com>, cc2@example.com"},
)
def test_custom_headers(self):
self.set_mock_result(accepted=6)
email = mail.EmailMessage(
'Subject', 'Body goes here', 'from@example.com', ['to1@example.com'],
cc=['cc1@example.com'],
headers={'Reply-To': 'another@example.com',
'X-MyHeader': 'my value',
'Message-ID': 'mycustommsgid@example.com'})
"Subject",
"Body goes here",
"from@example.com",
["to1@example.com"],
cc=["cc1@example.com"],
headers={
"Reply-To": "another@example.com",
"X-MyHeader": "my value",
"Message-ID": "mycustommsgid@example.com",
},
)
email.send()
data = self.get_api_call_json()
self.assertEqual(data["content"]["headers"], {
# Reply-To moved to separate param (below)
"X-MyHeader": "my value",
"Message-ID": "mycustommsgid@example.com",
"Cc": "cc1@example.com", # Cc header added
})
self.assertEqual(
data["content"]["headers"],
{
# Reply-To moved to separate param (below)
"X-MyHeader": "my value",
"Message-ID": "mycustommsgid@example.com",
"Cc": "cc1@example.com", # Cc header added
},
)
self.assertEqual(data["content"]["reply_to"], "another@example.com")
def test_html_message(self):
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
email = mail.EmailMultiAlternatives('Subject', text_content,
'from@example.com', ['to@example.com'])
text_content = "This is an important message."
html_content = "<p>This is an <strong>important</strong> message.</p>"
email = mail.EmailMultiAlternatives(
"Subject", text_content, "from@example.com", ["to@example.com"]
)
email.attach_alternative(html_content, "text/html")
email.send()
@@ -157,8 +209,10 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
self.assertNotIn("attachments", data["content"])
def test_html_only_message(self):
html_content = '<p>This is an <strong>important</strong> message.</p>'
email = mail.EmailMessage('Subject', html_content, 'from@example.com', ['to@example.com'])
html_content = "<p>This is an <strong>important</strong> message.</p>"
email = mail.EmailMessage(
"Subject", html_content, "from@example.com", ["to@example.com"]
)
email.content_subtype = "html" # Main content is now text/html
email.send()
@@ -167,19 +221,28 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
self.assertEqual(data["content"]["html"], html_content)
def test_reply_to(self):
email = mail.EmailMessage('Subject', 'Body goes here', 'from@example.com', ['to1@example.com'],
reply_to=['reply@example.com', 'Other <reply2@example.com>'],
headers={'X-Other': 'Keep'})
email = mail.EmailMessage(
"Subject",
"Body goes here",
"from@example.com",
["to1@example.com"],
reply_to=["reply@example.com", "Other <reply2@example.com>"],
headers={"X-Other": "Keep"},
)
email.send()
data = self.get_api_call_json()
self.assertEqual(data["content"]["reply_to"],
"reply@example.com, Other <reply2@example.com>")
self.assertEqual(data["content"]["headers"], {"X-Other": "Keep"}) # don't lose other headers
self.assertEqual(
data["content"]["reply_to"], "reply@example.com, Other <reply2@example.com>"
)
# don't lose other headers:
self.assertEqual(data["content"]["headers"], {"X-Other": "Keep"})
def test_attachments(self):
text_content = "* Item one\n* Item two\n* Item three"
self.message.attach(filename="test.txt", content=text_content, mimetype="text/plain")
self.message.attach(
filename="test.txt", content=text_content, mimetype="text/plain"
)
# Should guess mimetype if not provided...
png_content = b"PNG\xb4 pretend this is the contents of a png file"
@@ -187,7 +250,7 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
# Should work with a MIMEBase object (also tests no filename)...
pdf_content = b"PDF\xb4 pretend this is valid pdf params"
mimeattachment = MIMEBase('application', 'pdf')
mimeattachment = MIMEBase("application", "pdf")
mimeattachment.set_payload(pdf_content)
self.message.attach(mimeattachment)
@@ -197,7 +260,9 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
self.assertEqual(len(attachments), 3)
self.assertEqual(attachments[0]["type"], "text/plain")
self.assertEqual(attachments[0]["name"], "test.txt")
self.assertEqual(decode_att(attachments[0]["data"]).decode("ascii"), text_content)
self.assertEqual(
decode_att(attachments[0]["data"]).decode("ascii"), text_content
)
self.assertEqual(attachments[1]["type"], "image/png") # inferred from filename
self.assertEqual(attachments[1]["name"], "test.png")
self.assertEqual(decode_att(attachments[1]["data"]), png_content)
@@ -210,7 +275,9 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
def test_unicode_attachment_correctly_decoded(self):
# Slight modification from the Django unicode docs:
# http://django.readthedocs.org/en/latest/ref/unicode.html#email
self.message.attach("Une pièce jointe.html", '<p>\u2019</p>', mimetype='text/html')
self.message.attach(
"Une pièce jointe.html", "<p>\u2019</p>", mimetype="text/html"
)
self.message.send()
data = self.get_api_call_json()
attachments = data["content"]["attachments"]
@@ -223,7 +290,9 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
data = self.get_api_call_json()
attachment = data["content"]["attachments"][0]
self.assertEqual(attachment["type"], 'text/plain; charset="iso8859-1"')
self.assertEqual(decode_att(attachment["data"]), "Une pièce jointe".encode("iso8859-1"))
self.assertEqual(
decode_att(attachment["data"]), "Une pièce jointe".encode("iso8859-1")
)
def test_embedded_images(self):
image_filename = SAMPLE_IMAGE_FILENAME
@@ -231,7 +300,9 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
image_data = sample_image_content(image_filename)
cid = attach_inline_image_file(self.message, image_path)
html_content = '<p>This has an <img src="cid:%s" alt="inline" /> image.</p>' % cid
html_content = (
'<p>This has an <img src="cid:%s" alt="inline" /> image.</p>' % cid
)
self.message.attach_alternative(html_content, "text/html")
self.message.send()
@@ -241,7 +312,9 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
self.assertEqual(len(data["content"]["inline_images"]), 1)
self.assertEqual(data["content"]["inline_images"][0]["type"], "image/png")
self.assertEqual(data["content"]["inline_images"][0]["name"], cid)
self.assertEqual(decode_att(data["content"]["inline_images"][0]["data"]), image_data)
self.assertEqual(
decode_att(data["content"]["inline_images"][0]["data"]), image_data
)
# Make sure neither the html nor the inline image is treated as an attachment:
self.assertNotIn("attachments", data["content"])
@@ -250,9 +323,11 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
image_path = sample_image_path(image_filename)
image_data = sample_image_content(image_filename)
self.message.attach_file(image_path) # option 1: attach as a file
# option 1: attach as a file
self.message.attach_file(image_path)
image = MIMEImage(image_data) # option 2: construct the MIMEImage and attach it directly
# option 2: construct the MIMEImage and attach it directly
image = MIMEImage(image_data)
self.message.attach(image)
self.message.send()
@@ -305,18 +380,23 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
self.assertNotIn("reply_to", data["content"])
def test_empty_to(self):
# Test empty `to` -- but send requires at least one recipient somewhere (like cc)
# Test empty `to`--but send requires at least one recipient somewhere (like cc)
self.message.to = []
self.message.cc = ["cc@example.com"]
self.message.send()
data = self.get_api_call_json()
self.assertEqual(data["recipients"], [{
"address": {
"email": "cc@example.com",
# This results in a message with an empty To header, as desired:
"header_to": "",
},
}])
self.assertEqual(
data["recipients"],
[
{
"address": {
"email": "cc@example.com",
# This results in a message with an empty To header, as desired:
"header_to": "",
},
}
],
)
def test_api_failure(self):
self.set_mock_response(status_code=400)
@@ -337,11 +417,13 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
}]
}"""
self.set_mock_response(status_code=400, raw=failure_response)
with self.assertRaisesMessage(AnymailAPIError, "Helpful explanation from your ESP"):
with self.assertRaisesMessage(
AnymailAPIError, "Helpful explanation from your ESP"
):
self.message.send()
@tag('sparkpost')
@tag("sparkpost")
class SparkPostBackendAnymailFeatureTests(SparkPostBackendMockAPITestCase):
"""Test backend support for Anymail added features"""
@@ -352,10 +434,10 @@ class SparkPostBackendAnymailFeatureTests(SparkPostBackendMockAPITestCase):
self.assertEqual(data["return_path"], "bounce-handler@bounces.example.com")
def test_metadata(self):
self.message.metadata = {'user_id': "12345", 'items': 'spark, post'}
self.message.metadata = {"user_id": "12345", "items": "spark, post"}
self.message.send()
data = self.get_api_call_json()
self.assertEqual(data["metadata"], {'user_id': "12345", 'items': 'spark, post'})
self.assertEqual(data["metadata"], {"user_id": "12345", "items": "spark, post"})
def test_send_at(self):
utc_plus_6 = get_fixed_timezone(6 * 60)
@@ -407,7 +489,7 @@ class SparkPostBackendAnymailFeatureTests(SparkPostBackendMockAPITestCase):
self.assertEqual(data["campaign_id"], "receipt")
self.message.tags = ["receipt", "repeat-user"]
with self.assertRaisesMessage(AnymailUnsupportedFeature, 'multiple tags'):
with self.assertRaisesMessage(AnymailUnsupportedFeature, "multiple tags"):
self.message.send()
def test_tracking(self):
@@ -428,7 +510,9 @@ class SparkPostBackendAnymailFeatureTests(SparkPostBackendMockAPITestCase):
self.assertEqual(data["options"]["click_tracking"], True)
def test_template_id(self):
message = mail.EmailMultiAlternatives(from_email='from@example.com', to=['to@example.com'])
message = mail.EmailMultiAlternatives(
from_email="from@example.com", to=["to@example.com"]
)
message.template_id = "welcome_template"
message.send()
data = self.get_api_call_json()
@@ -440,51 +524,85 @@ class SparkPostBackendAnymailFeatureTests(SparkPostBackendMockAPITestCase):
def test_merge_data(self):
self.set_mock_result(accepted=4) # two 'to' plus one 'cc' for each 'to'
self.message.to = ['alice@example.com', 'Bob <bob@example.com>']
self.message.cc = ['cc@example.com']
self.message.to = ["alice@example.com", "Bob <bob@example.com>"]
self.message.cc = ["cc@example.com"]
self.message.body = "Hi {{address.name}}. Welcome to {{group}} at {{site}}."
self.message.merge_data = {
'alice@example.com': {'name': "Alice", 'group': "Developers"},
'bob@example.com': {'name': "Bob"}, # and leave group undefined
'nobody@example.com': {'name': "Not a recipient for this message"},
"alice@example.com": {"name": "Alice", "group": "Developers"},
"bob@example.com": {"name": "Bob"}, # and leave group undefined
"nobody@example.com": {"name": "Not a recipient for this message"},
}
self.message.merge_global_data = {'group': "Users", 'site': "ExampleCo"}
self.message.merge_global_data = {"group": "Users", "site": "ExampleCo"}
self.message.send()
data = self.get_api_call_json()
self.assertEqual({"group": "Users", "site": "ExampleCo"}, data["substitution_data"])
self.assertEqual([{
"address": {"email": "alice@example.com", "header_to": "alice@example.com"},
"substitution_data": {"name": "Alice", "group": "Developers"},
}, {
"address": {"email": "bob@example.com", "header_to": "Bob <bob@example.com>"},
"substitution_data": {"name": "Bob"},
}, { # duplicated for cc recipients...
"address": {"email": "cc@example.com", "header_to": "alice@example.com"},
"substitution_data": {"name": "Alice", "group": "Developers"},
}, {
"address": {"email": "cc@example.com", "header_to": "Bob <bob@example.com>"},
"substitution_data": {"name": "Bob"},
}], data["recipients"])
self.assertEqual(
{"group": "Users", "site": "ExampleCo"}, data["substitution_data"]
)
self.assertEqual(
[
{
"address": {
"email": "alice@example.com",
"header_to": "alice@example.com",
},
"substitution_data": {"name": "Alice", "group": "Developers"},
},
{
"address": {
"email": "bob@example.com",
"header_to": "Bob <bob@example.com>",
},
"substitution_data": {"name": "Bob"},
},
{ # duplicated for cc recipients...
"address": {
"email": "cc@example.com",
"header_to": "alice@example.com",
},
"substitution_data": {"name": "Alice", "group": "Developers"},
},
{
"address": {
"email": "cc@example.com",
"header_to": "Bob <bob@example.com>",
},
"substitution_data": {"name": "Bob"},
},
],
data["recipients"],
)
def test_merge_metadata(self):
self.set_mock_result(accepted=2)
self.message.to = ['alice@example.com', 'Bob <bob@example.com>']
self.message.to = ["alice@example.com", "Bob <bob@example.com>"]
self.message.merge_metadata = {
'alice@example.com': {'order_id': 123},
'bob@example.com': {'order_id': 678, 'tier': 'premium'},
"alice@example.com": {"order_id": 123},
"bob@example.com": {"order_id": 678, "tier": "premium"},
}
self.message.metadata = {'notification_batch': 'zx912'}
self.message.metadata = {"notification_batch": "zx912"}
self.message.send()
data = self.get_api_call_json()
self.assertEqual([{
"address": {"email": "alice@example.com", "header_to": "alice@example.com"},
"metadata": {"order_id": 123},
}, {
"address": {"email": "bob@example.com", "header_to": "Bob <bob@example.com>"},
"metadata": {"order_id": 678, "tier": "premium"}
}], data["recipients"])
self.assertEqual(
[
{
"address": {
"email": "alice@example.com",
"header_to": "alice@example.com",
},
"metadata": {"order_id": 123},
},
{
"address": {
"email": "bob@example.com",
"header_to": "Bob <bob@example.com>",
},
"metadata": {"order_id": 678, "tier": "premium"},
},
],
data["recipients"],
)
self.assertEqual(data["metadata"], {"notification_batch": "zx912"})
def test_default_omits_options(self):
@@ -498,7 +616,8 @@ class SparkPostBackendAnymailFeatureTests(SparkPostBackendMockAPITestCase):
data = self.get_api_call_json()
self.assertNotIn("campaign_id", data)
self.assertNotIn("metadata", data)
self.assertNotIn("options", data) # covers start_time, click_tracking, open_tracking
# covers start_time, click_tracking, open_tracking:
self.assertNotIn("options", data)
self.assertNotIn("substitution_data", data)
self.assertNotIn("template_id", data["content"])
@@ -517,64 +636,108 @@ class SparkPostBackendAnymailFeatureTests(SparkPostBackendMockAPITestCase):
self.message.send()
data = self.get_api_call_json()
self.assertEqual(data["description"], "The description")
self.assertEqual(data["options"], {
"transactional": True,
"click_tracking": True, # deep merge
})
self.assertDictMatches({
"use_draft_template": True,
"ab_test_id": "highlight_support_links",
"text": "Text Body", # deep merge
"subject": "Subject", # deep merge
}, data["content"])
self.assertEqual(
data["options"],
{
"transactional": True,
"click_tracking": True, # deep merge
},
)
self.assertDictMatches(
{
"use_draft_template": True,
"ab_test_id": "highlight_support_links",
"text": "Text Body", # deep merge
"subject": "Subject", # deep merge
},
data["content"],
)
def test_send_attaches_anymail_status(self):
"""The anymail_status should be attached to the message when it is sent """
"""The anymail_status should be attached to the message when it is sent"""
response_content = self.set_mock_result(accepted=1, rejected=0, id="9876543210")
msg = mail.EmailMessage('Subject', 'Message', 'from@example.com', ['to1@example.com'],)
msg = mail.EmailMessage(
"Subject",
"Message",
"from@example.com",
["to1@example.com"],
)
sent = msg.send()
self.assertEqual(sent, 1)
self.assertEqual(msg.anymail_status.status, {'queued'})
self.assertEqual(msg.anymail_status.message_id, '9876543210')
self.assertEqual(msg.anymail_status.recipients['to1@example.com'].status, 'queued')
self.assertEqual(msg.anymail_status.recipients['to1@example.com'].message_id, '9876543210')
self.assertEqual(msg.anymail_status.status, {"queued"})
self.assertEqual(msg.anymail_status.message_id, "9876543210")
self.assertEqual(
msg.anymail_status.recipients["to1@example.com"].status, "queued"
)
self.assertEqual(
msg.anymail_status.recipients["to1@example.com"].message_id, "9876543210"
)
self.assertEqual(msg.anymail_status.esp_response.content, response_content)
@override_settings(ANYMAIL_IGNORE_RECIPIENT_STATUS=True) # exception is tested later
@override_settings(
ANYMAIL_IGNORE_RECIPIENT_STATUS=True # exception is tested later
)
def test_send_all_rejected(self):
"""The anymail_status should be 'rejected' when all recipients rejected"""
self.set_mock_result(accepted=0, rejected=2)
msg = mail.EmailMessage('Subject', 'Message', 'from@example.com',
['to1@example.com', 'to2@example.com'],)
msg = mail.EmailMessage(
"Subject",
"Message",
"from@example.com",
["to1@example.com", "to2@example.com"],
)
msg.send()
self.assertEqual(msg.anymail_status.status, {'rejected'})
self.assertEqual(msg.anymail_status.recipients['to1@example.com'].status, 'rejected')
self.assertEqual(msg.anymail_status.recipients['to2@example.com'].status, 'rejected')
self.assertEqual(msg.anymail_status.status, {"rejected"})
self.assertEqual(
msg.anymail_status.recipients["to1@example.com"].status, "rejected"
)
self.assertEqual(
msg.anymail_status.recipients["to2@example.com"].status, "rejected"
)
def test_send_some_rejected(self):
"""The anymail_status should be 'unknown' when some recipients accepted and some rejected"""
"""
The anymail_status should be 'unknown'
when some recipients accepted and some rejected
"""
self.set_mock_result(accepted=1, rejected=1)
msg = mail.EmailMessage('Subject', 'Message', 'from@example.com',
['to1@example.com', 'to2@example.com'],)
msg = mail.EmailMessage(
"Subject",
"Message",
"from@example.com",
["to1@example.com", "to2@example.com"],
)
msg.send()
self.assertEqual(msg.anymail_status.status, {'unknown'})
self.assertEqual(msg.anymail_status.recipients['to1@example.com'].status, 'unknown')
self.assertEqual(msg.anymail_status.recipients['to2@example.com'].status, 'unknown')
self.assertEqual(msg.anymail_status.status, {"unknown"})
self.assertEqual(
msg.anymail_status.recipients["to1@example.com"].status, "unknown"
)
self.assertEqual(
msg.anymail_status.recipients["to2@example.com"].status, "unknown"
)
def test_send_unexpected_count(self):
"""The anymail_status should be 'unknown' when the total result count
doesn't match the number of recipients"""
doesn't match the number of recipients"""
self.set_mock_result(accepted=3, rejected=0) # but only 2 in the to-list
msg = mail.EmailMessage('Subject', 'Message', 'from@example.com',
['to1@example.com', 'to2@example.com'],)
msg = mail.EmailMessage(
"Subject",
"Message",
"from@example.com",
["to1@example.com", "to2@example.com"],
)
msg.send()
self.assertEqual(msg.anymail_status.status, {'unknown'})
self.assertEqual(msg.anymail_status.recipients['to1@example.com'].status, 'unknown')
self.assertEqual(msg.anymail_status.recipients['to2@example.com'].status, 'unknown')
self.assertEqual(msg.anymail_status.status, {"unknown"})
self.assertEqual(
msg.anymail_status.recipients["to1@example.com"].status, "unknown"
)
self.assertEqual(
msg.anymail_status.recipients["to2@example.com"].status, "unknown"
)
# noinspection PyUnresolvedReferences
def test_send_failed_anymail_status(self):
""" If the send fails, anymail_status should contain initial values"""
"""If the send fails, anymail_status should contain initial values"""
self.set_mock_response(status_code=400)
sent = self.message.send(fail_silently=True)
self.assertEqual(sent, 0)
@@ -585,7 +748,10 @@ class SparkPostBackendAnymailFeatureTests(SparkPostBackendMockAPITestCase):
# noinspection PyUnresolvedReferences
def test_send_unparsable_response(self):
"""If the send succeeds, but result is unexpected format, should raise an API exception"""
"""
If the send succeeds, but result is unexpected format,
should raise an API exception
"""
response_content = b"""{"wrong": "format"}"""
self.set_mock_response(raw=response_content)
with self.assertRaises(AnymailAPIError):
@@ -593,99 +759,142 @@ class SparkPostBackendAnymailFeatureTests(SparkPostBackendMockAPITestCase):
self.assertIsNone(self.message.anymail_status.status)
self.assertIsNone(self.message.anymail_status.message_id)
self.assertEqual(self.message.anymail_status.recipients, {})
self.assertEqual(self.message.anymail_status.esp_response.content, response_content)
self.assertEqual(
self.message.anymail_status.esp_response.content, response_content
)
def test_json_serialization_errors(self):
"""Try to provide more information about non-json-serializable data"""
self.message.tags = [Decimal('19.99')] # yeah, don't do this
self.message.tags = [Decimal("19.99")] # yeah, don't do this
with self.assertRaises(AnymailSerializationError) as cm:
self.message.send()
print(self.get_api_call_json())
err = cm.exception
self.assertIsInstance(err, TypeError) # compatibility with json.dumps
self.assertIn("Don't know how to send this data to SparkPost", str(err)) # our added context
self.assertRegex(str(err), r"Decimal.*is not JSON serializable") # original message
# our added context:
self.assertIn("Don't know how to send this data to SparkPost", str(err))
# original message:
self.assertRegex(str(err), r"Decimal.*is not JSON serializable")
@tag('sparkpost')
@tag("sparkpost")
class SparkPostBackendRecipientsRefusedTests(SparkPostBackendMockAPITestCase):
"""Should raise AnymailRecipientsRefused when *all* recipients are rejected or invalid"""
"""
Should raise AnymailRecipientsRefused when *all* recipients are rejected or invalid
"""
def test_recipients_refused(self):
self.set_mock_result(accepted=0, rejected=2)
msg = mail.EmailMessage('Subject', 'Body', 'from@example.com',
['invalid@localhost', 'reject@example.com'])
msg = mail.EmailMessage(
"Subject",
"Body",
"from@example.com",
["invalid@localhost", "reject@example.com"],
)
with self.assertRaises(AnymailRecipientsRefused):
msg.send()
def test_fail_silently(self):
self.set_mock_result(accepted=0, rejected=2)
sent = mail.send_mail('Subject', 'Body', 'from@example.com',
['invalid@localhost', 'reject@example.com'],
fail_silently=True)
sent = mail.send_mail(
"Subject",
"Body",
"from@example.com",
["invalid@localhost", "reject@example.com"],
fail_silently=True,
)
self.assertEqual(sent, 0)
def test_mixed_response(self):
"""If *any* recipients are valid or queued, no exception is raised"""
self.set_mock_result(accepted=2, rejected=2)
msg = mail.EmailMessage('Subject', 'Body', 'from@example.com',
['invalid@localhost', 'valid@example.com',
'reject@example.com', 'also.valid@example.com'])
msg = mail.EmailMessage(
"Subject",
"Body",
"from@example.com",
[
"invalid@localhost",
"valid@example.com",
"reject@example.com",
"also.valid@example.com",
],
)
sent = msg.send()
self.assertEqual(sent, 1) # one message sent, successfully, to 2 of 4 recipients
# one message sent, successfully, to 2 of 4 recipients:
self.assertEqual(sent, 1)
status = msg.anymail_status
# We don't know which recipients were rejected
self.assertEqual(status.recipients['invalid@localhost'].status, 'unknown')
self.assertEqual(status.recipients['valid@example.com'].status, 'unknown')
self.assertEqual(status.recipients['reject@example.com'].status, 'unknown')
self.assertEqual(status.recipients['also.valid@example.com'].status, 'unknown')
self.assertEqual(status.recipients["invalid@localhost"].status, "unknown")
self.assertEqual(status.recipients["valid@example.com"].status, "unknown")
self.assertEqual(status.recipients["reject@example.com"].status, "unknown")
self.assertEqual(status.recipients["also.valid@example.com"].status, "unknown")
@override_settings(ANYMAIL_IGNORE_RECIPIENT_STATUS=True)
def test_settings_override(self):
"""No exception with ignore setting"""
self.set_mock_result(accepted=0, rejected=2)
sent = mail.send_mail('Subject', 'Body', 'from@example.com',
['invalid@localhost', 'reject@example.com'])
sent = mail.send_mail(
"Subject",
"Body",
"from@example.com",
["invalid@localhost", "reject@example.com"],
)
self.assertEqual(sent, 1) # refused message is included in sent count
@tag('sparkpost')
@tag("sparkpost")
class SparkPostBackendConfigurationTests(SparkPostBackendMockAPITestCase):
"""Test various SparkPost client options"""
@override_settings(ANYMAIL={}) # clear SPARKPOST_API_KEY from SparkPostBackendMockAPITestCase
@override_settings(
# clear SPARKPOST_API_KEY from SparkPostBackendMockAPITestCase:
ANYMAIL={}
)
def test_missing_api_key(self):
with self.assertRaises(AnymailConfigurationError) as cm:
mail.send_mail('Subject', 'Message', 'from@example.com', ['to@example.com'])
mail.send_mail("Subject", "Message", "from@example.com", ["to@example.com"])
errmsg = str(cm.exception)
# Make sure the error mentions the different places to set the key
self.assertRegex(errmsg, r'\bSPARKPOST_API_KEY\b')
self.assertRegex(errmsg, r'\bANYMAIL_SPARKPOST_API_KEY\b')
self.assertRegex(errmsg, r"\bSPARKPOST_API_KEY\b")
self.assertRegex(errmsg, r"\bANYMAIL_SPARKPOST_API_KEY\b")
@override_settings(ANYMAIL={
"SPARKPOST_API_URL": "https://api.eu.sparkpost.com/api/v1",
"SPARKPOST_API_KEY": "test_api_key",
})
@override_settings(
ANYMAIL={
"SPARKPOST_API_URL": "https://api.eu.sparkpost.com/api/v1",
"SPARKPOST_API_KEY": "test_api_key",
}
)
def test_sparkpost_api_url(self):
mail.send_mail('Subject', 'Message', 'from@example.com', ['to@example.com'])
mail.send_mail("Subject", "Message", "from@example.com", ["to@example.com"])
self.assert_esp_called("https://api.eu.sparkpost.com/api/v1/transmissions/")
# can also override on individual connection (and even use non-versioned labs endpoint)
# can also override on individual connection
# (and even use non-versioned labs endpoint)
connection = mail.get_connection(api_url="https://api.sparkpost.com/api/labs")
mail.send_mail('Subject', 'Message', 'from@example.com', ['to@example.com'],
connection=connection)
mail.send_mail(
"Subject",
"Message",
"from@example.com",
["to@example.com"],
connection=connection,
)
self.assert_esp_called("https://api.sparkpost.com/api/labs/transmissions/")
def test_subaccount(self):
# A likely use case is supplying subaccount for a particular message.
# (For all messages, just set SPARKPOST_SUBACCOUNT in ANYMAIL settings.)
connection = mail.get_connection(subaccount=123)
mail.send_mail('Subject', 'Message', 'from@example.com', ['to@example.com'],
connection=connection)
mail.send_mail(
"Subject",
"Message",
"from@example.com",
["to@example.com"],
connection=connection,
)
headers = self.get_api_call_headers()
self.assertEqual(headers["X-MSYS-SUBACCOUNT"], 123)
# Make sure we're not setting the header on non-subaccount sends
mail.send_mail('Subject', 'Message', 'from@example.com', ['to@example.com'])
mail.send_mail("Subject", "Message", "from@example.com", ["to@example.com"])
headers = self.get_api_call_headers()
self.assertNotIn("X-MSYS-SUBACCOUNT", headers)