From 594906932177e0e0fd6b95e194c0d77186a07515 Mon Sep 17 00:00:00 2001 From: David Jean Louis Date: Tue, 5 Mar 2024 22:58:41 +0100 Subject: [PATCH] Mailgun: fix webhook error with null delivery-status Mailgun now sometimes posts `"delivery-status": null` in the tracking event payload. Avoid raising an AttributeError when that occurs. Fixes #361 --- CHANGELOG.rst | 8 ++++++++ anymail/webhooks/mailgun.py | 8 ++++---- tests/test_mailgun_webhooks.py | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f7a9df4..050786e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -44,6 +44,13 @@ Features (Thanks to `@Arondit`_ for the implementation.) +Fixes +~~~~~ + +* **Mailgun:** Avoid an error when Mailgun posts null delivery-status + to the event tracking webhook. (Thanks to `@izimobil`_ for the fix.) + + v10.2 ----- @@ -1595,6 +1602,7 @@ Features .. _@Flexonze: https://github.com/Flexonze .. _@gdvalderrama: https://github.com/gdvalderrama .. _@Honza-m: https://github.com/Honza-m +.. _@izimobil: https://github.com/izimobil .. _@janneThoft: https://github.com/janneThoft .. _@jc-ee: https://github.com/jc-ee .. _@joshkersey: https://github.com/joshkersey diff --git a/anymail/webhooks/mailgun.py b/anymail/webhooks/mailgun.py index a9f4949..233a737 100644 --- a/anymail/webhooks/mailgun.py +++ b/anymail/webhooks/mailgun.py @@ -172,12 +172,12 @@ class MailgunTrackingWebhookView(MailgunBaseWebhookView): try: delivery_status = event_data["delivery-status"] - except KeyError: - description = None - mta_response = None - else: + # if delivery_status is None, an AttributeError will be raised description = delivery_status.get("description") mta_response = delivery_status.get("message") + except (KeyError, AttributeError): + description = None + mta_response = None if "reason" in event_data: reject_reason = self.reject_reasons.get( diff --git a/tests/test_mailgun_webhooks.py b/tests/test_mailgun_webhooks.py index 946b0b5..1a59725 100644 --- a/tests/test_mailgun_webhooks.py +++ b/tests/test_mailgun_webhooks.py @@ -630,6 +630,22 @@ class MailgunTestCase(WebhookTestCase): self.assertEqual(event.event_type, "clicked") self.assertEqual(event.click_url, "https://example.com/test") + def test_delivery_status_is_none_event(self): + raw_event = mailgun_sign_payload( + { + "event-data": { + "event": "accepted", + "delivery-status": None, + } + } + ) + response = self.client.post( + "/anymail/mailgun/tracking/", + data=json.dumps(raw_event), + content_type="application/json", + ) + self.assertEqual(response.status_code, 200) + @tag("mailgun") @override_settings(ANYMAIL_MAILGUN_WEBHOOK_SIGNING_KEY=TEST_WEBHOOK_SIGNING_KEY)