From 4be12952a315ca8afb79e5deb6c5dbade3c887e9 Mon Sep 17 00:00:00 2001 From: medmunds Date: Fri, 11 Jan 2013 13:28:49 -0800 Subject: [PATCH] Add send-template tests (and fixes). Add test cases for send-template. Expand template_content dict into Mandrill's name/value array. Don't send template_content as "None" if missing. Clean up some variable names in the backend. --- djrill/mail/backends/djrill.py | 37 ++++++++------- djrill/tests/__init__.py | 1 + djrill/tests/test_mandrill_send_template.py | 50 +++++++++++++++++++++ 3 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 djrill/tests/test_mandrill_send_template.py diff --git a/djrill/mail/backends/djrill.py b/djrill/mail/backends/djrill.py index de12329..ac210ba 100644 --- a/djrill/mail/backends/djrill.py +++ b/djrill/mail/backends/djrill.py @@ -47,8 +47,8 @@ class DjrillBackend(BaseEmailBackend): raise ImproperlyConfigured("You have not set your mandrill api key " "in the settings.py file.") - self.api_action = self.api_url + "/messages/send.json" - self.template_api_action = self.api_url + "/messages/send-template.json" + self.api_send = self.api_url + "/messages/send.json" + self.api_send_template = self.api_url + "/messages/send-template.json" def send_messages(self, email_messages): if not email_messages: @@ -78,29 +78,28 @@ class DjrillBackend(BaseEmailBackend): raise return False + api_url = self.api_send + api_params = { + "key": self.api_key, + "message": msg_dict + } + # check if template is set in message to send it via # api url: /messages/send-template.json if hasattr(message, 'template_name'): - template_content = getattr(message, 'template_content', - None) - djrill_it = requests.post(self.template_api_action, - data=json.dumps({ - "key": self.api_key, - "template_name": message.template_name, - "template_content": template_content, - "message": msg_dict - })) - else: - djrill_it = requests.post(self.api_action, data=json.dumps({ - "key": self.api_key, - "message": msg_dict - })) + api_url = self.api_send_template + api_params['template_name'] = message.template_name + if hasattr(message, 'template_content'): + api_params['template_content'] = \ + self._expand_merge_vars(message.template_content) - if djrill_it.status_code != 200: + response = requests.post(api_url, data=json.dumps(api_params)) + + if response.status_code != 200: if not self.fail_silently: raise DjrillBackendHTTPError( - status_code=djrill_it.status_code, - response = djrill_it, + status_code=response.status_code, + response=response, log_message="Failed to send a message to %s, from %s" % (msg_dict['to'], msg_dict['from_email'])) return False diff --git a/djrill/tests/__init__.py b/djrill/tests/__init__.py index 82248c1..767ad30 100644 --- a/djrill/tests/__init__.py +++ b/djrill/tests/__init__.py @@ -1,3 +1,4 @@ from test_admin import * from test_legacy import * from test_mandrill_send import * +from test_mandrill_send_template import * diff --git a/djrill/tests/test_mandrill_send_template.py b/djrill/tests/test_mandrill_send_template.py new file mode 100644 index 0000000..4278907 --- /dev/null +++ b/djrill/tests/test_mandrill_send_template.py @@ -0,0 +1,50 @@ +from django.core import mail + +from djrill.tests.mock_backend import DjrillBackendMockAPITestCase + + +class DjrillMandrillSendTemplateTests(DjrillBackendMockAPITestCase): + """Test Djrill backend support for Mandrill send-template features""" + + def test_send_template(self): + msg = mail.EmailMessage('Subject', 'Text Body', + 'from@example.com', ['to@example.com']) + msg.template_name = "PERSONALIZED_SPECIALS" + msg.template_content = { + 'HEADLINE': "

Specials Just For *|FNAME|*

", + 'OFFER_BLOCK': "

Half off all fruit

" + } + msg.send() + self.assert_mandrill_called("/messages/send-template.json") + data = self.get_api_call_data() + self.assertEqual(data['template_name'], "PERSONALIZED_SPECIALS") + # Djrill expands simple python dicts into the more-verbose name/value + # structures the Mandrill API uses + self.assertEqual(data['template_content'], + [ {'name': "HEADLINE", + 'value': "

Specials Just For *|FNAME|*

"}, + {'name': "OFFER_BLOCK", + 'value': "

Half off all fruit

"} ] + ) + + def test_no_template_content(self): + # Just a template, without any template_content to be merged + msg = mail.EmailMessage('Subject', 'Text Body', + 'from@example.com', ['to@example.com']) + msg.template_name = "WELCOME_MESSAGE" + msg.send() + self.assert_mandrill_called("/messages/send-template.json") + data = self.get_api_call_data() + self.assertEqual(data['template_name'], "WELCOME_MESSAGE") + self.assertFalse('template_content' in data) + + def test_non_template_send(self): + # Make sure the non-template case still uses /messages/send.json + msg = mail.EmailMessage('Subject', 'Text Body', + 'from@example.com', ['to@example.com']) + msg.send() + self.assert_mandrill_called("/messages/send.json") + data = self.get_api_call_data() + self.assertFalse('template_name' in data) + self.assertFalse('template_content' in data) + self.assertFalse('async' in data)