mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 20:01:05 -05:00
Tests: add ability to check which Mandrill API endpoint was used.
Add DjrillBackendMockAPITestCase.assert_mandrill_called; use it in representative backend test cases. (Also make get_api_call_data work with various ways of calling requests.post.)
This commit is contained in:
@@ -14,7 +14,7 @@ class DjrillBackendMockAPITestCase(TestCase):
|
|||||||
self.content = content
|
self.content = content
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.patch = patch('requests.post')
|
self.patch = patch('requests.post', autospec=True)
|
||||||
self.mock_post = self.patch.start()
|
self.mock_post = self.patch.start()
|
||||||
self.mock_post.return_value = self.MockResponse()
|
self.mock_post.return_value = self.MockResponse()
|
||||||
|
|
||||||
@@ -28,6 +28,25 @@ class DjrillBackendMockAPITestCase(TestCase):
|
|||||||
self.patch.stop()
|
self.patch.stop()
|
||||||
settings.EMAIL_BACKEND = self.original_email_backend
|
settings.EMAIL_BACKEND = self.original_email_backend
|
||||||
|
|
||||||
|
def assert_mandrill_called(self, endpoint):
|
||||||
|
"""Verifies the (mock) Mandrill API was called on endpoint.
|
||||||
|
|
||||||
|
endpoint is a Mandrill API, e.g., "/messages/send.json"
|
||||||
|
"""
|
||||||
|
# This assumes the last (or only) call to requests.post is the
|
||||||
|
# Mandrill API call of interest.
|
||||||
|
if self.mock_post.call_args is None:
|
||||||
|
raise AssertionError("Mandrill API was not called")
|
||||||
|
(args, kwargs) = self.mock_post.call_args
|
||||||
|
try:
|
||||||
|
post_url = kwargs.get('url', None) or args[0]
|
||||||
|
except IndexError:
|
||||||
|
raise AssertionError("requests.post was called without an url (?!)")
|
||||||
|
if not post_url.endswith(endpoint):
|
||||||
|
raise AssertionError(
|
||||||
|
"requests.post was not called on %s\n(It was called on %s)"
|
||||||
|
% (endpoint, post_url))
|
||||||
|
|
||||||
def get_api_call_data(self):
|
def get_api_call_data(self):
|
||||||
"""Returns the data posted to the Mandrill API.
|
"""Returns the data posted to the Mandrill API.
|
||||||
|
|
||||||
@@ -36,9 +55,10 @@ class DjrillBackendMockAPITestCase(TestCase):
|
|||||||
if self.mock_post.call_args is None:
|
if self.mock_post.call_args is None:
|
||||||
raise AssertionError("Mandrill API was not called")
|
raise AssertionError("Mandrill API was not called")
|
||||||
(args, kwargs) = self.mock_post.call_args
|
(args, kwargs) = self.mock_post.call_args
|
||||||
if 'data' not in kwargs:
|
try:
|
||||||
raise AssertionError("requests.post was called without data kwarg "
|
post_data = kwargs.get('data', None) or args[1]
|
||||||
"-- Maybe tests need to be updated for backend changes?")
|
except IndexError:
|
||||||
return json.loads(kwargs['data'])
|
raise AssertionError("requests.post was called without data")
|
||||||
|
return json.loads(post_data)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ class DjrillBackendTests(DjrillBackendMockAPITestCase):
|
|||||||
def test_send_mail(self):
|
def test_send_mail(self):
|
||||||
mail.send_mail('Subject here', 'Here is the message.',
|
mail.send_mail('Subject here', 'Here is the message.',
|
||||||
'from@example.com', ['to@example.com'], fail_silently=False)
|
'from@example.com', ['to@example.com'], fail_silently=False)
|
||||||
|
self.assert_mandrill_called("/messages/send.json")
|
||||||
data = self.get_api_call_data()
|
data = self.get_api_call_data()
|
||||||
self.assertEqual(data['message']['subject'], "Subject here")
|
self.assertEqual(data['message']['subject'], "Subject here")
|
||||||
self.assertEqual(data['message']['text'], "Here is the message.")
|
self.assertEqual(data['message']['text'], "Here is the message.")
|
||||||
@@ -54,6 +55,7 @@ class DjrillBackendTests(DjrillBackendMockAPITestCase):
|
|||||||
headers={'Reply-To': 'another@example.com',
|
headers={'Reply-To': 'another@example.com',
|
||||||
'X-MyHeader': 'my value'})
|
'X-MyHeader': 'my value'})
|
||||||
email.send()
|
email.send()
|
||||||
|
self.assert_mandrill_called("/messages/send.json")
|
||||||
data = self.get_api_call_data()
|
data = self.get_api_call_data()
|
||||||
self.assertEqual(data['message']['subject'], "Subject")
|
self.assertEqual(data['message']['subject'], "Subject")
|
||||||
self.assertEqual(data['message']['text'], "Body goes here")
|
self.assertEqual(data['message']['text'], "Body goes here")
|
||||||
@@ -78,6 +80,7 @@ class DjrillBackendTests(DjrillBackendMockAPITestCase):
|
|||||||
'from@example.com', ['to@example.com'])
|
'from@example.com', ['to@example.com'])
|
||||||
email.attach_alternative(html_content, "text/html")
|
email.attach_alternative(html_content, "text/html")
|
||||||
email.send()
|
email.send()
|
||||||
|
self.assert_mandrill_called("/messages/send.json")
|
||||||
data = self.get_api_call_data()
|
data = self.get_api_call_data()
|
||||||
self.assertEqual(data['message']['text'], text_content)
|
self.assertEqual(data['message']['text'], text_content)
|
||||||
self.assertEqual(data['message']['html'], html_content)
|
self.assertEqual(data['message']['html'], html_content)
|
||||||
@@ -271,6 +274,7 @@ class DjrillMandrillFeatureTests(DjrillBackendMockAPITestCase):
|
|||||||
that your Mandrill account settings apply by default.
|
that your Mandrill account settings apply by default.
|
||||||
"""
|
"""
|
||||||
self.message.send()
|
self.message.send()
|
||||||
|
self.assert_mandrill_called("/messages/send.json")
|
||||||
data = self.get_api_call_data()
|
data = self.get_api_call_data()
|
||||||
self.assertFalse('from_name' in data['message'])
|
self.assertFalse('from_name' in data['message'])
|
||||||
self.assertFalse('track_opens' in data['message'])
|
self.assertFalse('track_opens' in data['message'])
|
||||||
|
|||||||
Reference in New Issue
Block a user