Skip to content

Commit f8af707

Browse files
authored
Fix inactive recipient email parsing
Fixes InactiveRecipientException parsing so email domains are not truncated at the first period. Adds coverage for single and multiple inactive recipients.
1 parent acef768 commit f8af707

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎postmark/exceptions.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __init__(
7878
request_id: str | None = None,
7979
):
8080
super().__init__(message, error_code, http_status, request_id)
81-
match = re.search(r"Found inactive addresses: ([^.]+)", message)
81+
match = re.search(r"Found inactive addresses:\s*(.+?)\.(?:\s|$)", message)
8282
self.inactive_recipients: list[str] = (
8383
[a for addr in match.group(1).split(",") if (a := addr.strip())]
8484
if match

‎tests/test_exceptions.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Tests for Postmark exception helpers."""
2+
3+
import pytest
4+
5+
from postmark.exceptions import InactiveRecipientException
6+
7+
8+
@pytest.mark.parametrize(
9+
("message", "expected"),
10+
[
11+
(
12+
"Found inactive addresses: john@example.com. Emails to these addresses "
13+
"are not accepted or delivered by Postmark.",
14+
["john@example.com"],
15+
),
16+
(
17+
"Found inactive addresses: john@example.com, jane@test.org. Emails to "
18+
"these addresses are not accepted or delivered by Postmark.",
19+
["john@example.com", "jane@test.org"],
20+
),
21+
(
22+
"Found inactive addresses: a@example.co, b@example.io, c@example.net.",
23+
["a@example.co", "b@example.io", "c@example.net"],
24+
),
25+
(
26+
"Found inactive addresses:john@example.com.",
27+
["john@example.com"],
28+
),
29+
(
30+
"Inactive recipient.",
31+
[],
32+
),
33+
],
34+
)
35+
def test_inactive_recipient_exception_parses_inactive_recipients(
36+
message: str, expected: list[str]
37+
):
38+
exc = InactiveRecipientException(message, error_code=406, http_status=406)
39+
40+
assert exc.inactive_recipients == expected

0 commit comments

Comments
 (0)