Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend): only unverify a user on a Postmark bounce that means the address failed (#15981)

Toran Bruce Richardscdaf463470d1
diff --git a/autogpt_platform/backend/backend/api/features/postmark/postmark.py b/autogpt_platform/backend/backend/api/features/postmark/postmark.py
index ca708285f0..e190b31596 100644
--- a/autogpt_platform/backend/backend/api/features/postmark/postmark.py
+++ b/autogpt_platform/backend/backend/api/features/postmark/postmark.py
@@ -133,6 +133,37 @@ async def _user_id_for(recipient: str, message_id: str) -> str | None:
return user.id
+# The address itself is bad or Postmark has stopped sending to it.
+_HARD_BOUNCES = {
+ PostmarkBounceEnum.HardBounce,
+ PostmarkBounceEnum.BadEmailAddress,
+ PostmarkBounceEnum.ManuallyDeactivated,
+ PostmarkBounceEnum.SpamComplaint,
+}
+# A temporary failure: Postmark retries, and the address may work tomorrow.
+_SOFT_BOUNCES = {
+ PostmarkBounceEnum.Transient,
+ PostmarkBounceEnum.SoftBounce,
+ PostmarkBounceEnum.DnsError,
+}
+
+
+def _address_has_failed(event: PostmarkBounceWebhook) -> bool:
+ """Whether a bounce means the address can no longer be mailed.
+
+ Postmark sends a Bounce webhook for every type it classifies, including
+ ones that are not delivery failures at all: an out-of-office reply
+ (AutoResponder), a challenge, a virus or spam notice, or something it
+ could not classify. For those, trust Postmark's own verdict: the address
+ has failed only if Postmark deactivated it (``Inactive``).
+ """
+ if event.TypeCode in _HARD_BOUNCES:
+ return True
+ if event.TypeCode in _SOFT_BOUNCES:
+ return False
+ return event.Inactive
+
+
async def bounce_handler(event: PostmarkBounceWebhook):
user_id = await _user_id_for(event.Email, event.MessageID)
if not user_id:
@@ -144,14 +175,10 @@ async def bounce_handler(event: PostmarkBounceWebhook):
occurred_at=event.BouncedAt,
properties={"bounce_type": event.Type},
)
- if event.TypeCode in [
- PostmarkBounceEnum.Transient,
- PostmarkBounceEnum.SoftBounce,
- PostmarkBounceEnum.DnsError,
- ]:
+ if not _address_has_failed(event):
logger.info(
- "Softish bounce %s for user %s, not setting email verification to false",
- event.TypeCode,
+ "%s bounce for user %s, not setting email verification to false",
+ event.TypeCode.name,
user_id,
)
return
diff --git a/autogpt_platform/backend/backend/api/features/postmark/webhook_test.py b/autogpt_platform/backend/backend/api/features/postmark/webhook_test.py
index ad87db732d..4fd47eca28 100644
--- a/autogpt_platform/backend/backend/api/features/postmark/webhook_test.py
+++ b/autogpt_platform/backend/backend/api/features/postmark/webhook_test.py
@@ -13,6 +13,7 @@ from backend.util import posthog_client
from . import postmark
from .analytics import parse_postmark_time, safe_link
+from .models import PostmarkBounceEnum
TOKEN = "test-webhook-token"
USER_ID = "3e53486c-cf57-477e-ba2a-cb02dc828e1a"
@@ -114,6 +115,73 @@ def test_a_soft_bounce_is_sent_but_keeps_the_address_verified(
unverify.assert_not_awaited()
+# Whether each bounce type unverifies the address, given Postmark's Inactive.
+_HARD = {"unverified": True, "unverified_if_inactive": True}
+_SOFT = {"unverified": False, "unverified_if_inactive": False}
+_NOT_A_FAILURE = {"unverified": False, "unverified_if_inactive": True}
+BOUNCE_OUTCOMES = {
+ PostmarkBounceEnum.HardBounce: _HARD,
+ PostmarkBounceEnum.Transient: _SOFT,
+ PostmarkBounceEnum.Unsubscribe: _NOT_A_FAILURE,
+ PostmarkBounceEnum.Subscribe: _NOT_A_FAILURE,
+ PostmarkBounceEnum.AutoResponder: _NOT_A_FAILURE,
+ PostmarkBounceEnum.AddressChange: _NOT_A_FAILURE,
+ PostmarkBounceEnum.DnsError: _SOFT,
+ PostmarkBounceEnum.SpamNotification: _NOT_A_FAILURE,
+ PostmarkBounceEnum.OpenRelayTest: _NOT_A_FAILURE,
+ PostmarkBounceEnum.Unknown: _NOT_A_FAILURE,
+ PostmarkBounceEnum.SoftBounce: _SOFT,
+ PostmarkBounceEnum.VirusNotification: _NOT_A_FAILURE,
+ PostmarkBounceEnum.ChallengeVerification: _NOT_A_FAILURE,
+ PostmarkBounceEnum.BadEmailAddress: _HARD,
+ PostmarkBounceEnum.SpamComplaint: _HARD,
+ PostmarkBounceEnum.ManuallyDeactivated: _HARD,
+ PostmarkBounceEnum.Unconfirmed: _NOT_A_FAILURE,
+ PostmarkBounceEnum.Blocked: _NOT_A_FAILURE,
+ PostmarkBounceEnum.SMTPApiError: _NOT_A_FAILURE,
+ PostmarkBounceEnum.InboundError: _NOT_A_FAILURE,
+ PostmarkBounceEnum.DMARCPolicy: _NOT_A_FAILURE,
+ PostmarkBounceEnum.TemplateRenderingFailed: _NOT_A_FAILURE,
+}
+
+
+def test_every_bounce_type_has_an_expected_outcome():
+ assert set(BOUNCE_OUTCOMES) == set(PostmarkBounceEnum)
+
+
[email protected]("inactive", [False, True], ids=["active", "inactive"])
+ "bounce_type", list(BOUNCE_OUTCOMES), ids=lambda bounce_type: bounce_type.name
+)
+def test_only_a_failed_address_is_unverified(
+ bounce_type: PostmarkBounceEnum,
+ inactive: bool,
+ posthog: Mock,
+ lookup: AsyncMock,
+ unverify: AsyncMock,
+):
+ """A hard bounce always unverifies and a temporary one never does. Any
+ other type, such as an out-of-office reply, only does once Postmark has
+ deactivated the address."""
+ _post(
+ _bounce(
+ type_name=bounce_type.name,
+ type_code=bounce_type.value,
+ inactive=inactive,
+ )
+ )
+
+ outcome = BOUNCE_OUTCOMES[bounce_type]
+ expected = outcome["unverified_if_inactive" if inactive else "unverified"]
+ assert posthog.capture.call_args.kwargs["properties"]["bounce_type"] == (
+ bounce_type.name
+ )
+ if expected:
+ unverify.assert_awaited_once_with(USER_ID, False)
+ else:
+ unverify.assert_not_awaited()
+
+
@pytest.mark.parametrize("record_type", ["Delivery", "Open", "Click", "Bounce"])
def test_an_address_with_no_account_sends_nothing(
record_type: str, posthog: Mock, lookup: AsyncMock, unverify: AsyncMock
@@ -302,7 +370,7 @@ def _click(link: str) -> dict:
}
-def _bounce(*, type_name: str, type_code: int) -> dict:
+def _bounce(*, type_name: str, type_code: int, inactive: bool = True) -> dict:
return {
"RecordType": "Bounce",
"ID": 42,
@@ -314,7 +382,7 @@ def _bounce(*, type_name: str, type_code: int) -> dict:
"Email": RECIPIENT,
"From": "[email protected]",
"BouncedAt": "2019-11-05T16:33:54.9070259Z",
- "Inactive": True,
+ "Inactive": inactive,
"DumpAvailable": False,
"CanActivate": True,
"Subject": "Your trial",