mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 03:41:05 -05:00
Reformat code with automated tools
Apply standardized code style
This commit is contained in:
@@ -9,28 +9,38 @@ from anymail.message import AnymailMessage
|
||||
|
||||
from .utils import AnymailTestMixin, sample_image_path
|
||||
|
||||
ANYMAIL_TEST_MAILJET_API_KEY = os.getenv('ANYMAIL_TEST_MAILJET_API_KEY')
|
||||
ANYMAIL_TEST_MAILJET_SECRET_KEY = os.getenv('ANYMAIL_TEST_MAILJET_SECRET_KEY')
|
||||
ANYMAIL_TEST_MAILJET_DOMAIN = os.getenv('ANYMAIL_TEST_MAILJET_DOMAIN')
|
||||
ANYMAIL_TEST_MAILJET_API_KEY = os.getenv("ANYMAIL_TEST_MAILJET_API_KEY")
|
||||
ANYMAIL_TEST_MAILJET_SECRET_KEY = os.getenv("ANYMAIL_TEST_MAILJET_SECRET_KEY")
|
||||
ANYMAIL_TEST_MAILJET_DOMAIN = os.getenv("ANYMAIL_TEST_MAILJET_DOMAIN")
|
||||
|
||||
|
||||
@tag('mailjet', 'live')
|
||||
@tag("mailjet", "live")
|
||||
@unittest.skipUnless(
|
||||
ANYMAIL_TEST_MAILJET_API_KEY and ANYMAIL_TEST_MAILJET_SECRET_KEY and ANYMAIL_TEST_MAILJET_DOMAIN,
|
||||
"Set ANYMAIL_TEST_MAILJET_API_KEY and ANYMAIL_TEST_MAILJET_SECRET_KEY and ANYMAIL_TEST_MAILJET_DOMAIN "
|
||||
"environment variables to run Mailjet integration tests")
|
||||
@override_settings(ANYMAIL={"MAILJET_API_KEY": ANYMAIL_TEST_MAILJET_API_KEY,
|
||||
"MAILJET_SECRET_KEY": ANYMAIL_TEST_MAILJET_SECRET_KEY,
|
||||
"MAILJET_SEND_DEFAULTS": {"esp_extra": {"SandboxMode": True}}, # don't actually send mail
|
||||
},
|
||||
EMAIL_BACKEND="anymail.backends.mailjet.EmailBackend")
|
||||
ANYMAIL_TEST_MAILJET_API_KEY
|
||||
and ANYMAIL_TEST_MAILJET_SECRET_KEY
|
||||
and ANYMAIL_TEST_MAILJET_DOMAIN,
|
||||
"Set ANYMAIL_TEST_MAILJET_API_KEY and ANYMAIL_TEST_MAILJET_SECRET_KEY"
|
||||
" and ANYMAIL_TEST_MAILJET_DOMAIN environment variables to run Mailjet"
|
||||
" integration tests",
|
||||
)
|
||||
@override_settings(
|
||||
ANYMAIL={
|
||||
"MAILJET_API_KEY": ANYMAIL_TEST_MAILJET_API_KEY,
|
||||
"MAILJET_SECRET_KEY": ANYMAIL_TEST_MAILJET_SECRET_KEY,
|
||||
"MAILJET_SEND_DEFAULTS": {
|
||||
"esp_extra": {"SandboxMode": True} # don't actually send mail
|
||||
},
|
||||
},
|
||||
EMAIL_BACKEND="anymail.backends.mailjet.EmailBackend",
|
||||
)
|
||||
class MailjetBackendIntegrationTests(AnymailTestMixin, SimpleTestCase):
|
||||
"""Mailjet API integration tests
|
||||
"""
|
||||
Mailjet API integration tests
|
||||
|
||||
These tests run against the **live** Mailjet API, using the
|
||||
environment variables `ANYMAIL_TEST_MAILJET_API_KEY` and `ANYMAIL_TEST_MAILJET_SECRET_KEY`
|
||||
as the API key and API secret key, respectively, and `ANYMAIL_TEST_MAILJET_DOMAIN` as
|
||||
a validated Mailjet sending domain. If those variables are not set, these tests won't run.
|
||||
These tests run against the **live** Mailjet API, using the environment variables
|
||||
`ANYMAIL_TEST_MAILJET_API_KEY` and `ANYMAIL_TEST_MAILJET_SECRET_KEY` as the API key
|
||||
and API secret key, respectively, and `ANYMAIL_TEST_MAILJET_DOMAIN` as a validated
|
||||
Mailjet sending domain. If those variables are not set, these tests won't run.
|
||||
|
||||
These tests enable Mailjet's SandboxMode to avoid sending any email;
|
||||
remove the esp_extra setting above if you are trying to actually send test messages.
|
||||
@@ -38,10 +48,14 @@ class MailjetBackendIntegrationTests(AnymailTestMixin, SimpleTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.from_email = 'test@%s' % ANYMAIL_TEST_MAILJET_DOMAIN
|
||||
self.message = AnymailMessage('Anymail Mailjet integration test', 'Text content',
|
||||
self.from_email, ['test+to1@anymail.dev'])
|
||||
self.message.attach_alternative('<p>HTML content</p>', "text/html")
|
||||
self.from_email = "test@%s" % ANYMAIL_TEST_MAILJET_DOMAIN
|
||||
self.message = AnymailMessage(
|
||||
"Anymail Mailjet integration test",
|
||||
"Text content",
|
||||
self.from_email,
|
||||
["test+to1@anymail.dev"],
|
||||
)
|
||||
self.message.attach_alternative("<p>HTML content</p>", "text/html")
|
||||
|
||||
def test_simple_send(self):
|
||||
# Example of getting the Mailjet send status and message id from the message
|
||||
@@ -49,12 +63,13 @@ class MailjetBackendIntegrationTests(AnymailTestMixin, SimpleTestCase):
|
||||
self.assertEqual(sent_count, 1)
|
||||
|
||||
anymail_status = self.message.anymail_status
|
||||
sent_status = anymail_status.recipients['test+to1@anymail.dev'].status
|
||||
message_id = anymail_status.recipients['test+to1@anymail.dev'].message_id
|
||||
sent_status = anymail_status.recipients["test+to1@anymail.dev"].status
|
||||
message_id = anymail_status.recipients["test+to1@anymail.dev"].message_id
|
||||
|
||||
self.assertEqual(sent_status, 'sent')
|
||||
self.assertRegex(message_id, r'.+')
|
||||
self.assertEqual(anymail_status.status, {sent_status}) # set of all recipient statuses
|
||||
self.assertEqual(sent_status, "sent")
|
||||
self.assertRegex(message_id, r".+")
|
||||
# set of all recipient statuses:
|
||||
self.assertEqual(anymail_status.status, {sent_status})
|
||||
self.assertEqual(anymail_status.message_id, message_id)
|
||||
|
||||
def test_all_options(self):
|
||||
@@ -62,12 +77,12 @@ class MailjetBackendIntegrationTests(AnymailTestMixin, SimpleTestCase):
|
||||
subject="Anymail Mailjet all-options integration test",
|
||||
body="This is the text body",
|
||||
from_email=formataddr(("Test Sender, Inc.", self.from_email)),
|
||||
to=['test+to1@anymail.dev', '"Recipient, 2nd" <test+to2@anymail.dev>'],
|
||||
cc=['test+cc1@anymail.dev', 'Copy 2 <test+cc1@anymail.dev>'],
|
||||
bcc=['test+bcc1@anymail.dev', 'Blind Copy 2 <test+bcc2@anymail.dev>'],
|
||||
reply_to=['"Reply, To" <reply2@example.com>'], # Mailjet only supports single reply_to
|
||||
to=["test+to1@anymail.dev", '"Recipient, 2nd" <test+to2@anymail.dev>'],
|
||||
cc=["test+cc1@anymail.dev", "Copy 2 <test+cc1@anymail.dev>"],
|
||||
bcc=["test+bcc1@anymail.dev", "Blind Copy 2 <test+bcc2@anymail.dev>"],
|
||||
# Mailjet only supports single reply_to:
|
||||
reply_to=['"Reply, To" <reply2@example.com>'],
|
||||
headers={"X-Anymail-Test": "value"},
|
||||
|
||||
metadata={"meta1": "simple string", "meta2": 2},
|
||||
tags=["tag 1"], # Mailjet only allows a single tag
|
||||
track_clicks=True,
|
||||
@@ -79,51 +94,56 @@ class MailjetBackendIntegrationTests(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.assertEqual(message.anymail_status.status, {'sent'})
|
||||
self.assertEqual(message.anymail_status.status, {"sent"})
|
||||
|
||||
def test_merge_data(self):
|
||||
message = AnymailMessage(
|
||||
subject="Anymail Mailjet merge_data test", # Mailjet doesn't support merge fields in the subject
|
||||
# Mailjet doesn't support merge fields in the subject
|
||||
subject="Anymail Mailjet merge_data test",
|
||||
body="This body includes merge data: [[var:value]]\n"
|
||||
"And global merge data: [[var:global]]",
|
||||
"And global merge data: [[var:global]]",
|
||||
from_email=formataddr(("Test From", self.from_email)),
|
||||
to=["test+to1@anymail.dev", "Recipient 2 <test+to2@anymail.dev>"],
|
||||
merge_data={
|
||||
'test+to1@anymail.dev': {'value': 'one'},
|
||||
'test+to2@anymail.dev': {'value': 'two'},
|
||||
},
|
||||
merge_global_data={
|
||||
'global': 'global_value'
|
||||
"test+to1@anymail.dev": {"value": "one"},
|
||||
"test+to2@anymail.dev": {"value": "two"},
|
||||
},
|
||||
merge_global_data={"global": "global_value"},
|
||||
)
|
||||
message.send()
|
||||
recipient_status = message.anymail_status.recipients
|
||||
self.assertEqual(recipient_status['test+to1@anymail.dev'].status, 'sent')
|
||||
self.assertEqual(recipient_status['test+to2@anymail.dev'].status, 'sent')
|
||||
self.assertEqual(recipient_status["test+to1@anymail.dev"].status, "sent")
|
||||
self.assertEqual(recipient_status["test+to2@anymail.dev"].status, "sent")
|
||||
|
||||
def test_stored_template(self):
|
||||
message = AnymailMessage(
|
||||
template_id='176375', # ID of the real template named 'test-template' in our Mailjet test account
|
||||
# ID of the real template named 'test-template' in our Mailjet test account:
|
||||
template_id="176375",
|
||||
to=["test+to1@anymail.dev"],
|
||||
merge_data={
|
||||
'test+to1@anymail.dev': {
|
||||
'name': "Test Recipient",
|
||||
"test+to1@anymail.dev": {
|
||||
"name": "Test Recipient",
|
||||
}
|
||||
},
|
||||
merge_global_data={
|
||||
'order': '12345',
|
||||
"order": "12345",
|
||||
},
|
||||
)
|
||||
message.from_email = None # use the template's sender email/name
|
||||
message.send()
|
||||
recipient_status = message.anymail_status.recipients
|
||||
self.assertEqual(recipient_status['test+to1@anymail.dev'].status, 'sent')
|
||||
self.assertEqual(recipient_status["test+to1@anymail.dev"].status, "sent")
|
||||
|
||||
@override_settings(ANYMAIL={"MAILJET_API_KEY": "Hey, that's not an API key!",
|
||||
"MAILJET_SECRET_KEY": "and this isn't the secret for it"})
|
||||
@override_settings(
|
||||
ANYMAIL={
|
||||
"MAILJET_API_KEY": "Hey, that's not an API key!",
|
||||
"MAILJET_SECRET_KEY": "and this isn't the secret for it",
|
||||
}
|
||||
)
|
||||
def test_invalid_api_key(self):
|
||||
with self.assertRaises(AnymailAPIError) as cm:
|
||||
self.message.send()
|
||||
|
||||
Reference in New Issue
Block a user