diff options
| author | Dennis Fink | 2026-11-16 22:21:19 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-16 22:21:19 +0100 |
| commit | 354a5b449077478b141683bafbab47c9dc169d21 (patch) | |
| tree | b2d0be9cb848faf2f9944bb69a63f9fd9a857dbd /tests/views/test_admin.py | |
| parent | f7d846b66ae4286518ecc20537ba19c5afadf7f4 (diff) | |
| download | webmentions-ssg-354a5b449077478b141683bafbab47c9dc169d21.tar.gz webmentions-ssg-354a5b449077478b141683bafbab47c9dc169d21.zip | |
feat(receiver): add sorting and page size controls
Allow received Webmentions to be sorted by status, source, target,
received time, or updated time with stable pagination and accessible
sortable headers.
Add configurable per-page sizing and preserve sorting, filters, and page
size across pagination and administrative actions.
Diffstat (limited to 'tests/views/test_admin.py')
| -rw-r--r-- | tests/views/test_admin.py | 309 |
1 files changed, 307 insertions, 2 deletions
diff --git a/tests/views/test_admin.py b/tests/views/test_admin.py index 73265bd..e82d94d 100644 --- a/tests/views/test_admin.py +++ b/tests/views/test_admin.py @@ -144,7 +144,209 @@ def test_received_filters_by_display_status( ) -def test_received_get_filter_form_renders_wtforms_fields( +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"<tr[^>]*>.*?</tr>", html, flags=re.DOTALL) + urls = re.findall(rf'<a href="(https://{column}\.example/[^"]+)">', "".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'<input\b(?=[^>]*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) @@ -155,21 +357,41 @@ def test_received_get_filter_form_renders_wtforms_fields( "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"): + 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'<form[^>]*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 + assert re.search(r'<option value="" selected>All statuses</option>', html) + + def test_received_filters_source_or_target_as_literal_substring( app: Flask, client: FlaskClient ) -> None: @@ -262,6 +484,13 @@ def test_received_filters_are_combined(app: Flask, client: FlaskClient) -> None: "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( @@ -311,6 +540,56 @@ def test_received_pagination_and_actions_keep_filters( } +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, @@ -374,6 +653,32 @@ def test_received_action_rejects_invalid_filters_before_mutating( @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}") |
