diff options
Diffstat (limited to 'tests/views/test_received.py')
| -rw-r--r-- | tests/views/test_received.py | 204 |
1 files changed, 201 insertions, 3 deletions
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 |
