Skip to content

Commit

Permalink
Use job_id as message_id if generation disabled
Browse files Browse the repository at this point in the history
When not generating unique per-recipient
message ids, use Unisender Go's "job_id"
as Anymail's `message_id` for all recipients.
The same job_id is provided in the send
API response and tracking webhooks.
  • Loading branch information
medmunds committed Feb 29, 2024
1 parent 599ed4c commit 7d6e98b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
4 changes: 3 additions & 1 deletion anymail/backends/unisender_go.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ def parse_recipient_status(
}
"""
parsed_response = self.deserialize_json_response(response, payload, message)
# job_id serves as message_id when not self.generate_message_id
job_id = parsed_response.get("job_id")
succeed_emails = {
recipient: AnymailRecipientStatus(
message_id=payload.message_ids.get(recipient), status="queued"
message_id=payload.message_ids.get(recipient, job_id), status="queued"
)
for recipient in parsed_response["emails"]
}
Expand Down
2 changes: 1 addition & 1 deletion anymail/webhooks/unisender_go.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def esp_to_anymail_event(self, esp_event: dict) -> AnymailTrackingEvent:
timestamp = datetime.fromisoformat(event_data["event_time"])
timestamp_utc = timestamp.replace(tzinfo=timezone.utc)
metadata = event_data.get("metadata", {})
message_id = metadata.pop("anymail_id", None)
message_id = metadata.pop("anymail_id", event_data.get("job_id"))

delivery_info = event_data.get("delivery_info", {})
delivery_status = delivery_info.get("delivery_status", "")
Expand Down
19 changes: 16 additions & 3 deletions tests/test_unisender_go_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,10 +697,23 @@ def test_rejected_recipient_status(self):

@override_settings(ANYMAIL_UNISENDER_GO_GENERATE_MESSAGE_ID=False)
def test_disable_generate_message_id(self):
"""
When not generating per-recipient message_id,
use Unisender Go's job_id for all recipients.
"""
self.set_mock_response(
success_emails=["[email protected]", "[email protected]"],
job_id="123456-000HHH-CcCc",
)
self.message.to = ["[email protected]", "[email protected]"]
self.message.send()
self.assertIsNone(self.message.anymail_status.message_id)
self.assertIsNone(
self.message.anymail_status.recipients["[email protected]"].message_id
self.assertEqual(self.message.anymail_status.message_id, "123456-000HHH-CcCc")
recipient_status = self.message.anymail_status.recipients
self.assertEqual(
recipient_status["[email protected]"].message_id, "123456-000HHH-CcCc"
)
self.assertEqual(
recipient_status["[email protected]"].message_id, "123456-000HHH-CcCc"
)

# noinspection PyUnresolvedReferences
Expand Down
9 changes: 6 additions & 3 deletions tests/test_unisender_go_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
EVENT_TYPE = EventType.SENT
EVENT_TIME = "2015-11-30 15:09:42"
EVENT_DATETIME = datetime.datetime(2015, 11, 30, 15, 9, 42, tzinfo=timezone.utc)
MESSAGE_ID = "1a3Q2V-0000OZ-S0"
JOB_ID = "1a3Q2V-0000OZ-S0"
DELIVERY_RESPONSE = "550 Spam rejected"
UNISENDER_TEST_EMAIL = "[email protected]"
TEST_API_KEY = "api_key"
Expand All @@ -30,7 +30,7 @@
{
"event_name": "transactional_email_status",
"event_data": {
"job_id": MESSAGE_ID,
"job_id": JOB_ID,
"metadata": {"key1": "val1", "anymail_id": TEST_EMAIL_ID},
"email": UNISENDER_TEST_EMAIL,
"status": EVENT_TYPE,
Expand Down Expand Up @@ -69,7 +69,7 @@
{
"event_name": "transactional_email_status",
"event_data": {
"job_id": MESSAGE_ID,
"job_id": JOB_ID,
"metadata": {},
"email": UNISENDER_TEST_EMAIL,
"status": EVENT_TYPE,
Expand Down Expand Up @@ -123,6 +123,9 @@ def test_without_delivery_info(self):
events = view.parse_events(request)

self.assertEqual(len(events), 1)
# Without metadata["anymail_id"], message_id uses the job_id.
# (This covers messages sent with "UNISENDER_GO_GENERATE_MESSAGE_ID": False.)
self.assertEqual(events[0].message_id, JOB_ID)

@override_settings(ANYMAIL_UNISENDER_GO_API_KEY=TEST_API_KEY)
def test_check_authorization(self):
Expand Down

0 comments on commit 7d6e98b

Please sign in to comment.