From 63af533d98aac82b6d4f069b217d3e837285508d Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Tue, 17 Nov 2026 22:10:40 +0100 Subject: feat(receiver): explain received Webmention failures Map known verification failures to human-readable explanations and show suggested next steps only for receiver-controlled problems while retaining raw reasons. Return a specific validation error when targets are outside `ALLOWED_TARGET_HOSTNAMES` and reuse the guidance for status history. --- tests/forms/test_validators.py | 4 +- tests/test_received_failure.py | 53 ++++++++++++++++++++ tests/views/test_endpoint.py | 16 ++++++ tests/views/test_received.py | 109 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 tests/test_received_failure.py (limited to 'tests') diff --git a/tests/forms/test_validators.py b/tests/forms/test_validators.py index 4234552..8822310 100644 --- a/tests/forms/test_validators.py +++ b/tests/forms/test_validators.py @@ -28,7 +28,9 @@ def test_allowed_hostname_accepts_configured_hostname(app: Flask) -> None: def test_allowed_hostname_rejects_unconfigured_hostname(app: Flask) -> None: app.config["ALLOWED_TARGET_HOSTNAMES"] = ["dennisfink.me"] form = URLForm(MultiDict({"url": "https://example.com/post"})) - with app.app_context(), pytest.raises(ValidationError, match="Invalid input"): + with app.app_context(), pytest.raises( + ValidationError, match="Target hostname is not in ALLOWED_TARGET_HOSTNAMES" + ): AllowedTargetHostname()(form, form.url) diff --git a/tests/test_received_failure.py b/tests/test_received_failure.py new file mode 100644 index 0000000..095d7e1 --- /dev/null +++ b/tests/test_received_failure.py @@ -0,0 +1,53 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import pytest + +from webmentions_ssg.received_failure import describe_received_failure + + +@pytest.mark.parametrize( + ("reason", "expected_fragment", "local_step"), + [ + ("Source does not mention target", "link to the target URL", False), + ("Source returned HTTP 410", "permanently gone", False), + ("Source document is too large", "maximum response size", False), + ("Source resolves to a non-public address", "non-public address", False), + ("Source hostname could not be resolved", "DNS", False), + ("Source could not be fetched", "could not fetch", False), + ("Invalid source hostname", "valid hostname", False), + ("Source domain is blocked", "blocklist", True), + ("Source domain is not allowlisted", "allowed source-domain", True), + ("Verification recovery limit reached", "automatic recovery stopped", True), + ("Target hostname is not in ALLOWED_TARGET_HOSTNAMES", "target hostname", True), + ( + "Target hostname is not in ALLOWED_TARGET_HOSTNAMES.", + "target hostname", + True, + ), + ("Source returned HTTP 404", "not found", False), + ("Source returned HTTP 401", "denied access", False), + ("Source returned HTTP 403", "denied access", False), + ("Source returned HTTP 429", "limiting requests", False), + ("Source returned HTTP 503", "server error", False), + ("Source returned HTTP 400", "returned HTTP 400", False), + ( + "Unsupported source content type: application/json", + "application/json", + False, + ), + ], +) +def test_only_receiver_controlled_failures_suggest_steps( + reason: str, expected_fragment: str, local_step: bool +) -> None: + guidance = describe_received_failure(reason) + assert guidance is not None + assert expected_fragment in guidance.explanation + assert bool(guidance.next_step) is local_step + + +@pytest.mark.parametrize("reason", ["unknown failure", "", "Source returned HTTP 999"]) +def test_unknown_received_failures_are_not_misclassified(reason: str) -> None: + assert describe_received_failure(reason) is None diff --git a/tests/views/test_endpoint.py b/tests/views/test_endpoint.py index 4a2abde..cb6f426 100644 --- a/tests/views/test_endpoint.py +++ b/tests/views/test_endpoint.py @@ -50,6 +50,22 @@ def test_endpoint_returns_form_errors( assert response.get_json() == errors +def test_endpoint_explains_disallowed_target_hostname_without_storing_a_mention( + app: Flask, client: FlaskClient +) -> None: + response = client.post( + "/endpoint", + data={"source": SOURCE_URL, "target": "https://unconfigured.example/post"}, + ) + + assert response.status_code == HTTPStatus.BAD_REQUEST + assert response.get_json() == { + "target": ["Target hostname is not in ALLOWED_TARGET_HOSTNAMES."] + } + with app.app_context(): + assert db.session.scalar(sa.select(ReceivedWebmention)) is None + + def test_endpoint_creates_webmention( app: Flask, client: FlaskClient, diff --git a/tests/views/test_received.py b/tests/views/test_received.py index f23119d..5ae42f7 100644 --- a/tests/views/test_received.py +++ b/tests/views/test_received.py @@ -1122,6 +1122,115 @@ def test_received_detail_shows_full_reason_timestamps_and_public_status( assert "No historical transitions were recorded" in html +@pytest.mark.parametrize( + ("reason", "explanation", "next_step"), + [ + ( + "Source does not mention target", + "does not contain a recognized link", + None, + ), + ( + "Source returned HTTP 404", + "source page was not found", + None, + ), + ( + "Source returned HTTP 503", + "source server returned a server error", + None, + ), + ( + "Source hostname could not be resolved", + "could not be resolved through DNS", + None, + ), + ( + "Source document is too large", + "configured maximum response size", + None, + ), + ( + "Source domain is blocked", + "receiver's blocklist", + "Review the blocklist in Moderation", + ), + ( + "Source domain is not allowlisted", + "allowed source-domain list", + "Review the allowlist in Moderation", + ), + ( + "Verification recovery limit reached", + "automatic recovery stopped", + "Check the verification workers", + ), + ( + "Target hostname is not in ALLOWED_TARGET_HOSTNAMES", + "target hostname is not configured", + "Check ALLOWED_TARGET_HOSTNAMES", + ), + ], +) +def test_received_detail_only_suggests_receiver_controlled_steps( + app: Flask, + client: FlaskClient, + reason: str, + explanation: str, + next_step: str | None, +) -> None: + log_in(app, client) + identifier = create_received_webmention(app, status="failed", failure_reason=reason) + + response = client.get(f"/received/{identifier}") + + assert response.status_code == HTTPStatus.OK + soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") + assert explanation in soup.get_text() + if next_step is not None: + assert "Suggested next step:" in soup.get_text() + assert next_step in soup.get_text() + else: + assert "Suggested next step:" not in soup.get_text() + details = soup.find("details") + assert details is not None + assert reason in details.get_text() + + + +def test_received_detail_explains_historical_failure_and_escapes_raw_text( + app: Flask, client: FlaskClient +) -> None: + from webmentions_ssg.models.received import ( + ReceivedWebmentionStatus, + ReceivedWebmentionStatusEvent, + ) + + log_in(app, client) + identifier = create_received_webmention(app, status="verified") + raw_reason = "Unsupported source content type: text/" + with app.app_context(): + db.session.add( + ReceivedWebmentionStatusEvent( + webmention_uuid=identifier, + status=ReceivedWebmentionStatus.FAILED, + action="Verification completed", + failure_reason=raw_reason, + occurred_at=datetime(2026, 11, 10, tzinfo=UTC), + ) + ) + db.session.commit() + + response = client.get(f"/received/{identifier}") + html = response.get_data(as_text=True) + soup = BeautifulSoup(html, "html.parser") + assert "unsupported content type" in soup.get_text() + assert "Suggested next step:" not in soup.get_text() + assert raw_reason in soup.get_text() + assert "