Skip to content

Commit

Permalink
Postmark: Add warning for postmarks test webhook workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
Ecno92 committed Apr 21, 2023
1 parent c202879 commit 953d44b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
15 changes: 14 additions & 1 deletion anymail/webhooks/postmark.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import json
import warnings

from django.utils.dateparse import parse_datetime

from ..exceptions import AnymailConfigurationError
from ..exceptions import AnymailConfigurationError, AnymailWarning
from ..inbound import AnymailInboundMessage
from ..signals import (
AnymailInboundEvent,
Expand Down Expand Up @@ -183,6 +184,18 @@ def esp_to_anymail_event(self, esp_event):
for attachment in esp_event.get("Attachments", [])
]

# Warning to the user regarding the workaround of above.
for attachment in esp_event.get("Attachments", []):
if "Data" in attachment:
warnings.warn(
"Received a test webhook attachment. "
"It is recommended to test with real inbound events. "
"See https://github.com/anymail/django-anymail/issues/304 "
"for more information.",
AnymailWarning,
)
break

message = AnymailInboundMessage.construct(
from_email=self._address(esp_event.get("FromFull")),
to=", ".join([self._address(to) for to in esp_event.get("ToFull", [])]),
Expand Down
15 changes: 9 additions & 6 deletions tests/test_postmark_inbound.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from django.test import tag

from anymail.exceptions import AnymailConfigurationError
from anymail.exceptions import AnymailConfigurationError, AnymailWarning
from anymail.inbound import AnymailInboundMessage
from anymail.signals import AnymailInboundEvent
from anymail.webhooks.postmark import PostmarkInboundWebhookView
Expand Down Expand Up @@ -178,11 +178,14 @@ def test_attachments(self):
]
}

response = self.client.post(
"/anymail/postmark/inbound/",
content_type="application/json",
data=json.dumps(raw_event),
)
with self.assertWarnsRegex(
AnymailWarning, r"Received a test webhook attachment. "
):
response = self.client.post(
"/anymail/postmark/inbound/",
content_type="application/json",
data=json.dumps(raw_event),
)
self.assertEqual(response.status_code, 200)
kwargs = self.assert_handler_called_once_with(
self.inbound_handler,
Expand Down

0 comments on commit 953d44b

Please sign in to comment.