diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/views/test_sent.py | 409 |
1 files changed, 393 insertions, 16 deletions
diff --git a/tests/views/test_sent.py b/tests/views/test_sent.py index 9f9e65f..644fc6f 100644 --- a/tests/views/test_sent.py +++ b/tests/views/test_sent.py @@ -12,6 +12,7 @@ from unittest.mock import Mock from urllib.parse import parse_qs, urlsplit import pytest +import sqlalchemy as sa from bs4 import BeautifulSoup, Tag from flask import Flask from flask.testing import FlaskClient @@ -20,6 +21,7 @@ from webmentions_ssg import db from webmentions_ssg.models.sources import ( SentWebmention, SentWebmentionStatus, + SentWebmentionStatusEvent, SourceDocument, ) from webmentions_ssg.models.user import User @@ -90,15 +92,18 @@ def test_sent_source_documents_table_hides_secondary_columns_on_mobile( assert table is not None headers = table.select("thead th") cells = table.select("tbody tr:first-child td") - assert len(headers) == len(cells) == 7 + assert len(headers) == len(cells) == 8 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_attribute_list("class") ) - for index in (0, 1, 3): # Path, count, status stay visible + for index in (0, 1, 3, 7): # Status, path, counts, details stay visible assert "d-none" not in cells[index].get_attribute_list("class") assert "text-break" in cells[1].get_attribute_list("class") + details = cells[7].select_one('a.btn[href^="/sent/"]') + assert details is not None + assert details.get_text(strip=True) == "Details" def test_sent_source_lists_webmentions(app: Flask, client: FlaskClient) -> None: @@ -211,9 +216,7 @@ def test_resend_sent_webmention_rejects_invalid_form( source_identifier, webmention_identifier = create_resendable_sent_webmention(app) mock_admin_action_form(sent_module, monkeypatch, valid=False) - response = client.post( - f"/sent/{source_identifier}/{webmention_identifier}/resend" - ) + response = client.post(f"/sent/{source_identifier}/{webmention_identifier}/resend") assert response.status_code == HTTPStatus.BAD_REQUEST @@ -270,9 +273,7 @@ def test_resend_sent_webmention_reopens_current_revision( webmention.next_retry_at = datetime.now(UTC) + timedelta(hours=24) db.session.commit() - response = client.post( - f"/sent/{source_identifier}/{webmention_identifier}/resend" - ) + response = client.post(f"/sent/{source_identifier}/{webmention_identifier}/resend") assert response.status_code == HTTPStatus.FOUND assert response.headers["Location"].endswith(f"/sent/{source_identifier}") @@ -290,6 +291,15 @@ def test_resend_sent_webmention_reopens_current_revision( assert webmention.webmention_endpoint is None assert webmention.endpoint_response_status is None assert webmention.status_url is None + events = db.session.scalars( + sa.select(SentWebmentionStatusEvent).where( + SentWebmentionStatusEvent.webmention_uuid == webmention_identifier + ) + ).all() + assert len(events) == 1 + assert events[0].status is None + assert events[0].action == "Manually queued for resend" + assert events[0].revision == 3 send_webmention.assert_called_once_with(webmention_identifier) @@ -400,9 +410,9 @@ def test_sent_filter_by_delivery_state_without_duplicate_rows( soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") paths = [] for row in soup.select("tbody tr"): - link = row.select_one("td a") - assert link is not None - paths.append(link.get_text(strip=True)) + path = row.select_one("td:nth-child(2)") + assert path is not None + paths.append(path.get_text(strip=True)) assert set(paths) == {f"blog/{name}/index.html" for name in expected} assert len(paths) == len(expected) assert ( @@ -515,9 +525,9 @@ def test_sent_sort_orders_by_count_and_path(app: Flask, client: FlaskClient) -> response = client.get("/sent", query_string={"sort": sort, "order": order}) assert response.status_code == HTTPStatus.OK soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") - link = soup.select_one("tbody tr td a") - assert link is not None - assert link.get_text(strip=True) == f"blog/{expected_first}/index.html" + path = soup.select_one("tbody tr td:nth-child(2)") + assert path is not None + assert path.get_text(strip=True) == f"blog/{expected_first}/index.html" def test_sent_filter_pagination_and_navigation_preserve_parameters( @@ -598,8 +608,7 @@ def test_rescan_sources_preserves_filter_query( scan = Mock() monkeypatch.setattr(sent_module, "manual_scan_sources", scan) response = client.post( - "/sent/rescan", - query_string={"status": "retrying", "per_page": 37, "page": 2}, + "/sent/rescan", query_string={"status": "retrying", "per_page": 37, "page": 2} ) assert response.status_code == HTTPStatus.FOUND assert parse_qs(urlsplit(response.headers["Location"]).query) == { @@ -706,3 +715,371 @@ def test_sent_empty_filter_result_keeps_rescan_modal( html = response.get_data(as_text=True) assert "No source documents match these filters." in html assert 'id="confirmation-modal"' in html + + +def test_sent_list_and_source_use_confirmation_actions_and_detail_history( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + source_id, mention_id = create_resendable_sent_webmention(app) + with app.app_context(): + mention = db.session.get(SentWebmention, mention_id) + assert mention is not None + mention.record_status_event("Delivery failed") + db.session.commit() + + listing = BeautifulSoup(client.get("/sent").get_data(as_text=True), "html.parser") + button = listing.select_one('button[data-action="/sent/failed/resend"]') + assert button is not None + assert button.get("data-bs-target") == "#confirmation-modal" + details = listing.select_one('tbody a.btn[href^="/sent/"]') + assert details is not None + assert urlsplit(_href(details)).path == f"/sent/{source_id}" + + detail = BeautifulSoup( + client.get(f"/sent/{source_id}").get_data(as_text=True), "html.parser" + ) + assert detail.select_one("section.card #source-details-heading") is not None + assert detail.select_one("section #sent-webmentions-heading") is not None + assert detail.select_one("section #status-history-heading") is not None + history = detail.select("#status-history-heading ~ ol li") + assert len(history) == 1 + assert "Delivery failed" in history[0].get_text() + assert "https://example.com/resend" in history[0].get_text() + assert detail.select_one("section table tbody tr") is not None + resend = detail.select_one('button[data-action*="resend-all"]') + assert resend is not None + assert resend.get_text(strip=True) == "Resend all" + + +@pytest.mark.parametrize( + "path", ["/sent/failed/resend", f"/sent/{uuid.uuid7()}/resend-all"] +) +def test_bulk_resend_requires_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("valid", [False, True]) +def test_global_resend_all_failed_ignores_filters_and_preserves_other_statuses( + app: Flask, + client: FlaskClient, + sent_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + valid: bool, +) -> None: + log_in(app, client) + first = make_source( + app, "first", statuses=(SentWebmentionStatus.FAILED, SentWebmentionStatus.SENT) + ) + second = make_source( + app, + "second", + statuses=(SentWebmentionStatus.FAILED,), + retrying=True, + processed=False, + ) + make_source(app, "third", statuses=(SentWebmentionStatus.NO_ENDPOINT, None)) + mock_admin_action_form(sent_module, monkeypatch, valid=valid) + send = Mock() + monkeypatch.setattr(sent_module, "send_webmention", send) + + response = client.post( + "/sent/failed/resend", + query_string={"status": "deleted", "q": "no-match", "page": 2}, + ) + assert response.status_code == ( + HTTPStatus.FOUND if valid else HTTPStatus.BAD_REQUEST + ) + if valid: + assert parse_qs(urlsplit(response.headers["Location"]).query) == { + "status": ["deleted"], + "q": ["no-match"], + "page": ["2"], + } + with app.app_context(): + all_mentions = db.session.scalars(sa.select(SentWebmention)).all() + affected = [m for m in all_mentions if m.source_uuid in (first, second)] + failed = [m for m in affected if m.target.endswith("/0")] + untouched = [m for m in all_mentions if m not in failed] + assert len(failed) == 2 + for mention in failed: + assert (mention.status is None) is valid + if valid: + assert mention.processed_revision is None + assert mention.attempt_count == 0 + assert mention.next_retry_at is None + assert any(m.status == SentWebmentionStatus.SENT for m in untouched) + assert any(m.status == SentWebmentionStatus.NO_ENDPOINT for m in untouched) + events = db.session.scalars(sa.select(SentWebmentionStatusEvent)).all() + assert len(events) == (2 if valid else 0) + assert {event.webmention_uuid for event in events} == ( + {m.uuid for m in failed} if valid else set() + ) + assert {call.args[0] for call in send.call_args_list} == ( + {m.uuid for m in failed} if valid else set() + ) + assert send.call_count == (2 if valid else 0) + + +def test_source_resend_all_includes_successful_and_pending_but_only_this_source( + app: Flask, + client: FlaskClient, + sent_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + source_id = make_source( + app, + "bulk", + statuses=( + SentWebmentionStatus.SENT, + SentWebmentionStatus.FAILED, + SentWebmentionStatus.NO_ENDPOINT, + None, + ), + ) + other_source = make_source(app, "other", statuses=(SentWebmentionStatus.FAILED,)) + mock_admin_action_form(sent_module, monkeypatch, valid=True) + send = Mock() + monkeypatch.setattr(sent_module, "send_webmention", send) + + response = client.post( + f"/sent/{source_id}/resend-all", + query_string={ + "status": "failing", + "source_page": 2, + "page": 3, + "history_page": 4, + }, + ) + assert response.status_code == HTTPStatus.FOUND + assert urlsplit(response.headers["Location"]).path == f"/sent/{source_id}" + assert parse_qs(urlsplit(response.headers["Location"]).query) == { + "status": ["failing"], + "source_page": ["2"], + "page": ["3"], + "history_page": ["4"], + } + with app.app_context(): + mentions = db.session.scalars( + sa.select(SentWebmention).where(SentWebmention.source_uuid == source_id) + ).all() + assert len(mentions) == 4 + assert all(m.status is None and m.processed_revision is None for m in mentions) + assert all(m.attempt_count == 0 for m in mentions) + assert all(m.sent_revision is None for m in mentions) + other = db.session.scalar( + sa.select(SentWebmention).where(SentWebmention.source_uuid == other_source) + ) + assert other is not None and other.status == SentWebmentionStatus.FAILED + history = db.session.scalars(sa.select(SentWebmentionStatusEvent)).all() + assert {event.webmention_uuid for event in history} == { + m.uuid for m in mentions + } + assert {call.args[0] for call in send.call_args_list} == {m.uuid for m in mentions} + assert send.call_count == 4 + + +def test_source_resend_all_rejects_unknown_source_and_invalid_form( + app: Flask, + client: FlaskClient, + sent_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + source_id = make_source(app, "protected", statuses=(SentWebmentionStatus.FAILED,)) + form = mock_admin_action_form(sent_module, monkeypatch, valid=False) + send = Mock() + monkeypatch.setattr(sent_module, "send_webmention", send) + assert ( + client.post(f"/sent/{source_id}/resend-all").status_code + == HTTPStatus.BAD_REQUEST + ) + form.validate_on_submit.assert_called_once_with() + mock_admin_action_form(sent_module, monkeypatch, valid=True) + assert ( + client.post(f"/sent/{uuid.uuid7()}/resend-all").status_code + == HTTPStatus.NOT_FOUND + ) + with app.app_context(): + mention = db.session.scalar(sa.select(SentWebmention)) + assert mention is not None and mention.status == SentWebmentionStatus.FAILED + send.assert_not_called() + + +def test_sent_source_history_is_paginated_independently_of_webmentions( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + source_id, mention_id = create_resendable_sent_webmention(app) + with app.app_context(): + mention = db.session.get(SentWebmention, mention_id) + assert mention is not None + for n in range(27): + mention.record_status_event(f"Historical delivery {n}") + db.session.commit() + + first_page = client.get(f"/sent/{source_id}") + assert first_page.status_code == HTTPStatus.OK + soup = BeautifulSoup(first_page.get_data(as_text=True), "html.parser") + history = soup.select("#status-history-heading ~ ol li") + assert len(history) == 25 + assert "Historical delivery 26" in history[0].get_text() + assert "Historical delivery 0" not in first_page.get_data(as_text=True) + links = [_href(link) for link in soup.select("#status-history-heading ~ div a")] + assert any("history_page=2" in link for link in links) + + second_page = client.get(f"/sent/{source_id}?history_page=2") + assert second_page.status_code == HTTPStatus.OK + soup = BeautifulSoup(second_page.get_data(as_text=True), "html.parser") + history = soup.select("#status-history-heading ~ ol li") + assert len(history) == 2 + assert "Historical delivery 0" in history[-1].get_text() + assert len(soup.select("section table tbody tr")) == 1 + + +def test_sent_source_history_links_keep_webmentions_page( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + source_id, mention_id = create_resendable_sent_webmention(app) + with app.app_context(): + source = db.session.get(SourceDocument, source_id) + mention = db.session.get(SentWebmention, mention_id) + assert source is not None and mention is not None + source.sent_webmentions.extend( + SentWebmention( + target=f"https://example.org/extra/{number}", + linked=True, + desired_revision=3, + ) + for number in range(25) + ) + for number in range(27): + mention.record_status_event(f"History event {number}") + db.session.commit() + + response = client.get(f"/sent/{source_id}?page=2&source_page=3") + assert response.status_code == HTTPStatus.OK + soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") + history_links = [ + (_href(link), parse_qs(urlsplit(_href(link)).query)) + for link in soup.select("#status-history-heading ~ div a") + if urlsplit(_href(link)).path == f"/sent/{source_id}" + ] + next_history = next( + href + for href, query in history_links + if query.get("history_page") == ["2"] + ) + query = parse_qs(urlsplit(next_history).query) + assert query["page"] == ["2"] + assert query["source_page"] == ["3"] + + following = client.get(next_history) + assert following.status_code == HTTPStatus.OK + next_soup = BeautifulSoup(following.get_data(as_text=True), "html.parser") + assert len(next_soup.select("#status-history-heading ~ ol li")) == 2 + assert len(next_soup.select("section table tbody tr")) == 1 + + +def test_sent_source_explains_current_failed_delivery( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + identifier = create_sent_source(app) + + response = client.get(f"/sent/{identifier}") + + assert response.status_code == HTTPStatus.OK + soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") + assert "Webmention endpoint rejected the submission" in soup.get_text() + assert "Check that your published source page links" in soup.get_text() + assert "Suggested next step:" in soup.get_text() + reason = soup.find("details") + assert reason is not None + assert "Webmention endpoint returned HTTP 400" in reason.get_text() + # Keep the existing target table and its resend action. + assert soup.select_one(".table-responsive table tbody tr button[data-action]") + + +def test_sent_source_explains_historical_failures_and_escapes_raw_reason( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + identifier = create_sent_source(app) + raw_reason = "Unexpected transport failure <script>alert('x')</script>" + with app.app_context(): + mention = db.session.scalar( + sa.select(SentWebmention).where(SentWebmention.source_uuid == identifier) + ) + assert mention is not None + db.session.add_all( + [ + SentWebmentionStatusEvent( + webmention_uuid=mention.uuid, + status=SentWebmentionStatus.FAILED, + action="Delivery failed; automatic retry scheduled", + revision=2, + attempt_count=1, + failure_reason="Webmention endpoint returned HTTP 503", + endpoint_response_status=503, + occurred_at=datetime(2026, 11, 12, tzinfo=UTC), + ), + SentWebmentionStatusEvent( + webmention_uuid=mention.uuid, + status=SentWebmentionStatus.FAILED, + action="Delivery failed", + revision=2, + attempt_count=2, + failure_reason=raw_reason, + occurred_at=datetime(2026, 11, 13, tzinfo=UTC), + ), + ] + ) + db.session.commit() + + response = client.get(f"/sent/{identifier}") + + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + soup = BeautifulSoup(html, "html.parser") + history = soup.select_one("#status-history-heading") + assert history is not None + assert "Webmention endpoint returned a server error" in soup.get_text() + assert "Automatic retries run while attempts remain" in soup.get_text() + assert raw_reason in soup.get_text() + assert "Check the application logs for details before resending." in soup.get_text() + assert "<script>" not in html + assert "<script>" in html + + +def test_sent_source_explains_no_endpoint_without_suggesting_remote_edits( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + identifier = create_sent_source(app) + with app.app_context(): + mention = db.session.scalar( + sa.select(SentWebmention).where(SentWebmention.source_uuid == identifier) + ) + assert mention is not None + mention.status = SentWebmentionStatus.NO_ENDPOINT + mention.failure_reason = "No Webmention endpoint discovered" + db.session.commit() + + response = client.get(f"/sent/{identifier}") + + assert response.status_code == HTTPStatus.OK + soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") + assert "does not advertise a usable Webmention endpoint" in soup.get_text() + assert "No Webmention endpoint discovered" in soup.get_text() + # The unrelated failed mention may have guidance; the no-endpoint row must not. + no_endpoint_row = next( + row + for row in soup.select(".table-responsive table tbody tr") + if "No Webmention endpoint discovered" in row.get_text() + ) + assert "Suggested next step:" not in no_endpoint_row.get_text() |
