aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDennis Fink2026-11-17 19:43:04 +0100
committerDennis Fink2026-11-17 19:43:04 +0100
commit5e24e8307f650e72881f747661a2b6e52db15f2d (patch)
tree585b456b9439a17957e8633f566c0725bb9d8595 /tests
parent6d6d079a3a99d3d3a922a640b6925eb2adf044de (diff)
downloadwebmentions-ssg-5e24e8307f650e72881f747661a2b6e52db15f2d.tar.gz
webmentions-ssg-5e24e8307f650e72881f747661a2b6e52db15f2d.zip
feat(receiver): add Webmention detail and status history
Add an authenticated detail view with full failure information, timestamps, actions, public status links, and recorded lifecycle transitions. Persist verification and resubmission events, backfill existing rows during migration, and clean up history when received Webmentions are deleted.
Diffstat (limited to '')
-rw-r--r--tests/tasks/test_retention.py39
-rw-r--r--tests/tasks/test_verification.py53
-rw-r--r--tests/views/test_endpoint.py49
-rw-r--r--tests/views/test_moderation.py12
-rw-r--r--tests/views/test_received.py204
5 files changed, 338 insertions, 19 deletions
diff --git a/tests/tasks/test_retention.py b/tests/tasks/test_retention.py
index a3891a9..f32f2cb 100644
--- a/tests/tasks/test_retention.py
+++ b/tests/tasks/test_retention.py
@@ -67,3 +67,42 @@ def test_retention_uses_admin_configured_days(app: Flask) -> None:
)
db.session.commit()
assert purge_old_received_webmentions() == 1
+
+
+def test_retention_removes_only_deleted_webmention_history(app: Flask) -> None:
+ from webmentions_ssg.models.received import (
+ ReceivedWebmentionStatus,
+ ReceivedWebmentionStatusEvent,
+ )
+
+ app.config["RECEIVED_RETENTION_DAYS"] = 7
+ now = datetime.now(UTC)
+ expired = uuid.uuid7()
+ kept = uuid.uuid7()
+ with app.app_context():
+ for identifier, source, age in (
+ (expired, "https://source.example/expired", 10),
+ (kept, "https://source.example/kept", 1),
+ ):
+ mention = ReceivedWebmention(
+ uuid=identifier,
+ source=source,
+ target="https://dennisfink.me/target",
+ status=ReceivedWebmentionStatus.FAILED,
+ updated_at=now - timedelta(days=age),
+ )
+ mention.status_events.append(
+ ReceivedWebmentionStatusEvent(
+ webmention_uuid=identifier,
+ status=ReceivedWebmentionStatus.FAILED,
+ action="Verification completed",
+ occurred_at=now - timedelta(days=age),
+ )
+ )
+ db.session.add(mention)
+ db.session.commit()
+
+ assert purge_old_received_webmentions() == 1
+ history = db.session.scalars(sa.select(ReceivedWebmentionStatusEvent)).all()
+ assert len(history) == 1
+ assert history[0].webmention_uuid == kept
diff --git a/tests/tasks/test_verification.py b/tests/tasks/test_verification.py
index 3547464..f36da44 100644
--- a/tests/tasks/test_verification.py
+++ b/tests/tasks/test_verification.py
@@ -781,3 +781,56 @@ def test_source_mentions_target_falls_back_for_unknown_encoding(
assert verification.source_mentions_target(SOURCE_URL, TARGET_URL)
fetch_source_document.assert_called_once_with(SOURCE_URL)
+
+
+def test_verification_records_claim_and_outcome_history(
+ app: Flask, verification: ModuleType
+) -> None:
+ from webmentions_ssg.models.received import (
+ ReceivedWebmentionStatus,
+ ReceivedWebmentionStatusEvent,
+ )
+
+ identifier = create_webmention(app, recovery_attempts=2)
+ with app.app_context():
+ claim = verification.claim_for_verification(identifier)
+ assert claim is not None
+ stored = verification.store_verification_outcome(
+ claim, ReceivedWebmentionStatus.FAILED, "Full verification failure"
+ )
+ assert stored
+ history = db.session.scalars(
+ sa.select(ReceivedWebmentionStatusEvent)
+ .where(ReceivedWebmentionStatusEvent.webmention_uuid == identifier)
+ .order_by(ReceivedWebmentionStatusEvent.id)
+ ).all()
+ assert [event.status for event in history] == [
+ ReceivedWebmentionStatus.VERIFYING,
+ ReceivedWebmentionStatus.FAILED,
+ ]
+ assert [event.recovery_attempts for event in history] == [2, 2]
+ assert history[1].failure_reason == "Full verification failure"
+
+
+def test_stale_recovery_records_history_and_attempt_count(
+ app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ from webmentions_ssg.models.received import ReceivedWebmentionStatusEvent
+
+ identifier = create_webmention(app, status="verifying", recovery_attempts=1)
+ set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
+ enqueue = Mock()
+ monkeypatch.setattr(verification, "verify_webmention", enqueue)
+
+ with app.app_context():
+ assert verification.recover_stale_verifications() == 1
+ events = db.session.scalars(
+ sa.select(ReceivedWebmentionStatusEvent).where(
+ ReceivedWebmentionStatusEvent.webmention_uuid == identifier
+ )
+ ).all()
+ assert len(events) == 1
+ assert events[0].action == "Stale verification recovered"
+ assert events[0].status == "received"
+ assert events[0].recovery_attempts == 2
+ enqueue.assert_called_once_with(identifier)
diff --git a/tests/views/test_endpoint.py b/tests/views/test_endpoint.py
index 23643d9..4a2abde 100644
--- a/tests/views/test_endpoint.py
+++ b/tests/views/test_endpoint.py
@@ -6,7 +6,7 @@ import uuid
from datetime import UTC, datetime, timedelta
from http import HTTPStatus
from types import ModuleType
-from unittest.mock import ANY, Mock
+from unittest.mock import Mock
import pytest
import sqlalchemy as sa
@@ -227,9 +227,9 @@ def test_endpoint_recovers_from_concurrent_insert(
assert response.headers["Location"].endswith(f"/status/{competing_identifier}")
assert execute.call_count == 3
- add.assert_called_once_with(ANY)
- rollback.assert_called_once_with()
- assert commit.call_count == 2
+ assert add.call_count == (2 if reopened else 1)
+ assert rollback.call_count == (1 if reopened else 2)
+ assert commit.call_count == (2 if reopened else 1)
if reopened:
verify_webmention.assert_called_once_with(competing_identifier)
@@ -469,3 +469,44 @@ def test_endpoint_rejects_bad_url_before_inserting_row(
)
== 0
)
+
+
+def test_endpoint_records_initial_and_resubmission_events(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ from webmentions_ssg.models.received import ReceivedWebmentionStatusEvent
+
+ mock_endpoint_form(views_module, monkeypatch, valid=True)
+ monkeypatch.setattr(views_module, "verify_webmention", Mock())
+
+ response = client.post("/endpoint")
+ assert response.status_code == HTTPStatus.CREATED
+ with app.app_context():
+ mention = db.session.scalar(sa.select(ReceivedWebmention))
+ assert mention is not None
+ identifier = mention.uuid
+ events = db.session.scalars(
+ sa.select(ReceivedWebmentionStatusEvent).where(
+ ReceivedWebmentionStatusEvent.webmention_uuid == identifier
+ )
+ ).all()
+ assert [event.action for event in events] == ["Received submission"]
+ mention.status = "failed"
+ mention.failure_reason = "Previous failure"
+ mention.updated_at = datetime.now(UTC) - timedelta(minutes=11)
+ db.session.commit()
+
+ assert client.post("/endpoint").status_code == HTTPStatus.CREATED
+ with app.app_context():
+ events = db.session.scalars(
+ sa.select(ReceivedWebmentionStatusEvent)
+ .where(ReceivedWebmentionStatusEvent.webmention_uuid == identifier)
+ .order_by(ReceivedWebmentionStatusEvent.id)
+ ).all()
+ assert [event.action for event in events] == [
+ "Received submission",
+ "Resubmitted after cooldown",
+ ]
diff --git a/tests/views/test_moderation.py b/tests/views/test_moderation.py
index 7f683d4..26b7cc4 100644
--- a/tests/views/test_moderation.py
+++ b/tests/views/test_moderation.py
@@ -96,18 +96,6 @@ def test_invalid_list_kind_and_missing_entry(app: Flask, client: FlaskClient) ->
assert client.post("/moderation/blocked/123/delete").status_code == 404
-def test_received_row_can_block_domain(app: Flask, client: FlaskClient) -> None:
- log_in(app, client)
- mention_id = create_received_webmention(app, source="https://spam.example/article")
- response = client.get("/received")
- assert b"Block this domain" in response.data
- response = client.post(f"/received/{mention_id}/block-source")
- assert response.status_code == HTTPStatus.FOUND
- with app.app_context():
- rule = db.session.scalar(sa.select(BlockedDomain))
- assert rule is not None and rule.domain == "spam.example"
-
-
def test_block_selected_source_domains_deduplicates_and_preserves_mentions(
app: Flask, client: FlaskClient
) -> None:
diff --git a/tests/views/test_received.py b/tests/views/test_received.py
index ec00e05..f23119d 100644
--- a/tests/views/test_received.py
+++ b/tests/views/test_received.py
@@ -12,6 +12,7 @@ from unittest.mock import Mock
from urllib.parse import parse_qs, urlsplit
import pytest
+import sqlalchemy as sa
from bs4 import BeautifulSoup
from flask import Flask
from flask.testing import FlaskClient
@@ -247,13 +248,14 @@ def test_received_page_size_is_free_form_and_controls_pagination(
assert response.status_code == HTTPStatus.OK
html = response.get_data(as_text=True)
assert "52 total" in html
- assert html.count('data-title="Reverify webmention?"') == expected_rows
+ assert html.count('aria-label="Details for Webmention ') == expected_rows
assert ("page=2" in html) == (page_size < 52)
response = client.get("/received", query_string={"per_page": 37, "page": 2})
assert response.status_code == HTTPStatus.OK
assert (
- response.get_data(as_text=True).count('data-title="Reverify webmention?"') == 15
+ response.get_data(as_text=True).count('aria-label="Details for Webmention ')
+ == 15
)
@@ -269,7 +271,7 @@ def test_received_default_page_size_is_capped_when_max_is_below_25(
assert response.status_code == HTTPStatus.OK
html = response.get_data(as_text=True)
- assert html.count('data-title="Reverify webmention?"') == 10
+ assert html.count('aria-label="Details for Webmention ') == 10
assert 'name="per_page"' in html
assert 'value="10"' in html
assert "page=2" in html
@@ -1061,3 +1063,199 @@ def test_received_bulk_delete_requires_real_csrf_token(
assert valid.status_code == HTTPStatus.FOUND
with app.app_context():
assert db.session.get(ReceivedWebmention, identifier) is None
+
+
+def test_received_detail_requires_login_and_returns_404(
+ app: Flask, client: FlaskClient
+) -> None:
+ identifier = create_received_webmention(app)
+ assert client.get(f"/received/{identifier}").status_code == HTTPStatus.FOUND
+
+ log_in(app, client)
+ assert client.get(f"/received/{uuid.uuid7()}").status_code == HTTPStatus.NOT_FOUND
+
+
+def test_received_list_links_to_details_without_showing_failure_text(
+ app: Flask, client: FlaskClient
+) -> None:
+ log_in(app, client)
+ identifier = create_received_webmention(
+ app,
+ status="failed",
+ failure_reason="Failure details should only appear on the detail page",
+ )
+
+ response = client.get("/received", query_string={"status": "declined"})
+ assert response.status_code == HTTPStatus.OK
+ html = response.get_data(as_text=True)
+ assert f"/received/{identifier}?page=1&amp;status=declined" in html
+ assert "Failure details should only appear on the detail page" not in html
+ assert 'data-title="Reverify webmention?"' not in html
+ assert 'data-received-bulk-action="reverify"' in html
+
+
+def test_received_detail_shows_full_reason_timestamps_and_public_status(
+ app: Flask, client: FlaskClient
+) -> None:
+ log_in(app, client)
+ reason = "First line & <unsafe>\n" + ("very-long-failure-reason-" * 30)
+ identifier = create_received_webmention(app, status="failed", failure_reason=reason)
+ with app.app_context():
+ mention = db.session.get(ReceivedWebmention, identifier)
+ assert mention is not None
+ mention.recovery_attempts = 2
+ db.session.commit()
+
+ response = client.get(f"/received/{identifier}")
+ assert response.status_code == HTTPStatus.OK
+ html = response.get_data(as_text=True)
+ soup = BeautifulSoup(html, "html.parser")
+ assert SOURCE_URL in html and TARGET_URL in html
+ assert "Recovery attempts" in html
+ assert "(automatic stale-verification recoveries" in html
+ assert "2" in soup.get_text()
+ assert reason in soup.get_text()
+ assert "<unsafe>" not in html
+ assert "&lt;unsafe&gt;" in html
+ assert "Received" in html and "Last updated" in html
+ assert f'href="http://localhost/status/{identifier}"' in html
+ assert "No historical transitions were recorded" in html
+
+
+def test_received_detail_keeps_filters_and_returns_after_actions(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ log_in(app, client)
+ identifier = create_received_webmention(app, status="failed")
+ query = {
+ "page": "3",
+ "status": "declined",
+ "q": "source.example",
+ "sort": "updated",
+ "order": "asc",
+ "per_page": "37",
+ }
+
+ response = client.get(f"/received/{identifier}", query_string=query)
+ html = response.get_data(as_text=True)
+ soup = BeautifulSoup(html, "html.parser")
+ back_link = soup.find(
+ "a", string=lambda value: bool(value and "Back to received" in value)
+ )
+ assert back_link is not None
+ assert parse_qs(urlsplit(back_link["href"]).query) == {
+ key: [value] for key, value in query.items()
+ }
+ action = soup.find(attrs={"data-title": "Reverify webmention?"})
+ assert action is not None
+ action_url = action["data-action"]
+ assert parse_qs(urlsplit(action_url).query) == {
+ **{key: [value] for key, value in query.items()},
+ "return_to": ["detail"],
+ }
+
+ mock_admin_action_form(views_module, monkeypatch, valid=True)
+ monkeypatch.setattr(views_module, "verify_webmention", Mock())
+ posted = client.post(action_url)
+ assert posted.status_code == HTTPStatus.FOUND
+ redirect_url = urlsplit(posted.headers["Location"])
+ assert redirect_url.path == f"/received/{identifier}"
+ assert parse_qs(redirect_url.query) == {
+ key: [value] for key, value in query.items()
+ }
+
+ with app.app_context():
+ from webmentions_ssg.models.received import ReceivedWebmentionStatusEvent
+
+ history = db.session.scalars(
+ sa.select(ReceivedWebmentionStatusEvent).where(
+ ReceivedWebmentionStatusEvent.webmention_uuid == identifier
+ )
+ ).all()
+ assert len(history) == 1
+ assert history[0].status == "received"
+ assert history[0].action == "Manually queued for reverification"
+
+
+def test_received_detail_shows_recorded_history_in_reverse_order(
+ app: Flask, client: FlaskClient
+) -> None:
+ from webmentions_ssg.models.received import (
+ ReceivedWebmentionStatus,
+ ReceivedWebmentionStatusEvent,
+ )
+
+ log_in(app, client)
+ identifier = create_received_webmention(app, status="failed")
+ with app.app_context():
+ db.session.add(
+ ReceivedWebmentionStatusEvent(
+ webmention_uuid=identifier,
+ status=ReceivedWebmentionStatus.FAILED,
+ action="Previous verification failed",
+ failure_reason="Temporary HTTP 503",
+ recovery_attempts=2,
+ occurred_at=datetime(2026, 11, 10, tzinfo=UTC),
+ )
+ )
+ db.session.add(
+ ReceivedWebmentionStatusEvent(
+ webmention_uuid=identifier,
+ status=ReceivedWebmentionStatus.RECEIVED,
+ action="Stale verification recovered",
+ recovery_attempts=3,
+ occurred_at=datetime(2026, 11, 11, tzinfo=UTC),
+ )
+ )
+ db.session.commit()
+
+ response = client.get(f"/received/{identifier}")
+ html = response.get_data(as_text=True)
+ assert html.index("Stale verification recovered") < html.index(
+ "Previous verification failed"
+ )
+ assert "Temporary HTTP 503" in html
+ assert "Recovery attempts: 3" in html
+ assert "2026-11-10 00:00:00 UTC" in html
+
+
+def test_received_bulk_delete_preserves_only_remaining_history(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ from webmentions_ssg.models.received import (
+ ReceivedWebmentionStatus,
+ ReceivedWebmentionStatusEvent,
+ )
+
+ log_in(app, client)
+ first = create_received_webmention(app, status="failed")
+ second = create_received_webmention(
+ app, source="https://other.example/post", status="failed"
+ )
+ with app.app_context():
+ for identifier in (first, second):
+ db.session.add(
+ ReceivedWebmentionStatusEvent(
+ webmention_uuid=identifier,
+ status=ReceivedWebmentionStatus.FAILED,
+ action="Verification completed",
+ occurred_at=datetime.now(UTC),
+ )
+ )
+ db.session.commit()
+
+ mock_admin_action_form(views_module, monkeypatch, valid=True)
+ response = client.post(
+ "/received/bulk/delete", data={"webmention_uuids": [str(first)]}
+ )
+ assert response.status_code == HTTPStatus.FOUND
+ with app.app_context():
+ history = db.session.scalars(sa.select(ReceivedWebmentionStatusEvent)).all()
+ assert len(history) == 1
+ assert history[0].webmention_uuid == second