mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 03:41:05 -05:00
Move tests out of app module
(Directory structure as suggested in [Django testing docs][1].) [1]: https://docs.djangoproject.com/en/1.9/topics/testing/advanced/#using-the-django-test-runner-to-test-reusable-applications
This commit is contained in:
18
tests/__init__.py
Normal file
18
tests/__init__.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Exposing all TestCases at the 'tests' module level
|
||||
# is required by the old (<=1.5) DjangoTestSuiteRunner.
|
||||
|
||||
from .test_mailgun_backend import *
|
||||
from .test_mailgun_integration import *
|
||||
|
||||
from .test_mandrill_backend import *
|
||||
from .test_mandrill_integration import *
|
||||
|
||||
from .test_postmark_backend import *
|
||||
from .test_postmark_integration import *
|
||||
|
||||
from .test_sendgrid_backend import *
|
||||
from .test_sendgrid_integration import *
|
||||
|
||||
# Djrill leftovers:
|
||||
from .test_mandrill_djrill_features import *
|
||||
from .test_mandrill_webhook import *
|
||||
176
tests/mock_requests_backend.py
Normal file
176
tests/mock_requests_backend.py
Normal file
@@ -0,0 +1,176 @@
|
||||
import json
|
||||
|
||||
from django.core import mail
|
||||
from django.test import SimpleTestCase
|
||||
import requests
|
||||
import six
|
||||
from mock import patch
|
||||
|
||||
from anymail.exceptions import AnymailAPIError
|
||||
|
||||
from .utils import AnymailTestMixin
|
||||
|
||||
UNSET = object()
|
||||
|
||||
|
||||
class RequestsBackendMockAPITestCase(SimpleTestCase, AnymailTestMixin):
|
||||
"""TestCase that uses Djrill EmailBackend with a mocked Mandrill API"""
|
||||
|
||||
DEFAULT_RAW_RESPONSE = b"""{"subclass": "should override"}"""
|
||||
|
||||
class MockResponse(requests.Response):
|
||||
"""requests.request return value mock sufficient for testing"""
|
||||
def __init__(self, status_code=200, raw=b"RESPONSE", encoding='utf-8'):
|
||||
super(RequestsBackendMockAPITestCase.MockResponse, self).__init__()
|
||||
self.status_code = status_code
|
||||
self.encoding = encoding
|
||||
self.raw = six.BytesIO(raw)
|
||||
|
||||
def setUp(self):
|
||||
super(RequestsBackendMockAPITestCase, self).setUp()
|
||||
self.patch_request = patch('requests.Session.request', autospec=True)
|
||||
self.mock_request = self.patch_request.start()
|
||||
self.addCleanup(self.patch_request.stop)
|
||||
self.set_mock_response()
|
||||
|
||||
def set_mock_response(self, status_code=200, raw=UNSET, encoding='utf-8'):
|
||||
if raw is UNSET:
|
||||
raw = self.DEFAULT_RAW_RESPONSE
|
||||
mock_response = self.MockResponse(status_code, raw, encoding)
|
||||
self.mock_request.return_value = mock_response
|
||||
return mock_response
|
||||
|
||||
def assert_esp_called(self, url, method="POST"):
|
||||
"""Verifies the (mock) ESP API was called on endpoint.
|
||||
|
||||
url can be partial, and is just checked against the end of the url requested"
|
||||
"""
|
||||
# This assumes the last (or only) call to requests.Session.request is the API call of interest.
|
||||
if self.mock_request.call_args is None:
|
||||
raise AssertionError("No ESP API was called")
|
||||
(args, kwargs) = self.mock_request.call_args
|
||||
try:
|
||||
actual_method = kwargs.get('method', None) or args[1]
|
||||
actual_url = kwargs.get('url', None) or args[2]
|
||||
except IndexError:
|
||||
raise AssertionError("API was called without a method or url (?!)")
|
||||
if actual_method != method:
|
||||
raise AssertionError("API was not called using %s. (%s was used instead.)"
|
||||
% (method, actual_method))
|
||||
if not actual_url.endswith(url):
|
||||
raise AssertionError("API was not called at %s\n(It was called at %s)"
|
||||
% (url, actual_url))
|
||||
|
||||
def get_api_call_arg(self, kwarg, pos, required=True):
|
||||
"""Returns an argument passed to the mock ESP API.
|
||||
|
||||
Fails test if API wasn't called.
|
||||
"""
|
||||
if self.mock_request.call_args is None:
|
||||
raise AssertionError("API was not called")
|
||||
(args, kwargs) = self.mock_request.call_args
|
||||
try:
|
||||
return kwargs.get(kwarg, None) or args[pos]
|
||||
except IndexError:
|
||||
if required:
|
||||
raise AssertionError("API was called without required %s" % kwarg)
|
||||
else:
|
||||
return None
|
||||
|
||||
def get_api_call_params(self, required=True):
|
||||
"""Returns the query params sent to the mock ESP API."""
|
||||
return self.get_api_call_arg('params', 3, required)
|
||||
|
||||
def get_api_call_data(self, required=True):
|
||||
"""Returns the raw data sent to the mock ESP API."""
|
||||
return self.get_api_call_arg('data', 4, required)
|
||||
|
||||
def get_api_call_json(self, required=True):
|
||||
"""Returns the data sent to the mock ESP API, json-parsed"""
|
||||
return json.loads(self.get_api_call_data(required))
|
||||
|
||||
def get_api_call_headers(self, required=True):
|
||||
"""Returns the headers sent to the mock ESP API"""
|
||||
return self.get_api_call_arg('headers', 5, required)
|
||||
|
||||
def get_api_call_files(self, required=True):
|
||||
"""Returns the files sent to the mock ESP API"""
|
||||
return self.get_api_call_arg('files', 7, required)
|
||||
|
||||
def get_api_call_auth(self, required=True):
|
||||
"""Returns the auth sent to the mock ESP API"""
|
||||
return self.get_api_call_arg('auth', 8, required)
|
||||
|
||||
def assert_esp_not_called(self, msg=None):
|
||||
if self.mock_request.called:
|
||||
raise AssertionError(msg or "ESP API was called and shouldn't have been")
|
||||
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
class SessionSharingTestCasesMixin(object):
|
||||
"""Mixin that tests connection sharing in any RequestsBackendMockAPITestCase
|
||||
|
||||
(Contains actual test cases, so can't be included in RequestsBackendMockAPITestCase
|
||||
itself, as that would re-run these tests several times for each backend, in
|
||||
each TestCase for the backend.)
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
super(SessionSharingTestCasesMixin, self).setUp()
|
||||
self.patch_close = patch('requests.Session.close', autospec=True)
|
||||
self.mock_close = self.patch_close.start()
|
||||
self.addCleanup(self.patch_close.stop)
|
||||
|
||||
def test_connection_sharing(self):
|
||||
"""RequestsBackend reuses one requests session when sending multiple messages"""
|
||||
datatuple = (
|
||||
('Subject 1', 'Body 1', 'from@example.com', ['to@example.com']),
|
||||
('Subject 2', 'Body 2', 'from@example.com', ['to@example.com']),
|
||||
)
|
||||
mail.send_mass_mail(datatuple)
|
||||
self.assertEqual(self.mock_request.call_count, 2)
|
||||
session1 = self.mock_request.call_args_list[0][0] # arg[0] (self) is session
|
||||
session2 = self.mock_request.call_args_list[1][0]
|
||||
self.assertEqual(session1, session2)
|
||||
self.assertEqual(self.mock_close.call_count, 1)
|
||||
|
||||
def test_caller_managed_connections(self):
|
||||
"""Calling code can created long-lived connection that it opens and closes"""
|
||||
connection = mail.get_connection()
|
||||
connection.open()
|
||||
mail.send_mail('Subject 1', 'body', 'from@example.com', ['to@example.com'], connection=connection)
|
||||
session1 = self.mock_request.call_args[0]
|
||||
self.assertEqual(self.mock_close.call_count, 0) # shouldn't be closed yet
|
||||
|
||||
mail.send_mail('Subject 2', 'body', 'from@example.com', ['to@example.com'], connection=connection)
|
||||
self.assertEqual(self.mock_close.call_count, 0) # still shouldn't be closed
|
||||
session2 = self.mock_request.call_args[0]
|
||||
self.assertEqual(session1, session2) # should have reused same session
|
||||
|
||||
connection.close()
|
||||
self.assertEqual(self.mock_close.call_count, 1)
|
||||
|
||||
def test_session_closed_after_exception(self):
|
||||
self.set_mock_response(status_code=500)
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
mail.send_mail('Subject', 'Message', 'from@example.com', ['to@example.com'])
|
||||
self.assertEqual(self.mock_close.call_count, 1)
|
||||
|
||||
def test_session_closed_after_fail_silently_exception(self):
|
||||
self.set_mock_response(status_code=500)
|
||||
sent = mail.send_mail('Subject', 'Message', 'from@example.com', ['to@example.com'],
|
||||
fail_silently=True)
|
||||
self.assertEqual(sent, 0)
|
||||
self.assertEqual(self.mock_close.call_count, 1)
|
||||
|
||||
def test_caller_managed_session_closed_after_exception(self):
|
||||
connection = mail.get_connection()
|
||||
connection.open()
|
||||
self.set_mock_response(status_code=500)
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
mail.send_mail('Subject', 'Message', 'from@example.com', ['to@example.com'],
|
||||
connection=connection)
|
||||
self.assertEqual(self.mock_close.call_count, 0) # wait for us to close it
|
||||
|
||||
connection.close()
|
||||
self.assertEqual(self.mock_close.call_count, 1)
|
||||
BIN
tests/sample_image.png
Normal file
BIN
tests/sample_image.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 579 B |
516
tests/test_mailgun_backend.py
Normal file
516
tests/test_mailgun_backend.py
Normal file
@@ -0,0 +1,516 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
from email.mime.base import MIMEBase
|
||||
from email.mime.image import MIMEImage
|
||||
|
||||
from django.core import mail
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.test import SimpleTestCase
|
||||
from django.test.utils import override_settings
|
||||
from django.utils.timezone import get_fixed_timezone, override as override_current_timezone
|
||||
|
||||
from anymail.exceptions import AnymailAPIError, AnymailSerializationError, AnymailUnsupportedFeature
|
||||
from anymail.message import attach_inline_image_file
|
||||
|
||||
from .mock_requests_backend import RequestsBackendMockAPITestCase, SessionSharingTestCasesMixin
|
||||
from .utils import sample_image_content, sample_image_path, SAMPLE_IMAGE_FILENAME, AnymailTestMixin
|
||||
|
||||
|
||||
@override_settings(EMAIL_BACKEND='anymail.backends.mailgun.MailgunBackend',
|
||||
ANYMAIL={'MAILGUN_API_KEY': 'test_api_key'})
|
||||
class MailgunBackendMockAPITestCase(RequestsBackendMockAPITestCase):
|
||||
DEFAULT_RAW_RESPONSE = b"""{
|
||||
"id": "<20160306015544.116301.25145@example.com>",
|
||||
"message": "Queued. Thank you."
|
||||
}"""
|
||||
|
||||
def setUp(self):
|
||||
super(MailgunBackendMockAPITestCase, self).setUp()
|
||||
# Simple message useful for many tests
|
||||
self.message = mail.EmailMultiAlternatives('Subject', 'Text Body', 'from@example.com', ['to@example.com'])
|
||||
|
||||
|
||||
class MailgunBackendStandardEmailTests(MailgunBackendMockAPITestCase):
|
||||
"""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)
|
||||
self.assert_esp_called('/example.com/messages')
|
||||
auth = self.get_api_call_auth()
|
||||
self.assertEqual(auth, ('api', 'test_api_key'))
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['subject'], "Subject here")
|
||||
self.assertEqual(data['text'], "Here is the message.")
|
||||
self.assertEqual(data['from'], "from@example.com")
|
||||
self.assertEqual(data['to'], ["to@example.com"])
|
||||
|
||||
def test_name_addr(self):
|
||||
"""Make sure RFC2822 name-addr format (with display-name) is allowed
|
||||
|
||||
(Test both sender and recipient addresses)
|
||||
"""
|
||||
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'])
|
||||
msg.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['from'], "From Name <from@example.com>")
|
||||
self.assertEqual(data['to'], ['Recipient #1 <to1@example.com>', 'to2@example.com'])
|
||||
self.assertEqual(data['cc'], ['Carbon Copy <cc1@example.com>', 'cc2@example.com'])
|
||||
self.assertEqual(data['bcc'], ['Blind Copy <bcc1@example.com>', 'bcc2@example.com'])
|
||||
|
||||
def test_email_message(self):
|
||||
email = mail.EmailMessage(
|
||||
'Subject', 'Body goes here', 'from@example.com',
|
||||
['to1@example.com', 'Also To <to2@example.com>'],
|
||||
bcc=['bcc1@example.com', 'Also BCC <bcc2@example.com>'],
|
||||
cc=['cc1@example.com', 'Also CC <cc2@example.com>'],
|
||||
headers={'Reply-To': 'another@example.com',
|
||||
'X-MyHeader': 'my value',
|
||||
'Message-ID': 'mycustommsgid@example.com'})
|
||||
email.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['subject'], "Subject")
|
||||
self.assertEqual(data['text'], "Body goes here")
|
||||
self.assertEqual(data['from'], "from@example.com")
|
||||
self.assertEqual(data['to'], ['to1@example.com', 'Also To <to2@example.com>'])
|
||||
self.assertEqual(data['bcc'], ['bcc1@example.com', 'Also BCC <bcc2@example.com>'])
|
||||
self.assertEqual(data['cc'], ['cc1@example.com', 'Also CC <cc2@example.com>'])
|
||||
self.assertEqual(data['h:Reply-To'], "another@example.com")
|
||||
self.assertEqual(data['h:X-MyHeader'], 'my value')
|
||||
self.assertEqual(data['h:Message-ID'], 'mycustommsgid@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'])
|
||||
email.attach_alternative(html_content, "text/html")
|
||||
email.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['text'], text_content)
|
||||
self.assertEqual(data['html'], html_content)
|
||||
# Don't accidentally send the html part as an attachment:
|
||||
files = self.get_api_call_files(required=False)
|
||||
self.assertIsNone(files)
|
||||
|
||||
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'])
|
||||
email.content_subtype = "html" # Main content is now text/html
|
||||
email.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertNotIn('text', data)
|
||||
self.assertEqual(data['html'], html_content)
|
||||
|
||||
def test_reply_to(self):
|
||||
# reply_to is new in Django 1.8 -- before that, you can simply include it in headers
|
||||
try:
|
||||
# noinspection PyArgumentList
|
||||
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'})
|
||||
except TypeError:
|
||||
# Pre-Django 1.8
|
||||
return self.skipTest("Django version doesn't support EmailMessage(reply_to)")
|
||||
email.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['h:Reply-To'], 'reply@example.com, Other <reply2@example.com>')
|
||||
self.assertEqual(data['h:X-Other'], 'Keep') # don't lose other headers
|
||||
|
||||
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")
|
||||
|
||||
# Should guess mimetype if not provided...
|
||||
png_content = b"PNG\xb4 pretend this is the contents of a png file"
|
||||
self.message.attach(filename="test.png", content=png_content)
|
||||
|
||||
# Should work with a MIMEBase object (also tests no filename)...
|
||||
pdf_content = b"PDF\xb4 pretend this is valid pdf data"
|
||||
mimeattachment = MIMEBase('application', 'pdf')
|
||||
mimeattachment.set_payload(pdf_content)
|
||||
self.message.attach(mimeattachment)
|
||||
|
||||
self.message.send()
|
||||
files = self.get_api_call_files()
|
||||
attachments = [value for (field, value) in files if field == 'attachment']
|
||||
self.assertEqual(len(attachments), 3)
|
||||
self.assertEqual(attachments[0], ('test.txt', text_content, 'text/plain'))
|
||||
self.assertEqual(attachments[1], ('test.png', png_content, 'image/png')) # type inferred from filename
|
||||
self.assertEqual(attachments[2], (None, pdf_content, 'application/pdf')) # no filename
|
||||
# Make sure the image attachment is not treated as embedded:
|
||||
inlines = [value for (field, value) in files if field == 'inline']
|
||||
self.assertEqual(len(inlines), 0)
|
||||
|
||||
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.send()
|
||||
files = self.get_api_call_files()
|
||||
attachments = [value for (field, value) in files if field == 'attachment']
|
||||
self.assertEqual(len(attachments), 1)
|
||||
|
||||
def test_embedded_images(self):
|
||||
image_filename = SAMPLE_IMAGE_FILENAME
|
||||
image_path = sample_image_path(image_filename)
|
||||
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
|
||||
self.message.attach_alternative(html_content, "text/html")
|
||||
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['html'], html_content)
|
||||
|
||||
files = self.get_api_call_files()
|
||||
inlines = [value for (field, value) in files if field == 'inline']
|
||||
self.assertEqual(len(inlines), 1)
|
||||
self.assertEqual(inlines[0], (cid, image_data, "image/png")) # filename is cid; type is guessed
|
||||
# Make sure neither the html nor the inline image is treated as an attachment:
|
||||
attachments = [value for (field, value) in files if field == 'attachment']
|
||||
self.assertEqual(len(attachments), 0)
|
||||
|
||||
def test_attached_images(self):
|
||||
image_filename = SAMPLE_IMAGE_FILENAME
|
||||
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
|
||||
|
||||
image = MIMEImage(image_data) # option 2: construct the MIMEImage and attach it directly
|
||||
self.message.attach(image)
|
||||
|
||||
self.message.send()
|
||||
files = self.get_api_call_files()
|
||||
attachments = [value for (field, value) in files if field == 'attachment']
|
||||
self.assertEqual(len(attachments), 2)
|
||||
self.assertEqual(attachments[0], (image_filename, image_data, 'image/png'))
|
||||
self.assertEqual(attachments[1], (None, image_data, 'image/png')) # name unknown -- not attached as file
|
||||
# Make sure the image attachments are not treated as inline:
|
||||
inlines = [value for (field, value) in files if field == 'inline']
|
||||
self.assertEqual(len(inlines), 0)
|
||||
|
||||
def test_multiple_html_alternatives(self):
|
||||
# Multiple alternatives not allowed
|
||||
self.message.attach_alternative("<p>First html is OK</p>", "text/html")
|
||||
self.message.attach_alternative("<p>But not second html</p>", "text/html")
|
||||
with self.assertRaises(AnymailUnsupportedFeature):
|
||||
self.message.send()
|
||||
|
||||
def test_html_alternative(self):
|
||||
# Only html alternatives allowed
|
||||
self.message.attach_alternative("{'not': 'allowed'}", "application/json")
|
||||
with self.assertRaises(AnymailUnsupportedFeature):
|
||||
self.message.send()
|
||||
|
||||
def test_alternatives_fail_silently(self):
|
||||
# Make sure fail_silently is respected
|
||||
self.message.attach_alternative("{'not': 'allowed'}", "application/json")
|
||||
sent = self.message.send(fail_silently=True)
|
||||
self.assert_esp_not_called("API should not be called when send fails silently")
|
||||
self.assertEqual(sent, 0)
|
||||
|
||||
def test_suppress_empty_address_lists(self):
|
||||
"""Empty to, cc, bcc, and reply_to shouldn't generate empty headers"""
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertNotIn('cc', data)
|
||||
self.assertNotIn('bcc', data)
|
||||
self.assertNotIn('h:Reply-To', data)
|
||||
|
||||
# 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_data()
|
||||
self.assertNotIn('to', data)
|
||||
|
||||
def test_api_failure(self):
|
||||
self.set_mock_response(status_code=400)
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
sent = mail.send_mail('Subject', 'Body', 'from@example.com', ['to@example.com'])
|
||||
self.assertEqual(sent, 0)
|
||||
|
||||
# Make sure fail_silently is respected
|
||||
self.set_mock_response(status_code=400)
|
||||
sent = mail.send_mail('Subject', 'Body', 'from@example.com', ['to@example.com'], fail_silently=True)
|
||||
self.assertEqual(sent, 0)
|
||||
|
||||
def test_api_error_includes_details(self):
|
||||
"""AnymailAPIError should include ESP's error message"""
|
||||
# JSON error response:
|
||||
error_response = b"""{"message": "Helpful explanation from your ESP"}"""
|
||||
self.set_mock_response(status_code=400, raw=error_response)
|
||||
with self.assertRaisesMessage(AnymailAPIError, "Helpful explanation from your ESP"):
|
||||
self.message.send()
|
||||
|
||||
# Non-JSON error response:
|
||||
self.set_mock_response(status_code=500, raw=b"Invalid API key")
|
||||
with self.assertRaisesMessage(AnymailAPIError, "Invalid API key"):
|
||||
self.message.send()
|
||||
|
||||
# No content in the error response:
|
||||
self.set_mock_response(status_code=502, raw=None)
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
self.message.send()
|
||||
|
||||
|
||||
class MailgunBackendAnymailFeatureTests(MailgunBackendMockAPITestCase):
|
||||
"""Test backend support for Anymail added features"""
|
||||
|
||||
def test_metadata(self):
|
||||
self.message.metadata = {'user_id': "12345", 'items': ['mail', 'gun']}
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
# note values get serialized to json:
|
||||
self.assertEqual(data['v:user_id'], '12345') # simple values are transmitted as-is
|
||||
self.assertEqual(data['v:items'], '["mail", "gun"]') # complex values get json-serialized
|
||||
|
||||
def test_send_at(self):
|
||||
utc_plus_6 = get_fixed_timezone(6 * 60)
|
||||
utc_minus_8 = get_fixed_timezone(-8 * 60)
|
||||
|
||||
with override_current_timezone(utc_plus_6):
|
||||
# Timezone-aware datetime converted to UTC:
|
||||
self.message.send_at = datetime(2016, 3, 4, 5, 6, 7, tzinfo=utc_minus_8)
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['o:deliverytime'], "Fri, 04 Mar 2016 13:06:07 GMT") # 05:06 UTC-8 == 13:06 UTC
|
||||
|
||||
# Timezone-naive datetime assumed to be Django current_timezone
|
||||
self.message.send_at = datetime(2022, 10, 11, 12, 13, 14, 567)
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['o:deliverytime'], "Tue, 11 Oct 2022 06:13:14 GMT") # 12:13 UTC+6 == 06:13 UTC
|
||||
|
||||
# Date-only treated as midnight in current timezone
|
||||
self.message.send_at = date(2022, 10, 22)
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['o:deliverytime'], "Fri, 21 Oct 2022 18:00:00 GMT") # 00:00 UTC+6 == 18:00-1d UTC
|
||||
|
||||
# POSIX timestamp
|
||||
self.message.send_at = 1651820889 # 2022-05-06 07:08:09 UTC
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['o:deliverytime'], "Fri, 06 May 2022 07:08:09 GMT")
|
||||
|
||||
# String passed unchanged (this is *not* portable between ESPs)
|
||||
self.message.send_at = "Thu, 13 Oct 2022 18:02:00 GMT"
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['o:deliverytime'], "Thu, 13 Oct 2022 18:02:00 GMT")
|
||||
|
||||
def test_tags(self):
|
||||
self.message.tags = ["receipt", "repeat-user"]
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['o:tag'], ["receipt", "repeat-user"])
|
||||
|
||||
def test_tracking(self):
|
||||
# Test one way...
|
||||
self.message.track_opens = True
|
||||
self.message.track_clicks = False
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['o:tracking-opens'], 'yes')
|
||||
self.assertEqual(data['o:tracking-clicks'], 'no')
|
||||
|
||||
# ...and the opposite way
|
||||
self.message.track_opens = False
|
||||
self.message.track_clicks = True
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['o:tracking-opens'], 'no')
|
||||
self.assertEqual(data['o:tracking-clicks'], 'yes')
|
||||
|
||||
def test_sender_domain(self):
|
||||
"""Mailgun send domain can come from from_email or esp_extra"""
|
||||
# You could also use ANYMAIL_SEND_DEFAULTS={'esp_extra': {'sender_domain': 'your-domain.com'}}
|
||||
# (The mailgun_integration_tests do that.)
|
||||
self.message.from_email = "Test From <from@from-email.example.com>"
|
||||
self.message.send()
|
||||
self.assert_esp_called('/from-email.example.com/messages') # API url includes the sender-domain
|
||||
|
||||
self.message.esp_extra = {'sender_domain': 'esp-extra.example.com'}
|
||||
self.message.send()
|
||||
self.assert_esp_called('/esp-extra.example.com/messages') # overrides from_email
|
||||
|
||||
def test_default_omits_options(self):
|
||||
"""Make sure by default we don't send any ESP-specific options.
|
||||
|
||||
Options not specified by the caller should be omitted entirely from
|
||||
the API call (*not* sent as False or empty). This ensures
|
||||
that your ESP account settings apply by default.
|
||||
"""
|
||||
self.message.send()
|
||||
self.assert_esp_called('/example.com/messages')
|
||||
data = self.get_api_call_data()
|
||||
mailgun_fields = {key: value for key, value in data.items()
|
||||
if key.startswith('o:') or key.startswith('v:')}
|
||||
self.assertEqual(mailgun_fields, {})
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
def test_send_attaches_anymail_status(self):
|
||||
""" The anymail_status should be attached to the message when it is sent """
|
||||
response_content = b"""{
|
||||
"id": "<12345.67890@example.com>",
|
||||
"message": "Queued. Thank you."
|
||||
}"""
|
||||
self.set_mock_response(raw=response_content)
|
||||
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, '<12345.67890@example.com>')
|
||||
self.assertEqual(msg.anymail_status.recipients['to1@example.com'].status, 'queued')
|
||||
self.assertEqual(msg.anymail_status.recipients['to1@example.com'].message_id, '<12345.67890@example.com>')
|
||||
self.assertEqual(msg.anymail_status.esp_response.content, response_content)
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
def test_send_failed_anymail_status(self):
|
||||
""" If the send fails, anymail_status should contain initial values"""
|
||||
self.set_mock_response(status_code=500)
|
||||
sent = self.message.send(fail_silently=True)
|
||||
self.assertEqual(sent, 0)
|
||||
self.assertIsNone(self.message.anymail_status.status)
|
||||
self.assertIsNone(self.message.anymail_status.message_id)
|
||||
self.assertEqual(self.message.anymail_status.recipients, {})
|
||||
self.assertIsNone(self.message.anymail_status.esp_response)
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
def test_send_unparsable_response(self):
|
||||
"""If the send succeeds, but a non-JSON API response, should raise an API exception"""
|
||||
mock_response = self.set_mock_response(status_code=200,
|
||||
raw=b"yikes, this isn't a real response")
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
self.message.send()
|
||||
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, mock_response)
|
||||
|
||||
def test_json_serialization_errors(self):
|
||||
"""Try to provide more information about non-json-serializable data"""
|
||||
self.message.metadata = {'total': Decimal('19.99')}
|
||||
with self.assertRaises(AnymailSerializationError) as cm:
|
||||
self.message.send()
|
||||
print(self.get_api_call_data())
|
||||
err = cm.exception
|
||||
self.assertIsInstance(err, TypeError) # compatibility with json.dumps
|
||||
self.assertIn("Don't know how to send this data to Mailgun", str(err)) # our added context
|
||||
self.assertIn("Decimal('19.99') is not JSON serializable", str(err)) # original message
|
||||
|
||||
|
||||
class MailgunBackendRecipientsRefusedTests(MailgunBackendMockAPITestCase):
|
||||
"""Should raise AnymailRecipientsRefused when *all* recipients are rejected or invalid"""
|
||||
|
||||
# Mailgun doesn't check email bounce or complaint lists at time of send --
|
||||
# it always just queues the message. You'll need to listen for the "rejected"
|
||||
# and "failed" events to detect refused recipients.
|
||||
|
||||
# The one exception is a completely invalid email, which will return a 400 response
|
||||
# and show up as an AnymailAPIError at send time.
|
||||
INVALID_TO_RESPONSE = b"""{
|
||||
"message": "'to' parameter is not a valid address. please check documentation"
|
||||
}"""
|
||||
|
||||
def test_invalid_email(self):
|
||||
self.set_mock_response(status_code=400, raw=self.INVALID_TO_RESPONSE)
|
||||
msg = mail.EmailMessage('Subject', 'Body', 'from@example.com', to=['not a valid email'])
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
msg.send()
|
||||
|
||||
def test_fail_silently(self):
|
||||
self.set_mock_response(status_code=400, raw=self.INVALID_TO_RESPONSE)
|
||||
sent = mail.send_mail('Subject', 'Body', 'from@example.com', ['not a valid email'],
|
||||
fail_silently=True)
|
||||
self.assertEqual(sent, 0)
|
||||
|
||||
|
||||
class MailgunBackendSessionSharingTestCase(SessionSharingTestCasesMixin, MailgunBackendMockAPITestCase):
|
||||
"""Requests session sharing tests"""
|
||||
pass # tests are defined in the mixin
|
||||
|
||||
|
||||
@override_settings(ANYMAIL_SEND_DEFAULTS={
|
||||
'metadata': {'global': 'globalvalue', 'other': 'othervalue'},
|
||||
'tags': ['globaltag'],
|
||||
'track_clicks': True,
|
||||
'track_opens': True,
|
||||
'esp_extra': {'o:globaloption': 'globalsetting'},
|
||||
})
|
||||
class MailgunBackendSendDefaultsTests(MailgunBackendMockAPITestCase):
|
||||
"""Tests backend support for global SEND_DEFAULTS"""
|
||||
|
||||
def test_send_defaults(self):
|
||||
"""Test that global send defaults are applied"""
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
# All these values came from ANYMAIL_SEND_DEFAULTS:
|
||||
self.assertEqual(data['v:global'], 'globalvalue')
|
||||
self.assertEqual(data['v:other'], 'othervalue')
|
||||
self.assertEqual(data['o:tag'], ['globaltag'])
|
||||
self.assertEqual(data['o:tracking-clicks'], 'yes')
|
||||
self.assertEqual(data['o:tracking-opens'], 'yes')
|
||||
self.assertEqual(data['o:globaloption'], 'globalsetting')
|
||||
|
||||
def test_merge_message_with_send_defaults(self):
|
||||
"""Test that individual message settings are *merged into* the global send defaults"""
|
||||
self.message.metadata = {'message': 'messagevalue', 'other': 'override'}
|
||||
self.message.tags = ['messagetag']
|
||||
self.message.track_clicks = False
|
||||
self.message.esp_extra = {'o:messageoption': 'messagesetting'}
|
||||
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
# All these values came from ANYMAIL_SEND_DEFAULTS + message.*:
|
||||
self.assertEqual(data['v:global'], 'globalvalue')
|
||||
self.assertEqual(data['v:message'], 'messagevalue') # additional metadata
|
||||
self.assertEqual(data['v:other'], 'override') # override global value
|
||||
self.assertEqual(data['o:tag'], ['globaltag', 'messagetag']) # tags concatenated
|
||||
self.assertEqual(data['o:tracking-clicks'], 'no') # message overrides
|
||||
self.assertEqual(data['o:tracking-opens'], 'yes')
|
||||
self.assertEqual(data['o:globaloption'], 'globalsetting')
|
||||
self.assertEqual(data['o:messageoption'], 'messagesetting') # additional esp_extra
|
||||
|
||||
@override_settings(ANYMAIL_MAILGUN_SEND_DEFAULTS={
|
||||
'tags': ['esptag'],
|
||||
'metadata': {'esp': 'espvalue'},
|
||||
'track_opens': False,
|
||||
})
|
||||
def test_esp_send_defaults(self):
|
||||
"""Test that ESP-specific send defaults override individual global defaults"""
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
# All these values came from ANYMAIL_SEND_DEFAULTS plus ANYMAIL_MAILGUN_SEND_DEFAULTS:
|
||||
self.assertNotIn('v:global', data) # entire metadata overridden
|
||||
self.assertEqual(data['v:esp'], 'espvalue')
|
||||
self.assertEqual(data['o:tag'], ['esptag']) # entire tags overridden
|
||||
self.assertEqual(data['o:tracking-clicks'], 'yes') # we didn't override the global track_clicks
|
||||
self.assertEqual(data['o:tracking-opens'], 'no')
|
||||
self.assertEqual(data['o:globaloption'], 'globalsetting') # we didn't override the global esp_extra
|
||||
|
||||
|
||||
@override_settings(EMAIL_BACKEND="anymail.backends.mailgun.MailgunBackend")
|
||||
class MailgunBackendImproperlyConfiguredTests(SimpleTestCase, AnymailTestMixin):
|
||||
"""Test ESP backend without required settings in place"""
|
||||
|
||||
def test_missing_api_key(self):
|
||||
with self.assertRaises(ImproperlyConfigured) as cm:
|
||||
mail.send_mail('Subject', 'Message', 'from@example.com', ['to@example.com'])
|
||||
errmsg = str(cm.exception)
|
||||
# Make sure the error mentions MAILGUN_API_KEY and ANYMAIL_MAILGUN_API_KEY
|
||||
self.assertRegex(errmsg, r'\bMAILGUN_API_KEY\b')
|
||||
self.assertRegex(errmsg, r'\bANYMAIL_MAILGUN_API_KEY\b')
|
||||
164
tests/test_mailgun_integration.py
Normal file
164
tests/test_mailgun_integration.py
Normal file
@@ -0,0 +1,164 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import os
|
||||
import unittest
|
||||
from datetime import datetime, timedelta
|
||||
from time import mktime, sleep
|
||||
|
||||
import requests
|
||||
from django.test import SimpleTestCase
|
||||
from django.test.utils import override_settings
|
||||
|
||||
from anymail.exceptions import AnymailAPIError
|
||||
from anymail.message import AnymailMessage
|
||||
|
||||
from .utils import AnymailTestMixin, sample_image_path
|
||||
|
||||
MAILGUN_TEST_API_KEY = os.getenv('MAILGUN_TEST_API_KEY')
|
||||
MAILGUN_TEST_DOMAIN = os.getenv('MAILGUN_TEST_DOMAIN')
|
||||
|
||||
|
||||
@unittest.skipUnless(MAILGUN_TEST_API_KEY and MAILGUN_TEST_DOMAIN,
|
||||
"Set MAILGUN_TEST_API_KEY and MAILGUN_TEST_DOMAIN environment variables "
|
||||
"to run Mailgun integration tests")
|
||||
@override_settings(ANYMAIL_MAILGUN_API_KEY=MAILGUN_TEST_API_KEY,
|
||||
ANYMAIL_MAILGUN_SEND_DEFAULTS={
|
||||
'esp_extra': {'o:testmode': 'yes',
|
||||
'sender_domain': MAILGUN_TEST_DOMAIN}},
|
||||
EMAIL_BACKEND="anymail.backends.mailgun.MailgunBackend")
|
||||
class MailgunBackendIntegrationTests(SimpleTestCase, AnymailTestMixin):
|
||||
"""Mailgun API integration tests
|
||||
|
||||
These tests run against the **live** Mailgun API, using the
|
||||
environment variable `MAILGUN_TEST_API_KEY` as the API key
|
||||
and `MAILGUN_TEST_DOMAIN` as the sender domain.
|
||||
If those variables are not set, these tests won't run.
|
||||
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
super(MailgunBackendIntegrationTests, self).setUp()
|
||||
self.message = AnymailMessage('Anymail integration test', 'Text content',
|
||||
'from@example.com', ['to@example.com'])
|
||||
self.message.attach_alternative('<p>HTML content</p>', "text/html")
|
||||
|
||||
def fetch_mailgun_events(self, message_id, event=None,
|
||||
initial_delay=2, retry_delay=1, max_retries=5):
|
||||
"""Return list of Mailgun events related to message_id"""
|
||||
url = "https://api.mailgun.net/v3/%s/events" % MAILGUN_TEST_DOMAIN
|
||||
auth = ("api", MAILGUN_TEST_API_KEY)
|
||||
|
||||
# Despite the docs, Mailgun's events API actually expects the message-id
|
||||
# without the <...> brackets (so, not exactly "as returned by the messages API")
|
||||
# https://documentation.mailgun.com/api-events.html#filter-field
|
||||
params = {'message-id': message_id[1:-1]} # strip <...>
|
||||
if event is not None:
|
||||
params['event'] = event
|
||||
|
||||
# It can take a few seconds for the events to show up
|
||||
# in Mailgun's logs, so retry a few times if necessary:
|
||||
sleep(initial_delay)
|
||||
for retry in range(max_retries):
|
||||
if retry > 0:
|
||||
sleep(retry_delay)
|
||||
response = requests.get(url, auth=auth, params=params)
|
||||
response.raise_for_status()
|
||||
items = response.json()["items"]
|
||||
if len(items) > 0:
|
||||
return items
|
||||
return None
|
||||
|
||||
def test_simple_send(self):
|
||||
# Example of getting the Mailgun send status and message id from the message
|
||||
sent_count = self.message.send()
|
||||
self.assertEqual(sent_count, 1)
|
||||
|
||||
anymail_status = self.message.anymail_status
|
||||
sent_status = anymail_status.recipients['to@example.com'].status
|
||||
message_id = anymail_status.recipients['to@example.com'].message_id
|
||||
|
||||
self.assertEqual(sent_status, 'queued') # Mailgun always queues
|
||||
self.assertGreater(len(message_id), 0) # don't know what it'll be, but it should exist
|
||||
|
||||
self.assertEqual(anymail_status.status, {sent_status}) # set of all recipient statuses
|
||||
self.assertEqual(anymail_status.message_id, message_id)
|
||||
|
||||
def test_all_options(self):
|
||||
send_at = datetime.now().replace(microsecond=0) + timedelta(minutes=2)
|
||||
send_at_timestamp = mktime(send_at.timetuple()) # python3: send_at.timestamp()
|
||||
message = AnymailMessage(
|
||||
subject="Anymail all-options integration test",
|
||||
body="This is the text body",
|
||||
from_email="Test From <from@example.com>",
|
||||
to=["to1@example.com", "Recipient 2 <to2@example.com>"],
|
||||
cc=["cc1@example.com", "Copy 2 <cc2@example.com>"],
|
||||
bcc=["bcc1@example.com", "Blind Copy 2 <bcc2@example.com>"],
|
||||
reply_to=["reply1@example.com", "Reply 2 <reply2@example.com>"],
|
||||
headers={"X-Anymail-Test": "value"},
|
||||
|
||||
metadata={"meta1": "simple string", "meta2": 2, "meta3": {"complex": "value"}},
|
||||
send_at=send_at,
|
||||
tags=["tag 1", "tag 2"],
|
||||
track_clicks=False,
|
||||
track_opens=True,
|
||||
)
|
||||
message.attach("attachment1.txt", "Here is some\ntext for you", "text/plain")
|
||||
message.attach("attachment2.csv", "ID,Name\n1,3", "text/csv")
|
||||
cid = message.attach_inline_image_file(sample_image_path(), domain=MAILGUN_TEST_DOMAIN)
|
||||
message.attach_alternative(
|
||||
"<div>This is the <i>html</i> body <img src='cid:%s'></div>" % cid,
|
||||
"text/html")
|
||||
|
||||
message.send()
|
||||
self.assertEqual(message.anymail_status.status, {'queued'}) # Mailgun always queues
|
||||
message_id = message.anymail_status.message_id
|
||||
|
||||
events = self.fetch_mailgun_events(message_id, event="accepted")
|
||||
if events is None:
|
||||
self.skipTest("No Mailgun 'accepted' event after 30sec -- can't complete this test")
|
||||
return
|
||||
|
||||
event = events.pop()
|
||||
self.assertCountEqual(event["tags"], ["tag 1", "tag 2"]) # don't care about order
|
||||
self.assertEqual(event["user-variables"],
|
||||
{"meta1": "simple string",
|
||||
"meta2": "2", # numbers become strings
|
||||
"meta3": '{"complex": "value"}'}) # complex values become json
|
||||
|
||||
self.assertEqual(event["message"]["scheduled-for"], send_at_timestamp)
|
||||
self.assertCountEqual(event["message"]["recipients"],
|
||||
['to1@example.com', 'to2@example.com', 'cc1@example.com', 'cc2@example.com',
|
||||
'bcc1@example.com', 'bcc2@example.com']) # don't care about order
|
||||
|
||||
headers = event["message"]["headers"]
|
||||
self.assertEqual(headers["from"], "Test From <from@example.com>")
|
||||
self.assertEqual(headers["to"], "to1@example.com, Recipient 2 <to2@example.com>")
|
||||
self.assertEqual(headers["subject"], "Anymail all-options integration test")
|
||||
|
||||
attachments = event["message"]["attachments"]
|
||||
self.assertEqual(len(attachments), 2) # because inline image shouldn't be an attachment
|
||||
self.assertEqual(attachments[0]["filename"], "attachment1.txt")
|
||||
self.assertEqual(attachments[0]["content-type"], "text/plain")
|
||||
self.assertEqual(attachments[1]["filename"], "attachment2.csv")
|
||||
self.assertEqual(attachments[1]["content-type"], "text/csv")
|
||||
|
||||
# No other fields are verifiable from the event data.
|
||||
# (We could try fetching the message from event["storage"]["url"]
|
||||
# to verify content and other headers.)
|
||||
|
||||
def test_invalid_from(self):
|
||||
self.message.from_email = 'webmaster'
|
||||
with self.assertRaises(AnymailAPIError) as cm:
|
||||
self.message.send()
|
||||
err = cm.exception
|
||||
self.assertEqual(err.status_code, 400)
|
||||
self.assertIn("'from' parameter is not a valid address", str(err))
|
||||
|
||||
@override_settings(ANYMAIL_MAILGUN_API_KEY="Hey, that's not an API key!")
|
||||
def test_invalid_api_key(self):
|
||||
with self.assertRaises(AnymailAPIError) as cm:
|
||||
self.message.send()
|
||||
err = cm.exception
|
||||
self.assertEqual(err.status_code, 401)
|
||||
# Mailgun doesn't offer any additional explanation in its response body
|
||||
# self.assertIn("Forbidden", str(err))
|
||||
538
tests/test_mandrill_backend.py
Normal file
538
tests/test_mandrill_backend.py
Normal file
@@ -0,0 +1,538 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import unittest
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
from email.mime.base import MIMEBase
|
||||
from email.mime.image import MIMEImage
|
||||
|
||||
from django.core import mail
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.test import SimpleTestCase
|
||||
from django.test.utils import override_settings
|
||||
from django.utils.timezone import get_fixed_timezone, override as override_current_timezone
|
||||
|
||||
from anymail.exceptions import (AnymailAPIError, AnymailRecipientsRefused,
|
||||
AnymailSerializationError, AnymailUnsupportedFeature)
|
||||
from anymail.message import attach_inline_image
|
||||
|
||||
from .mock_requests_backend import RequestsBackendMockAPITestCase, SessionSharingTestCasesMixin
|
||||
from .utils import sample_image_content, sample_image_path, SAMPLE_IMAGE_FILENAME, AnymailTestMixin, decode_att
|
||||
|
||||
|
||||
@override_settings(EMAIL_BACKEND='anymail.backends.mandrill.MandrillBackend',
|
||||
ANYMAIL={'MANDRILL_API_KEY': 'test_api_key'})
|
||||
class MandrillBackendMockAPITestCase(RequestsBackendMockAPITestCase):
|
||||
DEFAULT_RAW_RESPONSE = b"""[{
|
||||
"email": "to@example.com",
|
||||
"status": "sent",
|
||||
"_id": "abc123",
|
||||
"reject_reason": null
|
||||
}]"""
|
||||
|
||||
def setUp(self):
|
||||
super(MandrillBackendMockAPITestCase, self).setUp()
|
||||
# Simple message useful for many tests
|
||||
self.message = mail.EmailMultiAlternatives('Subject', 'Text Body', 'from@example.com', ['to@example.com'])
|
||||
|
||||
|
||||
class MandrillBackendStandardEmailTests(MandrillBackendMockAPITestCase):
|
||||
"""Test backend support for Django mail wrappers"""
|
||||
|
||||
def test_send_mail(self):
|
||||
mail.send_mail('Subject here', 'Here is the message.',
|
||||
'from@example.com', ['to@example.com'], fail_silently=False)
|
||||
self.assert_esp_called("/messages/send.json")
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['key'], "test_api_key")
|
||||
self.assertEqual(data['message']['subject'], "Subject here")
|
||||
self.assertEqual(data['message']['text'], "Here is the message.")
|
||||
self.assertNotIn('from_name', data['message'])
|
||||
self.assertEqual(data['message']['from_email'], "from@example.com")
|
||||
self.assertEqual(data['message']['to'], [{'email': 'to@example.com', 'name': '', 'type': 'to'}])
|
||||
|
||||
def test_name_addr(self):
|
||||
"""Make sure RFC2822 name-addr format (with display-name) is allowed
|
||||
|
||||
(Test both sender and recipient addresses)
|
||||
"""
|
||||
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'])
|
||||
msg.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['from_name'], "From Name")
|
||||
self.assertEqual(data['message']['from_email'], "from@example.com")
|
||||
self.assertEqual(data['message']['to'], [
|
||||
{'email': 'to1@example.com', 'name': 'Recipient #1', 'type': 'to'},
|
||||
{'email': 'to2@example.com', 'name': '', 'type': 'to'},
|
||||
{'email': 'cc1@example.com', 'name': 'Carbon Copy', 'type': 'cc'},
|
||||
{'email': 'cc2@example.com', 'name': '', 'type': 'cc'},
|
||||
{'email': 'bcc1@example.com', 'name': 'Blind Copy', 'type': 'bcc'},
|
||||
{'email': 'bcc2@example.com', 'name': '', 'type': 'bcc'},
|
||||
])
|
||||
|
||||
def test_email_message(self):
|
||||
email = mail.EmailMessage(
|
||||
'Subject', 'Body goes here',
|
||||
'from@example.com',
|
||||
['to1@example.com', 'Also To <to2@example.com>'],
|
||||
bcc=['bcc1@example.com', 'Also BCC <bcc2@example.com>'],
|
||||
cc=['cc1@example.com', 'Also CC <cc2@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['message']['subject'], "Subject")
|
||||
self.assertEqual(data['message']['text'], "Body goes here")
|
||||
self.assertEqual(data['message']['from_email'], "from@example.com")
|
||||
self.assertEqual(data['message']['headers'],
|
||||
{'Reply-To': 'another@example.com',
|
||||
'X-MyHeader': 'my value',
|
||||
'Message-ID': 'mycustommsgid@example.com'})
|
||||
# Verify recipients correctly identified as "to", "cc", or "bcc"
|
||||
self.assertEqual(data['message']['to'], [
|
||||
{'email': 'to1@example.com', 'name': '', 'type': 'to'},
|
||||
{'email': 'to2@example.com', 'name': 'Also To', 'type': 'to'},
|
||||
{'email': 'cc1@example.com', 'name': '', 'type': 'cc'},
|
||||
{'email': 'cc2@example.com', 'name': 'Also CC', 'type': 'cc'},
|
||||
{'email': 'bcc1@example.com', 'name': '', 'type': 'bcc'},
|
||||
{'email': 'bcc2@example.com', 'name': 'Also BCC', 'type': 'bcc'},
|
||||
])
|
||||
# Don't use Mandrill's bcc_address "logging" feature for bcc's:
|
||||
self.assertNotIn('bcc_address', data['message'])
|
||||
|
||||
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'])
|
||||
email.attach_alternative(html_content, "text/html")
|
||||
email.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['text'], text_content)
|
||||
self.assertEqual(data['message']['html'], html_content)
|
||||
# Don't accidentally send the html part as an attachment:
|
||||
self.assertFalse('attachments' in data['message'])
|
||||
|
||||
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'])
|
||||
email.content_subtype = "html" # Main content is now text/html
|
||||
email.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertNotIn('text', data['message'])
|
||||
self.assertEqual(data['message']['html'], html_content)
|
||||
|
||||
def test_reply_to(self):
|
||||
# reply_to is new in Django 1.8 -- before that, you can simply include it in headers
|
||||
try:
|
||||
# noinspection PyArgumentList
|
||||
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'})
|
||||
except TypeError:
|
||||
# Pre-Django 1.8
|
||||
raise unittest.SkipTest("Django version doesn't support EmailMessage(reply_to)")
|
||||
email.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['headers']['Reply-To'],
|
||||
'reply@example.com, Other <reply2@example.com>')
|
||||
self.assertEqual(data['message']['headers']['X-Other'], 'Keep') # don't lose other headers
|
||||
|
||||
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")
|
||||
|
||||
# Should guess mimetype if not provided...
|
||||
png_content = b"PNG\xb4 pretend this is the contents of a png file"
|
||||
self.message.attach(filename="test.png", content=png_content)
|
||||
|
||||
# Should work with a MIMEBase object (also tests no filename)...
|
||||
pdf_content = b"PDF\xb4 pretend this is valid pdf data"
|
||||
mimeattachment = MIMEBase('application', 'pdf')
|
||||
mimeattachment.set_payload(pdf_content)
|
||||
self.message.attach(mimeattachment)
|
||||
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
attachments = data['message']['attachments']
|
||||
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]["content"]).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]["content"]), png_content)
|
||||
self.assertEqual(attachments[2]["type"], "application/pdf")
|
||||
self.assertEqual(attachments[2]["name"], "") # none
|
||||
self.assertEqual(decode_att(attachments[2]["content"]), pdf_content)
|
||||
# Make sure the image attachment is not treated as embedded:
|
||||
self.assertFalse('images' in data['message'])
|
||||
|
||||
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.send()
|
||||
data = self.get_api_call_json()
|
||||
attachments = data['message']['attachments']
|
||||
self.assertEqual(len(attachments), 1)
|
||||
|
||||
def test_embedded_images(self):
|
||||
image_data = sample_image_content() # Read from a png file
|
||||
|
||||
cid = attach_inline_image(self.message, image_data)
|
||||
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()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(len(data['message']['images']), 1)
|
||||
self.assertEqual(data['message']['images'][0]["type"], "image/png")
|
||||
self.assertEqual(data['message']['images'][0]["name"], cid)
|
||||
self.assertEqual(decode_att(data['message']['images'][0]["content"]), image_data)
|
||||
# Make sure neither the html nor the inline image is treated as an attachment:
|
||||
self.assertFalse('attachments' in data['message'])
|
||||
|
||||
def test_attached_images(self):
|
||||
image_filename = SAMPLE_IMAGE_FILENAME
|
||||
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
|
||||
|
||||
image = MIMEImage(image_data) # option 2: construct the MIMEImage and attach it directly
|
||||
self.message.attach(image)
|
||||
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
attachments = data['message']['attachments']
|
||||
self.assertEqual(len(attachments), 2)
|
||||
self.assertEqual(attachments[0]["type"], "image/png")
|
||||
self.assertEqual(attachments[0]["name"], image_filename)
|
||||
self.assertEqual(decode_att(attachments[0]["content"]), image_data)
|
||||
self.assertEqual(attachments[1]["type"], "image/png")
|
||||
self.assertEqual(attachments[1]["name"], "") # unknown -- not attached as file
|
||||
self.assertEqual(decode_att(attachments[1]["content"]), image_data)
|
||||
# Make sure the image attachments are not treated as embedded:
|
||||
self.assertFalse('images' in data['message'])
|
||||
|
||||
def test_multiple_html_alternatives(self):
|
||||
# Multiple alternatives not allowed
|
||||
self.message.attach_alternative("<p>First html is OK</p>", "text/html")
|
||||
self.message.attach_alternative("<p>But not second html</p>", "text/html")
|
||||
with self.assertRaises(AnymailUnsupportedFeature):
|
||||
self.message.send()
|
||||
|
||||
def test_html_alternative(self):
|
||||
# Only html alternatives allowed
|
||||
self.message.attach_alternative("{'not': 'allowed'}", "application/json")
|
||||
with self.assertRaises(AnymailUnsupportedFeature):
|
||||
self.message.send()
|
||||
|
||||
def test_alternatives_fail_silently(self):
|
||||
# Make sure fail_silently is respected
|
||||
self.message.attach_alternative("{'not': 'allowed'}", "application/json")
|
||||
sent = self.message.send(fail_silently=True)
|
||||
self.assert_esp_not_called("API should not be called when send fails silently")
|
||||
self.assertEqual(sent, 0)
|
||||
|
||||
def test_api_failure(self):
|
||||
self.set_mock_response(status_code=400)
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
sent = mail.send_mail('Subject', 'Body', 'from@example.com', ['to@example.com'])
|
||||
self.assertEqual(sent, 0)
|
||||
|
||||
# Make sure fail_silently is respected
|
||||
self.set_mock_response(status_code=400)
|
||||
sent = mail.send_mail('Subject', 'Body', 'from@example.com', ['to@example.com'],
|
||||
fail_silently=True)
|
||||
self.assertEqual(sent, 0)
|
||||
|
||||
def test_api_error_includes_details(self):
|
||||
"""AnymailAPIError should include ESP's error message"""
|
||||
self.set_mock_response(status_code=400, raw=b"""{
|
||||
"status": "error",
|
||||
"code": 12,
|
||||
"name": "Error_Name",
|
||||
"message": "Helpful explanation from Mandrill"
|
||||
}""")
|
||||
with self.assertRaisesMessage(AnymailAPIError, "Helpful explanation from Mandrill"):
|
||||
self.message.send()
|
||||
|
||||
# Non-JSON error response:
|
||||
self.set_mock_response(status_code=500, raw=b"Invalid API key")
|
||||
with self.assertRaisesMessage(AnymailAPIError, "Invalid API key"):
|
||||
self.message.send()
|
||||
|
||||
# No content in the error response:
|
||||
self.set_mock_response(status_code=502, raw=None)
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
self.message.send()
|
||||
|
||||
|
||||
class MandrillBackendAnymailFeatureTests(MandrillBackendMockAPITestCase):
|
||||
"""Test backend support for Anymail added features"""
|
||||
|
||||
def test_metadata(self):
|
||||
self.message.metadata = {'user_id': "12345", 'items': 6}
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['metadata'], {'user_id': "12345", 'items': 6})
|
||||
|
||||
def test_send_at(self):
|
||||
utc_plus_6 = get_fixed_timezone(6 * 60)
|
||||
utc_minus_8 = get_fixed_timezone(-8 * 60)
|
||||
|
||||
with override_current_timezone(utc_plus_6):
|
||||
# Timezone-naive datetime assumed to be Django current_timezone
|
||||
self.message.send_at = datetime(2022, 10, 11, 12, 13, 14, 567)
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['send_at'], "2022-10-11 06:13:14") # 12:13 UTC+6 == 06:13 UTC
|
||||
|
||||
# Timezone-aware datetime converted to UTC:
|
||||
self.message.send_at = datetime(2016, 3, 4, 5, 6, 7, tzinfo=utc_minus_8)
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['send_at'], "2016-03-04 13:06:07") # 05:06 UTC-8 == 13:06 UTC
|
||||
|
||||
# Date-only treated as midnight in current timezone
|
||||
self.message.send_at = date(2022, 10, 22)
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['send_at'], "2022-10-21 18:00:00") # 00:00 UTC+6 == 18:00-1d UTC
|
||||
|
||||
# POSIX timestamp
|
||||
self.message.send_at = 1651820889 # 2022-05-06 07:08:09 UTC
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['send_at'], "2022-05-06 07:08:09")
|
||||
|
||||
# String passed unchanged (this is *not* portable between ESPs)
|
||||
self.message.send_at = "2013-11-12 01:02:03"
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['send_at'], "2013-11-12 01:02:03")
|
||||
|
||||
def test_tags(self):
|
||||
self.message.tags = ["receipt", "repeat-user"]
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['tags'], ["receipt", "repeat-user"])
|
||||
|
||||
def test_tracking(self):
|
||||
# Test one way...
|
||||
self.message.track_opens = True
|
||||
self.message.track_clicks = False
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['track_opens'], True)
|
||||
self.assertEqual(data['message']['track_clicks'], False)
|
||||
|
||||
# ...and the opposite way
|
||||
self.message.track_opens = False
|
||||
self.message.track_clicks = True
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['track_opens'], False)
|
||||
self.assertEqual(data['message']['track_clicks'], True)
|
||||
|
||||
def test_default_omits_options(self):
|
||||
"""Make sure by default we don't send any ESP-specific options.
|
||||
|
||||
Options not specified by the caller should be omitted entirely from
|
||||
the API call (*not* sent as False or empty). This ensures
|
||||
that your ESP account settings apply by default.
|
||||
"""
|
||||
self.message.send()
|
||||
self.assert_esp_called("/messages/send.json")
|
||||
data = self.get_api_call_json()
|
||||
self.assertNotIn('metadata', data['message'])
|
||||
self.assertNotIn('send_at', data)
|
||||
self.assertNotIn('tags', data['message'])
|
||||
self.assertNotIn('track_opens', data['message'])
|
||||
self.assertNotIn('track_clicks', data['message'])
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
def test_send_attaches_anymail_status(self):
|
||||
""" The anymail_status should be attached to the message when it is sent """
|
||||
response_content = b'[{"email": "to1@example.com", "status": "sent", "_id": "abc123"}]'
|
||||
self.set_mock_response(raw=response_content)
|
||||
msg = mail.EmailMessage('Subject', 'Message', 'from@example.com', ['to1@example.com'],)
|
||||
sent = msg.send()
|
||||
self.assertEqual(sent, 1)
|
||||
self.assertEqual(msg.anymail_status.status, {'sent'})
|
||||
self.assertEqual(msg.anymail_status.message_id, 'abc123')
|
||||
self.assertEqual(msg.anymail_status.recipients['to1@example.com'].status, 'sent')
|
||||
self.assertEqual(msg.anymail_status.recipients['to1@example.com'].message_id, 'abc123')
|
||||
self.assertEqual(msg.anymail_status.esp_response.content, response_content)
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
def test_send_failed_anymail_status(self):
|
||||
""" If the send fails, anymail_status should contain initial values"""
|
||||
self.set_mock_response(status_code=500)
|
||||
sent = self.message.send(fail_silently=True)
|
||||
self.assertEqual(sent, 0)
|
||||
self.assertIsNone(self.message.anymail_status.status)
|
||||
self.assertIsNone(self.message.anymail_status.message_id)
|
||||
self.assertEqual(self.message.anymail_status.recipients, {})
|
||||
self.assertIsNone(self.message.anymail_status.esp_response)
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
def test_send_unparsable_response(self):
|
||||
"""If the send succeeds, but a non-JSON API response, should raise an API exception"""
|
||||
mock_response = self.set_mock_response(status_code=200,
|
||||
raw=b"yikes, this isn't a real response")
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
self.message.send()
|
||||
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, mock_response)
|
||||
|
||||
def test_json_serialization_errors(self):
|
||||
"""Try to provide more information about non-json-serializable data"""
|
||||
self.message.metadata = {'total': Decimal('19.99')}
|
||||
with self.assertRaises(AnymailSerializationError) as cm:
|
||||
self.message.send()
|
||||
print(self.get_api_call_data())
|
||||
err = cm.exception
|
||||
self.assertIsInstance(err, TypeError) # compatibility with json.dumps
|
||||
self.assertIn("Don't know how to send this data to Mandrill", str(err)) # our added context
|
||||
self.assertIn("Decimal('19.99') is not JSON serializable", str(err)) # original message
|
||||
|
||||
|
||||
class MandrillBackendRecipientsRefusedTests(MandrillBackendMockAPITestCase):
|
||||
"""Should raise AnymailRecipientsRefused when *all* recipients are rejected or invalid"""
|
||||
|
||||
def test_recipients_refused(self):
|
||||
msg = mail.EmailMessage('Subject', 'Body', 'from@example.com',
|
||||
['invalid@localhost', 'reject@test.mandrillapp.com'])
|
||||
self.set_mock_response(raw=b"""[
|
||||
{"email": "invalid@localhost", "status": "invalid"},
|
||||
{"email": "reject@test.mandrillapp.com", "status": "rejected"}
|
||||
]""")
|
||||
with self.assertRaises(AnymailRecipientsRefused):
|
||||
msg.send()
|
||||
|
||||
def test_fail_silently(self):
|
||||
self.set_mock_response(raw=b"""[
|
||||
{"email": "invalid@localhost", "status": "invalid"},
|
||||
{"email": "reject@test.mandrillapp.com", "status": "rejected"}
|
||||
]""")
|
||||
sent = mail.send_mail('Subject', 'Body', 'from@example.com',
|
||||
['invalid@localhost', 'reject@test.mandrillapp.com'],
|
||||
fail_silently=True)
|
||||
self.assertEqual(sent, 0)
|
||||
|
||||
def test_mixed_response(self):
|
||||
"""If *any* recipients are valid or queued, no exception is raised"""
|
||||
msg = mail.EmailMessage('Subject', 'Body', 'from@example.com',
|
||||
['invalid@localhost', 'valid@example.com',
|
||||
'reject@test.mandrillapp.com', 'also.valid@example.com'])
|
||||
self.set_mock_response(raw=b"""[
|
||||
{"email": "invalid@localhost", "status": "invalid"},
|
||||
{"email": "valid@example.com", "status": "sent"},
|
||||
{"email": "reject@test.mandrillapp.com", "status": "rejected"},
|
||||
{"email": "also.valid@example.com", "status": "queued"}
|
||||
]""")
|
||||
sent = msg.send()
|
||||
self.assertEqual(sent, 1) # one message sent, successfully, to 2 of 4 recipients
|
||||
status = msg.anymail_status
|
||||
self.assertEqual(status.recipients['invalid@localhost'].status, 'invalid')
|
||||
self.assertEqual(status.recipients['valid@example.com'].status, 'sent')
|
||||
self.assertEqual(status.recipients['reject@test.mandrillapp.com'].status, 'rejected')
|
||||
self.assertEqual(status.recipients['also.valid@example.com'].status, 'queued')
|
||||
|
||||
@override_settings(ANYMAIL_IGNORE_RECIPIENT_STATUS=True)
|
||||
def test_settings_override(self):
|
||||
"""No exception with ignore setting"""
|
||||
self.set_mock_response(raw=b"""[
|
||||
{"email": "invalid@localhost", "status": "invalid"},
|
||||
{"email": "reject@test.mandrillapp.com", "status": "rejected"}
|
||||
]""")
|
||||
sent = mail.send_mail('Subject', 'Body', 'from@example.com',
|
||||
['invalid@localhost', 'reject@test.mandrillapp.com'])
|
||||
self.assertEqual(sent, 1) # refused message is included in sent count
|
||||
|
||||
|
||||
class MandrillBackendSessionSharingTestCase(SessionSharingTestCasesMixin, MandrillBackendMockAPITestCase):
|
||||
"""Requests session sharing tests"""
|
||||
pass # tests are defined in the mixin
|
||||
|
||||
|
||||
@override_settings(ANYMAIL_SEND_DEFAULTS={
|
||||
'metadata': {'global': 'globalvalue', 'other': 'othervalue'},
|
||||
'tags': ['globaltag'],
|
||||
'track_clicks': True,
|
||||
'track_opens': True,
|
||||
# 'esp_extra': {'globaloption': 'globalsetting'}, # Mandrill doesn't support esp_extra yet
|
||||
})
|
||||
class MandrillBackendSendDefaultsTests(MandrillBackendMockAPITestCase):
|
||||
"""Tests backend support for global SEND_DEFAULTS"""
|
||||
|
||||
def test_send_defaults(self):
|
||||
"""Test that global send defaults are applied"""
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
# All these values came from ANYMAIL_SEND_DEFAULTS:
|
||||
self.assertEqual(data['message']['metadata'], {'global': 'globalvalue', 'other': 'othervalue'})
|
||||
self.assertEqual(data['message']['tags'], ['globaltag'])
|
||||
self.assertEqual(data['message']['track_clicks'], True)
|
||||
self.assertEqual(data['message']['track_opens'], True)
|
||||
# self.assertEqual(data['globaloption'], 'globalsetting')
|
||||
|
||||
def test_merge_message_with_send_defaults(self):
|
||||
"""Test that individual message settings are *merged into* the global send defaults"""
|
||||
self.message.metadata = {'message': 'messagevalue', 'other': 'override'}
|
||||
self.message.tags = ['messagetag']
|
||||
self.message.track_clicks = False
|
||||
# self.message.esp_extra = {'messageoption': 'messagesetting'}
|
||||
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
# All these values came from ANYMAIL_SEND_DEFAULTS + message.*:
|
||||
self.assertEqual(data['message']['metadata'],
|
||||
{'global': 'globalvalue', 'message': 'messagevalue', 'other': 'override'}) # merged
|
||||
self.assertEqual(data['message']['tags'], ['globaltag', 'messagetag']) # tags concatenated
|
||||
self.assertEqual(data['message']['track_clicks'], False) # message overrides
|
||||
self.assertEqual(data['message']['track_opens'], True)
|
||||
# self.assertEqual(data['globaloption'], 'globalsetting')
|
||||
# self.assertEqual(data['messageoption'], 'messagesetting') # additional esp_extra
|
||||
|
||||
@override_settings(ANYMAIL_MANDRILL_SEND_DEFAULTS={
|
||||
'tags': ['esptag'],
|
||||
'metadata': {'esp': 'espvalue'},
|
||||
'track_opens': False,
|
||||
})
|
||||
def test_esp_send_defaults(self):
|
||||
"""Test that ESP-specific send defaults override individual global defaults"""
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
# All these values came from ANYMAIL_SEND_DEFAULTS plus ANYMAIL_MAILGUN_SEND_DEFAULTS:
|
||||
self.assertEqual(data['message']['metadata'], {'esp': 'espvalue'}) # entire metadata overridden
|
||||
self.assertEqual(data['message']['tags'], ['esptag']) # entire tags overridden
|
||||
self.assertEqual(data['message']['track_clicks'], True) # we didn't override the global track_clicks
|
||||
self.assertEqual(data['message']['track_opens'], False)
|
||||
# self.assertEqual(data['globaloption'], 'globalsetting') # we didn't override the global esp_extra
|
||||
|
||||
|
||||
@override_settings(EMAIL_BACKEND="anymail.backends.mandrill.MandrillBackend")
|
||||
class MandrillBackendImproperlyConfiguredTests(SimpleTestCase, AnymailTestMixin):
|
||||
"""Test backend without required settings"""
|
||||
|
||||
def test_missing_api_key(self):
|
||||
with self.assertRaises(ImproperlyConfigured) as cm:
|
||||
mail.send_mail('Subject', 'Message', 'from@example.com', ['to@example.com'])
|
||||
errmsg = str(cm.exception)
|
||||
self.assertRegex(errmsg, r'\bMANDRILL_API_KEY\b')
|
||||
self.assertRegex(errmsg, r'\bANYMAIL_MANDRILL_API_KEY\b')
|
||||
333
tests/test_mandrill_djrill_features.py
Normal file
333
tests/test_mandrill_djrill_features.py
Normal file
@@ -0,0 +1,333 @@
|
||||
from datetime import date
|
||||
from django.core import mail
|
||||
from django.test import override_settings
|
||||
|
||||
from anymail.exceptions import AnymailAPIError, AnymailSerializationError
|
||||
|
||||
from .test_mandrill_backend import MandrillBackendMockAPITestCase
|
||||
|
||||
|
||||
class MandrillBackendDjrillFeatureTests(MandrillBackendMockAPITestCase):
|
||||
"""Test backend support for features leftover from Djrill"""
|
||||
|
||||
# Most of these features should be moved to esp_extra.
|
||||
# The template and merge_ta
|
||||
|
||||
def test_djrill_message_options(self):
|
||||
self.message.url_strip_qs = True
|
||||
self.message.important = True
|
||||
self.message.auto_text = True
|
||||
self.message.auto_html = True
|
||||
self.message.inline_css = True
|
||||
self.message.preserve_recipients = True
|
||||
self.message.view_content_link = False
|
||||
self.message.tracking_domain = "click.example.com"
|
||||
self.message.signing_domain = "example.com"
|
||||
self.message.return_path_domain = "support.example.com"
|
||||
self.message.subaccount = "marketing-dept"
|
||||
self.message.async = True
|
||||
self.message.ip_pool = "Bulk Pool"
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['url_strip_qs'], True)
|
||||
self.assertEqual(data['message']['important'], True)
|
||||
self.assertEqual(data['message']['auto_text'], True)
|
||||
self.assertEqual(data['message']['auto_html'], True)
|
||||
self.assertEqual(data['message']['inline_css'], True)
|
||||
self.assertEqual(data['message']['preserve_recipients'], True)
|
||||
self.assertEqual(data['message']['view_content_link'], False)
|
||||
self.assertEqual(data['message']['tracking_domain'], "click.example.com")
|
||||
self.assertEqual(data['message']['signing_domain'], "example.com")
|
||||
self.assertEqual(data['message']['return_path_domain'], "support.example.com")
|
||||
self.assertEqual(data['message']['subaccount'], "marketing-dept")
|
||||
self.assertEqual(data['async'], True)
|
||||
self.assertEqual(data['ip_pool'], "Bulk Pool")
|
||||
|
||||
def test_google_analytics(self):
|
||||
self.message.google_analytics_domains = ["example.com"]
|
||||
self.message.google_analytics_campaign = "Email Receipts"
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['google_analytics_domains'], ["example.com"])
|
||||
self.assertEqual(data['message']['google_analytics_campaign'], "Email Receipts")
|
||||
|
||||
def test_recipient_metadata(self):
|
||||
self.message.recipient_metadata = {
|
||||
# Anymail expands simple python dicts into the more-verbose
|
||||
# rcpt/values structures the Mandrill API uses
|
||||
"customer@example.com": {'cust_id': "67890", 'order_id': "54321"},
|
||||
"guest@example.com": {'cust_id': "94107", 'order_id': "43215"}
|
||||
}
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['recipient_metadata'],
|
||||
[{'rcpt': "customer@example.com",
|
||||
'values': {'cust_id': "67890", 'order_id': "54321"}},
|
||||
{'rcpt': "guest@example.com",
|
||||
'values': {'cust_id': "94107", 'order_id': "43215"}}
|
||||
])
|
||||
|
||||
def test_no_subaccount_by_default(self):
|
||||
mail.send_mail('Subject', 'Body', 'from@example.com', ['to@example.com'])
|
||||
data = self.get_api_call_json()
|
||||
self.assertFalse('subaccount' in data['message'])
|
||||
|
||||
@override_settings(ANYMAIL_MANDRILL_SEND_DEFAULTS={'subaccount': 'test_subaccount'})
|
||||
def test_subaccount_setting(self):
|
||||
mail.send_mail('Subject', 'Body', 'from@example.com', ['to@example.com'])
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['subaccount'], "test_subaccount")
|
||||
|
||||
@override_settings(ANYMAIL_MANDRILL_SEND_DEFAULTS={'subaccount': 'global_setting_subaccount'})
|
||||
def test_subaccount_message_overrides_setting(self):
|
||||
message = mail.EmailMessage('Subject', 'Body', 'from@example.com', ['to@example.com'])
|
||||
message.subaccount = "individual_message_subaccount" # should override global setting
|
||||
message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['subaccount'], "individual_message_subaccount")
|
||||
|
||||
def test_default_omits_options(self):
|
||||
"""Make sure by default we don't send any Mandrill-specific options.
|
||||
|
||||
Options not specified by the caller should be omitted entirely from
|
||||
the Mandrill API call (*not* sent as False or empty). This ensures
|
||||
that your Mandrill account settings apply by default.
|
||||
"""
|
||||
self.message.send()
|
||||
self.assert_esp_called("/messages/send.json")
|
||||
data = self.get_api_call_json()
|
||||
self.assertFalse('from_name' in data['message'])
|
||||
self.assertFalse('bcc_address' in data['message'])
|
||||
self.assertFalse('important' in data['message'])
|
||||
self.assertFalse('auto_text' in data['message'])
|
||||
self.assertFalse('auto_html' in data['message'])
|
||||
self.assertFalse('inline_css' in data['message'])
|
||||
self.assertFalse('url_strip_qs' in data['message'])
|
||||
self.assertFalse('preserve_recipients' in data['message'])
|
||||
self.assertFalse('view_content_link' in data['message'])
|
||||
self.assertFalse('tracking_domain' in data['message'])
|
||||
self.assertFalse('signing_domain' in data['message'])
|
||||
self.assertFalse('return_path_domain' in data['message'])
|
||||
self.assertFalse('subaccount' in data['message'])
|
||||
self.assertFalse('google_analytics_domains' in data['message'])
|
||||
self.assertFalse('google_analytics_campaign' in data['message'])
|
||||
self.assertFalse('merge_language' in data['message'])
|
||||
self.assertFalse('global_merge_vars' in data['message'])
|
||||
self.assertFalse('merge_vars' in data['message'])
|
||||
self.assertFalse('recipient_metadata' in data['message'])
|
||||
# Options at top level of api params (not in message dict):
|
||||
self.assertFalse('async' in data)
|
||||
self.assertFalse('ip_pool' in data)
|
||||
|
||||
def test_dates_not_serialized(self):
|
||||
"""Old versions of predecessor package Djrill accidentally serialized dates to ISO"""
|
||||
self.message.metadata = {'SHIP_DATE': date(2015, 12, 2)}
|
||||
with self.assertRaises(AnymailSerializationError):
|
||||
self.message.send()
|
||||
|
||||
|
||||
@override_settings(ANYMAIL_SEND_DEFAULTS={
|
||||
'from_name': 'Djrill Test',
|
||||
'important': True,
|
||||
'auto_text': True,
|
||||
'auto_html': True,
|
||||
'inline_css': True,
|
||||
'url_strip_qs': True,
|
||||
'preserve_recipients': True,
|
||||
'view_content_link': True,
|
||||
'subaccount': 'example-subaccount',
|
||||
'tracking_domain': 'example.com',
|
||||
'signing_domain': 'example.com',
|
||||
'return_path_domain': 'example.com',
|
||||
'google_analytics_domains': ['example.com/test'],
|
||||
'google_analytics_campaign': ['UA-00000000-1'],
|
||||
'merge_language': 'mailchimp',
|
||||
'global_merge_vars': {'TEST': 'djrill'},
|
||||
'async': True,
|
||||
'ip_pool': 'Pool1',
|
||||
'invalid': 'invalid',
|
||||
})
|
||||
class MandrillBackendDjrillSendDefaultsTests(MandrillBackendMockAPITestCase):
|
||||
"""Tests backend support for global SEND_DEFAULTS"""
|
||||
|
||||
def test_global_options(self):
|
||||
"""Test that any global settings get passed through
|
||||
"""
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['from_name'], 'Djrill Test')
|
||||
self.assertTrue(data['message']['important'])
|
||||
self.assertTrue(data['message']['auto_text'])
|
||||
self.assertTrue(data['message']['auto_html'])
|
||||
self.assertTrue(data['message']['inline_css'])
|
||||
self.assertTrue(data['message']['url_strip_qs'])
|
||||
self.assertTrue(data['message']['preserve_recipients'])
|
||||
self.assertTrue(data['message']['view_content_link'])
|
||||
self.assertEqual(data['message']['subaccount'], 'example-subaccount')
|
||||
self.assertEqual(data['message']['tracking_domain'], 'example.com')
|
||||
self.assertEqual(data['message']['signing_domain'], 'example.com')
|
||||
self.assertEqual(data['message']['return_path_domain'], 'example.com')
|
||||
self.assertEqual(data['message']['google_analytics_domains'], ['example.com/test'])
|
||||
self.assertEqual(data['message']['google_analytics_campaign'], ['UA-00000000-1'])
|
||||
self.assertEqual(data['message']['merge_language'], 'mailchimp')
|
||||
self.assertEqual(data['message']['global_merge_vars'],
|
||||
[{'name': 'TEST', 'content': 'djrill'}])
|
||||
self.assertFalse('merge_vars' in data['message'])
|
||||
self.assertFalse('recipient_metadata' in data['message'])
|
||||
# Options at top level of api params (not in message dict):
|
||||
self.assertTrue(data['async'])
|
||||
self.assertEqual(data['ip_pool'], 'Pool1')
|
||||
# Option that shouldn't be added
|
||||
self.assertFalse('invalid' in data['message'])
|
||||
|
||||
def test_global_options_override(self):
|
||||
"""Test that manually settings options overrides global settings
|
||||
"""
|
||||
self.message.from_name = "override"
|
||||
self.message.important = False
|
||||
self.message.auto_text = False
|
||||
self.message.auto_html = False
|
||||
self.message.inline_css = False
|
||||
self.message.url_strip_qs = False
|
||||
self.message.preserve_recipients = False
|
||||
self.message.view_content_link = False
|
||||
self.message.subaccount = "override"
|
||||
self.message.tracking_domain = "override.example.com"
|
||||
self.message.signing_domain = "override.example.com"
|
||||
self.message.return_path_domain = "override.example.com"
|
||||
self.message.google_analytics_domains = ['override.example.com']
|
||||
self.message.google_analytics_campaign = ['UA-99999999-1']
|
||||
self.message.merge_language = 'handlebars'
|
||||
self.message.async = False
|
||||
self.message.ip_pool = "Bulk Pool"
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['from_name'], 'override')
|
||||
self.assertFalse(data['message']['important'])
|
||||
self.assertFalse(data['message']['auto_text'])
|
||||
self.assertFalse(data['message']['auto_html'])
|
||||
self.assertFalse(data['message']['inline_css'])
|
||||
self.assertFalse(data['message']['url_strip_qs'])
|
||||
self.assertFalse(data['message']['preserve_recipients'])
|
||||
self.assertFalse(data['message']['view_content_link'])
|
||||
self.assertEqual(data['message']['subaccount'], 'override')
|
||||
self.assertEqual(data['message']['tracking_domain'], 'override.example.com')
|
||||
self.assertEqual(data['message']['signing_domain'], 'override.example.com')
|
||||
self.assertEqual(data['message']['return_path_domain'], 'override.example.com')
|
||||
self.assertEqual(data['message']['google_analytics_domains'], ['override.example.com'])
|
||||
self.assertEqual(data['message']['google_analytics_campaign'], ['UA-99999999-1'])
|
||||
self.assertEqual(data['message']['merge_language'], 'handlebars')
|
||||
self.assertEqual(data['message']['global_merge_vars'], [{'name': 'TEST', 'content': 'djrill'}])
|
||||
# Options at top level of api params (not in message dict):
|
||||
self.assertFalse(data['async'])
|
||||
self.assertEqual(data['ip_pool'], 'Bulk Pool')
|
||||
|
||||
def test_global_merge(self):
|
||||
# Test that global settings merge in
|
||||
self.message.global_merge_vars = {'GREETING': "Hello"}
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['global_merge_vars'],
|
||||
[{'name': "GREETING", 'content': "Hello"},
|
||||
{'name': 'TEST', 'content': 'djrill'}])
|
||||
|
||||
def test_global_merge_overwrite(self):
|
||||
# Test that global merge settings are overwritten
|
||||
self.message.global_merge_vars = {'TEST': "Hello"}
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['global_merge_vars'],
|
||||
[{'name': 'TEST', 'content': 'Hello'}])
|
||||
|
||||
|
||||
class MandrillBackendDjrillTemplateTests(MandrillBackendMockAPITestCase):
|
||||
"""Test backend support for ESP templating features"""
|
||||
|
||||
# Holdovers from Djrill, until we design Anymail's normalized esp-template support
|
||||
|
||||
def test_merge_data(self):
|
||||
# Anymail expands simple python dicts into the more-verbose name/content
|
||||
# structures the Mandrill API uses
|
||||
self.message.merge_language = "mailchimp"
|
||||
self.message.global_merge_vars = {'GREETING': "Hello",
|
||||
'ACCOUNT_TYPE': "Basic"}
|
||||
self.message.merge_vars = {
|
||||
"customer@example.com": {'GREETING': "Dear Customer",
|
||||
'ACCOUNT_TYPE': "Premium"},
|
||||
"guest@example.com": {'GREETING': "Dear Guest"},
|
||||
}
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['message']['merge_language'], "mailchimp")
|
||||
self.assertEqual(data['message']['global_merge_vars'],
|
||||
[{'name': 'ACCOUNT_TYPE', 'content': "Basic"},
|
||||
{'name': "GREETING", 'content': "Hello"}])
|
||||
self.assertEqual(data['message']['merge_vars'],
|
||||
[{'rcpt': "customer@example.com",
|
||||
'vars': [{'name': 'ACCOUNT_TYPE', 'content': "Premium"},
|
||||
{'name': "GREETING", 'content': "Dear Customer"}]},
|
||||
{'rcpt': "guest@example.com",
|
||||
'vars': [{'name': "GREETING", 'content': "Dear Guest"}]}
|
||||
])
|
||||
|
||||
def test_send_template(self):
|
||||
self.message.template_name = "PERSONALIZED_SPECIALS"
|
||||
self.message.template_content = {
|
||||
'HEADLINE': "<h1>Specials Just For *|FNAME|*</h1>",
|
||||
'OFFER_BLOCK': "<p><em>Half off</em> all fruit</p>"
|
||||
}
|
||||
self.message.send()
|
||||
self.assert_esp_called("/messages/send-template.json")
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['template_name'], "PERSONALIZED_SPECIALS")
|
||||
# Anymail expands simple python dicts into the more-verbose name/content
|
||||
# structures the Mandrill API uses
|
||||
self.assertEqual(data['template_content'],
|
||||
[{'name': "HEADLINE",
|
||||
'content': "<h1>Specials Just For *|FNAME|*</h1>"},
|
||||
{'name': "OFFER_BLOCK",
|
||||
'content': "<p><em>Half off</em> all fruit</p>"}])
|
||||
|
||||
def test_send_template_without_from_field(self):
|
||||
self.message.template_name = "PERSONALIZED_SPECIALS"
|
||||
self.message.use_template_from = True
|
||||
self.message.send()
|
||||
self.assert_esp_called("/messages/send-template.json")
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['template_name'], "PERSONALIZED_SPECIALS")
|
||||
self.assertFalse('from_email' in data['message'])
|
||||
self.assertFalse('from_name' in data['message'])
|
||||
|
||||
def test_send_template_without_from_field_api_failure(self):
|
||||
self.set_mock_response(status_code=400)
|
||||
self.message.template_name = "PERSONALIZED_SPECIALS"
|
||||
self.message.use_template_from = True
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
self.message.send()
|
||||
|
||||
def test_send_template_without_subject_field(self):
|
||||
self.message.template_name = "PERSONALIZED_SPECIALS"
|
||||
self.message.use_template_subject = True
|
||||
self.message.send()
|
||||
self.assert_esp_called("/messages/send-template.json")
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['template_name'], "PERSONALIZED_SPECIALS")
|
||||
self.assertFalse('subject' in data['message'])
|
||||
|
||||
def test_no_template_content(self):
|
||||
# Just a template, without any template_content to be merged
|
||||
self.message.template_name = "WELCOME_MESSAGE"
|
||||
self.message.send()
|
||||
self.assert_esp_called("/messages/send-template.json")
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['template_name'], "WELCOME_MESSAGE")
|
||||
self.assertEqual(data['template_content'], []) # Mandrill requires this field
|
||||
|
||||
def test_non_template_send(self):
|
||||
# Make sure the non-template case still uses /messages/send.json
|
||||
self.message.send()
|
||||
self.assert_esp_called("/messages/send.json")
|
||||
data = self.get_api_call_json()
|
||||
self.assertFalse('template_name' in data)
|
||||
self.assertFalse('template_content' in data)
|
||||
self.assertFalse('async' in data)
|
||||
138
tests/test_mandrill_integration.py
Normal file
138
tests/test_mandrill_integration.py
Normal file
@@ -0,0 +1,138 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from django.core import mail
|
||||
from django.test import SimpleTestCase
|
||||
from django.test.utils import override_settings
|
||||
|
||||
from anymail.exceptions import AnymailAPIError, AnymailRecipientsRefused
|
||||
from anymail.message import AnymailMessage
|
||||
|
||||
from .utils import AnymailTestMixin, sample_image_path
|
||||
|
||||
MANDRILL_TEST_API_KEY = os.getenv('MANDRILL_TEST_API_KEY')
|
||||
|
||||
|
||||
@unittest.skipUnless(MANDRILL_TEST_API_KEY,
|
||||
"Set MANDRILL_TEST_API_KEY environment variable to run integration tests")
|
||||
@override_settings(MANDRILL_API_KEY=MANDRILL_TEST_API_KEY,
|
||||
EMAIL_BACKEND="anymail.backends.mandrill.MandrillBackend")
|
||||
class MandrillBackendIntegrationTests(SimpleTestCase, AnymailTestMixin):
|
||||
"""Mandrill API integration tests
|
||||
|
||||
These tests run against the **live** Mandrill API, using the
|
||||
environment variable `MANDRILL_TEST_API_KEY` as the API key.
|
||||
If that variable is not set, these tests won't run.
|
||||
|
||||
See https://mandrill.zendesk.com/hc/en-us/articles/205582447
|
||||
for info on Mandrill test keys.
|
||||
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
super(MandrillBackendIntegrationTests, self).setUp()
|
||||
self.message = mail.EmailMultiAlternatives('Anymail Mandrill integration test', 'Text content',
|
||||
'from@example.com', ['to@example.com'])
|
||||
self.message.attach_alternative('<p>HTML content</p>', "text/html")
|
||||
|
||||
def test_simple_send(self):
|
||||
# Example of getting the Mandrill send status and _id from the message
|
||||
sent_count = self.message.send()
|
||||
self.assertEqual(sent_count, 1)
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
anymail_status = self.message.anymail_status
|
||||
sent_status = anymail_status.recipients['to@example.com'].status
|
||||
message_id = anymail_status.recipients['to@example.com'].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
|
||||
|
||||
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)
|
||||
|
||||
def test_all_options(self):
|
||||
message = AnymailMessage(
|
||||
subject="Anymail all-options integration test",
|
||||
body="This is the text body",
|
||||
from_email="Test From <from@example.com>",
|
||||
to=["to1@example.com", "Recipient 2 <to2@example.com>"],
|
||||
cc=["cc1@example.com", "Copy 2 <cc2@example.com>"],
|
||||
bcc=["bcc1@example.com", "Blind Copy 2 <bcc2@example.com>"],
|
||||
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,
|
||||
)
|
||||
message.attach("attachment1.txt", "Here is some\ntext for you", "text/plain")
|
||||
message.attach("attachment2.csv", "ID,Name\n1,Amy Lina", "text/csv")
|
||||
cid = message.attach_inline_image_file(sample_image_path())
|
||||
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")
|
||||
|
||||
message.send()
|
||||
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
|
||||
with self.assertRaises(AnymailAPIError) as cm:
|
||||
self.message.send()
|
||||
err = cm.exception
|
||||
self.assertEqual(err.status_code, 500)
|
||||
self.assertIn("email address is invalid", str(err))
|
||||
|
||||
def test_invalid_to(self):
|
||||
# Example of detecting when a recipient is not a valid email address
|
||||
self.message.to = ['invalid@localhost']
|
||||
try:
|
||||
self.message.send()
|
||||
except AnymailRecipientsRefused:
|
||||
# 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'})
|
||||
else:
|
||||
# Sometimes Mandrill queues these test sends
|
||||
# noinspection PyUnresolvedReferences
|
||||
if self.message.anymail_status.status == {'queued'}:
|
||||
self.skipTest("Mandrill queued the send -- can't complete this test")
|
||||
else:
|
||||
self.fail("Djrill 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']
|
||||
try:
|
||||
self.message.send()
|
||||
except AnymailRecipientsRefused:
|
||||
# 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'})
|
||||
else:
|
||||
# Sometimes Mandrill queues these test sends
|
||||
# noinspection PyUnresolvedReferences
|
||||
if self.message.anymail_status.status == {'queued'}:
|
||||
self.skipTest("Mandrill queued the send -- can't complete this test")
|
||||
else:
|
||||
self.fail("Djrill 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):
|
||||
# Example of trying to send with an invalid MANDRILL_API_KEY
|
||||
with self.assertRaises(AnymailAPIError) as cm:
|
||||
self.message.send()
|
||||
err = cm.exception
|
||||
self.assertEqual(err.status_code, 500)
|
||||
# Make sure the exception message includes Mandrill's response:
|
||||
self.assertIn("Invalid API key", str(err))
|
||||
128
tests/test_mandrill_webhook.py
Normal file
128
tests/test_mandrill_webhook.py
Normal file
@@ -0,0 +1,128 @@
|
||||
import hashlib
|
||||
import hmac
|
||||
import json
|
||||
from base64 import b64encode
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.test import TestCase
|
||||
from django.test.utils import override_settings
|
||||
|
||||
from anymail.compat import b
|
||||
from anymail.signals import webhook_event
|
||||
|
||||
|
||||
class DjrillWebhookSecretMixinTests(TestCase):
|
||||
"""
|
||||
Test mixin used in optional Mandrill webhook support
|
||||
"""
|
||||
|
||||
def test_missing_secret(self):
|
||||
with self.assertRaises(ImproperlyConfigured):
|
||||
self.client.get('/webhook/')
|
||||
|
||||
@override_settings(DJRILL_WEBHOOK_SECRET='abc123')
|
||||
def test_incorrect_secret(self):
|
||||
response = self.client.head('/webhook/?secret=wrong')
|
||||
self.assertEqual(response.status_code, 403)
|
||||
|
||||
@override_settings(DJRILL_WEBHOOK_SECRET='abc123')
|
||||
def test_default_secret_name(self):
|
||||
response = self.client.head('/webhook/?secret=abc123')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
@override_settings(DJRILL_WEBHOOK_SECRET='abc123', DJRILL_WEBHOOK_SECRET_NAME='verysecret')
|
||||
def test_custom_secret_name(self):
|
||||
response = self.client.head('/webhook/?verysecret=abc123')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
|
||||
@override_settings(DJRILL_WEBHOOK_SECRET='abc123',
|
||||
DJRILL_WEBHOOK_SIGNATURE_KEY="signature")
|
||||
class DjrillWebhookSignatureMixinTests(TestCase):
|
||||
"""
|
||||
Test mixin used in optional Mandrill webhook signature support
|
||||
"""
|
||||
|
||||
def test_incorrect_settings(self):
|
||||
with self.assertRaises(ImproperlyConfigured):
|
||||
self.client.post('/webhook/?secret=abc123')
|
||||
|
||||
@override_settings(DJRILL_WEBHOOK_URL="/webhook/?secret=abc123",
|
||||
DJRILL_WEBHOOK_SIGNATURE_KEY = "anothersignature")
|
||||
def test_unauthorized(self):
|
||||
response = self.client.post(settings.DJRILL_WEBHOOK_URL)
|
||||
self.assertEqual(response.status_code, 403)
|
||||
|
||||
@override_settings(DJRILL_WEBHOOK_URL="/webhook/?secret=abc123")
|
||||
def test_signature(self):
|
||||
signature = hmac.new(key=b(settings.DJRILL_WEBHOOK_SIGNATURE_KEY),
|
||||
msg=b(settings.DJRILL_WEBHOOK_URL+"mandrill_events[]"),
|
||||
digestmod=hashlib.sha1)
|
||||
hash_string = b64encode(signature.digest())
|
||||
response = self.client.post('/webhook/?secret=abc123', data={"mandrill_events":"[]"},
|
||||
**{"HTTP_X_MANDRILL_SIGNATURE": hash_string})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
|
||||
@override_settings(DJRILL_WEBHOOK_SECRET='abc123')
|
||||
class DjrillWebhookViewTests(TestCase):
|
||||
"""
|
||||
Test optional Mandrill webhook view
|
||||
"""
|
||||
|
||||
def test_head_request(self):
|
||||
response = self.client.head('/webhook/?secret=abc123')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_post_request_invalid_json(self):
|
||||
response = self.client.post('/webhook/?secret=abc123')
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
def test_post_request_valid_json(self):
|
||||
response = self.client.post('/webhook/?secret=abc123', {
|
||||
'mandrill_events': json.dumps([{"event": "send", "msg": {}}])
|
||||
})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_webhook_send_signal(self):
|
||||
self.signal_received_count = 0
|
||||
test_event = {"event": "send", "msg": {}}
|
||||
|
||||
def my_callback(sender, event_type, data, **kwargs):
|
||||
self.signal_received_count += 1
|
||||
self.assertEqual(event_type, 'send')
|
||||
self.assertEqual(data, test_event)
|
||||
|
||||
try:
|
||||
webhook_event.connect(my_callback, weak=False) # local callback func, so don't use weak ref
|
||||
response = self.client.post('/webhook/?secret=abc123', {
|
||||
'mandrill_events': json.dumps([test_event])
|
||||
})
|
||||
finally:
|
||||
webhook_event.disconnect(my_callback)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(self.signal_received_count, 1)
|
||||
|
||||
def test_webhook_sync_event(self):
|
||||
# Mandrill sync events use a different format from other events
|
||||
# https://mandrill.zendesk.com/hc/en-us/articles/205583297-Sync-Event-Webhook-format
|
||||
self.signal_received_count = 0
|
||||
test_event = {"type": "whitelist", "action": "add"}
|
||||
|
||||
def my_callback(sender, event_type, data, **kwargs):
|
||||
self.signal_received_count += 1
|
||||
self.assertEqual(event_type, 'whitelist_add') # synthesized event_type
|
||||
self.assertEqual(data, test_event)
|
||||
|
||||
try:
|
||||
webhook_event.connect(my_callback, weak=False) # local callback func, so don't use weak ref
|
||||
response = self.client.post('/webhook/?secret=abc123', {
|
||||
'mandrill_events': json.dumps([test_event])
|
||||
})
|
||||
finally:
|
||||
webhook_event.disconnect(my_callback)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(self.signal_received_count, 1)
|
||||
579
tests/test_postmark_backend.py
Normal file
579
tests/test_postmark_backend.py
Normal file
@@ -0,0 +1,579 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from base64 import b64encode
|
||||
from decimal import Decimal
|
||||
from email.mime.base import MIMEBase
|
||||
from email.mime.image import MIMEImage
|
||||
|
||||
from django.core import mail
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.test import SimpleTestCase
|
||||
from django.test.utils import override_settings
|
||||
|
||||
from anymail.exceptions import (AnymailAPIError, AnymailSerializationError,
|
||||
AnymailUnsupportedFeature, AnymailRecipientsRefused)
|
||||
from anymail.message import attach_inline_image_file
|
||||
|
||||
from .mock_requests_backend import RequestsBackendMockAPITestCase, SessionSharingTestCasesMixin
|
||||
from .utils import sample_image_content, sample_image_path, SAMPLE_IMAGE_FILENAME, AnymailTestMixin, decode_att
|
||||
|
||||
|
||||
@override_settings(EMAIL_BACKEND='anymail.backends.postmark.PostmarkBackend',
|
||||
ANYMAIL={'POSTMARK_SERVER_TOKEN': 'test_server_token'})
|
||||
class PostmarkBackendMockAPITestCase(RequestsBackendMockAPITestCase):
|
||||
DEFAULT_RAW_RESPONSE = b"""{
|
||||
"To": "to@example.com",
|
||||
"SubmittedAt": "2016-03-12T15:27:50.4468803-05:00",
|
||||
"MessageID": "b4007d94-33f1-4e78-a783-97417d6c80e6",
|
||||
"ErrorCode":0,
|
||||
"Message":"OK"
|
||||
}"""
|
||||
|
||||
def setUp(self):
|
||||
super(PostmarkBackendMockAPITestCase, self).setUp()
|
||||
# Simple message useful for many tests
|
||||
self.message = mail.EmailMultiAlternatives('Subject', 'Text Body', 'from@example.com', ['to@example.com'])
|
||||
|
||||
|
||||
class PostmarkBackendStandardEmailTests(PostmarkBackendMockAPITestCase):
|
||||
"""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@sender.example.com', ['to@example.com'], fail_silently=False)
|
||||
self.assert_esp_called('/email')
|
||||
headers = self.get_api_call_headers()
|
||||
self.assertEqual(headers["X-Postmark-Server-Token"], "test_server_token")
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['Subject'], "Subject here")
|
||||
self.assertEqual(data['TextBody'], "Here is the message.")
|
||||
self.assertEqual(data['From'], "from@sender.example.com")
|
||||
self.assertEqual(data['To'], "to@example.com")
|
||||
|
||||
def test_name_addr(self):
|
||||
"""Make sure RFC2822 name-addr format (with display-name) is allowed
|
||||
|
||||
(Test both sender and recipient addresses)
|
||||
"""
|
||||
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'])
|
||||
msg.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['From'], 'From Name <from@example.com>')
|
||||
self.assertEqual(data['To'], 'Recipient #1 <to1@example.com>, to2@example.com')
|
||||
self.assertEqual(data['Cc'], 'Carbon Copy <cc1@example.com>, cc2@example.com')
|
||||
self.assertEqual(data['Bcc'], 'Blind Copy <bcc1@example.com>, bcc2@example.com')
|
||||
|
||||
def test_email_message(self):
|
||||
email = mail.EmailMessage(
|
||||
'Subject', 'Body goes here', 'from@example.com',
|
||||
['to1@example.com', 'Also To <to2@example.com>'],
|
||||
bcc=['bcc1@example.com', 'Also BCC <bcc2@example.com>'],
|
||||
cc=['cc1@example.com', 'Also CC <cc2@example.com>'],
|
||||
headers={'Reply-To': 'another@example.com',
|
||||
'X-MyHeader': 'my value',
|
||||
'Message-ID': 'mycustommsgid@sales.example.com'}) # should override backend msgid
|
||||
email.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['Subject'], "Subject")
|
||||
self.assertEqual(data['TextBody'], "Body goes here")
|
||||
self.assertEqual(data['From'], "from@example.com")
|
||||
self.assertEqual(data['To'], 'to1@example.com, Also To <to2@example.com>')
|
||||
self.assertEqual(data['Bcc'], 'bcc1@example.com, Also BCC <bcc2@example.com>')
|
||||
self.assertEqual(data['Cc'], 'cc1@example.com, Also CC <cc2@example.com>')
|
||||
self.assertCountEqual(data['Headers'], [
|
||||
{'Name': 'Message-ID', 'Value': 'mycustommsgid@sales.example.com'},
|
||||
{'Name': 'Reply-To', 'Value': 'another@example.com'},
|
||||
{'Name': 'X-MyHeader', 'Value': 'my value'},
|
||||
])
|
||||
|
||||
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'])
|
||||
email.attach_alternative(html_content, "text/html")
|
||||
email.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['TextBody'], text_content)
|
||||
self.assertEqual(data['HtmlBody'], html_content)
|
||||
# Don't accidentally send the html part as an attachment:
|
||||
self.assertNotIn('Attachments', data)
|
||||
|
||||
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'])
|
||||
email.content_subtype = "html" # Main content is now text/html
|
||||
email.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertNotIn('TextBody', data)
|
||||
self.assertEqual(data['HtmlBody'], html_content)
|
||||
|
||||
def test_extra_headers(self):
|
||||
self.message.extra_headers = {'X-Custom': 'string', 'X-Num': 123}
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertCountEqual(data['Headers'], [
|
||||
{'Name': 'X-Custom', 'Value': 'string'},
|
||||
{'Name': 'X-Num', 'Value': 123}
|
||||
])
|
||||
|
||||
def test_extra_headers_serialization_error(self):
|
||||
self.message.extra_headers = {'X-Custom': Decimal(12.5)}
|
||||
with self.assertRaisesMessage(AnymailSerializationError, "Decimal('12.5')"):
|
||||
self.message.send()
|
||||
|
||||
def test_reply_to(self):
|
||||
# reply_to is new in Django 1.8 -- before that, you can simply include it in headers
|
||||
try:
|
||||
# noinspection PyArgumentList
|
||||
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'})
|
||||
except TypeError:
|
||||
# Pre-Django 1.8
|
||||
return self.skipTest("Django version doesn't support EmailMessage(reply_to)")
|
||||
email.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['ReplyTo'], 'reply@example.com, Other <reply2@example.com>')
|
||||
self.assertEqual(data['Headers'], [{'Name': 'X-Other', 'Value': 'Keep'}]) # don't lose other headers
|
||||
|
||||
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")
|
||||
|
||||
# Should guess mimetype if not provided...
|
||||
png_content = b"PNG\xb4 pretend this is the contents of a png file"
|
||||
self.message.attach(filename="test.png", content=png_content)
|
||||
|
||||
# Should work with a MIMEBase object (also tests no filename)...
|
||||
pdf_content = b"PDF\xb4 pretend this is valid pdf data"
|
||||
mimeattachment = MIMEBase('application', 'pdf')
|
||||
mimeattachment.set_payload(pdf_content)
|
||||
self.message.attach(mimeattachment)
|
||||
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
attachments = data['Attachments']
|
||||
self.assertEqual(len(attachments), 3)
|
||||
self.assertEqual(attachments[0]["Name"], "test.txt")
|
||||
self.assertEqual(attachments[0]["ContentType"], "text/plain")
|
||||
self.assertEqual(decode_att(attachments[0]["Content"]).decode('ascii'), text_content)
|
||||
self.assertNotIn('ContentID', attachments[0])
|
||||
|
||||
self.assertEqual(attachments[1]["ContentType"], "image/png") # inferred from filename
|
||||
self.assertEqual(attachments[1]["Name"], "test.png")
|
||||
self.assertEqual(decode_att(attachments[1]["Content"]), png_content)
|
||||
self.assertNotIn('ContentID', attachments[1]) # make sure image not treated as inline
|
||||
|
||||
self.assertEqual(attachments[2]["ContentType"], "application/pdf")
|
||||
self.assertEqual(attachments[2]["Name"], "") # none
|
||||
self.assertEqual(decode_att(attachments[2]["Content"]), pdf_content)
|
||||
self.assertNotIn('ContentID', attachments[2])
|
||||
|
||||
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.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['Attachments'], [{
|
||||
'Name': 'Une pièce jointe.html',
|
||||
'ContentType': 'text/html',
|
||||
'Content': b64encode('<p>\u2019</p>'.encode('utf-8')).decode('ascii')
|
||||
}])
|
||||
|
||||
def test_embedded_images(self):
|
||||
image_filename = SAMPLE_IMAGE_FILENAME
|
||||
image_path = sample_image_path(image_filename)
|
||||
image_data = sample_image_content(image_filename)
|
||||
|
||||
cid = attach_inline_image_file(self.message, image_path) # Read from a png file
|
||||
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()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['HtmlBody'], html_content)
|
||||
|
||||
attachments = data['Attachments']
|
||||
self.assertEqual(len(attachments), 1)
|
||||
self.assertEqual(attachments[0]['Name'], image_filename)
|
||||
self.assertEqual(attachments[0]['ContentType'], 'image/png')
|
||||
self.assertEqual(decode_att(attachments[0]["Content"]), image_data)
|
||||
self.assertEqual(attachments[0]["ContentID"], 'cid:%s' % cid)
|
||||
|
||||
def test_attached_images(self):
|
||||
image_filename = SAMPLE_IMAGE_FILENAME
|
||||
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
|
||||
|
||||
image = MIMEImage(image_data) # option 2: construct the MIMEImage and attach it directly
|
||||
self.message.attach(image)
|
||||
|
||||
image_data_b64 = b64encode(image_data).decode('ascii')
|
||||
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['Attachments'], [
|
||||
{
|
||||
'Name': image_filename, # the named one
|
||||
'ContentType': 'image/png',
|
||||
'Content': image_data_b64,
|
||||
},
|
||||
{
|
||||
'Name': '', # the unnamed one
|
||||
'ContentType': 'image/png',
|
||||
'Content': image_data_b64,
|
||||
},
|
||||
])
|
||||
|
||||
def test_multiple_html_alternatives(self):
|
||||
# Multiple alternatives not allowed
|
||||
self.message.attach_alternative("<p>First html is OK</p>", "text/html")
|
||||
self.message.attach_alternative("<p>But not second html</p>", "text/html")
|
||||
with self.assertRaises(AnymailUnsupportedFeature):
|
||||
self.message.send()
|
||||
|
||||
def test_html_alternative(self):
|
||||
# Only html alternatives allowed
|
||||
self.message.attach_alternative("{'not': 'allowed'}", "application/json")
|
||||
with self.assertRaises(AnymailUnsupportedFeature):
|
||||
self.message.send()
|
||||
|
||||
def test_alternatives_fail_silently(self):
|
||||
# Make sure fail_silently is respected
|
||||
self.message.attach_alternative("{'not': 'allowed'}", "application/json")
|
||||
sent = self.message.send(fail_silently=True)
|
||||
self.assert_esp_not_called("API should not be called when send fails silently")
|
||||
self.assertEqual(sent, 0)
|
||||
|
||||
def test_suppress_empty_address_lists(self):
|
||||
"""Empty to, cc, bcc, and reply_to shouldn't generate empty fields"""
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertNotIn('Cc', data)
|
||||
self.assertNotIn('Bcc', data)
|
||||
self.assertNotIn('ReplyTo', data)
|
||||
|
||||
# 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.assertNotIn('To', data)
|
||||
|
||||
def test_api_failure(self):
|
||||
self.set_mock_response(status_code=500)
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
sent = mail.send_mail('Subject', 'Body', 'from@example.com', ['to@example.com'])
|
||||
self.assertEqual(sent, 0)
|
||||
|
||||
# Make sure fail_silently is respected
|
||||
self.set_mock_response(status_code=500)
|
||||
sent = mail.send_mail('Subject', 'Body', 'from@example.com', ['to@example.com'], fail_silently=True)
|
||||
self.assertEqual(sent, 0)
|
||||
|
||||
def test_api_error_includes_details(self):
|
||||
"""AnymailAPIError should include ESP's error message"""
|
||||
# JSON error response:
|
||||
error_response = b"""{
|
||||
"ErrorCode": 451,
|
||||
"Message": "Helpful explanation from Postmark."
|
||||
}"""
|
||||
self.set_mock_response(status_code=200, raw=error_response)
|
||||
with self.assertRaisesMessage(AnymailAPIError, "Helpful explanation from Postmark"):
|
||||
self.message.send()
|
||||
|
||||
# Non-JSON error response:
|
||||
self.set_mock_response(status_code=500, raw=b"Ack! Bad proxy!")
|
||||
with self.assertRaisesMessage(AnymailAPIError, "Ack! Bad proxy!"):
|
||||
self.message.send()
|
||||
|
||||
# No content in the error response:
|
||||
self.set_mock_response(status_code=502, raw=None)
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
self.message.send()
|
||||
|
||||
|
||||
class PostmarkBackendAnymailFeatureTests(PostmarkBackendMockAPITestCase):
|
||||
"""Test backend support for Anymail added features"""
|
||||
|
||||
def test_metadata(self):
|
||||
self.message.metadata = {'user_id': "12345", 'items': 6}
|
||||
with self.assertRaisesMessage(AnymailUnsupportedFeature, 'metadata'):
|
||||
self.message.send()
|
||||
|
||||
def test_send_at(self):
|
||||
self.message.send_at = 1651820889 # 2022-05-06 07:08:09 UTC
|
||||
with self.assertRaisesMessage(AnymailUnsupportedFeature, 'send_at'):
|
||||
self.message.send()
|
||||
|
||||
def test_tags(self):
|
||||
self.message.tags = ["receipt"]
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['Tag'], "receipt")
|
||||
|
||||
self.message.tags = ["receipt", "repeat-user"]
|
||||
with self.assertRaisesMessage(AnymailUnsupportedFeature, 'multiple tags'):
|
||||
self.message.send()
|
||||
|
||||
def test_track_opens(self):
|
||||
self.message.track_opens = True
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['TrackOpens'], True)
|
||||
|
||||
def test_track_clicks(self):
|
||||
self.message.track_clicks = True
|
||||
with self.assertRaisesMessage(AnymailUnsupportedFeature, 'track_clicks'):
|
||||
self.message.send()
|
||||
|
||||
def test_default_omits_options(self):
|
||||
"""Make sure by default we don't send any ESP-specific options.
|
||||
|
||||
Options not specified by the caller should be omitted entirely from
|
||||
the API call (*not* sent as False or empty). This ensures
|
||||
that your ESP account settings apply by default.
|
||||
"""
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertNotIn('Tag', data)
|
||||
self.assertNotIn('TrackOpens', data)
|
||||
|
||||
def test_esp_extra(self):
|
||||
self.message.esp_extra = {
|
||||
'FuturePostmarkOption': 'some-value',
|
||||
}
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
self.assertEqual(data['FuturePostmarkOption'], 'some-value')
|
||||
|
||||
def test_message_server_token(self):
|
||||
# Can override server-token on a per-message basis:
|
||||
self.message.esp_extra = {
|
||||
'server_token': 'token_for_this_message_only',
|
||||
}
|
||||
self.message.send()
|
||||
headers = self.get_api_call_headers()
|
||||
self.assertEqual(headers["X-Postmark-Server-Token"], "token_for_this_message_only")
|
||||
data = self.get_api_call_json()
|
||||
self.assertNotIn('server_token', data) # not in the json
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
def test_send_attaches_anymail_status(self):
|
||||
""" The anymail_status should be attached to the message when it is sent """
|
||||
response_content = b"""{
|
||||
"MessageID":"abcdef01-2345-6789-0123-456789abcdef",
|
||||
"ErrorCode":0,
|
||||
"Message":"OK"
|
||||
}"""
|
||||
self.set_mock_response(raw=response_content)
|
||||
msg = mail.EmailMessage('Subject', 'Message', 'from@example.com', ['to1@example.com'],)
|
||||
sent = msg.send()
|
||||
self.assertEqual(sent, 1)
|
||||
self.assertEqual(msg.anymail_status.status, {'sent'})
|
||||
self.assertEqual(msg.anymail_status.message_id, 'abcdef01-2345-6789-0123-456789abcdef')
|
||||
self.assertEqual(msg.anymail_status.recipients['to1@example.com'].status, 'sent')
|
||||
self.assertEqual(msg.anymail_status.recipients['to1@example.com'].message_id,
|
||||
'abcdef01-2345-6789-0123-456789abcdef')
|
||||
self.assertEqual(msg.anymail_status.esp_response.content, response_content)
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
def test_send_failed_anymail_status(self):
|
||||
""" If the send fails, anymail_status should contain initial values"""
|
||||
self.set_mock_response(status_code=500)
|
||||
sent = self.message.send(fail_silently=True)
|
||||
self.assertEqual(sent, 0)
|
||||
self.assertIsNone(self.message.anymail_status.status)
|
||||
self.assertIsNone(self.message.anymail_status.message_id)
|
||||
self.assertEqual(self.message.anymail_status.recipients, {})
|
||||
self.assertIsNone(self.message.anymail_status.esp_response)
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
def test_send_unparsable_response(self):
|
||||
"""If the send succeeds, but a non-JSON API response, should raise an API exception"""
|
||||
mock_response = self.set_mock_response(status_code=200,
|
||||
raw=b"yikes, this isn't a real response")
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
self.message.send()
|
||||
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, mock_response)
|
||||
|
||||
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
|
||||
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 Postmark", str(err)) # our added context
|
||||
self.assertIn("Decimal('19.99') is not JSON serializable", str(err)) # original message
|
||||
|
||||
|
||||
class PostmarkBackendRecipientsRefusedTests(PostmarkBackendMockAPITestCase):
|
||||
"""Should raise AnymailRecipientsRefused when *all* recipients are rejected or invalid"""
|
||||
|
||||
def test_recipients_inactive(self):
|
||||
self.set_mock_response(
|
||||
status_code=422,
|
||||
raw=b'{"ErrorCode":406,'
|
||||
b'"Message":"You tried to send to a recipient that has been marked as inactive.\\n'
|
||||
b'Found inactive addresses: hardbounce@example.com, spam@example.com.\\n'
|
||||
b'Inactive recipients are ones that have generated a hard bounce or a spam complaint."}'
|
||||
)
|
||||
msg = mail.EmailMessage('Subject', 'Body', 'from@example.com',
|
||||
['hardbounce@example.com', 'Hates Spam <spam@example.com>'])
|
||||
with self.assertRaises(AnymailRecipientsRefused):
|
||||
msg.send()
|
||||
status = msg.anymail_status
|
||||
self.assertEqual(status.recipients['hardbounce@example.com'].status, 'rejected')
|
||||
self.assertEqual(status.recipients['spam@example.com'].status, 'rejected')
|
||||
|
||||
def test_recipients_invalid(self):
|
||||
self.set_mock_response(
|
||||
status_code=422,
|
||||
raw=b"""{"ErrorCode":300,"Message":"Invalid 'To' address: 'invalid@localhost'."}"""
|
||||
)
|
||||
msg = mail.EmailMessage('Subject', 'Body', 'from@example.com', ['invalid@localhost'])
|
||||
with self.assertRaises(AnymailRecipientsRefused):
|
||||
msg.send()
|
||||
status = msg.anymail_status
|
||||
self.assertEqual(status.recipients['invalid@localhost'].status, 'invalid')
|
||||
|
||||
def test_from_email_invalid(self):
|
||||
# Invalid 'From' address generates same Postmark ErrorCode 300 as invalid 'To',
|
||||
# but should raise a different Anymail error
|
||||
self.set_mock_response(
|
||||
status_code=422,
|
||||
raw=b"""{"ErrorCode":300,"Message":"Invalid 'From' address: 'invalid@localhost'."}"""
|
||||
)
|
||||
msg = mail.EmailMessage('Subject', 'Body', 'invalid@localhost', ['to@example.com'])
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
msg.send()
|
||||
|
||||
def test_fail_silently(self):
|
||||
self.set_mock_response(
|
||||
status_code=422,
|
||||
raw=b'{"ErrorCode":406,'
|
||||
b'"Message":"You tried to send to a recipient that has been marked as inactive.\\n'
|
||||
b'Found inactive addresses: hardbounce@example.com, spam@example.com.\\n'
|
||||
b'Inactive recipients are ones that have generated a hard bounce or a spam complaint."}'
|
||||
)
|
||||
msg = mail.EmailMessage('Subject', 'Body', 'from@example.com',
|
||||
['hardbounce@example.com', 'Hates Spam <spam@example.com>'])
|
||||
msg.send(fail_silently=True)
|
||||
status = msg.anymail_status
|
||||
self.assertEqual(status.recipients['hardbounce@example.com'].status, 'rejected')
|
||||
self.assertEqual(status.recipients['spam@example.com'].status, 'rejected')
|
||||
|
||||
@override_settings(ANYMAIL_IGNORE_RECIPIENT_STATUS=True)
|
||||
def test_ignore_recipient_status(self):
|
||||
self.set_mock_response(
|
||||
status_code=422,
|
||||
raw=b'{"ErrorCode":406,'
|
||||
b'"Message":"You tried to send to a recipient that has been marked as inactive.\\n'
|
||||
b'Found inactive addresses: hardbounce@example.com, spam@example.com.\\n'
|
||||
b'Inactive recipients are ones that have generated a hard bounce or a spam complaint. "}'
|
||||
)
|
||||
msg = mail.EmailMessage('Subject', 'Body', 'from@example.com',
|
||||
['hardbounce@example.com', 'Hates Spam <spam@example.com>'])
|
||||
msg.send()
|
||||
status = msg.anymail_status
|
||||
self.assertEqual(status.recipients['hardbounce@example.com'].status, 'rejected')
|
||||
self.assertEqual(status.recipients['spam@example.com'].status, 'rejected')
|
||||
|
||||
def test_mixed_response(self):
|
||||
"""If *any* recipients are valid or queued, no exception is raised"""
|
||||
self.set_mock_response(
|
||||
status_code=200,
|
||||
raw=b'{"To":"hardbounce@example.com, valid@example.com, Hates Spam <spam@example.com>",'
|
||||
b'"SubmittedAt":"2016-03-12T22:59:06.2505871-05:00",'
|
||||
b'"MessageID":"089dce03-feee-408e-9f0c-ee69bf1c5f35",'
|
||||
b'"ErrorCode":0,'
|
||||
b'"Message":"Message OK, but will not deliver to these inactive addresses:'
|
||||
b' hardbounce@example.com, spam@example.com.'
|
||||
b' Inactive recipients are ones that have generated a hard bounce or a spam complaint."}'
|
||||
)
|
||||
msg = mail.EmailMessage('Subject', 'Body', 'from@example.com',
|
||||
['hardbounce@example.com', 'valid@example.com', 'Hates Spam <spam@example.com>'])
|
||||
sent = msg.send()
|
||||
self.assertEqual(sent, 1) # one message sent, successfully, to 1 of 3 recipients
|
||||
status = msg.anymail_status
|
||||
self.assertEqual(status.recipients['hardbounce@example.com'].status, 'rejected')
|
||||
self.assertEqual(status.recipients['valid@example.com'].status, 'sent')
|
||||
self.assertEqual(status.recipients['spam@example.com'].status, 'rejected')
|
||||
|
||||
|
||||
class PostmarkBackendSessionSharingTestCase(SessionSharingTestCasesMixin, PostmarkBackendMockAPITestCase):
|
||||
"""Requests session sharing tests"""
|
||||
pass # tests are defined in the mixin
|
||||
|
||||
|
||||
@override_settings(ANYMAIL_SEND_DEFAULTS={
|
||||
'tags': ['globaltag'],
|
||||
'track_opens': True,
|
||||
'esp_extra': {'globaloption': 'globalsetting'},
|
||||
})
|
||||
class PostmarkBackendSendDefaultsTests(PostmarkBackendMockAPITestCase):
|
||||
"""Tests backend support for global SEND_DEFAULTS"""
|
||||
|
||||
def test_send_defaults(self):
|
||||
"""Test that global send defaults are applied"""
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
# All these values came from ANYMAIL_SEND_DEFAULTS:
|
||||
self.assertEqual(data['Tag'], 'globaltag')
|
||||
self.assertEqual(data['TrackOpens'], True)
|
||||
self.assertEqual(data['globaloption'], 'globalsetting')
|
||||
|
||||
def test_merge_message_with_send_defaults(self):
|
||||
"""Test that individual message settings are *merged into* the global send defaults"""
|
||||
self.message.tags = None # can't really append (since only one tag), but can suppress it
|
||||
self.message.track_opens = False
|
||||
self.message.esp_extra = {'messageoption': 'messagesetting'}
|
||||
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
# All these values came from ANYMAIL_SEND_DEFAULTS + message.*:
|
||||
self.assertNotIn('Tag', data)
|
||||
self.assertEqual(data['TrackOpens'], False)
|
||||
self.assertEqual(data['globaloption'], 'globalsetting')
|
||||
self.assertEqual(data['messageoption'], 'messagesetting') # additional esp_extra
|
||||
|
||||
@override_settings(ANYMAIL_POSTMARK_SEND_DEFAULTS={
|
||||
'tags': ['esptag'],
|
||||
'track_opens': False,
|
||||
})
|
||||
def test_esp_send_defaults(self):
|
||||
"""Test that ESP-specific send defaults override individual global defaults"""
|
||||
self.message.send()
|
||||
data = self.get_api_call_json()
|
||||
# All these values came from ANYMAIL_SEND_DEFAULTS plus ANYMAIL_SENDGRID_SEND_DEFAULTS:
|
||||
self.assertEqual(data['Tag'], 'esptag') # entire tags overridden
|
||||
self.assertEqual(data['TrackOpens'], False) # esp override
|
||||
self.assertEqual(data['globaloption'], 'globalsetting') # we didn't override the global esp_extra
|
||||
|
||||
|
||||
@override_settings(EMAIL_BACKEND="anymail.backends.postmark.PostmarkBackend")
|
||||
class PostmarkBackendImproperlyConfiguredTests(SimpleTestCase, AnymailTestMixin):
|
||||
"""Test ESP backend without required settings in place"""
|
||||
|
||||
def test_missing_api_key(self):
|
||||
with self.assertRaises(ImproperlyConfigured) as cm:
|
||||
mail.send_mail('Subject', 'Message', 'from@example.com', ['to@example.com'])
|
||||
errmsg = str(cm.exception)
|
||||
self.assertRegex(errmsg, r'\bPOSTMARK_SERVER_TOKEN\b')
|
||||
self.assertRegex(errmsg, r'\bANYMAIL_POSTMARK_SERVER_TOKEN\b')
|
||||
82
tests/test_postmark_integration.py
Normal file
82
tests/test_postmark_integration.py
Normal file
@@ -0,0 +1,82 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.test import SimpleTestCase
|
||||
from django.test.utils import override_settings
|
||||
|
||||
from anymail.exceptions import AnymailAPIError
|
||||
from anymail.message import AnymailMessage
|
||||
|
||||
from .utils import AnymailTestMixin, sample_image_path
|
||||
|
||||
|
||||
@override_settings(ANYMAIL_POSTMARK_SERVER_TOKEN="POSTMARK_API_TEST",
|
||||
EMAIL_BACKEND="anymail.backends.postmark.PostmarkBackend")
|
||||
class PostmarkBackendIntegrationTests(SimpleTestCase, AnymailTestMixin):
|
||||
"""Postmark API integration tests
|
||||
|
||||
These tests run against the **live** Postmark API, but using a
|
||||
test key that's not capable of sending actual email.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
super(PostmarkBackendIntegrationTests, self).setUp()
|
||||
self.message = AnymailMessage('Anymail Postmark integration test', 'Text content',
|
||||
'from@example.com', ['to@example.com'])
|
||||
self.message.attach_alternative('<p>HTML content</p>', "text/html")
|
||||
|
||||
def test_simple_send(self):
|
||||
# Example of getting the SendGrid send status and message id from the message
|
||||
sent_count = self.message.send()
|
||||
self.assertEqual(sent_count, 1)
|
||||
|
||||
anymail_status = self.message.anymail_status
|
||||
sent_status = anymail_status.recipients['to@example.com'].status
|
||||
message_id = anymail_status.recipients['to@example.com'].message_id
|
||||
|
||||
self.assertEqual(sent_status, 'sent')
|
||||
self.assertGreater(len(message_id), 0) # non-empty string
|
||||
self.assertEqual(anymail_status.status, {sent_status}) # set of all recipient statuses
|
||||
self.assertEqual(anymail_status.message_id, message_id)
|
||||
|
||||
def test_all_options(self):
|
||||
message = AnymailMessage(
|
||||
subject="Anymail all-options integration test",
|
||||
body="This is the text body",
|
||||
from_email="Test From <from@example.com>",
|
||||
to=["to1@example.com", "Recipient 2 <to2@example.com>"],
|
||||
cc=["cc1@example.com", "Copy 2 <cc2@example.com>"],
|
||||
bcc=["bcc1@example.com", "Blind Copy 2 <bcc2@example.com>"],
|
||||
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,
|
||||
)
|
||||
message.attach("attachment1.txt", "Here is some\ntext for you", "text/plain")
|
||||
message.attach("attachment2.csv", "ID,Name\n1,Amy Lina", "text/csv")
|
||||
cid = message.attach_inline_image_file(sample_image_path())
|
||||
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")
|
||||
|
||||
message.send()
|
||||
self.assertEqual(message.anymail_status.status, {'sent'})
|
||||
|
||||
def test_invalid_from(self):
|
||||
self.message.from_email = 'webmaster@localhost' # Django's default From
|
||||
with self.assertRaises(AnymailAPIError) as cm:
|
||||
self.message.send()
|
||||
err = cm.exception
|
||||
self.assertEqual(err.status_code, 422)
|
||||
self.assertIn("Invalid 'From' address", str(err))
|
||||
|
||||
@override_settings(ANYMAIL_POSTMARK_SERVER_TOKEN="Hey, that's not a server token!")
|
||||
def test_invalid_server_token(self):
|
||||
with self.assertRaises(AnymailAPIError) as cm:
|
||||
self.message.send()
|
||||
err = cm.exception
|
||||
self.assertEqual(err.status_code, 401)
|
||||
# Make sure the exception message includes Postmark's response:
|
||||
self.assertIn("Bad or missing Server API token", str(err))
|
||||
568
tests/test_sendgrid_backend.py
Normal file
568
tests/test_sendgrid_backend.py
Normal file
@@ -0,0 +1,568 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import json
|
||||
from calendar import timegm
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
from email.mime.base import MIMEBase
|
||||
from email.mime.image import MIMEImage
|
||||
|
||||
from django.core import mail
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.test import SimpleTestCase
|
||||
from django.test.utils import override_settings
|
||||
from django.utils.timezone import get_fixed_timezone, override as override_current_timezone
|
||||
|
||||
from anymail.exceptions import AnymailAPIError, AnymailSerializationError, AnymailUnsupportedFeature
|
||||
from anymail.message import attach_inline_image_file
|
||||
|
||||
from .mock_requests_backend import RequestsBackendMockAPITestCase, SessionSharingTestCasesMixin
|
||||
from .utils import sample_image_content, sample_image_path, SAMPLE_IMAGE_FILENAME, AnymailTestMixin
|
||||
|
||||
|
||||
@override_settings(EMAIL_BACKEND='anymail.backends.sendgrid.SendGridBackend',
|
||||
ANYMAIL={'SENDGRID_API_KEY': 'test_api_key'})
|
||||
class SendGridBackendMockAPITestCase(RequestsBackendMockAPITestCase):
|
||||
DEFAULT_RAW_RESPONSE = b"""{
|
||||
"message": "success"
|
||||
}"""
|
||||
|
||||
def setUp(self):
|
||||
super(SendGridBackendMockAPITestCase, self).setUp()
|
||||
# Simple message useful for many tests
|
||||
self.message = mail.EmailMultiAlternatives('Subject', 'Text Body', 'from@example.com', ['to@example.com'])
|
||||
|
||||
def get_smtpapi(self):
|
||||
"""Returns the x-smtpapi data passed to the mock requests call"""
|
||||
data = self.get_api_call_data()
|
||||
return json.loads(data["x-smtpapi"])
|
||||
|
||||
|
||||
class SendGridBackendStandardEmailTests(SendGridBackendMockAPITestCase):
|
||||
"""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@sender.example.com', ['to@example.com'], fail_silently=False)
|
||||
self.assert_esp_called('/api/mail.send.json')
|
||||
http_headers = self.get_api_call_headers()
|
||||
self.assertEqual(http_headers["Authorization"], "Bearer test_api_key")
|
||||
|
||||
query = self.get_api_call_params(required=False)
|
||||
if query:
|
||||
self.assertNotIn('api_user', query)
|
||||
self.assertNotIn('api_key', query)
|
||||
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['subject'], "Subject here")
|
||||
self.assertEqual(data['text'], "Here is the message.")
|
||||
self.assertEqual(data['from'], "from@sender.example.com")
|
||||
self.assertEqual(data['to'], ["to@example.com"])
|
||||
# make sure backend assigned a Message-ID for event tracking
|
||||
email_headers = json.loads(data['headers'])
|
||||
self.assertRegex(email_headers['Message-ID'], r'\<.+@sender\.example\.com\>') # id uses from_email's domain
|
||||
|
||||
@override_settings(ANYMAIL={'SENDGRID_USERNAME': 'sg_username', 'SENDGRID_PASSWORD': 'sg_password'})
|
||||
def test_user_pass_auth(self):
|
||||
"""Make sure alternative USERNAME/PASSWORD auth works"""
|
||||
mail.send_mail('Subject here', 'Here is the message.',
|
||||
'from@sender.example.com', ['to@example.com'], fail_silently=False)
|
||||
self.assert_esp_called('/api/mail.send.json')
|
||||
query = self.get_api_call_params()
|
||||
self.assertEqual(query['api_user'], 'sg_username')
|
||||
self.assertEqual(query['api_key'], 'sg_password')
|
||||
http_headers = self.get_api_call_headers(required=False)
|
||||
if http_headers:
|
||||
self.assertNotIn('Authorization', http_headers)
|
||||
|
||||
def test_name_addr(self):
|
||||
"""Make sure RFC2822 name-addr format (with display-name) is allowed
|
||||
|
||||
(Test both sender and recipient addresses)
|
||||
"""
|
||||
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'])
|
||||
msg.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['from'], "from@example.com")
|
||||
self.assertEqual(data['fromname'], "From Name")
|
||||
self.assertEqual(data['to'], ['to1@example.com', 'to2@example.com'])
|
||||
self.assertEqual(data['toname'], ['Recipient #1', ' ']) # note space -- SendGrid balks on ''
|
||||
self.assertEqual(data['cc'], ['cc1@example.com', 'cc2@example.com'])
|
||||
self.assertEqual(data['ccname'], ['Carbon Copy', ' '])
|
||||
self.assertEqual(data['bcc'], ['bcc1@example.com', 'bcc2@example.com'])
|
||||
self.assertEqual(data['bccname'], ['Blind Copy', ' '])
|
||||
|
||||
def test_email_message(self):
|
||||
email = mail.EmailMessage(
|
||||
'Subject', 'Body goes here', 'from@example.com',
|
||||
['to1@example.com', 'Also To <to2@example.com>'],
|
||||
bcc=['bcc1@example.com', 'Also BCC <bcc2@example.com>'],
|
||||
cc=['cc1@example.com', 'Also CC <cc2@example.com>'],
|
||||
headers={'Reply-To': 'another@example.com',
|
||||
'X-MyHeader': 'my value',
|
||||
'Message-ID': 'mycustommsgid@sales.example.com'}) # should override backend msgid
|
||||
email.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['subject'], "Subject")
|
||||
self.assertEqual(data['text'], "Body goes here")
|
||||
self.assertEqual(data['from'], "from@example.com")
|
||||
self.assertEqual(data['to'], ['to1@example.com', 'to2@example.com'])
|
||||
self.assertEqual(data['toname'], [' ', 'Also To'])
|
||||
self.assertEqual(data['bcc'], ['bcc1@example.com', 'bcc2@example.com'])
|
||||
self.assertEqual(data['bccname'], [' ', 'Also BCC'])
|
||||
self.assertEqual(data['cc'], ['cc1@example.com', 'cc2@example.com'])
|
||||
self.assertEqual(data['ccname'], [' ', 'Also CC'])
|
||||
self.assertJSONEqual(data['headers'], {
|
||||
'Message-ID': 'mycustommsgid@sales.example.com',
|
||||
'Reply-To': 'another@example.com',
|
||||
'X-MyHeader': 'my value',
|
||||
})
|
||||
|
||||
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'])
|
||||
email.attach_alternative(html_content, "text/html")
|
||||
email.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['text'], text_content)
|
||||
self.assertEqual(data['html'], html_content)
|
||||
# Don't accidentally send the html part as an attachment:
|
||||
files = self.get_api_call_files(required=False)
|
||||
self.assertIsNone(files)
|
||||
|
||||
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'])
|
||||
email.content_subtype = "html" # Main content is now text/html
|
||||
email.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertNotIn('text', data)
|
||||
self.assertEqual(data['html'], html_content)
|
||||
|
||||
def test_extra_headers(self):
|
||||
self.message.extra_headers = {'X-Custom': 'string', 'X-Num': 123}
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
headers = json.loads(data['headers'])
|
||||
self.assertEqual(headers['X-Custom'], 'string')
|
||||
self.assertEqual(headers['X-Num'], '123') # number converted to string (per SendGrid requirement)
|
||||
|
||||
def test_extra_headers_serialization_error(self):
|
||||
self.message.extra_headers = {'X-Custom': Decimal(12.5)}
|
||||
with self.assertRaisesMessage(AnymailSerializationError, "Decimal('12.5')"):
|
||||
self.message.send()
|
||||
|
||||
def test_reply_to(self):
|
||||
# reply_to is new in Django 1.8 -- before that, you can simply include it in headers
|
||||
try:
|
||||
# noinspection PyArgumentList
|
||||
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'})
|
||||
except TypeError:
|
||||
# Pre-Django 1.8
|
||||
return self.skipTest("Django version doesn't support EmailMessage(reply_to)")
|
||||
email.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertNotIn('replyto', data) # don't use SendGrid's replyto (it's broken); just use headers
|
||||
headers = json.loads(data['headers'])
|
||||
self.assertEqual(headers['Reply-To'], 'reply@example.com, Other <reply2@example.com>')
|
||||
self.assertEqual(headers['X-Other'], 'Keep') # don't lose other headers
|
||||
|
||||
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")
|
||||
|
||||
# Should guess mimetype if not provided...
|
||||
png_content = b"PNG\xb4 pretend this is the contents of a png file"
|
||||
self.message.attach(filename="test.png", content=png_content)
|
||||
|
||||
# Should work with a MIMEBase object (also tests no filename)...
|
||||
pdf_content = b"PDF\xb4 pretend this is valid pdf data"
|
||||
mimeattachment = MIMEBase('application', 'pdf')
|
||||
mimeattachment.set_payload(pdf_content)
|
||||
self.message.attach(mimeattachment)
|
||||
|
||||
self.message.send()
|
||||
files = self.get_api_call_files()
|
||||
self.assertEqual(files, {
|
||||
'files[test.txt]': ('test.txt', text_content, 'text/plain'),
|
||||
'files[test.png]': ('test.png', png_content, 'image/png'), # type inferred from filename
|
||||
'files[]': ('', pdf_content, 'application/pdf'), # no filename
|
||||
})
|
||||
|
||||
def test_attachment_name_conflicts(self):
|
||||
# It's not clear how to (or whether) supply multiple attachments with
|
||||
# the same name to SendGrid's API. Anymail treats this case as unsupported.
|
||||
self.message.attach('foo.txt', 'content', 'text/plain')
|
||||
self.message.attach('bar.txt', 'content', 'text/plain')
|
||||
self.message.attach('foo.txt', 'different content', 'text/plain')
|
||||
with self.assertRaisesMessage(AnymailUnsupportedFeature,
|
||||
"multiple attachments with the same filename") as cm:
|
||||
self.message.send()
|
||||
self.assertIn('foo.txt', str(cm.exception)) # say which filename
|
||||
|
||||
def test_unnamed_attachment_conflicts(self):
|
||||
# Same as previous test, but with None/empty filenames
|
||||
self.message.attach(None, 'content', 'text/plain')
|
||||
self.message.attach('', 'different content', 'text/plain')
|
||||
with self.assertRaisesMessage(AnymailUnsupportedFeature, "multiple unnamed attachments"):
|
||||
self.message.send()
|
||||
|
||||
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.send()
|
||||
files = self.get_api_call_files()
|
||||
self.assertEqual(files['files[Une pièce jointe.html]'],
|
||||
('Une pièce jointe.html', '<p>\u2019</p>', 'text/html'))
|
||||
|
||||
def test_embedded_images(self):
|
||||
image_filename = SAMPLE_IMAGE_FILENAME
|
||||
image_path = sample_image_path(image_filename)
|
||||
image_data = sample_image_content(image_filename)
|
||||
|
||||
cid = attach_inline_image_file(self.message, image_path) # Read from a png file
|
||||
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()
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['html'], html_content)
|
||||
|
||||
files = self.get_api_call_files()
|
||||
self.assertEqual(files, {
|
||||
'files[%s]' % image_filename: (image_filename, image_data, "image/png"),
|
||||
})
|
||||
self.assertEqual(data['content[%s]' % image_filename], cid)
|
||||
|
||||
def test_attached_images(self):
|
||||
image_filename = SAMPLE_IMAGE_FILENAME
|
||||
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
|
||||
|
||||
image = MIMEImage(image_data) # option 2: construct the MIMEImage and attach it directly
|
||||
self.message.attach(image)
|
||||
|
||||
self.message.send()
|
||||
files = self.get_api_call_files()
|
||||
self.assertEqual(files, {
|
||||
'files[%s]' % image_filename: (image_filename, image_data, "image/png"), # the named one
|
||||
'files[]': ('', image_data, "image/png"), # the unnamed one
|
||||
})
|
||||
|
||||
def test_multiple_html_alternatives(self):
|
||||
# Multiple alternatives not allowed
|
||||
self.message.attach_alternative("<p>First html is OK</p>", "text/html")
|
||||
self.message.attach_alternative("<p>But not second html</p>", "text/html")
|
||||
with self.assertRaises(AnymailUnsupportedFeature):
|
||||
self.message.send()
|
||||
|
||||
def test_html_alternative(self):
|
||||
# Only html alternatives allowed
|
||||
self.message.attach_alternative("{'not': 'allowed'}", "application/json")
|
||||
with self.assertRaises(AnymailUnsupportedFeature):
|
||||
self.message.send()
|
||||
|
||||
def test_alternatives_fail_silently(self):
|
||||
# Make sure fail_silently is respected
|
||||
self.message.attach_alternative("{'not': 'allowed'}", "application/json")
|
||||
sent = self.message.send(fail_silently=True)
|
||||
self.assert_esp_not_called("API should not be called when send fails silently")
|
||||
self.assertEqual(sent, 0)
|
||||
|
||||
def test_suppress_empty_address_lists(self):
|
||||
"""Empty to, cc, bcc, and reply_to shouldn't generate empty headers"""
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertNotIn('cc', data)
|
||||
self.assertNotIn('ccname', data)
|
||||
self.assertNotIn('bcc', data)
|
||||
self.assertNotIn('bccname', data)
|
||||
headers = json.loads(data['headers'])
|
||||
self.assertNotIn('Reply-To', headers)
|
||||
|
||||
# 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_data()
|
||||
self.assertNotIn('to', data)
|
||||
self.assertNotIn('toname', data)
|
||||
|
||||
def test_api_failure(self):
|
||||
self.set_mock_response(status_code=400)
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
sent = mail.send_mail('Subject', 'Body', 'from@example.com', ['to@example.com'])
|
||||
self.assertEqual(sent, 0)
|
||||
|
||||
# Make sure fail_silently is respected
|
||||
self.set_mock_response(status_code=400)
|
||||
sent = mail.send_mail('Subject', 'Body', 'from@example.com', ['to@example.com'], fail_silently=True)
|
||||
self.assertEqual(sent, 0)
|
||||
|
||||
def test_api_error_includes_details(self):
|
||||
"""AnymailAPIError should include ESP's error message"""
|
||||
# JSON error response:
|
||||
error_response = b"""{
|
||||
"message": "error",
|
||||
"errors": [
|
||||
"Helpful explanation from SendGrid",
|
||||
"and more"
|
||||
]
|
||||
}"""
|
||||
self.set_mock_response(status_code=200, raw=error_response)
|
||||
with self.assertRaisesRegex(AnymailAPIError,
|
||||
r"\bHelpful explanation from SendGrid\b.*and more\b"):
|
||||
self.message.send()
|
||||
|
||||
# Non-JSON error response:
|
||||
self.set_mock_response(status_code=500, raw=b"Ack! Bad proxy!")
|
||||
with self.assertRaisesMessage(AnymailAPIError, "Ack! Bad proxy!"):
|
||||
self.message.send()
|
||||
|
||||
# No content in the error response:
|
||||
self.set_mock_response(status_code=502, raw=None)
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
self.message.send()
|
||||
|
||||
|
||||
class SendGridBackendAnymailFeatureTests(SendGridBackendMockAPITestCase):
|
||||
"""Test backend support for Anymail added features"""
|
||||
|
||||
def test_metadata(self):
|
||||
# Note: SendGrid doesn't handle complex types in metadata
|
||||
self.message.metadata = {'user_id': "12345", 'items': 6}
|
||||
self.message.send()
|
||||
smtpapi = self.get_smtpapi()
|
||||
self.assertEqual(smtpapi['unique_args'], {'user_id': "12345", 'items': 6})
|
||||
|
||||
def test_send_at(self):
|
||||
utc_plus_6 = get_fixed_timezone(6 * 60)
|
||||
utc_minus_8 = get_fixed_timezone(-8 * 60)
|
||||
|
||||
with override_current_timezone(utc_plus_6):
|
||||
# Timezone-aware datetime converted to UTC:
|
||||
self.message.send_at = datetime(2016, 3, 4, 5, 6, 7, tzinfo=utc_minus_8)
|
||||
self.message.send()
|
||||
smtpapi = self.get_smtpapi()
|
||||
self.assertEqual(smtpapi['send_at'], timegm((2016, 3, 4, 13, 6, 7))) # 05:06 UTC-8 == 13:06 UTC
|
||||
|
||||
# Timezone-naive datetime assumed to be Django current_timezone
|
||||
self.message.send_at = datetime(2022, 10, 11, 12, 13, 14, 567) # microseconds should get stripped
|
||||
self.message.send()
|
||||
smtpapi = self.get_smtpapi()
|
||||
self.assertEqual(smtpapi['send_at'], timegm((2022, 10, 11, 6, 13, 14))) # 12:13 UTC+6 == 06:13 UTC
|
||||
|
||||
# Date-only treated as midnight in current timezone
|
||||
self.message.send_at = date(2022, 10, 22)
|
||||
self.message.send()
|
||||
smtpapi = self.get_smtpapi()
|
||||
self.assertEqual(smtpapi['send_at'], timegm((2022, 10, 21, 18, 0, 0))) # 00:00 UTC+6 == 18:00-1d UTC
|
||||
|
||||
# POSIX timestamp
|
||||
self.message.send_at = 1651820889 # 2022-05-06 07:08:09 UTC
|
||||
self.message.send()
|
||||
smtpapi = self.get_smtpapi()
|
||||
self.assertEqual(smtpapi['send_at'], 1651820889)
|
||||
|
||||
def test_tags(self):
|
||||
self.message.tags = ["receipt", "repeat-user"]
|
||||
self.message.send()
|
||||
smtpapi = self.get_smtpapi()
|
||||
self.assertCountEqual(smtpapi['category'], ["receipt", "repeat-user"])
|
||||
|
||||
def test_tracking(self):
|
||||
# Test one way...
|
||||
self.message.track_clicks = False
|
||||
self.message.track_opens = True
|
||||
self.message.send()
|
||||
smtpapi = self.get_smtpapi()
|
||||
self.assertEqual(smtpapi['filters']['clicktrack'], {'settings': {'enable': 0}})
|
||||
self.assertEqual(smtpapi['filters']['opentrack'], {'settings': {'enable': 1}})
|
||||
|
||||
# ...and the opposite way
|
||||
self.message.track_clicks = True
|
||||
self.message.track_opens = False
|
||||
self.message.send()
|
||||
smtpapi = self.get_smtpapi()
|
||||
self.assertEqual(smtpapi['filters']['clicktrack'], {'settings': {'enable': 1}})
|
||||
self.assertEqual(smtpapi['filters']['opentrack'], {'settings': {'enable': 0}})
|
||||
|
||||
def test_default_omits_options(self):
|
||||
"""Make sure by default we don't send any ESP-specific options.
|
||||
|
||||
Options not specified by the caller should be omitted entirely from
|
||||
the API call (*not* sent as False or empty). This ensures
|
||||
that your ESP account settings apply by default.
|
||||
"""
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
self.assertNotIn('x-smtpapi', data)
|
||||
|
||||
def test_esp_extra(self):
|
||||
self.message.tags = ["tag"]
|
||||
self.message.esp_extra = {
|
||||
'x-smtpapi': {'asm_group_id': 1},
|
||||
'newthing': "some param not supported by Anymail",
|
||||
}
|
||||
self.message.send()
|
||||
# Additional send params:
|
||||
data = self.get_api_call_data()
|
||||
self.assertEqual(data['newthing'], "some param not supported by Anymail")
|
||||
# Should merge x-smtpapi
|
||||
smtpapi = self.get_smtpapi()
|
||||
self.assertEqual(smtpapi['category'], ["tag"])
|
||||
self.assertEqual(smtpapi['asm_group_id'], 1)
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
def test_send_attaches_anymail_status(self):
|
||||
""" The anymail_status should be attached to the message when it is sent """
|
||||
# the DEFAULT_RAW_RESPONSE above is the *only* success response SendGrid returns,
|
||||
# so no need to override it here
|
||||
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.assertRegex(msg.anymail_status.message_id, r'\<.+@example\.com\>') # don't know exactly what it'll be
|
||||
self.assertEqual(msg.anymail_status.recipients['to1@example.com'].status, 'queued')
|
||||
self.assertEqual(msg.anymail_status.recipients['to1@example.com'].message_id,
|
||||
msg.anymail_status.message_id)
|
||||
self.assertEqual(msg.anymail_status.esp_response.content, self.DEFAULT_RAW_RESPONSE)
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
def test_send_failed_anymail_status(self):
|
||||
""" If the send fails, anymail_status should contain initial values"""
|
||||
self.set_mock_response(status_code=500)
|
||||
sent = self.message.send(fail_silently=True)
|
||||
self.assertEqual(sent, 0)
|
||||
self.assertIsNone(self.message.anymail_status.status)
|
||||
self.assertIsNone(self.message.anymail_status.message_id)
|
||||
self.assertEqual(self.message.anymail_status.recipients, {})
|
||||
self.assertIsNone(self.message.anymail_status.esp_response)
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
def test_send_unparsable_response(self):
|
||||
"""If the send succeeds, but a non-JSON API response, should raise an API exception"""
|
||||
mock_response = self.set_mock_response(status_code=200,
|
||||
raw=b"yikes, this isn't a real response")
|
||||
with self.assertRaises(AnymailAPIError):
|
||||
self.message.send()
|
||||
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, mock_response)
|
||||
|
||||
def test_json_serialization_errors(self):
|
||||
"""Try to provide more information about non-json-serializable data"""
|
||||
self.message.metadata = {'total': Decimal('19.99')}
|
||||
with self.assertRaises(AnymailSerializationError) as cm:
|
||||
self.message.send()
|
||||
print(self.get_api_call_data())
|
||||
err = cm.exception
|
||||
self.assertIsInstance(err, TypeError) # compatibility with json.dumps
|
||||
self.assertIn("Don't know how to send this data to SendGrid", str(err)) # our added context
|
||||
self.assertIn("Decimal('19.99') is not JSON serializable", str(err)) # original message
|
||||
|
||||
|
||||
class SendGridBackendRecipientsRefusedTests(SendGridBackendMockAPITestCase):
|
||||
"""Should raise AnymailRecipientsRefused when *all* recipients are rejected or invalid"""
|
||||
|
||||
# SendGrid doesn't check email bounce or complaint lists at time of send --
|
||||
# it always just queues the message. You'll need to listen for the "rejected"
|
||||
# and "failed" events to detect refused recipients.
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class SendGridBackendSessionSharingTestCase(SessionSharingTestCasesMixin, SendGridBackendMockAPITestCase):
|
||||
"""Requests session sharing tests"""
|
||||
pass # tests are defined in the mixin
|
||||
|
||||
|
||||
@override_settings(ANYMAIL_SEND_DEFAULTS={
|
||||
'metadata': {'global': 'globalvalue', 'other': 'othervalue'},
|
||||
'tags': ['globaltag'],
|
||||
'track_clicks': True,
|
||||
'track_opens': True,
|
||||
'esp_extra': {'globaloption': 'globalsetting'},
|
||||
})
|
||||
class SendGridBackendSendDefaultsTests(SendGridBackendMockAPITestCase):
|
||||
"""Tests backend support for global SEND_DEFAULTS"""
|
||||
|
||||
def test_send_defaults(self):
|
||||
"""Test that global send defaults are applied"""
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
smtpapi = self.get_smtpapi()
|
||||
# All these values came from ANYMAIL_SEND_DEFAULTS:
|
||||
self.assertEqual(smtpapi['unique_args'], {'global': 'globalvalue', 'other': 'othervalue'})
|
||||
self.assertEqual(smtpapi['category'], ['globaltag'])
|
||||
self.assertEqual(smtpapi['filters']['clicktrack']['settings']['enable'], 1)
|
||||
self.assertEqual(smtpapi['filters']['opentrack']['settings']['enable'], 1)
|
||||
self.assertEqual(data['globaloption'], 'globalsetting')
|
||||
|
||||
def test_merge_message_with_send_defaults(self):
|
||||
"""Test that individual message settings are *merged into* the global send defaults"""
|
||||
self.message.metadata = {'message': 'messagevalue', 'other': 'override'}
|
||||
self.message.tags = ['messagetag']
|
||||
self.message.track_clicks = False
|
||||
self.message.esp_extra = {'messageoption': 'messagesetting'}
|
||||
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
smtpapi = self.get_smtpapi()
|
||||
# All these values came from ANYMAIL_SEND_DEFAULTS + message.*:
|
||||
self.assertEqual(smtpapi['unique_args'], {
|
||||
'global': 'globalvalue',
|
||||
'message': 'messagevalue', # additional metadata
|
||||
'other': 'override', # override global value
|
||||
})
|
||||
self.assertCountEqual(smtpapi['category'], ['globaltag', 'messagetag']) # tags concatenated
|
||||
self.assertEqual(smtpapi['filters']['clicktrack']['settings']['enable'], 0) # message overrides
|
||||
self.assertEqual(smtpapi['filters']['opentrack']['settings']['enable'], 1)
|
||||
self.assertEqual(data['globaloption'], 'globalsetting')
|
||||
self.assertEqual(data['messageoption'], 'messagesetting') # additional esp_extra
|
||||
|
||||
@override_settings(ANYMAIL_SENDGRID_SEND_DEFAULTS={
|
||||
'tags': ['esptag'],
|
||||
'metadata': {'esp': 'espvalue'},
|
||||
'track_opens': False,
|
||||
})
|
||||
def test_esp_send_defaults(self):
|
||||
"""Test that ESP-specific send defaults override individual global defaults"""
|
||||
self.message.send()
|
||||
data = self.get_api_call_data()
|
||||
smtpapi = self.get_smtpapi()
|
||||
# All these values came from ANYMAIL_SEND_DEFAULTS plus ANYMAIL_SENDGRID_SEND_DEFAULTS:
|
||||
self.assertEqual(smtpapi['unique_args'], {'esp': 'espvalue'}) # entire metadata overridden
|
||||
self.assertCountEqual(smtpapi['category'], ['esptag']) # entire tags overridden
|
||||
self.assertEqual(smtpapi['filters']['clicktrack']['settings']['enable'], 1) # no override
|
||||
self.assertEqual(smtpapi['filters']['opentrack']['settings']['enable'], 0) # esp override
|
||||
self.assertEqual(data['globaloption'], 'globalsetting') # we didn't override the global esp_extra
|
||||
|
||||
|
||||
@override_settings(EMAIL_BACKEND="anymail.backends.sendgrid.SendGridBackend")
|
||||
class SendGridBackendImproperlyConfiguredTests(SimpleTestCase, AnymailTestMixin):
|
||||
"""Test ESP backend without required settings in place"""
|
||||
|
||||
def test_missing_auth(self):
|
||||
with self.assertRaises(ImproperlyConfigured) as cm:
|
||||
mail.send_mail('Subject', 'Message', 'from@example.com', ['to@example.com'])
|
||||
errmsg = str(cm.exception)
|
||||
# Make sure the exception mentions all the auth keys:
|
||||
self.assertRegex(errmsg, r'\bSENDGRID_API_KEY\b')
|
||||
self.assertRegex(errmsg, r'\bSENDGRID_USERNAME\b')
|
||||
self.assertRegex(errmsg, r'\bSENDGRID_PASSWORD\b')
|
||||
127
tests/test_sendgrid_integration.py
Normal file
127
tests/test_sendgrid_integration.py
Normal file
@@ -0,0 +1,127 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import os
|
||||
import unittest
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from django.core.mail import send_mail
|
||||
from django.test import SimpleTestCase
|
||||
from django.test.utils import override_settings
|
||||
|
||||
from anymail.exceptions import AnymailAPIError
|
||||
from anymail.message import AnymailMessage
|
||||
|
||||
from .utils import AnymailTestMixin, sample_image_path
|
||||
|
||||
# For API_KEY auth tests:
|
||||
SENDGRID_TEST_API_KEY = os.getenv('SENDGRID_TEST_API_KEY')
|
||||
|
||||
# For USERNAME/PASSWORD auth tests:
|
||||
SENDGRID_TEST_USERNAME = os.getenv('SENDGRID_TEST_USERNAME')
|
||||
SENDGRID_TEST_PASSWORD = os.getenv('SENDGRID_TEST_PASSWORD')
|
||||
|
||||
|
||||
@unittest.skipUnless(SENDGRID_TEST_API_KEY,
|
||||
"Set SENDGRID_TEST_API_KEY environment variable "
|
||||
"to run SendGrid integration tests")
|
||||
@override_settings(ANYMAIL_SENDGRID_API_KEY=SENDGRID_TEST_API_KEY,
|
||||
EMAIL_BACKEND="anymail.backends.sendgrid.SendGridBackend")
|
||||
class SendGridBackendIntegrationTests(SimpleTestCase, AnymailTestMixin):
|
||||
"""SendGrid API integration tests
|
||||
|
||||
These tests run against the **live** SendGrid API, using the
|
||||
environment variable `SENDGRID_TEST_API_KEY` as the API key
|
||||
If those variables are not set, these tests won't run.
|
||||
|
||||
SendGrid doesn't offer a test mode -- it tries to send everything
|
||||
you ask. To avoid stacking up a pile of undeliverable @example.com
|
||||
emails, the tests use SendGrid's "sink domain" @sink.sendgrid.net.
|
||||
https://support.sendgrid.com/hc/en-us/articles/201995663-Safely-Test-Your-Sending-Speed
|
||||
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
super(SendGridBackendIntegrationTests, self).setUp()
|
||||
self.message = AnymailMessage('Anymail SendGrid integration test', 'Text content',
|
||||
'from@example.com', ['to@sink.sendgrid.net'])
|
||||
self.message.attach_alternative('<p>HTML content</p>', "text/html")
|
||||
|
||||
def test_simple_send(self):
|
||||
# Example of getting the SendGrid send status and message id from the message
|
||||
sent_count = self.message.send()
|
||||
self.assertEqual(sent_count, 1)
|
||||
|
||||
anymail_status = self.message.anymail_status
|
||||
sent_status = anymail_status.recipients['to@sink.sendgrid.net'].status
|
||||
message_id = anymail_status.recipients['to@sink.sendgrid.net'].message_id
|
||||
|
||||
self.assertEqual(sent_status, 'queued') # SendGrid always queues
|
||||
self.assertRegex(message_id, r'\<.+@example\.com\>') # should use from_email's domain
|
||||
self.assertEqual(anymail_status.status, {sent_status}) # set of all recipient statuses
|
||||
self.assertEqual(anymail_status.message_id, message_id)
|
||||
|
||||
def test_all_options(self):
|
||||
send_at = datetime.now().replace(microsecond=0) + timedelta(minutes=2)
|
||||
message = AnymailMessage(
|
||||
subject="Anymail all-options integration test FILES",
|
||||
body="This is the text body",
|
||||
from_email="Test From <from@example.com>",
|
||||
to=["to1@sink.sendgrid.net", "Recipient 2 <to2@sink.sendgrid.net>"],
|
||||
cc=["cc1@sink.sendgrid.net", "Copy 2 <cc2@sink.sendgrid.net>"],
|
||||
bcc=["bcc1@sink.sendgrid.net", "Blind Copy 2 <bcc2@sink.sendgrid.net>"],
|
||||
reply_to=["reply1@example.com", "Reply 2 <reply2@example.com>"],
|
||||
headers={"X-Anymail-Test": "value"},
|
||||
|
||||
metadata={"meta1": "simple string", "meta2": 2},
|
||||
send_at=send_at,
|
||||
tags=["tag 1", "tag 2"],
|
||||
track_clicks=True,
|
||||
track_opens=True,
|
||||
)
|
||||
message.attach("attachment1.txt", "Here is some\ntext for you", "text/plain")
|
||||
message.attach("attachment2.csv", "ID,Name\n1,Amy Lina", "text/csv")
|
||||
cid = message.attach_inline_image_file(sample_image_path())
|
||||
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")
|
||||
|
||||
message.send()
|
||||
self.assertEqual(message.anymail_status.status, {'queued'}) # SendGrid always queues
|
||||
|
||||
@override_settings(ANYMAIL_SENDGRID_API_KEY="Hey, that's not an API key!")
|
||||
def test_invalid_api_key(self):
|
||||
with self.assertRaises(AnymailAPIError) as cm:
|
||||
self.message.send()
|
||||
err = cm.exception
|
||||
self.assertEqual(err.status_code, 400)
|
||||
# Make sure the exception message includes SendGrid's response:
|
||||
self.assertIn("authorization grant is invalid", str(err))
|
||||
|
||||
|
||||
@unittest.skipUnless(SENDGRID_TEST_USERNAME and SENDGRID_TEST_PASSWORD,
|
||||
"Set SENDGRID_TEST_USERNAME and SENDGRID_TEST_PASSWORD"
|
||||
"environment variables to run SendGrid integration tests")
|
||||
@override_settings(ANYMAIL_SENDGRID_USERNAME=SENDGRID_TEST_USERNAME,
|
||||
ANYMAIL_SENDGRID_PASSWORD=SENDGRID_TEST_PASSWORD,
|
||||
EMAIL_BACKEND="anymail.backends.sendgrid.SendGridBackend")
|
||||
class SendGridBackendUserPassIntegrationTests(SimpleTestCase, AnymailTestMixin):
|
||||
"""SendGrid username/password API integration tests
|
||||
|
||||
(See notes above for the API-key tests)
|
||||
"""
|
||||
|
||||
def test_valid_auth(self):
|
||||
sent_count = send_mail('Anymail SendGrid username/password integration test',
|
||||
'Text content', 'from@example.com', ['to@sink.sendgrid.net'])
|
||||
self.assertEqual(sent_count, 1)
|
||||
|
||||
@override_settings(ANYMAIL_SENDGRID_PASSWORD="Hey, this isn't the password!")
|
||||
def test_invalid_auth(self):
|
||||
with self.assertRaises(AnymailAPIError) as cm:
|
||||
send_mail('Anymail SendGrid username/password integration test',
|
||||
'Text content', 'from@example.com', ['to@sink.sendgrid.net'])
|
||||
err = cm.exception
|
||||
self.assertEqual(err.status_code, 400)
|
||||
# Make sure the exception message includes SendGrid's response:
|
||||
self.assertIn("Bad username / password", str(err))
|
||||
47
tests/utils.py
Normal file
47
tests/utils.py
Normal file
@@ -0,0 +1,47 @@
|
||||
# Anymail test utils
|
||||
import os
|
||||
import unittest
|
||||
from base64 import b64decode
|
||||
|
||||
import six
|
||||
|
||||
|
||||
def decode_att(att):
|
||||
"""Returns the original data from base64-encoded attachment content"""
|
||||
return b64decode(att.encode('ascii'))
|
||||
|
||||
|
||||
SAMPLE_IMAGE_FILENAME = "sample_image.png"
|
||||
|
||||
|
||||
def sample_image_path(filename=SAMPLE_IMAGE_FILENAME):
|
||||
"""Returns path to an actual image file in the tests directory"""
|
||||
test_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
path = os.path.join(test_dir, filename)
|
||||
return path
|
||||
|
||||
|
||||
def sample_image_content(filename=SAMPLE_IMAGE_FILENAME):
|
||||
"""Returns contents of an actual image file from the tests directory"""
|
||||
filename = sample_image_path(filename)
|
||||
with open(filename, "rb") as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
class AnymailTestMixin:
|
||||
"""Helpful additional methods for Anymail tests"""
|
||||
|
||||
pass
|
||||
# Plus these methods added below:
|
||||
# assertCountEqual
|
||||
# assertRaisesRegex
|
||||
# assertRegex
|
||||
|
||||
# Add the Python 3 TestCase assertions, if they're not already there.
|
||||
# (The six implementations cause infinite recursion if installed on
|
||||
# a py3 TestCase.)
|
||||
for method in ('assertCountEqual', 'assertRaisesRegex', 'assertRegex'):
|
||||
try:
|
||||
getattr(unittest.TestCase, method)
|
||||
except AttributeError:
|
||||
setattr(AnymailTestMixin, method, getattr(six, method))
|
||||
Reference in New Issue
Block a user