Skip to content

Commit

Permalink
Fixed #361: AttributeError in mailgun webhooks
Browse files Browse the repository at this point in the history
This fixes the case of delivery-status being None in the event data
posted to mailgun webhook handler.
See: #361
  • Loading branch information
izimobil committed Mar 5, 2024
1 parent a2c0ed6 commit 2d4a8f1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
8 changes: 4 additions & 4 deletions anymail/webhooks/mailgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ def esp_to_anymail_event(self, esp_event):

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(
Expand Down
57 changes: 57 additions & 0 deletions tests/test_mailgun_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,63 @@ def test_clicked_event(self):
self.assertEqual(event.event_type, "clicked")
self.assertEqual(event.click_url, "https://example.com/test")

def test_no_delivery_status(self):
raw_event = mailgun_sign_payload(
{
"signature": {
"timestamp": "1534108637",
"token": "651869375b9df3c98fc15c4889b102119add1235c38fc92824",
"signature": "...",
},
"event-data": {
"tags": [],
"timestamp": 1534108637.153125,
"storage": {
"url": "https://sw.api.mailgun.net/v3/domains/"
"example.org/messages/eyJwI...",
"key": "eyJwI...",
},
"recipient-domain": "example.com",
"id": "hTWCTD81RtiDN-...",
"campaigns": [],
"user-variables": {},
"flags": {
"is-routed": False,
"is-authenticated": True,
"is-system-test": False,
"is-test-mode": False,
},
"log-level": "info",
"envelope": {
"sending-ip": "333.123.123.200",
"sender": "[email protected]",
"transport": "smtp",
"targets": "[email protected]",
},
"message": {
"headers": {
"to": "[email protected]",
"message-id": "20180812211713.1.DF5966851B4BAA99"
"@example.org",
"from": "[email protected]",
"subject": "Testing",
},
"attachments": [],
"size": 809,
},
"recipient": "[email protected]",
"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)
Expand Down

0 comments on commit 2d4a8f1

Please sign in to comment.