aboutsummaryrefslogtreecommitdiff
path: root/tests/views/test_sources.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/views/test_sources.py')
-rw-r--r--tests/views/test_sources.py366
1 files changed, 362 insertions, 4 deletions
diff --git a/tests/views/test_sources.py b/tests/views/test_sources.py
index 673fc9f..c851833 100644
--- a/tests/views/test_sources.py
+++ b/tests/views/test_sources.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 bs4 import BeautifulSoup
@@ -14,7 +17,11 @@ from flask import Flask
from flask.testing import FlaskClient
from webmentions_ssg import db
-from webmentions_ssg.models.sources import SentWebmention
+from webmentions_ssg.models.sources import (
+ SentWebmention,
+ SentWebmentionStatus,
+ SourceDocument,
+)
from webmentions_ssg.models.user import User
from .helpers import (
@@ -72,12 +79,12 @@ def test_sent_sources_table_hides_secondary_columns_on_mobile(
headers = table.select("thead th")
cells = table.select("tbody tr:first-child td")
assert len(headers) == len(cells) == 7
- for index in (1, 3, 4, 5): # URL, revision, created, last seen
+ for index in (2, 4, 5, 6): # URL, revision, created, last seen
for cell in (headers[index], cells[index]):
assert {"d-none", "d-md-table-cell"} <= set(cell.get("class", []))
- for index in (0, 2, 6): # Path, count, status stay visible
+ for index in (0, 1, 3): # Path, count, status stay visible
assert "d-none" not in cells[index].get("class", [])
- assert "text-break" in cells[0].get("class", [])
+ assert "text-break" in cells[1].get("class", [])
def test_sent_source_lists_webmentions(app: Flask, client: FlaskClient) -> None:
@@ -298,3 +305,354 @@ def test_admin_only_links_safe_existing_status_urls(
html = response.get_data(as_text=True)
assert (f'href="{status_url}"' in html) is safe
assert status_url in html
+
+
+def make_source(
+ app: Flask,
+ name: str,
+ *,
+ statuses: tuple[SentWebmentionStatus | None, ...] = (SentWebmentionStatus.SENT,),
+ retrying: bool = False,
+ processed: bool = True,
+ deleted: bool = False,
+ last_seen_at: datetime | None = None,
+ revision: int = 1,
+) -> uuid.UUID:
+ """Create distinguishable sources with outgoing rows for list filtering."""
+ now = last_seen_at or datetime(2026, 11, 12, 12, tzinfo=UTC)
+ with app.app_context():
+ source = SourceDocument(
+ path=f"blog/{name}/index.html",
+ url=f"https://source.example/{name}/",
+ content_hash="a" * 32,
+ revision=revision,
+ last_seen_at=now,
+ revised_at=now,
+ deleted_at=now if deleted else None,
+ )
+ for number, status in enumerate(statuses):
+ source.sent_webmentions.append(
+ SentWebmention(
+ target=f"https://target.example/{name}/{number}",
+ linked=True,
+ desired_revision=revision,
+ processed_revision=revision if processed else None,
+ status=status,
+ attempt_count=2 if retrying else 0,
+ next_retry_at=now + timedelta(days=10) if retrying else None,
+ )
+ )
+ db.session.add(source)
+ db.session.commit()
+ return source.uuid
+
+
+@pytest.mark.parametrize(
+ ("status", "expected"),
+ [
+ ("failing", {"failed", "retrying", "stale", "multiple"}),
+ ("retrying", {"retrying"}),
+ ("deleted", {"deleted"}),
+ ],
+)
+def test_sources_filter_by_delivery_state_without_duplicate_rows(
+ app: Flask, client: FlaskClient, status: str, expected: set[str]
+) -> None:
+ log_in(app, client)
+ make_source(app, "failed", statuses=(SentWebmentionStatus.FAILED,))
+ make_source(
+ app,
+ "retrying",
+ statuses=(SentWebmentionStatus.FAILED,),
+ retrying=True,
+ processed=False,
+ )
+ make_source(app, "stale", statuses=(SentWebmentionStatus.FAILED,), retrying=True)
+ make_source(
+ app,
+ "multiple",
+ statuses=(SentWebmentionStatus.FAILED, SentWebmentionStatus.FAILED),
+ )
+ make_source(app, "deleted", deleted=True)
+ make_source(app, "sent")
+ make_source(app, "pending", statuses=(None,), processed=False)
+
+ response = client.get("/sources", query_string={"status": status})
+ assert response.status_code == HTTPStatus.OK
+ soup = BeautifulSoup(response.get_data(as_text=True), "html.parser")
+ paths = [
+ row.select_one("td a").get_text(strip=True) for row in soup.select("tbody tr")
+ ]
+ assert set(paths) == {f"blog/{name}/index.html" for name in expected}
+ assert len(paths) == len(expected)
+ assert (
+ soup.select_one(f'select[name="status"] option[value="{status}"][selected]')
+ is not None
+ )
+
+
+@pytest.mark.parametrize(
+ ("q", "expected"),
+ [
+ ("blog/alpha", "alpha"),
+ ("source.example/alpha", "alpha"),
+ ("target.example/alpha", "alpha"),
+ ("alpha/0", "alpha"),
+ ("alpha%", None),
+ ],
+)
+def test_sources_search_matches_path_source_url_and_sent_target(
+ app: Flask, client: FlaskClient, q: str, expected: str | None
+) -> None:
+ log_in(app, client)
+ make_source(app, "alpha")
+ make_source(app, "beta")
+
+ response = client.get("/sources", query_string={"q": f" {q} "})
+ assert response.status_code == HTTPStatus.OK
+ html = response.get_data(as_text=True)
+ assert ("blog/alpha/index.html" in html) == (expected == "alpha")
+ assert "blog/beta/index.html" not in html
+ if expected is None:
+ assert "No sources match these filters." in html
+
+
+def test_sources_filter_by_inclusive_last_seen_dates(
+ app: Flask, client: FlaskClient
+) -> None:
+ log_in(app, client)
+ make_source(app, "old", last_seen_at=datetime(2026, 11, 7, tzinfo=UTC))
+ make_source(app, "included", last_seen_at=datetime(2026, 11, 10, 22, tzinfo=UTC))
+ make_source(app, "new", last_seen_at=datetime(2026, 11, 15, tzinfo=UTC))
+ response = client.get(
+ "/sources", query_string={"date_from": "2026-11-08", "date_to": "2026-11-10"}
+ )
+ assert response.status_code == HTTPStatus.OK
+ html = response.get_data(as_text=True)
+ assert "blog/included/index.html" in html
+ assert "blog/old/index.html" not in html
+ assert "blog/new/index.html" not in html
+
+
+@pytest.mark.parametrize(
+ "sort", ["path", "url", "webmentions", "revision", "created", "last_seen", "status"]
+)
+def test_sources_sort_links_preserve_filters(
+ app: Flask, client: FlaskClient, sort: str
+) -> None:
+ log_in(app, client)
+ make_source(app, "alpha", statuses=(SentWebmentionStatus.FAILED,))
+ response = client.get(
+ "/sources",
+ query_string={
+ "status": "failing",
+ "q": "alpha",
+ "date_from": "2026-11-01",
+ "sort": sort,
+ "order": "asc",
+ "per_page": 37,
+ },
+ )
+ assert response.status_code == HTTPStatus.OK
+ html = response.get_data(as_text=True)
+ soup = BeautifulSoup(html, "html.parser")
+ assert soup.select_one('th[aria-sort="ascending"]') is not None
+ links = [
+ parse_qs(urlsplit(unescape(a["href"])).query)
+ for a in soup.select("th a[href]")
+ if urlsplit(unescape(a["href"])).path == "/sources"
+ ]
+ assert len(links) == 7
+ for link in links:
+ assert link["status"] == ["failing"]
+ assert link["q"] == ["alpha"]
+ assert link["date_from"] == ["2026-11-01"]
+ assert link["per_page"] == ["37"]
+ assert link["sort"][0] in (
+ "path",
+ "url",
+ "webmentions",
+ "revision",
+ "created",
+ "last_seen",
+ "status",
+ )
+ assert any(link["sort"] == [sort] and link["order"] == ["desc"] for link in links)
+
+
+def test_sources_sort_orders_by_count_and_path(app: Flask, client: FlaskClient) -> None:
+ log_in(app, client)
+ make_source(app, "z", statuses=(SentWebmentionStatus.SENT,))
+ make_source(
+ app, "a", statuses=(SentWebmentionStatus.SENT, SentWebmentionStatus.FAILED)
+ )
+ for sort, order, expected_first in (
+ ("webmentions", "desc", "a"),
+ ("webmentions", "asc", "z"),
+ ("path", "asc", "a"),
+ ("path", "desc", "z"),
+ ):
+ response = client.get("/sources", query_string={"sort": sort, "order": order})
+ assert response.status_code == HTTPStatus.OK
+ soup = BeautifulSoup(response.get_data(as_text=True), "html.parser")
+ assert (
+ soup.select_one("tbody tr td a").get_text(strip=True)
+ == f"blog/{expected_first}/index.html"
+ )
+
+
+def test_sources_filter_pagination_and_navigation_preserve_parameters(
+ app: Flask, client: FlaskClient
+) -> None:
+ log_in(app, client)
+ for number in range(28):
+ make_source(app, f"failed-{number:02}", statuses=(SentWebmentionStatus.FAILED,))
+ filters = {
+ "status": "failing",
+ "q": "failed",
+ "sort": "path",
+ "order": "asc",
+ "per_page": "10",
+ }
+ response = client.get("/sources", query_string={**filters, "page": "2"})
+ assert response.status_code == HTTPStatus.OK
+ html = response.get_data(as_text=True)
+ assert "28 sources" in html
+ soup = BeautifulSoup(html, "html.parser")
+ assert len(soup.select("tbody tr")) == 10
+ urls = [unescape(a["href"]) for a in soup.select("a[href]")]
+ assert any(
+ urlsplit(url).path == "/sources"
+ and parse_qs(urlsplit(url).query)
+ == {**{k: [v] for k, v in filters.items()}, "page": ["3"]}
+ for url in urls
+ )
+ detail_urls = [
+ url for url in urls if re.search(r"/sources/[0-9a-f-]{36}", urlsplit(url).path)
+ ]
+ assert detail_urls
+ assert parse_qs(urlsplit(detail_urls[0]).query) == {
+ **{k: [v] for k, v in filters.items()},
+ "source_page": ["2"],
+ }
+ detail = client.get(detail_urls[0])
+ assert detail.status_code == HTTPStatus.OK
+ back = BeautifulSoup(detail.get_data(as_text=True), "html.parser").select_one(
+ 'a[href*="/sources?"]'
+ )
+ assert back is not None
+ assert parse_qs(urlsplit(unescape(back["href"])).query) == {
+ **{k: [v] for k, v in filters.items()},
+ "page": ["2"],
+ }
+
+
+@pytest.mark.parametrize(
+ "invalid",
+ [
+ "status=unknown",
+ "sort=unsupported",
+ "order=reverse",
+ "per_page=0",
+ "per_page=101",
+ "per_page=abc",
+ "date_from=2026-11-8",
+ "date_to=bad",
+ "date_from=2026-11-10&date_to=2026-11-01",
+ ],
+)
+def test_sources_bad_filters_return_400(
+ app: Flask, client: FlaskClient, invalid: str
+) -> None:
+ log_in(app, client)
+ assert client.get(f"/sources?{invalid}").status_code == HTTPStatus.BAD_REQUEST
+
+
+def test_rescan_sources_preserves_filter_query(
+ app: Flask,
+ client: FlaskClient,
+ sources_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ log_in(app, client)
+ mock_admin_action_form(sources_module, monkeypatch, valid=True)
+ scan = Mock()
+ monkeypatch.setattr(sources_module, "manual_scan_sources", scan)
+ response = client.post(
+ "/sources/rescan",
+ query_string={"status": "retrying", "per_page": 37, "page": 2},
+ )
+ assert response.status_code == HTTPStatus.FOUND
+ assert parse_qs(urlsplit(response.headers["Location"]).query) == {
+ "status": ["retrying"],
+ "per_page": ["37"],
+ "page": ["2"],
+ }
+ scan.assert_called_once_with()
+
+
+def test_sources_show_failed_and_retrying_counts_and_retry_deadline(
+ app: Flask, client: FlaskClient
+) -> None:
+ log_in(app, client)
+ identifier = make_source(
+ app,
+ "retry-badges",
+ statuses=(SentWebmentionStatus.FAILED,),
+ retrying=True,
+ processed=False,
+ )
+ response = client.get("/sources", query_string={"status": "retrying"})
+ assert response.status_code == HTTPStatus.OK
+ html = response.get_data(as_text=True)
+ assert "1 failed" in html
+ assert "1 awaiting retry" in html
+
+ response = client.get(f"/sources/{identifier}")
+ assert response.status_code == HTTPStatus.OK
+ assert "Retry scheduled after attempt 2:" in response.get_data(as_text=True)
+
+
+def test_resend_preserves_source_filter_context(
+ app: Flask,
+ client: FlaskClient,
+ sources_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ log_in(app, client)
+ source_id, mention_id = create_resendable_sent_webmention(app)
+ mock_admin_action_form(sources_module, monkeypatch, valid=True)
+ send = Mock()
+ monkeypatch.setattr(sources_module, "send_webmention", send)
+
+ response = client.post(
+ f"/sources/{source_id}/{mention_id}/resend",
+ query_string={
+ "status": "failing",
+ "q": "resend",
+ "sort": "path",
+ "source_page": 2,
+ "page": 3,
+ },
+ )
+ assert response.status_code == HTTPStatus.FOUND
+ assert urlsplit(response.headers["Location"]).path == f"/sources/{source_id}"
+ assert parse_qs(urlsplit(response.headers["Location"]).query) == {
+ "status": ["failing"],
+ "q": ["resend"],
+ "sort": ["path"],
+ "source_page": ["2"],
+ "page": ["3"],
+ }
+ send.assert_called_once_with(mention_id)
+
+
+def test_sources_empty_filter_result_keeps_rescan_modal(
+ app: Flask, client: FlaskClient
+) -> None:
+ log_in(app, client)
+ response = client.get("/sources", query_string={"status": "failing"})
+ assert response.status_code == HTTPStatus.OK
+ html = response.get_data(as_text=True)
+ assert "No sources match these filters." in html
+ assert 'id="confirmation-modal"' in html