diff options
| author | Dennis Fink | 2026-11-16 17:58:36 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-16 17:58:36 +0100 |
| commit | f7d846b66ae4286518ecc20537ba19c5afadf7f4 (patch) | |
| tree | b72e509875dccf21b43f83b92c4aa017df0de37b | |
| parent | 09353e2fd7a66987076f33f65ac4f0fc8831e887 (diff) | |
| download | webmentions-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/test_forms.py | 43 | ||||
| -rw-r--r-- | tests/views/helpers.py | 3 | ||||
| -rw-r--r-- | tests/views/test_admin.py | 264 | ||||
| -rw-r--r-- | webmentions_ssg/forms.py | 55 | ||||
| -rw-r--r-- | webmentions_ssg/templates/received.html | 38 | ||||
| -rw-r--r-- | webmentions_ssg/views/admin.py | 93 |
6 files changed, 485 insertions, 11 deletions
diff --git a/tests/test_forms.py b/tests/test_forms.py index 707ac09..7f09da6 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -13,6 +13,7 @@ from webmentions_ssg.forms import ( LoginForm, MaxURLBytes, RejectNonPublicIPLiteral, + ReceivedWebmentionFilterForm, WebmentionSubmissionForm, ) @@ -143,6 +144,48 @@ def test_login_form_accepts_credentials(app: Flask) -> None: assert form.validate() +def test_received_filter_form_accepts_valid_get_params(app: Flask) -> None: + with app.test_request_context("/received", method="GET"): + form = ReceivedWebmentionFilterForm( + formdata=MultiDict( + { + "status": "declined", + "q": "example.org", + "date_from": "2026-11-08", + "date_to": "2026-11-14", + } + ), + meta={"csrf": False}, + ) + assert form.validate() + assert form.status.data == "declined" + assert form.q.data == "example.org" + assert form.date_from.data.isoformat() == "2026-11-08" + assert form.date_to.data.isoformat() == "2026-11-14" + assert "csrf_token" not in form._fields + + +@pytest.mark.parametrize( + "data", + [ + {"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_filter_form_rejects_invalid_get_params( + app: Flask, data: dict[str, str] +) -> None: + with app.test_request_context("/received", method="GET"): + form = ReceivedWebmentionFilterForm( + formdata=MultiDict(data), meta={"csrf": False} + ) + assert not form.validate() + assert form.errors + + def test_admin_action_form_accepts_submission(app: Flask) -> None: with app.test_request_context(method="POST"): form = ConfirmActionForm(meta={"csrf": False}) diff --git a/tests/views/helpers.py b/tests/views/helpers.py index 3c74a1b..354102b 100644 --- a/tests/views/helpers.py +++ b/tests/views/helpers.py @@ -141,6 +141,7 @@ def create_received_webmention( target: str = TARGET_URL, status: str = "verified", failure_reason: str | None = None, + updated_at: datetime | None = None, ) -> uuid.UUID: with app.app_context(): webmention = ReceivedWebmention( @@ -150,6 +151,8 @@ def create_received_webmention( status=status, failure_reason=failure_reason, ) + if updated_at is not None: + webmention.updated_at = updated_at db.session.add(webmention) db.session.commit() 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}") diff --git a/webmentions_ssg/forms.py b/webmentions_ssg/forms.py index 1ce591d..d97cc69 100644 --- a/webmentions_ssg/forms.py +++ b/webmentions_ssg/forms.py @@ -9,9 +9,18 @@ import ipaddress import httpx from flask import current_app from flask_wtf import FlaskForm -from wtforms import Field, PasswordField, StringField, SubmitField, ValidationError +from wtforms import ( + DateField, + Field, + PasswordField, + SearchField, + SelectField, + StringField, + SubmitField, + ValidationError, +) from wtforms.form import BaseForm -from wtforms.validators import InputRequired, StopValidation +from wtforms.validators import InputRequired, Optional, StopValidation from .net import is_public_address, normalized_hostname from .validation import is_control_character @@ -165,6 +174,48 @@ class WebmentionSubmissionForm(FlaskForm): raise ValidationError(field.gettext("Field must not be equal to target.")) +class CanonicalDate: + """Require HTML date inputs to use the canonical YYYY-MM-DD format.""" + + def __call__(self, form: BaseForm, field: DateField) -> None: + if ( + field.data is not None + and field.raw_data + and field.raw_data[0] != field.data.isoformat() + ): + raise ValidationError(field.gettext("Invalid date.")) + + +class ReceivedWebmentionFilterForm(FlaskForm): + """GET filters for the received Webmention list.""" + + status = SelectField( + "Status", + choices={ + "": "All statuses", + "pending": "Pending", + "verifying": "Verifying", + "verified": "Verified", + "declined": "Declined", + }, + default="", + ) + q = SearchField("Source or target") + date_from = DateField( + "Updated from (UTC)", validators=[Optional(), CanonicalDate()] + ) + date_to = DateField("Updated to (UTC)", validators=[Optional(), CanonicalDate()]) + + def validate_date_to(self, field: DateField) -> None: + """Require the end date to be on or after the start date.""" + if ( + self.date_from.data is not None + and field.data is not None + and field.data < self.date_from.data + ): + raise ValidationError("End date must be on or after start date.") + + class ConfirmActionForm(FlaskForm): """Form for confirming an administrative action.""" diff --git a/webmentions_ssg/templates/received.html b/webmentions_ssg/templates/received.html index f94d104..9034ca8 100644 --- a/webmentions_ssg/templates/received.html +++ b/webmentions_ssg/templates/received.html @@ -39,6 +39,32 @@ SPDX-License-Identifier: BSD-3-Clause <h1 class="mb-0">Received Webmentions</h1> <span class="text-body-secondary">{{ webmentions.total }} total</span> </div> + <form action="{{ url_for('webmentions.received_list') }}" + method="get" + class="row g-2 align-items-end mb-4" + role="search"> + <div class="col-sm-6 col-lg"> + {{ filter_form.status.label(class="form-label") }} + {{ filter_form.status(class="form-select") }} + </div> + <div class="col-sm-6 col-lg"> + {{ filter_form.q.label(class="form-label") }} + {{ filter_form.q(class="form-control", placeholder="Search URLs") }} + </div> + <div class="col-sm-6 col-lg"> + {{ filter_form.date_from.label(class="form-label") }} + {{ filter_form.date_from(class="form-control") }} + </div> + <div class="col-sm-6 col-lg"> + {{ filter_form.date_to.label(class="form-label") }} + {{ filter_form.date_to(class="form-control") }} + </div> + <div class="col-auto d-flex gap-2 ms-auto"> + <button type="submit" class="btn btn-primary">Filter</button> + <a href="{{ url_for('webmentions.received_list') }}" + class="btn btn-outline-secondary">Clear</a> + </div> + </form> {% if webmentions.items %} <table class="table table-bordered table-striped table-hover align-middle"> <thead> @@ -79,7 +105,7 @@ SPDX-License-Identifier: BSD-3-Clause class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#confirmation-modal" - data-action="{{ url_for('webmentions.reverify_received_webmention', webmention_uuid=webmention.uuid, page=webmentions.page) }}" + data-action="{{ url_for('webmentions.reverify_received_webmention', webmention_uuid=webmention.uuid, page=webmentions.page, **filters) }}" data-title="Reverify webmention?" data-message="The webmention will be fetched and verified again." data-confirm-label="Reverify" @@ -88,7 +114,7 @@ SPDX-License-Identifier: BSD-3-Clause class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#confirmation-modal" - data-action="{{ url_for('webmentions.delete_received_webmention', webmention_uuid=webmention.uuid, page=webmentions.page) }}" + data-action="{{ url_for('webmentions.delete_received_webmention', webmention_uuid=webmention.uuid, page=webmentions.page, **filters) }}" data-title="Delete webmention?" data-message="This permanently deletes the webmention and cannot be undone." data-confirm-label="Delete" @@ -99,11 +125,15 @@ SPDX-License-Identifier: BSD-3-Clause </tbody> </table> {% if webmentions.pages > 1 %} - <div class="mt-3">{{ render_pagination(webmentions) }}</div> + <div class="mt-3">{{ render_pagination(webmentions, args=filters) }}</div> {% endif %} {% include "includes/confirmation_modal.html" %} {% else %} - <p class="text-body-secondary">No Webmentions have been received yet.</p> + {% if filters %} + <p class="text-body-secondary">No Webmentions match these filters.</p> + {% else %} + <p class="text-body-secondary">No Webmentions have been received yet.</p> + {% endif %} {% endif %} {% endblock %} diff --git a/webmentions_ssg/views/admin.py b/webmentions_ssg/views/admin.py index 23abe65..5246d3a 100644 --- a/webmentions_ssg/views/admin.py +++ b/webmentions_ssg/views/admin.py @@ -5,6 +5,7 @@ """Authenticated incoming and outgoing Webmention management.""" import uuid +from datetime import UTC, datetime, time from http import HTTPStatus import sqlalchemy as sa @@ -15,12 +16,49 @@ from sqlalchemy.orm import selectinload from .. import forms from ..extensions import db -from ..models import ReceivedWebmention, SentWebmention, SourceDocument +from ..models import ( + ReceivedWebmention, + ReceivedWebmentionStatus, + SentWebmention, + SourceDocument, +) from ..tasks.scanner import manual_scan_sources from ..tasks.sender import send_webmention from ..tasks.verification import verify_webmention from .blueprint import webmentions_bp +RECEIVED_STATUS_FILTERS = { + "pending": (ReceivedWebmentionStatus.RECEIVED,), + "verifying": (ReceivedWebmentionStatus.VERIFYING,), + "verified": (ReceivedWebmentionStatus.VERIFIED,), + "declined": ( + ReceivedWebmentionStatus.FAILED, + ReceivedWebmentionStatus.SOURCE_REMOVED, + ), +} + + +def received_filter_form() -> forms.ReceivedWebmentionFilterForm: + """Validate query-string filters without requiring CSRF on the GET form.""" + form = forms.ReceivedWebmentionFilterForm( + formdata=request.args, meta={"csrf": False} + ) + if not form.validate(): + abort(HTTPStatus.BAD_REQUEST) + form.q.data = (form.q.data or "").strip() + return form + + +def received_filter_args(form: forms.ReceivedWebmentionFilterForm) -> dict[str, str]: + """Return validated, nonempty filters for pagination and redirects.""" + values = { + "status": form.status.data, + "q": form.q.data, + "date_from": form.date_from.data.isoformat() if form.date_from.data else "", + "date_to": form.date_to.data.isoformat() if form.date_to.data else "", + } + return {key: value for key, value in values.items() if value} + @webmentions_bp.route("/received") @login_required @@ -29,15 +67,44 @@ def received_list() -> ResponseReturnValue: :returns: Rendered page containing the paginated received Webmentions. """ + filter_form = received_filter_form() + filters = received_filter_args(filter_form) + + query = sa.select(ReceivedWebmention) + + if status := filter_form.status.data: + query = query.where( + ReceivedWebmention.status.in_(RECEIVED_STATUS_FILTERS[status]) + ) + + if text := filter_form.q.data: + query = query.where( + sa.or_( + ReceivedWebmention.source.icontains(text, autoescape=True), + ReceivedWebmention.target.icontains(text, autoescape=True), + ) + ) + + if (start := filter_form.date_from.data) is not None: + query = query.where( + ReceivedWebmention.updated_at >= datetime.combine(start, time.min, UTC) + ) + + if (end := filter_form.date_to.data) is not None: + query = query.where( + ReceivedWebmention.updated_at <= datetime.combine(end, time.max, UTC) + ) + webmentions = db.paginate( - sa.select(ReceivedWebmention).order_by(ReceivedWebmention.uuid.desc()), - per_page=25, + query.order_by(ReceivedWebmention.uuid.desc()), per_page=25 ) return render_template( "received.html", title="Received Webmentions", webmentions=webmentions, + filters=filters, + filter_form=filter_form, action_form=forms.ConfirmActionForm(), ) @@ -53,6 +120,7 @@ def delete_received_webmention(webmention_uuid: uuid.UUID) -> ResponseReturnValu if not forms.ConfirmActionForm().validate_on_submit(): abort(HTTPStatus.BAD_REQUEST) + filters = received_filter_args(received_filter_form()) webmention = db.get_or_404(ReceivedWebmention, webmention_uuid) db.session.delete(webmention) @@ -61,7 +129,14 @@ def delete_received_webmention(webmention_uuid: uuid.UUID) -> ResponseReturnValu flash(f"Webmention {webmention.uuid} deleted.", "success") return redirect( - url_for("webmentions.received_list", page=request.args.get("page", 1, type=int)) + url_for( + "webmentions.received_list", + page=request.args.get("page", 1, type=int), + status=filters.get("status"), + q=filters.get("q"), + date_from=filters.get("date_from"), + date_to=filters.get("date_to"), + ) ) @@ -78,6 +153,7 @@ def reverify_received_webmention(webmention_uuid: uuid.UUID) -> ResponseReturnVa if not forms.ConfirmActionForm().validate_on_submit(): abort(HTTPStatus.BAD_REQUEST) + filters = received_filter_args(received_filter_form()) webmention = db.get_or_404(ReceivedWebmention, webmention_uuid) webmention.reopen_verification() @@ -89,7 +165,14 @@ def reverify_received_webmention(webmention_uuid: uuid.UUID) -> ResponseReturnVa flash(f"Webmention {webmention.uuid} queued for reverification.", "success") return redirect( - url_for("webmentions.received_list", page=request.args.get("page", 1, type=int)) + url_for( + "webmentions.received_list", + page=request.args.get("page", 1, type=int), + status=filters.get("status"), + q=filters.get("q"), + date_from=filters.get("date_from"), + date_to=filters.get("date_to"), + ) ) |
