Normalize ESP response and recipient status

This commit is contained in:
medmunds
2016-03-05 17:22:27 -08:00
parent 518e6e86f8
commit 3a93648481
5 changed files with 118 additions and 83 deletions

View File

@@ -38,11 +38,17 @@ class DjrillIntegrationTests(TestCase):
# Example of getting the Mandrill send status and _id from the message
sent_count = self.message.send()
self.assertEqual(sent_count, 1)
# noinspection PyUnresolvedReferences
response = self.message.mandrill_response
self.assertIn(response[0]['status'], ['sent', 'queued']) # successful send (could still bounce later)
self.assertEqual(response[0]['email'], 'to@example.com')
self.assertGreater(len(response[0]['_id']), 0)
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_invalid_from(self):
# Example of trying to send from an invalid address
@@ -61,15 +67,15 @@ class DjrillIntegrationTests(TestCase):
try:
self.message.send()
except AnymailRecipientsRefused:
# Mandrill refused to deliver the mail -- message.mandrill_response will tell you why:
# Mandrill refused to deliver the mail -- message.anymail_status will tell you why:
# noinspection PyUnresolvedReferences
response = self.message.mandrill_response
self.assertEqual(response[0]['status'], 'invalid')
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
response = self.message.mandrill_response
if response[0]['status'] == 'queued':
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")
@@ -80,16 +86,15 @@ class DjrillIntegrationTests(TestCase):
try:
self.message.send()
except AnymailRecipientsRefused:
# Mandrill refused to deliver the mail -- message.mandrill_response will tell you why:
# Mandrill refused to deliver the mail -- message.anymail_status will tell you why:
# noinspection PyUnresolvedReferences
response = self.message.mandrill_response
self.assertEqual(response[0]['status'], 'rejected')
self.assertEqual(response[0]['reject_reason'], 'test')
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
response = self.message.mandrill_response
if response[0]['status'] == 'queued':
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")

View File

@@ -525,39 +525,43 @@ class DjrillMandrillFeatureTests(DjrillBackendMockAPITestCase):
self.assertFalse('async' in data)
self.assertFalse('ip_pool' in data)
def test_send_attaches_mandrill_response(self):
""" The mandrill_response should be attached to the message when it is sent """
response = [{'email': 'to1@example.com', 'status': 'sent'}]
# noinspection PyUnresolvedReferences
def test_send_attaches_anymail_status(self):
""" The anymail_status should be attached to the message when it is sent """
response = [{'email': 'to1@example.com', 'status': 'sent', '_id': 'abc123'}]
self.mock_post.return_value = self.MockResponse(raw=six.b(json.dumps(response)))
msg = mail.EmailMessage('Subject', 'Message', 'from@example.com', ['to1@example.com'],)
sent = msg.send()
self.assertEqual(sent, 1)
# noinspection PyUnresolvedReferences
self.assertEqual(msg.mandrill_response, response)
# noinspection PyUnresolvedReferences
self.assertEqual(msg.anymail_status, "sent")
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.json(), response)
def test_send_failed_mandrill_response(self):
""" If the send fails, mandrill_response should be set to None """
# noinspection PyUnresolvedReferences
def test_send_failed_anymail_status(self):
""" If the send fails, anymail_status should contain initial values"""
self.mock_post.return_value = self.MockResponse(status_code=500)
msg = mail.EmailMessage('Subject', 'Message', 'from@example.com', ['to1@example.com'],)
sent = msg.send(fail_silently=True)
self.assertEqual(sent, 0)
# noinspection PyUnresolvedReferences
self.assertIsNone(msg.mandrill_response)
# noinspection PyUnresolvedReferences
self.assertIsNone(msg.anymail_status)
self.assertIsNone(msg.anymail_status.status)
self.assertIsNone(msg.anymail_status.message_id)
self.assertEqual(msg.anymail_status.recipients, {})
self.assertIsNone(msg.anymail_status.esp_response)
# noinspection PyUnresolvedReferences
def test_send_unparsable_mandrill_response(self):
"""If the send succeeds, but a non-JSON API response, should raise an API exception"""
self.mock_post.return_value = self.MockResponse(status_code=500, raw=b"this isn't json")
self.mock_post.return_value = self.MockResponse(status_code=200, raw=b"this isn't json")
msg = mail.EmailMessage('Subject', 'Message', 'from@example.com', ['to1@example.com'],)
with self.assertRaises(AnymailAPIError):
msg.send()
# noinspection PyUnresolvedReferences
self.assertIsNone(msg.mandrill_response)
# noinspection PyUnresolvedReferences
self.assertIsNone(msg.anymail_status)
self.assertIsNone(msg.anymail_status.status)
self.assertIsNone(msg.anymail_status.message_id)
self.assertEqual(msg.anymail_status.recipients, {})
self.assertEqual(msg.anymail_status.esp_response, self.mock_post.return_value)
def test_json_serialization_errors(self):
"""Try to provide more information about non-json-serializable data"""