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:
medmunds
2013-01-11 12:44:06 -08:00
parent 3b237a59db
commit 207e94e6d0
2 changed files with 29 additions and 5 deletions

View File

@@ -14,7 +14,7 @@ class DjrillBackendMockAPITestCase(TestCase):
self.content = content
def setUp(self):
self.patch = patch('requests.post')
self.patch = patch('requests.post', autospec=True)
self.mock_post = self.patch.start()
self.mock_post.return_value = self.MockResponse()
@@ -28,6 +28,25 @@ class DjrillBackendMockAPITestCase(TestCase):
self.patch.stop()
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):
"""Returns the data posted to the Mandrill API.
@@ -36,9 +55,10 @@ class DjrillBackendMockAPITestCase(TestCase):
if self.mock_post.call_args is None:
raise AssertionError("Mandrill API was not called")
(args, kwargs) = self.mock_post.call_args
if 'data' not in kwargs:
raise AssertionError("requests.post was called without data kwarg "
"-- Maybe tests need to be updated for backend changes?")
return json.loads(kwargs['data'])
try:
post_data = kwargs.get('data', None) or args[1]
except IndexError:
raise AssertionError("requests.post was called without data")
return json.loads(post_data)

View File

@@ -15,6 +15,7 @@ class DjrillBackendTests(DjrillBackendMockAPITestCase):
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_mandrill_called("/messages/send.json")
data = self.get_api_call_data()
self.assertEqual(data['message']['subject'], "Subject here")
self.assertEqual(data['message']['text'], "Here is the message.")
@@ -54,6 +55,7 @@ class DjrillBackendTests(DjrillBackendMockAPITestCase):
headers={'Reply-To': 'another@example.com',
'X-MyHeader': 'my value'})
email.send()
self.assert_mandrill_called("/messages/send.json")
data = self.get_api_call_data()
self.assertEqual(data['message']['subject'], "Subject")
self.assertEqual(data['message']['text'], "Body goes here")
@@ -78,6 +80,7 @@ class DjrillBackendTests(DjrillBackendMockAPITestCase):
'from@example.com', ['to@example.com'])
email.attach_alternative(html_content, "text/html")
email.send()
self.assert_mandrill_called("/messages/send.json")
data = self.get_api_call_data()
self.assertEqual(data['message']['text'], text_content)
self.assertEqual(data['message']['html'], html_content)
@@ -271,6 +274,7 @@ class DjrillMandrillFeatureTests(DjrillBackendMockAPITestCase):
that your Mandrill account settings apply by default.
"""
self.message.send()
self.assert_mandrill_called("/messages/send.json")
data = self.get_api_call_data()
self.assertFalse('from_name' in data['message'])
self.assertFalse('track_opens' in data['message'])