aboutsummaryrefslogtreecommitdiff
path: root/tests/views/test_admin.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-16 17:58:36 +0100
committerDennis Fink2026-11-16 17:58:36 +0100
commitf7d846b66ae4286518ecc20537ba19c5afadf7f4 (patch)
treeb72e509875dccf21b43f83b92c4aa017df0de37b /tests/views/test_admin.py
parent09353e2fd7a66987076f33f65ac4f0fc8831e887 (diff)
downloadwebmentions-ssg-f7d846b66ae4286518ecc20537ba19c5afadf7f4.tar.gz
webmentions-ssg-f7d846b66ae4286518ecc20537ba19c5afadf7f4.zip
feat(receiver): add received Webmention filters
Add status, URL search, and updated-date filters to the received Webmention list with strict query validation and inclusive UTC date ranges. Preserve active filters across pagination, delete, and reverify actions.
Diffstat (limited to '')
-rw-r--r--tests/views/test_admin.py264
1 files changed, 264 insertions, 0 deletions
diff --git a/tests/views/test_admin.py b/tests/views/test_admin.py
index fa5575b..73265bd 100644
--- a/tests/views/test_admin.py
+++ b/tests/views/test_admin.py
@@ -2,11 +2,14 @@
#
# SPDX-License-Identifier: BSD-3-Clause
+import re
import uuid
from datetime import UTC, datetime, timedelta
+from html import unescape
from http import HTTPStatus
from types import ModuleType
from unittest.mock import Mock
+from urllib.parse import parse_qs, urlsplit
import pytest
from flask import Flask
@@ -109,6 +112,267 @@ def test_received_lists_webmentions(app: Flask, client: FlaskClient) -> None:
assert TARGET_URL in html
+@pytest.mark.parametrize(
+ ("filter_status", "expected_statuses"),
+ [
+ ("pending", {"received"}),
+ ("verifying", {"verifying"}),
+ ("verified", {"verified"}),
+ ("declined", {"failed", "source_removed"}),
+ ],
+)
+def test_received_filters_by_display_status(
+ app: Flask, client: FlaskClient, filter_status: str, expected_statuses: set[str]
+) -> None:
+ log_in(app, client)
+ statuses = ("received", "verifying", "verified", "failed", "source_removed")
+ for status in statuses:
+ create_received_webmention(
+ app, source=f"https://{status}.example/post", status=status
+ )
+
+ response = client.get("/received", query_string={"status": filter_status})
+
+ assert response.status_code == HTTPStatus.OK
+ html = response.get_data(as_text=True)
+ for status in statuses:
+ assert (f"https://{status}.example/post" in html) == (
+ status in expected_statuses
+ )
+ assert re.search(
+ rf'<option\b(?=[^>]*\bvalue="{filter_status}")(?=[^>]*\bselected\b)[^>]*>', html
+ )
+
+
+def test_received_get_filter_form_renders_wtforms_fields(
+ app: Flask, client: FlaskClient
+) -> None:
+ log_in(app, client)
+ response = client.get(
+ "/received",
+ query_string={
+ "status": "pending",
+ "q": "example.org",
+ "date_from": "2026-11-08",
+ "date_to": "2026-11-14",
+ },
+ )
+
+ assert response.status_code == HTTPStatus.OK
+ html = response.get_data(as_text=True)
+ for field in ("status", "q", "date_from", "date_to"):
+ assert f'name="{field}"' in html
+ assert 'type="search"' in html
+ assert html.count('type="date"') == 2
+ assert 'value="example.org"' in html
+ assert 'value="2026-11-08"' in html
+ assert 'value="2026-11-14"' in html
+ assert re.search(r'<form[^>]*method="get"[^>]*role="search"', html)
+
+
+def test_received_filters_source_or_target_as_literal_substring(
+ app: Flask, client: FlaskClient
+) -> None:
+ log_in(app, client)
+ create_received_webmention(
+ app, source="https://site.example/Alpha_10%", target="https://other.example/a"
+ )
+ create_received_webmention(
+ app, source="https://other.example/b", target="https://site.example/alpha_10%"
+ )
+ create_received_webmention(
+ app, source="https://site.example/alphaX100", target="https://other.example/c"
+ )
+
+ response = client.get("/received", query_string={"q": "ALPHA_10%"})
+
+ assert response.status_code == HTTPStatus.OK
+ html = response.get_data(as_text=True)
+ assert "https://other.example/a" in html
+ assert "https://other.example/b" in html
+ assert "https://other.example/c" not in html
+
+
+def test_received_filters_by_inclusive_updated_date_range(
+ app: Flask, client: FlaskClient
+) -> None:
+ log_in(app, client)
+ dates = {
+ "before": datetime(2026, 11, 7, 23, 59, 59, tzinfo=UTC),
+ "first": datetime(2026, 11, 8, tzinfo=UTC),
+ "last": datetime(2026, 11, 14, 23, 59, 59, 999999, tzinfo=UTC),
+ "after": datetime(2026, 11, 15, tzinfo=UTC),
+ }
+ for label, updated_at in dates.items():
+ create_received_webmention(
+ app,
+ source=f"https://{label}.example/post",
+ status="failed",
+ updated_at=updated_at,
+ )
+
+ response = client.get(
+ "/received",
+ query_string={
+ "status": "declined",
+ "date_from": "2026-11-08",
+ "date_to": "2026-11-14",
+ },
+ )
+
+ assert response.status_code == HTTPStatus.OK
+ html = response.get_data(as_text=True)
+ for label in ("first", "last"):
+ assert f"https://{label}.example/post" in html
+ for label in ("before", "after"):
+ assert f"https://{label}.example/post" not in html
+
+
+def test_received_filters_are_combined(app: Flask, client: FlaskClient) -> None:
+ log_in(app, client)
+ for status, source, day in (
+ ("failed", "https://match.example/a", 12),
+ ("verified", "https://match.example/b", 12),
+ ("failed", "https://other.example/c", 12),
+ ("failed", "https://match.example/d", 1),
+ ):
+ create_received_webmention(
+ app,
+ source=source,
+ status=status,
+ updated_at=datetime(2026, 11, day, tzinfo=UTC),
+ )
+
+ response = client.get(
+ "/received?status=declined&q=match.example&date_from=2026-11-08&date_to=2026-11-14"
+ )
+
+ html = response.get_data(as_text=True)
+ assert "https://match.example/a" in html
+ for suffix in ("b", "c", "d"):
+ assert f"/{suffix}</a>" not in html
+ assert "1 total" in html
+
+
+@pytest.mark.parametrize(
+ "query_string",
+ [
+ "status=unknown",
+ "date_from=not-a-date",
+ "date_from=2026-11-8",
+ "date_to=2026-02-30",
+ "date_from=2026-11-15&date_to=2026-11-08",
+ ],
+)
+def test_received_rejects_invalid_filters(
+ app: Flask, client: FlaskClient, query_string: str
+) -> None:
+ log_in(app, client)
+ assert client.get(f"/received?{query_string}").status_code == HTTPStatus.BAD_REQUEST
+
+
+def test_received_pagination_and_actions_keep_filters(
+ app: Flask, client: FlaskClient
+) -> None:
+ log_in(app, client)
+ for index in range(26):
+ create_received_webmention(
+ app,
+ source=f"https://match.example/{index}",
+ status="failed",
+ updated_at=datetime(2026, 11, 12, tzinfo=UTC),
+ )
+
+ filters = {
+ "status": "declined",
+ "q": "match.example",
+ "date_from": "2026-11-08",
+ "date_to": "2026-11-14",
+ }
+ response = client.get("/received", query_string=filters)
+
+ assert response.status_code == HTTPStatus.OK
+ html = response.get_data(as_text=True)
+ assert "26 total" in html
+ pagination_links = re.findall(r'href="([^"]+)"', html)
+ action_links = re.findall(r'data-action="([^"]+)"', html)
+ assert action_links
+ expected_filters = {key: [value] for key, value in filters.items()}
+ assert any(
+ urlsplit(unescape(link)).path == "/received"
+ and parse_qs(urlsplit(unescape(link)).query)
+ == {**expected_filters, "page": ["2"]}
+ for link in pagination_links
+ )
+ for link in action_links:
+ assert parse_qs(urlsplit(unescape(link)).query) == {
+ **expected_filters,
+ "page": ["1"],
+ }
+
+
+@pytest.mark.parametrize("action", ["delete", "reverify"])
+def test_received_action_redirect_keeps_filters(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+ action: str,
+) -> None:
+ log_in(app, client)
+ identifier = create_received_webmention(app)
+ mock_admin_action_form(views_module, monkeypatch, valid=True)
+ monkeypatch.setattr(views_module, "verify_webmention", Mock())
+
+ response = client.post(
+ f"/received/{identifier}/{action}",
+ query_string={
+ "page": 3,
+ "status": "declined",
+ "q": "example.org/a&b",
+ "date_from": "2026-11-08",
+ "date_to": "2026-11-14",
+ },
+ )
+
+ assert response.status_code == HTTPStatus.FOUND
+ redirect_url = urlsplit(response.headers["Location"])
+ assert redirect_url.path == "/received"
+ assert parse_qs(redirect_url.query) == {
+ "page": ["3"],
+ "status": ["declined"],
+ "q": ["example.org/a&b"],
+ "date_from": ["2026-11-08"],
+ "date_to": ["2026-11-14"],
+ }
+
+
+@pytest.mark.parametrize("action", ["delete", "reverify"])
+def test_received_action_rejects_invalid_filters_before_mutating(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+ action: str,
+) -> None:
+ log_in(app, client)
+ identifier = create_received_webmention(app, status="received")
+ mock_admin_action_form(views_module, monkeypatch, valid=True)
+ verify = Mock()
+ monkeypatch.setattr(views_module, "verify_webmention", verify)
+
+ response = client.post(
+ f"/received/{identifier}/{action}", query_string={"status": "invalid"}
+ )
+
+ assert response.status_code == HTTPStatus.BAD_REQUEST
+ with app.app_context():
+ row = db.session.get(ReceivedWebmention, identifier)
+ assert row is not None
+ assert row.status == "received"
+ verify.assert_not_called()
+
+
@pytest.mark.parametrize("action", ["delete", "reverify"])
def test_received_admin_actions_require_login(client: FlaskClient, action: str) -> None:
response = client.post(f"/received/{uuid.uuid7()}/{action}")