# SPDX-FileCopyrightText: 2026 Dennis Fink # # 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 bs4 import BeautifulSoup from flask import Flask from flask.testing import FlaskClient from webmentions_ssg import db from webmentions_ssg.models import ReceivedWebmention, SentWebmention, User from .helpers import ( SOURCE_URL, TARGET_URL, create_outgoing, create_received_webmention, create_resendable_sent_webmention, create_sent_source, log_in, mock_admin_action_form, ) @pytest.fixture def views_module(app: Flask) -> ModuleType: _ = app from webmentions_ssg.views import admin as module return module @pytest.mark.parametrize("path", ["/sources", f"/sources/{uuid.uuid7()}"]) def test_sent_views_require_login(client: FlaskClient, path: str) -> None: response = client.get(path) assert response.status_code == HTTPStatus.FOUND assert "/login" in response.headers["Location"] def test_sent_lists_sources(app: Flask, client: FlaskClient) -> None: log_in(app, client) identifier = create_sent_source(app) response = client.get("/sources") assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) assert "blog/example/index.html" in html assert "https://dennisfink.me/blog/example/" in html assert f"/sources/{identifier}" in html def test_sent_source_lists_webmentions(app: Flask, client: FlaskClient) -> None: log_in(app, client) identifier = create_sent_source(app) response = client.get(f"/sources/{identifier}") assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) assert "blog/example/index.html" in html assert "https://dennisfink.me/blog/example/" in html first_target = "https://example.com/a" second_target = "https://example.com/b" assert first_target in html assert second_target in html assert html.index(first_target) < html.index(second_target) assert "Webmention endpoint returned HTTP 400" in html assert "https://example.com/webmention" in html def test_sent_source_returns_404_for_unknown_source( app: Flask, client: FlaskClient ) -> None: log_in(app, client) response = client.get(f"/sources/{uuid.uuid7()}") assert response.status_code == HTTPStatus.NOT_FOUND def test_received_requires_login(client: FlaskClient) -> None: response = client.get("/received") assert response.status_code == HTTPStatus.FOUND assert "/login" in response.headers["Location"] def test_received_lists_webmentions(app: Flask, client: FlaskClient) -> None: log_in(app, client) create_received_webmention(app) response = client.get("/received") assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) assert SOURCE_URL in html 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']*\bvalue="{filter_status}")(?=[^>]*\bselected\b)[^>]*>', html ) def test_received_filter_args_uses_typed_dict_fields( app: Flask, views_module: ModuleType ) -> None: with app.test_request_context( "/received", query_string={ "status": "declined", "q": " example.org ", "date_from": "2026-11-08", "date_to": "2026-11-14", "sort": "updated", "order": "asc", "per_page": "37", }, ): form = views_module.received_filter_form() assert set(views_module.ReceivedFilterArgs.__annotations__) == set(form._fields) assert views_module.received_filter_args(form) == { "status": "declined", "q": "example.org", "date_from": "2026-11-08", "date_to": "2026-11-14", "sort": "updated", "order": "asc", "per_page": "37", } @pytest.mark.parametrize("max_page_size", [10, 100]) def test_received_filter_args_omits_defaults_for_configured_page_size( app: Flask, views_module: ModuleType, max_page_size: int ) -> None: app.config["RECEIVED_MAX_PAGE_SIZE"] = max_page_size with app.test_request_context("/received"): form = views_module.received_filter_form() assert form.per_page.data == min(25, max_page_size) assert views_module.received_filter_args(form) == {} @pytest.mark.parametrize( ("sort", "order", "expected"), [ ("received", "desc", ("removed", "failed", "verified", "pending")), ("received", "asc", ("pending", "verified", "failed", "removed")), ("updated", "desc", ("verified", "failed", "pending", "removed")), ("updated", "asc", ("removed", "pending", "failed", "verified")), ("status", "asc", ("pending", "verified", "removed", "failed")), ("status", "desc", ("removed", "failed", "verified", "pending")), ], ) def test_received_sorting( app: Flask, client: FlaskClient, sort: str, order: str, expected: tuple[str, ...] ) -> None: log_in(app, client) for label, status, day in ( ("pending", "received", 12), ("verified", "verified", 14), ("failed", "failed", 13), ("removed", "source_removed", 11), ): create_received_webmention( app, source=f"https://{label}.example/post", status=status, updated_at=datetime(2026, 11, day, tzinfo=UTC), ) response = client.get("/received", query_string={"sort": sort, "order": order}) assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) indices = [html.index(f"https://{label}.example/post") for label in expected] assert indices == sorted(indices) assert f'aria-sort="{"ascending" if order == "asc" else "descending"}"' in html @pytest.mark.parametrize("column", ["source", "target"]) @pytest.mark.parametrize("order", ["asc", "desc"]) def test_received_sorts_by_source_or_target( app: Flask, client: FlaskClient, column: str, order: str ) -> None: log_in(app, client) # Deliberately create rows out of lexical order for both URL columns. for source_name, target_name in ( ("beta", "alpha"), ("alpha", "gamma"), ("gamma", "beta"), ): create_received_webmention( app, source=f"https://source.example/{source_name}", target=f"https://target.example/{target_name}", ) response = client.get("/received", query_string={"sort": column, "order": order}) assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) rows = re.findall(r"]*>.*?", html, flags=re.DOTALL) urls = re.findall(rf'', "".join(rows)) assert urls == sorted(urls, reverse=(order == "desc")) assert len(urls) == 3 assert f'aria-sort="{"ascending" if order == "asc" else "descending"}"' in html def test_received_sort_links_preserve_filters_and_page_size( app: Flask, client: FlaskClient ) -> None: log_in(app, client) create_received_webmention(app, source="https://some.example/post", status="failed") response = client.get( "/received", query_string={ "status": "declined", "q": "some.example", "sort": "updated", "order": "desc", "per_page": "37", }, ) assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) links = [ parse_qs(urlsplit(unescape(link)).query) for link in re.findall(r'href="([^"]+)"', html) if urlsplit(unescape(link)).path == "/received" ] for sort, order in ( ("received", "desc"), ("updated", "asc"), ("status", "asc"), ("source", "asc"), ("target", "asc"), ): assert { "status": ["declined"], "q": ["some.example"], "sort": [sort], "order": [order], "per_page": ["37"], } in links assert 'name="sort"' in html assert 'name="order"' in html assert 'name="per_page"' in html assert 'value="37"' in html def test_received_page_size_is_free_form_and_controls_pagination( app: Flask, client: FlaskClient ) -> None: log_in(app, client) for index in range(52): create_received_webmention(app, source=f"https://source.example/{index}") for page_size, expected_rows in ((25, 25), (37, 37), (50, 50), (100, 52)): response = client.get("/received", query_string={"per_page": page_size}) 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 ("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 ) def test_received_default_page_size_is_capped_when_max_is_below_25( app: Flask, client: FlaskClient ) -> None: log_in(app, client) app.config["RECEIVED_MAX_PAGE_SIZE"] = 10 for index in range(11): create_received_webmention(app, source=f"https://source.example/{index}") response = client.get("/received") assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) assert html.count('data-title="Reverify webmention?"') == 10 assert 'name="per_page"' in html assert 'value="10"' in html assert "page=2" in html def test_received_page_size_respects_configured_maximum( app: Flask, client: FlaskClient ) -> None: log_in(app, client) app.config["RECEIVED_MAX_PAGE_SIZE"] = 50 response = client.get("/received", query_string={"per_page": 50}) assert response.status_code == HTTPStatus.OK assert re.search( r']*name="per_page")(?=[^>]*max="50")', response.get_data(as_text=True), ) assert client.get("/received?per_page=51").status_code == HTTPStatus.BAD_REQUEST def test_received_get_filter_controls_render_serialized_values( 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", "sort": "updated", "order": "asc", "per_page": "37", }, ) assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) for field in ("status", "q", "date_from", "date_to", "sort", "order", "per_page"): 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 'name="sort" type="hidden" value="updated"' in html assert 'name="order" type="hidden" value="asc"' in html assert 'value="37"' in html assert re.search(r']*method="get"[^>]*role="search"', html) def test_received_get_filter_controls_render_defaults( app: Flask, client: FlaskClient ) -> None: log_in(app, client) response = client.get("/received") assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) assert 'name="sort" type="hidden" value="received"' in html assert 'name="order" type="hidden" value="desc"' in html assert 'value="25"' in html status_option = BeautifulSoup(html, "html.parser").select_one( 'select[name="status"] option[value=""][selected]' ) assert status_option is not None assert status_option.get_text(strip=True) == "All statuses" 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}" 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", "sort=unknown", "order=sideways", "per_page=0", "per_page=-1", "per_page=101", "per_page=abc", "per_page=1.5", ], ) 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"], } def test_received_pagination_and_actions_keep_sort_and_page_size( app: Flask, client: FlaskClient ) -> None: log_in(app, client) for index in range(38): create_received_webmention(app, source=f"https://source.example/{index}") response = client.get( "/received", query_string={"sort": "updated", "order": "asc", "per_page": 37} ) assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) expected = {"sort": ["updated"], "order": ["asc"], "per_page": ["37"]} assert any( urlsplit(unescape(link)).path == "/received" and parse_qs(urlsplit(unescape(link)).query) == {**expected, "page": ["2"]} for link in re.findall(r'href="([^"]+)"', html) ) for link in re.findall(r'data-action="([^"]+)"', html): assert parse_qs(urlsplit(unescape(link)).query) == {**expected, "page": ["1"]} @pytest.mark.parametrize("action", ["delete", "reverify"]) def test_received_action_redirect_keeps_sort_and_page_size( 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, "sort": "updated", "order": "asc", "per_page": 37}, ) assert response.status_code == HTTPStatus.FOUND assert parse_qs(urlsplit(response.headers["Location"]).query) == { "page": ["3"], "sort": ["updated"], "order": ["asc"], "per_page": ["37"], } @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"]) @pytest.mark.parametrize("invalid", ["sort=invalid", "order=invalid", "per_page=101"]) def test_received_action_rejects_invalid_sort_and_page_size_before_mutating( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, action: str, invalid: 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}?{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}") assert response.status_code == HTTPStatus.FOUND assert "/login" in response.headers["Location"] @pytest.mark.parametrize("action", ["delete", "reverify"]) def test_received_admin_actions_reject_invalid_form( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, action: str, ) -> None: log_in(app, client) identifier = create_received_webmention(app) form = mock_admin_action_form(views_module, monkeypatch, valid=False) response = client.post(f"/received/{identifier}/{action}") assert response.status_code == HTTPStatus.BAD_REQUEST form.validate_on_submit.assert_called_once_with() @pytest.mark.parametrize("action", ["delete", "reverify"]) def test_received_admin_actions_return_404_for_unknown_webmention( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, action: str, ) -> None: log_in(app, client) mock_admin_action_form(views_module, monkeypatch, valid=True) response = client.post(f"/received/{uuid.uuid7()}/{action}") assert response.status_code == HTTPStatus.NOT_FOUND def test_delete_received_webmention( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, ) -> None: log_in(app, client) identifier = create_received_webmention(app) mock_admin_action_form(views_module, monkeypatch, valid=True) response = client.post(f"/received/{identifier}/delete?page=3") assert response.status_code == HTTPStatus.FOUND assert response.headers["Location"].endswith("/received?page=3") with app.app_context(): assert db.session.get(ReceivedWebmention, identifier) is None def test_reverify_received_webmention( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, ) -> None: log_in(app, client) identifier = create_received_webmention( app, status="failed", failure_reason="Previous failure" ) with app.app_context(): row = db.session.get(ReceivedWebmention, identifier) assert row is not None row.recovery_attempts = 3 db.session.commit() mock_admin_action_form(views_module, monkeypatch, valid=True) verify_webmention = Mock() monkeypatch.setattr(views_module, "verify_webmention", verify_webmention) response = client.post(f"/received/{identifier}/reverify?page=2") assert response.status_code == HTTPStatus.FOUND assert response.headers["Location"].endswith("/received?page=2") with app.app_context(): webmention = db.session.get(ReceivedWebmention, identifier) assert webmention is not None assert webmention.status == "received" assert webmention.failure_reason is None assert webmention.recovery_attempts == 0 verify_webmention.assert_called_once_with(identifier) def test_received_bulk_toolbar_and_shared_confirmation_modal( app: Flask, client: FlaskClient ) -> None: log_in(app, client) first = create_received_webmention(app) second = create_received_webmention(app, source="https://other.example/post") response = client.get("/received") assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) assert html.count('class="form-check-input received-select"') == 2 assert f'value="{first}"' in html assert f'value="{second}"' in html assert 'id="received-select-all"' in html assert 'id="received-selected-count"' in html assert 'data-received-bulk-action="reverify"' in html assert 'data-received-bulk-action="delete"' in html assert 'data-title="Delete all declined webmentions?"' in html assert html.count('id="confirmation-modal"') == 1 assert 'id="confirmation-modal-form"' in html assert 'received-bulk-actions.js' in html def test_received_delete_all_declined_available_without_matching_rows( app: Flask, client: FlaskClient ) -> None: log_in(app, client) create_received_webmention(app, status="verified") response = client.get("/received", query_string={"status": "declined"}) assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) assert 'data-title="Delete all declined webmentions?"' in html assert 'id="confirmation-modal-form"' in html assert 'data-received-bulk-action="delete"' not in html assert 'id="received-select-all"' not in html @pytest.mark.parametrize( "path", ["/received/bulk/reverify", "/received/bulk/delete", "/received/declined/delete"], ) def test_received_bulk_actions_require_login(client: FlaskClient, path: str) -> None: response = client.post(path) assert response.status_code == HTTPStatus.FOUND assert "/login" in response.headers["Location"] @pytest.mark.parametrize( "path", ["/received/bulk/reverify", "/received/bulk/delete", "/received/declined/delete"], ) def test_received_bulk_actions_reject_invalid_form( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, path: str, ) -> None: log_in(app, client) identifier = create_received_webmention(app, status="failed") form = mock_admin_action_form(views_module, monkeypatch, valid=False) response = client.post(path, data={"webmention_uuids": str(identifier)}) assert response.status_code == HTTPStatus.BAD_REQUEST form.validate_on_submit.assert_called_once_with() with app.app_context(): assert db.session.get(ReceivedWebmention, identifier).status == "failed" @pytest.mark.parametrize( "path", ["/received/bulk/reverify", "/received/bulk/delete", "/received/declined/delete"], ) def test_received_bulk_actions_reject_invalid_filters_before_mutating( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, path: str, ) -> None: log_in(app, client) identifier = create_received_webmention(app, status="failed") mock_admin_action_form(views_module, monkeypatch, valid=True) verify = Mock() monkeypatch.setattr(views_module, "verify_webmention", verify) response = client.post( path, query_string={"sort": "invalid"}, data={"webmention_uuids": str(identifier)}, ) assert response.status_code == HTTPStatus.BAD_REQUEST with app.app_context(): assert db.session.get(ReceivedWebmention, identifier).status == "failed" verify.assert_not_called() @pytest.mark.parametrize("action", ["reverify", "delete"]) @pytest.mark.parametrize("selection", [[], ["bad-uuid"]]) def test_received_bulk_actions_reject_empty_or_malformed_selection( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, action: str, selection: list[str], ) -> None: log_in(app, client) identifier = create_received_webmention(app, status="failed") mock_admin_action_form(views_module, monkeypatch, valid=True) verify = Mock() monkeypatch.setattr(views_module, "verify_webmention", verify) response = client.post( f"/received/bulk/{action}", data={"webmention_uuids": selection} ) assert response.status_code == HTTPStatus.BAD_REQUEST with app.app_context(): assert db.session.get(ReceivedWebmention, identifier).status == "failed" verify.assert_not_called() @pytest.mark.parametrize("action", ["reverify", "delete"]) def test_received_bulk_actions_reject_duplicate_oversized_or_stale_selection( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, action: str, ) -> None: log_in(app, client) first = create_received_webmention(app, status="failed") second = create_received_webmention(app, source="https://other.example/post") mock_admin_action_form(views_module, monkeypatch, valid=True) verify = Mock() monkeypatch.setattr(views_module, "verify_webmention", verify) path = f"/received/bulk/{action}" duplicate = client.post(path, data={"webmention_uuids": [str(first)] * 2}) assert duplicate.status_code == HTTPStatus.BAD_REQUEST stale = client.post( path, data={"webmention_uuids": [str(first), str(uuid.uuid7())]} ) assert stale.status_code == HTTPStatus.NOT_FOUND app.config["RECEIVED_MAX_PAGE_SIZE"] = 1 oversized = client.post( path, data={"webmention_uuids": [str(first), str(second)]} ) assert oversized.status_code == HTTPStatus.BAD_REQUEST with app.app_context(): assert db.session.get(ReceivedWebmention, first).status == "failed" assert db.session.get(ReceivedWebmention, second) is not None verify.assert_not_called() def test_delete_selected_received_webmentions( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, ) -> None: log_in(app, client) first = create_received_webmention(app, status="failed") second = create_received_webmention(app, source="https://other.example/post") unselected = create_received_webmention(app, source="https://third.example/post") mock_admin_action_form(views_module, monkeypatch, valid=True) response = client.post( "/received/bulk/delete", query_string={"page": 3, "sort": "source", "order": "asc", "per_page": 37}, data={"webmention_uuids": [str(first), str(second)]}, ) assert response.status_code == HTTPStatus.FOUND assert parse_qs(urlsplit(response.headers["Location"]).query) == { "page": ["3"], "sort": ["source"], "order": ["asc"], "per_page": ["37"], } with app.app_context(): assert db.session.get(ReceivedWebmention, first) is None assert db.session.get(ReceivedWebmention, second) is None assert db.session.get(ReceivedWebmention, unselected) is not None def test_reverify_selected_received_webmentions( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, ) -> None: log_in(app, client) first = create_received_webmention(app, status="failed", failure_reason="Failure") second = create_received_webmention( app, source="https://other.example/post", status="source_removed" ) unselected = create_received_webmention( app, source="https://third.example/post", status="failed" ) mock_admin_action_form(views_module, monkeypatch, valid=True) verify = Mock() monkeypatch.setattr(views_module, "verify_webmention", verify) response = client.post( "/received/bulk/reverify", query_string={"status": "declined", "page": 2}, data={"webmention_uuids": [str(first), str(second)]}, ) assert response.status_code == HTTPStatus.FOUND assert parse_qs(urlsplit(response.headers["Location"]).query) == { "status": ["declined"], "page": ["2"], } with app.app_context(): for identifier in (first, second): webmention = db.session.get(ReceivedWebmention, identifier) assert webmention is not None assert webmention.status == "received" assert webmention.failure_reason is None assert webmention.recovery_attempts == 0 assert db.session.get(ReceivedWebmention, unselected).status == "failed" assert verify.call_count == 2 assert {call.args[0] for call in verify.call_args_list} == {first, second} def test_delete_all_declined_received_webmentions_ignores_age_and_filters( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, ) -> None: log_in(app, client) failed = create_received_webmention(app, status="failed", updated_at=datetime.now(UTC)) removed = create_received_webmention( app, source="https://other.example/post", status="source_removed", updated_at=datetime.now(UTC) - timedelta(days=60), ) retained = [ create_received_webmention( app, source=f"https://{status}.example/post", status=status ) for status in ("received", "verifying", "verified") ] mock_admin_action_form(views_module, monkeypatch, valid=True) response = client.post( "/received/declined/delete", query_string={"page": 4, "status": "verified", "q": "no-match"}, ) assert response.status_code == HTTPStatus.FOUND assert parse_qs(urlsplit(response.headers["Location"]).query) == { "page": ["4"], "status": ["verified"], "q": ["no-match"], } with app.app_context(): assert db.session.get(ReceivedWebmention, failed) is None assert db.session.get(ReceivedWebmention, removed) is None for identifier in retained: assert db.session.get(ReceivedWebmention, identifier) is not None def test_delete_all_declined_received_webmentions_when_none_exist( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, ) -> None: log_in(app, client) identifier = create_received_webmention(app, status="verified") mock_admin_action_form(views_module, monkeypatch, valid=True) response = client.post("/received/declined/delete") assert response.status_code == HTTPStatus.FOUND with app.app_context(): assert db.session.get(ReceivedWebmention, identifier) is not None def test_received_bulk_delete_requires_real_csrf_token( app: Flask, client: FlaskClient ) -> None: log_in(app, client) identifier = create_received_webmention(app, status="failed") app.config["WTF_CSRF_ENABLED"] = True invalid = client.post( "/received/bulk/delete", data={"webmention_uuids": str(identifier)} ) assert invalid.status_code == HTTPStatus.BAD_REQUEST html = client.get("/received").get_data(as_text=True) match = re.search( r']*name="csrf_token")(?=[^>]*value="([^"]+)")[^>]*>', html, ) assert match is not None valid = client.post( "/received/bulk/delete", data={"webmention_uuids": str(identifier), "csrf_token": match.group(1)}, ) assert valid.status_code == HTTPStatus.FOUND with app.app_context(): assert db.session.get(ReceivedWebmention, identifier) is None def test_rescan_sent_sources_requires_login(client: FlaskClient) -> None: response = client.post("/sources/rescan") assert response.status_code == HTTPStatus.FOUND assert "/login" in response.headers["Location"] @pytest.mark.parametrize("valid", [False, True]) def test_rescan_sent_sources( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, valid: bool, ) -> None: log_in(app, client) form = mock_admin_action_form(views_module, monkeypatch, valid=valid) manual_scan_sources = Mock() monkeypatch.setattr(views_module, "manual_scan_sources", manual_scan_sources) response = client.post("/sources/rescan") assert response.status_code == ( HTTPStatus.FOUND if valid else HTTPStatus.BAD_REQUEST ) form.validate_on_submit.assert_called_once_with() if valid: assert response.headers["Location"].endswith("/sources") manual_scan_sources.assert_called_once_with() else: manual_scan_sources.assert_not_called() def test_resend_sent_webmention_requires_login(client: FlaskClient) -> None: response = client.post(f"/sources/{uuid.uuid7()}/{uuid.uuid7()}/resend") assert response.status_code == HTTPStatus.FOUND assert "/login" in response.headers["Location"] def test_resend_sent_webmention_rejects_invalid_form( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, ) -> None: log_in(app, client) source_identifier, webmention_identifier = create_resendable_sent_webmention(app) mock_admin_action_form(views_module, monkeypatch, valid=False) response = client.post( f"/sources/{source_identifier}/{webmention_identifier}/resend" ) assert response.status_code == HTTPStatus.BAD_REQUEST def test_resend_sent_webmention_returns_404_for_unknown_webmention( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, ) -> None: log_in(app, client) source_identifier = create_sent_source(app) mock_admin_action_form(views_module, monkeypatch, valid=True) response = client.post(f"/sources/{source_identifier}/{uuid.uuid7()}/resend") assert response.status_code == HTTPStatus.NOT_FOUND def test_resend_sent_webmention_rejects_wrong_source( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, ) -> None: log_in(app, client) _, webmention_identifier = create_resendable_sent_webmention(app) wrong_source_identifier = create_sent_source(app) mock_admin_action_form(views_module, monkeypatch, valid=True) response = client.post( f"/sources/{wrong_source_identifier}/{webmention_identifier}/resend" ) assert response.status_code == HTTPStatus.NOT_FOUND def test_resend_sent_webmention_reopens_current_revision( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, ) -> None: log_in(app, client) source_identifier, webmention_identifier = create_resendable_sent_webmention(app) mock_admin_action_form(views_module, monkeypatch, valid=True) send_webmention = Mock() monkeypatch.setattr(views_module, "send_webmention", send_webmention) with app.app_context(): webmention = db.session.get(SentWebmention, webmention_identifier) assert webmention is not None webmention.attempt_count = 6 webmention.next_retry_at = datetime.now(UTC) + timedelta(hours=24) db.session.commit() response = client.post( f"/sources/{source_identifier}/{webmention_identifier}/resend" ) assert response.status_code == HTTPStatus.FOUND assert response.headers["Location"].endswith(f"/sources/{source_identifier}") with app.app_context(): webmention = db.session.get(SentWebmention, webmention_identifier) assert webmention is not None assert webmention.desired_revision == 3 assert webmention.processed_revision is None assert webmention.attempt_count == 0 assert webmention.next_retry_at is None assert webmention.sent_revision == 2 assert webmention.status is None assert webmention.failure_reason is None assert webmention.webmention_endpoint is None assert webmention.endpoint_response_status is None assert webmention.status_url is None send_webmention.assert_called_once_with(webmention_identifier) @pytest.mark.parametrize( ("status_url", "safe"), [ ("https://example.com/status", True), ("http://example.com/status", True), ("javascript:alert(1)", False), ("data:text/html,evil", False), ("ftp://example.com/status", False), ], ) def test_admin_only_links_safe_existing_status_urls( app: Flask, client: FlaskClient, status_url: str, safe: bool ) -> None: source_identifier, _ = create_outgoing(app, status_url=status_url) with app.app_context(): user = User(username="admin", password="test") db.session.add(user) db.session.commit() user_id = user.get_id() with client.session_transaction() as session: session["_user_id"] = str(user_id) session["_fresh"] = True response = client.get(f"/sources/{source_identifier}") assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) assert (f'href="{status_url}"' in html) is safe assert status_url in html