diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/views/test_endpoint.py | 49 | ||||
| -rw-r--r-- | tests/views/test_moderation.py | 12 | ||||
| -rw-r--r-- | tests/views/test_received.py | 204 |
3 files changed, 246 insertions, 19 deletions
diff --git a/tests/views/test_endpoint.py b/tests/views/test_endpoint.py index 23643d9..4a2abde 100644 --- a/tests/views/test_endpoint.py +++ b/tests/views/test_endpoint.py @@ -6,7 +6,7 @@ import uuid from datetime import UTC, datetime, timedelta from http import HTTPStatus from types import ModuleType -from unittest.mock import ANY, Mock +from unittest.mock import Mock import pytest import sqlalchemy as sa @@ -227,9 +227,9 @@ def test_endpoint_recovers_from_concurrent_insert( assert response.headers["Location"].endswith(f"/status/{competing_identifier}") assert execute.call_count == 3 - add.assert_called_once_with(ANY) - rollback.assert_called_once_with() - assert commit.call_count == 2 + assert add.call_count == (2 if reopened else 1) + assert rollback.call_count == (1 if reopened else 2) + assert commit.call_count == (2 if reopened else 1) if reopened: verify_webmention.assert_called_once_with(competing_identifier) @@ -469,3 +469,44 @@ def test_endpoint_rejects_bad_url_before_inserting_row( ) == 0 ) + + +def test_endpoint_records_initial_and_resubmission_events( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + from webmentions_ssg.models.received import ReceivedWebmentionStatusEvent + + mock_endpoint_form(views_module, monkeypatch, valid=True) + monkeypatch.setattr(views_module, "verify_webmention", Mock()) + + response = client.post("/endpoint") + assert response.status_code == HTTPStatus.CREATED + with app.app_context(): + mention = db.session.scalar(sa.select(ReceivedWebmention)) + assert mention is not None + identifier = mention.uuid + events = db.session.scalars( + sa.select(ReceivedWebmentionStatusEvent).where( + ReceivedWebmentionStatusEvent.webmention_uuid == identifier + ) + ).all() + assert [event.action for event in events] == ["Received submission"] + mention.status = "failed" + mention.failure_reason = "Previous failure" + mention.updated_at = datetime.now(UTC) - timedelta(minutes=11) + db.session.commit() + + assert client.post("/endpoint").status_code == HTTPStatus.CREATED + with app.app_context(): + events = db.session.scalars( + sa.select(ReceivedWebmentionStatusEvent) + .where(ReceivedWebmentionStatusEvent.webmention_uuid == identifier) + .order_by(ReceivedWebmentionStatusEvent.id) + ).all() + assert [event.action for event in events] == [ + "Received submission", + "Resubmitted after cooldown", + ] diff --git a/tests/views/test_moderation.py b/tests/views/test_moderation.py index 7f683d4..26b7cc4 100644 --- a/tests/views/test_moderation.py +++ b/tests/views/test_moderation.py @@ -96,18 +96,6 @@ def test_invalid_list_kind_and_missing_entry(app: Flask, client: FlaskClient) -> assert client.post("/moderation/blocked/123/delete").status_code == 404 -def test_received_row_can_block_domain(app: Flask, client: FlaskClient) -> None: - log_in(app, client) - mention_id = create_received_webmention(app, source="https://spam.example/article") - response = client.get("/received") - assert b"Block this domain" in response.data - response = client.post(f"/received/{mention_id}/block-source") - assert response.status_code == HTTPStatus.FOUND - with app.app_context(): - rule = db.session.scalar(sa.select(BlockedDomain)) - assert rule is not None and rule.domain == "spam.example" - - def test_block_selected_source_domains_deduplicates_and_preserves_mentions( app: Flask, client: FlaskClient ) -> None: diff --git a/tests/views/test_received.py b/tests/views/test_received.py index ec00e05..f23119d 100644 --- a/tests/views/test_received.py +++ b/tests/views/test_received.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 from flask import Flask from flask.testing import FlaskClient @@ -247,13 +248,14 @@ def test_received_page_size_is_free_form_and_controls_pagination( 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 html.count('aria-label="Details for 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 + response.get_data(as_text=True).count('aria-label="Details for Webmention ') + == 15 ) @@ -269,7 +271,7 @@ def test_received_default_page_size_is_capped_when_max_is_below_25( assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) - assert html.count('data-title="Reverify webmention?"') == 10 + assert html.count('aria-label="Details for Webmention ') == 10 assert 'name="per_page"' in html assert 'value="10"' in html assert "page=2" in html @@ -1061,3 +1063,199 @@ def test_received_bulk_delete_requires_real_csrf_token( assert valid.status_code == HTTPStatus.FOUND with app.app_context(): assert db.session.get(ReceivedWebmention, identifier) is None + + +def test_received_detail_requires_login_and_returns_404( + app: Flask, client: FlaskClient +) -> None: + identifier = create_received_webmention(app) + assert client.get(f"/received/{identifier}").status_code == HTTPStatus.FOUND + + log_in(app, client) + assert client.get(f"/received/{uuid.uuid7()}").status_code == HTTPStatus.NOT_FOUND + + +def test_received_list_links_to_details_without_showing_failure_text( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + identifier = create_received_webmention( + app, + status="failed", + failure_reason="Failure details should only appear on the detail page", + ) + + response = client.get("/received", query_string={"status": "declined"}) + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + assert f"/received/{identifier}?page=1&status=declined" in html + assert "Failure details should only appear on the detail page" not in html + assert 'data-title="Reverify webmention?"' not in html + assert 'data-received-bulk-action="reverify"' in html + + +def test_received_detail_shows_full_reason_timestamps_and_public_status( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + reason = "First line & <unsafe>\n" + ("very-long-failure-reason-" * 30) + identifier = create_received_webmention(app, status="failed", failure_reason=reason) + with app.app_context(): + mention = db.session.get(ReceivedWebmention, identifier) + assert mention is not None + mention.recovery_attempts = 2 + db.session.commit() + + response = client.get(f"/received/{identifier}") + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + soup = BeautifulSoup(html, "html.parser") + assert SOURCE_URL in html and TARGET_URL in html + assert "Recovery attempts" in html + assert "(automatic stale-verification recoveries" in html + assert "2" in soup.get_text() + assert reason in soup.get_text() + assert "<unsafe>" not in html + assert "<unsafe>" in html + assert "Received" in html and "Last updated" in html + assert f'href="http://localhost/status/{identifier}"' in html + assert "No historical transitions were recorded" in html + + +def test_received_detail_keeps_filters_and_returns_after_actions( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + identifier = create_received_webmention(app, status="failed") + query = { + "page": "3", + "status": "declined", + "q": "source.example", + "sort": "updated", + "order": "asc", + "per_page": "37", + } + + response = client.get(f"/received/{identifier}", query_string=query) + html = response.get_data(as_text=True) + soup = BeautifulSoup(html, "html.parser") + back_link = soup.find( + "a", string=lambda value: bool(value and "Back to received" in value) + ) + assert back_link is not None + assert parse_qs(urlsplit(back_link["href"]).query) == { + key: [value] for key, value in query.items() + } + action = soup.find(attrs={"data-title": "Reverify webmention?"}) + assert action is not None + action_url = action["data-action"] + assert parse_qs(urlsplit(action_url).query) == { + **{key: [value] for key, value in query.items()}, + "return_to": ["detail"], + } + + mock_admin_action_form(views_module, monkeypatch, valid=True) + monkeypatch.setattr(views_module, "verify_webmention", Mock()) + posted = client.post(action_url) + assert posted.status_code == HTTPStatus.FOUND + redirect_url = urlsplit(posted.headers["Location"]) + assert redirect_url.path == f"/received/{identifier}" + assert parse_qs(redirect_url.query) == { + key: [value] for key, value in query.items() + } + + with app.app_context(): + from webmentions_ssg.models.received import ReceivedWebmentionStatusEvent + + history = db.session.scalars( + sa.select(ReceivedWebmentionStatusEvent).where( + ReceivedWebmentionStatusEvent.webmention_uuid == identifier + ) + ).all() + assert len(history) == 1 + assert history[0].status == "received" + assert history[0].action == "Manually queued for reverification" + + +def test_received_detail_shows_recorded_history_in_reverse_order( + app: Flask, client: FlaskClient +) -> None: + from webmentions_ssg.models.received import ( + ReceivedWebmentionStatus, + ReceivedWebmentionStatusEvent, + ) + + log_in(app, client) + identifier = create_received_webmention(app, status="failed") + with app.app_context(): + db.session.add( + ReceivedWebmentionStatusEvent( + webmention_uuid=identifier, + status=ReceivedWebmentionStatus.FAILED, + action="Previous verification failed", + failure_reason="Temporary HTTP 503", + recovery_attempts=2, + occurred_at=datetime(2026, 11, 10, tzinfo=UTC), + ) + ) + db.session.add( + ReceivedWebmentionStatusEvent( + webmention_uuid=identifier, + status=ReceivedWebmentionStatus.RECEIVED, + action="Stale verification recovered", + recovery_attempts=3, + occurred_at=datetime(2026, 11, 11, tzinfo=UTC), + ) + ) + db.session.commit() + + response = client.get(f"/received/{identifier}") + html = response.get_data(as_text=True) + assert html.index("Stale verification recovered") < html.index( + "Previous verification failed" + ) + assert "Temporary HTTP 503" in html + assert "Recovery attempts: 3" in html + assert "2026-11-10 00:00:00 UTC" in html + + +def test_received_bulk_delete_preserves_only_remaining_history( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + from webmentions_ssg.models.received import ( + ReceivedWebmentionStatus, + ReceivedWebmentionStatusEvent, + ) + + log_in(app, client) + first = create_received_webmention(app, status="failed") + second = create_received_webmention( + app, source="https://other.example/post", status="failed" + ) + with app.app_context(): + for identifier in (first, second): + db.session.add( + ReceivedWebmentionStatusEvent( + webmention_uuid=identifier, + status=ReceivedWebmentionStatus.FAILED, + action="Verification completed", + occurred_at=datetime.now(UTC), + ) + ) + db.session.commit() + + mock_admin_action_form(views_module, monkeypatch, valid=True) + response = client.post( + "/received/bulk/delete", data={"webmention_uuids": [str(first)]} + ) + assert response.status_code == HTTPStatus.FOUND + with app.app_context(): + history = db.session.scalars(sa.select(ReceivedWebmentionStatusEvent)).all() + assert len(history) == 1 + assert history[0].webmention_uuid == second |
