mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-21 04:11:06 -05:00
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.
This commit is contained in:
@@ -47,8 +47,8 @@ class DjrillBackend(BaseEmailBackend):
|
|||||||
raise ImproperlyConfigured("You have not set your mandrill api key "
|
raise ImproperlyConfigured("You have not set your mandrill api key "
|
||||||
"in the settings.py file.")
|
"in the settings.py file.")
|
||||||
|
|
||||||
self.api_action = self.api_url + "/messages/send.json"
|
self.api_send = self.api_url + "/messages/send.json"
|
||||||
self.template_api_action = self.api_url + "/messages/send-template.json"
|
self.api_send_template = self.api_url + "/messages/send-template.json"
|
||||||
|
|
||||||
def send_messages(self, email_messages):
|
def send_messages(self, email_messages):
|
||||||
if not email_messages:
|
if not email_messages:
|
||||||
@@ -78,29 +78,28 @@ class DjrillBackend(BaseEmailBackend):
|
|||||||
raise
|
raise
|
||||||
return False
|
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
|
# check if template is set in message to send it via
|
||||||
# api url: /messages/send-template.json
|
# api url: /messages/send-template.json
|
||||||
if hasattr(message, 'template_name'):
|
if hasattr(message, 'template_name'):
|
||||||
template_content = getattr(message, 'template_content',
|
api_url = self.api_send_template
|
||||||
None)
|
api_params['template_name'] = message.template_name
|
||||||
djrill_it = requests.post(self.template_api_action,
|
if hasattr(message, 'template_content'):
|
||||||
data=json.dumps({
|
api_params['template_content'] = \
|
||||||
"key": self.api_key,
|
self._expand_merge_vars(message.template_content)
|
||||||
"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
|
|
||||||
}))
|
|
||||||
|
|
||||||
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:
|
if not self.fail_silently:
|
||||||
raise DjrillBackendHTTPError(
|
raise DjrillBackendHTTPError(
|
||||||
status_code=djrill_it.status_code,
|
status_code=response.status_code,
|
||||||
response = djrill_it,
|
response=response,
|
||||||
log_message="Failed to send a message to %s, from %s" %
|
log_message="Failed to send a message to %s, from %s" %
|
||||||
(msg_dict['to'], msg_dict['from_email']))
|
(msg_dict['to'], msg_dict['from_email']))
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
from test_admin import *
|
from test_admin import *
|
||||||
from test_legacy import *
|
from test_legacy import *
|
||||||
from test_mandrill_send import *
|
from test_mandrill_send import *
|
||||||
|
from test_mandrill_send_template import *
|
||||||
|
|||||||
50
djrill/tests/test_mandrill_send_template.py
Normal file
50
djrill/tests/test_mandrill_send_template.py
Normal file
@@ -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': "<h1>Specials Just For *|FNAME|*</h1>",
|
||||||
|
'OFFER_BLOCK': "<p><em>Half off</em> all fruit</p>"
|
||||||
|
}
|
||||||
|
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': "<h1>Specials Just For *|FNAME|*</h1>"},
|
||||||
|
{'name': "OFFER_BLOCK",
|
||||||
|
'value': "<p><em>Half off</em> all fruit</p>"} ]
|
||||||
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
Reference in New Issue
Block a user