diff --git a/anymail/backends/unisender_go.py b/anymail/backends/unisender_go.py index 7ecd5015..9b8d3f81 100644 --- a/anymail/backends/unisender_go.py +++ b/anymail/backends/unisender_go.py @@ -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"] } diff --git a/anymail/webhooks/unisender_go.py b/anymail/webhooks/unisender_go.py index 8121b60a..52d9e831 100644 --- a/anymail/webhooks/unisender_go.py +++ b/anymail/webhooks/unisender_go.py @@ -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", "") diff --git a/tests/test_unisender_go_backend.py b/tests/test_unisender_go_backend.py index 1964ace6..e6eea61a 100644 --- a/tests/test_unisender_go_backend.py +++ b/tests/test_unisender_go_backend.py @@ -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=["to1@example.com", "to2@example.com"], + job_id="123456-000HHH-CcCc", + ) + self.message.to = ["to1@example.com", "to2@example.com"] self.message.send() - self.assertIsNone(self.message.anymail_status.message_id) - self.assertIsNone( - self.message.anymail_status.recipients["to@example.com"].message_id + self.assertEqual(self.message.anymail_status.message_id, "123456-000HHH-CcCc") + recipient_status = self.message.anymail_status.recipients + self.assertEqual( + recipient_status["to1@example.com"].message_id, "123456-000HHH-CcCc" + ) + self.assertEqual( + recipient_status["to2@example.com"].message_id, "123456-000HHH-CcCc" ) # noinspection PyUnresolvedReferences diff --git a/tests/test_unisender_go_webhooks.py b/tests/test_unisender_go_webhooks.py index ff39ebbf..970800bd 100644 --- a/tests/test_unisender_go_webhooks.py +++ b/tests/test_unisender_go_webhooks.py @@ -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 = "recipient.email@example.com" TEST_API_KEY = "api_key" @@ -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, @@ -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, @@ -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):