Postmark: Fix incorrect single 'to' handling with template but no merge data

Fixes #227
This commit is contained in:
medmunds
2021-02-24 12:19:13 -08:00
committed by Mike Edmunds
parent 7ff647c303
commit f4c99abddf
3 changed files with 27 additions and 10 deletions

View File

@@ -33,8 +33,8 @@ vNext
Fixes Fixes
~~~~~ ~~~~~
* **Postmark:** Fix Postmark API error when sending with a template that doesn't * **Postmark:** Fix two different errors when sending with a template but no merge
require any merge data. (Thanks to `@Tobeyforce`_ for reporting it.) data. (Thanks to `@kareemcoding`_ and `@Tobeyforce`_ for reporting them.)
v8.2 v8.2
@@ -1226,6 +1226,7 @@ Features
.. _@janneThoft: https://github.com/janneThoft .. _@janneThoft: https://github.com/janneThoft
.. _@jc-ee: https://github.com/jc-ee .. _@jc-ee: https://github.com/jc-ee
.. _@joshkersey: https://github.com/joshkersey .. _@joshkersey: https://github.com/joshkersey
.. _@kareemcoding: https://github.com/kareemcoding
.. _@kika115: https://github.com/kika115 .. _@kika115: https://github.com/kika115
.. _@Lekensteyn: https://github.com/Lekensteyn .. _@Lekensteyn: https://github.com/Lekensteyn
.. _@lewistaylor: https://github.com/lewistaylor .. _@lewistaylor: https://github.com/lewistaylor

View File

@@ -160,7 +160,7 @@ class PostmarkPayload(RequestsPayload):
super().__init__(message, defaults, backend, headers=headers, *args, **kwargs) super().__init__(message, defaults, backend, headers=headers, *args, **kwargs)
def get_api_endpoint(self): def get_api_endpoint(self):
batch_send = self.is_batch() and len(self.to_emails) > 1 batch_send = self.is_batch()
if 'TemplateAlias' in self.data or 'TemplateId' in self.data or 'TemplateModel' in self.data: if 'TemplateAlias' in self.data or 'TemplateId' in self.data or 'TemplateModel' in self.data:
if batch_send: if batch_send:
return "email/batchWithTemplates" return "email/batchWithTemplates"
@@ -187,8 +187,8 @@ class PostmarkPayload(RequestsPayload):
elif api_endpoint == "email/batch": elif api_endpoint == "email/batch":
data = [self.data_for_recipient(to) for to in self.to_emails] data = [self.data_for_recipient(to) for to in self.to_emails]
elif api_endpoint == "email/withTemplate/": elif api_endpoint == "email/withTemplate/":
assert len(self.to_emails) == 1 assert self.merge_data is None and self.merge_metadata is None # else it's a batch send
data = self.data_for_recipient(self.to_emails[0]) data = self.data
else: else:
raise AssertionError("PostmarkPayload.serialize_data missing" raise AssertionError("PostmarkPayload.serialize_data missing"
" case for api_endpoint %r" % api_endpoint) " case for api_endpoint %r" % api_endpoint)

View File

@@ -398,6 +398,19 @@ class PostmarkBackendAnymailFeatureTests(PostmarkBackendMockAPITestCase):
# Postmark requires TemplateModel (can be empty) with TemplateId/TemplateAlias # Postmark requires TemplateModel (can be empty) with TemplateId/TemplateAlias
self.assertEqual(data['TemplateModel'], {}) self.assertEqual(data['TemplateModel'], {})
def test_template_multiple_recipients(self):
# This is a non-batch (no merge_data) template send
message = AnymailMessage(
from_email='from@example.com',
to=['to@example.com', "Also to <to2@example.com>"],
template_id=1234567,
)
message.send()
self.assert_esp_called('/email/withTemplate/')
data = self.get_api_call_json()
self.assertEqual(data['To'], 'to@example.com, Also to <to2@example.com>')
self.assertEqual(data['TemplateId'], 1234567)
_mock_batch_response = json.dumps([{ _mock_batch_response = json.dumps([{
"ErrorCode": 0, "ErrorCode": 0,
"Message": "OK", "Message": "OK",
@@ -464,14 +477,17 @@ class PostmarkBackendAnymailFeatureTests(PostmarkBackendMockAPITestCase):
) )
message.send() message.send()
self.assert_esp_called('/email/withTemplate/') # because merge_data is set, it's treated as a batch send
self.assert_esp_called('/email/batchWithTemplates')
data = self.get_api_call_json() data = self.get_api_call_json()
self.assertEqual(data, { self.assertEqual(data, {
"From": "from@example.com", 'Messages': [{
"To": "alice@example.com", "From": "from@example.com",
"TemplateId": 1234567, "To": "alice@example.com",
"TemplateModel": {"name": "Alice", "group": "Developers", "site": "ExampleCo"}, "TemplateId": 1234567,
"TemplateModel": {"name": "Alice", "group": "Developers", "site": "ExampleCo"},
}]
}) })
recipients = message.anymail_status.recipients recipients = message.anymail_status.recipients