From 6e508ab7b333c9fe28d40972885c850bc572baa8 Mon Sep 17 00:00:00 2001 From: Abdul Hadi Date: Sat, 1 Aug 2026 16:59:38 +0500 Subject: [PATCH] Fix inactive recipient email parsing --- postmark/exceptions.py | 2 +- tests/test_exceptions.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/test_exceptions.py diff --git a/postmark/exceptions.py b/postmark/exceptions.py index 0f08aa0..4760504 100644 --- a/postmark/exceptions.py +++ b/postmark/exceptions.py @@ -78,7 +78,7 @@ def __init__( request_id: str | None = None, ): super().__init__(message, error_code, http_status, request_id) - match = re.search(r"Found inactive addresses: ([^.]+)", message) + match = re.search(r"Found inactive addresses:\s*(.+?)\.(?:\s|$)", message) self.inactive_recipients: list[str] = ( [a for addr in match.group(1).split(",") if (a := addr.strip())] if match diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py new file mode 100644 index 0000000..d759830 --- /dev/null +++ b/tests/test_exceptions.py @@ -0,0 +1,40 @@ +"""Tests for Postmark exception helpers.""" + +import pytest + +from postmark.exceptions import InactiveRecipientException + + +@pytest.mark.parametrize( + ("message", "expected"), + [ + ( + "Found inactive addresses: john@example.com. Emails to these addresses " + "are not accepted or delivered by Postmark.", + ["john@example.com"], + ), + ( + "Found inactive addresses: john@example.com, jane@test.org. Emails to " + "these addresses are not accepted or delivered by Postmark.", + ["john@example.com", "jane@test.org"], + ), + ( + "Found inactive addresses: a@example.co, b@example.io, c@example.net.", + ["a@example.co", "b@example.io", "c@example.net"], + ), + ( + "Found inactive addresses:john@example.com.", + ["john@example.com"], + ), + ( + "Inactive recipient.", + [], + ), + ], +) +def test_inactive_recipient_exception_parses_inactive_recipients( + message: str, expected: list[str] +): + exc = InactiveRecipientException(message, error_code=406, http_status=406) + + assert exc.inactive_recipients == expected