mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 11:51:05 -05:00
Reformat code with automated tools
Apply standardized code style
This commit is contained in:
@@ -10,16 +10,20 @@ from anymail.message import AnymailMessage
|
||||
|
||||
from .utils import AnymailTestMixin, sample_image_path
|
||||
|
||||
ANYMAIL_TEST_MANDRILL_API_KEY = os.getenv('ANYMAIL_TEST_MANDRILL_API_KEY')
|
||||
ANYMAIL_TEST_MANDRILL_DOMAIN = os.getenv('ANYMAIL_TEST_MANDRILL_DOMAIN')
|
||||
ANYMAIL_TEST_MANDRILL_API_KEY = os.getenv("ANYMAIL_TEST_MANDRILL_API_KEY")
|
||||
ANYMAIL_TEST_MANDRILL_DOMAIN = os.getenv("ANYMAIL_TEST_MANDRILL_DOMAIN")
|
||||
|
||||
|
||||
@tag('mandrill', 'live')
|
||||
@unittest.skipUnless(ANYMAIL_TEST_MANDRILL_API_KEY and ANYMAIL_TEST_MANDRILL_DOMAIN,
|
||||
"Set ANYMAIL_TEST_MANDRILL_API_KEY and ANYMAIL_TEST_MANDRILL_DOMAIN "
|
||||
"environment variables to run integration tests")
|
||||
@override_settings(MANDRILL_API_KEY=ANYMAIL_TEST_MANDRILL_API_KEY,
|
||||
EMAIL_BACKEND="anymail.backends.mandrill.EmailBackend")
|
||||
@tag("mandrill", "live")
|
||||
@unittest.skipUnless(
|
||||
ANYMAIL_TEST_MANDRILL_API_KEY and ANYMAIL_TEST_MANDRILL_DOMAIN,
|
||||
"Set ANYMAIL_TEST_MANDRILL_API_KEY and ANYMAIL_TEST_MANDRILL_DOMAIN "
|
||||
"environment variables to run integration tests",
|
||||
)
|
||||
@override_settings(
|
||||
MANDRILL_API_KEY=ANYMAIL_TEST_MANDRILL_API_KEY,
|
||||
EMAIL_BACKEND="anymail.backends.mandrill.EmailBackend",
|
||||
)
|
||||
class MandrillBackendIntegrationTests(AnymailTestMixin, SimpleTestCase):
|
||||
"""Mandrill API integration tests
|
||||
|
||||
@@ -33,17 +37,23 @@ class MandrillBackendIntegrationTests(AnymailTestMixin, SimpleTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.from_email = self.addr('from')
|
||||
self.message = mail.EmailMultiAlternatives('Anymail Mandrill integration test', 'Text content',
|
||||
self.from_email, [self.addr('test+to1')])
|
||||
self.message.attach_alternative('<p>HTML content</p>', "text/html")
|
||||
self.from_email = self.addr("from")
|
||||
self.message = mail.EmailMultiAlternatives(
|
||||
"Anymail Mandrill integration test",
|
||||
"Text content",
|
||||
self.from_email,
|
||||
[self.addr("test+to1")],
|
||||
)
|
||||
self.message.attach_alternative("<p>HTML content</p>", "text/html")
|
||||
|
||||
def addr(self, username, display_name=None):
|
||||
"""Construct test email address within our test domain"""
|
||||
# Because integration tests run within a Mandrill trial account,
|
||||
# both sender and recipient addresses must be within the test domain.
|
||||
# (Other recipient addresses will be rejected with 'recipient-domain-mismatch'.)
|
||||
email = '{username}@{domain}'.format(username=username, domain=ANYMAIL_TEST_MANDRILL_DOMAIN)
|
||||
email = "{username}@{domain}".format(
|
||||
username=username, domain=ANYMAIL_TEST_MANDRILL_DOMAIN
|
||||
)
|
||||
if display_name is not None:
|
||||
return formataddr((display_name, email))
|
||||
else:
|
||||
@@ -60,11 +70,15 @@ class MandrillBackendIntegrationTests(AnymailTestMixin, SimpleTestCase):
|
||||
sent_status = anymail_status.recipients[to_email].status
|
||||
message_id = anymail_status.recipients[to_email].message_id
|
||||
|
||||
self.assertIn(sent_status, ['sent', 'queued']) # successful send (could still bounce later)
|
||||
self.assertGreater(len(message_id), 0) # don't know what it'll be, but it should exist
|
||||
# successful send (could still bounce later):
|
||||
self.assertIn(sent_status, ["sent", "queued"])
|
||||
# don't know what it'll be, but it should exist:
|
||||
self.assertGreater(len(message_id), 0)
|
||||
|
||||
self.assertEqual(anymail_status.status, {sent_status}) # set of all recipient statuses
|
||||
self.assertEqual(anymail_status.message_id, message_id) # because only a single recipient (else would be a set)
|
||||
# set of all recipient statuses:
|
||||
self.assertEqual(anymail_status.status, {sent_status})
|
||||
# because only a single recipient (else would be a set):
|
||||
self.assertEqual(anymail_status.message_id, message_id)
|
||||
|
||||
def test_all_options(self):
|
||||
message = AnymailMessage(
|
||||
@@ -76,7 +90,6 @@ class MandrillBackendIntegrationTests(AnymailTestMixin, SimpleTestCase):
|
||||
bcc=[self.addr("test+bcc1"), self.addr("test+bcc2", "Blind Copy 2")],
|
||||
reply_to=["reply1@example.com", "Reply 2 <reply2@example.com>"],
|
||||
headers={"X-Anymail-Test": "value"},
|
||||
|
||||
# no metadata, send_at, track_clicks support
|
||||
tags=["tag 1"], # max one tag
|
||||
track_opens=True,
|
||||
@@ -87,15 +100,18 @@ class MandrillBackendIntegrationTests(AnymailTestMixin, SimpleTestCase):
|
||||
message.attach_alternative(
|
||||
"<p><b>HTML:</b> with <a href='http://example.com'>link</a>"
|
||||
"and image: <img src='cid:%s'></div>" % cid,
|
||||
"text/html")
|
||||
"text/html",
|
||||
)
|
||||
|
||||
message.send()
|
||||
self.assertTrue(message.anymail_status.status.issubset({'queued', 'sent'}))
|
||||
self.assertTrue(message.anymail_status.status.issubset({"queued", "sent"}))
|
||||
|
||||
def test_invalid_from(self):
|
||||
# Example of trying to send from an invalid address
|
||||
# Mandrill returns a 500 response (which raises a MandrillAPIError)
|
||||
self.message.from_email = 'webmaster@localhost' # Django default DEFAULT_FROM_EMAIL
|
||||
self.message.from_email = (
|
||||
"webmaster@localhost" # Django default DEFAULT_FROM_EMAIL
|
||||
)
|
||||
with self.assertRaises(AnymailAPIError) as cm:
|
||||
self.message.send()
|
||||
err = cm.exception
|
||||
@@ -104,41 +120,54 @@ class MandrillBackendIntegrationTests(AnymailTestMixin, SimpleTestCase):
|
||||
|
||||
def test_invalid_to(self):
|
||||
# Example of detecting when a recipient is not a valid email address
|
||||
self.message.to = ['invalid@localhost']
|
||||
self.message.to = ["invalid@localhost"]
|
||||
try:
|
||||
self.message.send()
|
||||
except AnymailRecipientsRefused:
|
||||
# Mandrill refused to deliver the mail -- message.anymail_status will tell you why:
|
||||
# Mandrill refused to deliver the mail -- message.anymail_status
|
||||
# will tell you why:
|
||||
# noinspection PyUnresolvedReferences
|
||||
anymail_status = self.message.anymail_status
|
||||
self.assertEqual(anymail_status.recipients['invalid@localhost'].status, 'invalid')
|
||||
self.assertEqual(anymail_status.status, {'invalid'})
|
||||
self.assertEqual(
|
||||
anymail_status.recipients["invalid@localhost"].status, "invalid"
|
||||
)
|
||||
self.assertEqual(anymail_status.status, {"invalid"})
|
||||
else:
|
||||
# Sometimes Mandrill queues these test sends
|
||||
# noinspection PyUnresolvedReferences
|
||||
if self.message.anymail_status.status == {'queued'}:
|
||||
if self.message.anymail_status.status == {"queued"}:
|
||||
self.skipTest("Mandrill queued the send -- can't complete this test")
|
||||
else:
|
||||
self.fail("Anymail did not raise AnymailRecipientsRefused for invalid recipient")
|
||||
self.fail(
|
||||
"Anymail did not raise AnymailRecipientsRefused"
|
||||
" for invalid recipient"
|
||||
)
|
||||
|
||||
def test_rejected_to(self):
|
||||
# Example of detecting when a recipient is on Mandrill's rejection blacklist
|
||||
self.message.to = ['reject@test.mandrillapp.com']
|
||||
self.message.to = ["reject@test.mandrillapp.com"]
|
||||
try:
|
||||
self.message.send()
|
||||
except AnymailRecipientsRefused:
|
||||
# Mandrill refused to deliver the mail -- message.anymail_status will tell you why:
|
||||
# Mandrill refused to deliver the mail -- message.anymail_status will
|
||||
# tell you why:
|
||||
# noinspection PyUnresolvedReferences
|
||||
anymail_status = self.message.anymail_status
|
||||
self.assertEqual(anymail_status.recipients['reject@test.mandrillapp.com'].status, 'rejected')
|
||||
self.assertEqual(anymail_status.status, {'rejected'})
|
||||
self.assertEqual(
|
||||
anymail_status.recipients["reject@test.mandrillapp.com"].status,
|
||||
"rejected",
|
||||
)
|
||||
self.assertEqual(anymail_status.status, {"rejected"})
|
||||
else:
|
||||
# Sometimes Mandrill queues these test sends
|
||||
# noinspection PyUnresolvedReferences
|
||||
if self.message.anymail_status.status == {'queued'}:
|
||||
if self.message.anymail_status.status == {"queued"}:
|
||||
self.skipTest("Mandrill queued the send -- can't complete this test")
|
||||
else:
|
||||
self.fail("Anymail did not raise AnymailRecipientsRefused for blacklist recipient")
|
||||
self.fail(
|
||||
"Anymail did not raise AnymailRecipientsRefused"
|
||||
" for blacklist recipient"
|
||||
)
|
||||
|
||||
@override_settings(MANDRILL_API_KEY="Hey, that's not an API key!")
|
||||
def test_invalid_api_key(self):
|
||||
|
||||
Reference in New Issue
Block a user