Skip to content

Commit

Permalink
Postmark: add a workaround for handling test webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ecno92 committed Apr 21, 2023
1 parent d9a80e7 commit c202879
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ vNext

*Unreleased changes*

Fixes
~~~~~

* **Postmark:** Workaround for handling inbound test webhooks.
(`More info <https://github.com/anymail/django-anymail/issues/304>`__)

Other
~~~~~

Expand Down
8 changes: 7 additions & 1 deletion anymail/webhooks/postmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,13 @@ def esp_to_anymail_event(self, esp_event):
attachments = [
AnymailInboundMessage.construct_attachment(
content_type=attachment["ContentType"],
content=attachment["Content"],
content=(
attachment.get("Content")
# WORKAROUND:
# The test webhooks are not like their real webhooks
# This allows the test webhooks to be parsed.
or attachment["Data"]
),
base64=True,
filename=attachment.get("Name", "") or None,
content_id=attachment.get("ContentID", "") or None,
Expand Down
20 changes: 19 additions & 1 deletion tests/test_postmark_inbound.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ def test_attachments(self):
"ContentType": 'message/rfc822; charset="us-ascii"',
"ContentLength": len(email_content),
},
# This is an attachement like send by the test webhook
# A workaround is implemented to handle it.
# Once Postmark solves the bug on their side this workaround
# can be reverted.
{
"Name": "test.txt",
"ContentType": "text/plain",
"Data": "VGhpcyBpcyBhdHRhY2htZW50IGNvbnRlbnRzLCBiYXNlLTY0IGVuY29kZWQu",
"ContentLength": 45,
},
]
}

Expand All @@ -183,7 +193,7 @@ def test_attachments(self):
event = kwargs["event"]
message = event.message
attachments = message.attachments # AnymailInboundMessage convenience accessor
self.assertEqual(len(attachments), 2)
self.assertEqual(len(attachments), 3)
self.assertEqual(attachments[0].get_filename(), "test.txt")
self.assertEqual(attachments[0].get_content_type(), "text/plain")
self.assertEqual(attachments[0].get_content_text(), "test attachment")
Expand All @@ -192,6 +202,14 @@ def test_attachments(self):
attachments[1].get_content_bytes(), email_content
)

# Attachment of test webhook
self.assertEqual(attachments[2].get_filename(), "test.txt")
self.assertEqual(attachments[2].get_content_type(), "text/plain")
self.assertEqual(
attachments[2].get_content_text(),
"This is attachment contents, base-64 encoded.",
)

inlines = message.inline_attachments
self.assertEqual(len(inlines), 1)
inline = inlines["abc123"]
Expand Down

0 comments on commit c202879

Please sign in to comment.