diff options
Diffstat (limited to 'tests/views')
| -rw-r--r-- | tests/views/test_admin.py | 328 |
1 files changed, 327 insertions, 1 deletions
diff --git a/tests/views/test_admin.py b/tests/views/test_admin.py index e82d94d..a475ad5 100644 --- a/tests/views/test_admin.py +++ b/tests/views/test_admin.py @@ -12,6 +12,7 @@ from unittest.mock import Mock from urllib.parse import parse_qs, urlsplit import pytest +from bs4 import BeautifulSoup from flask import Flask from flask.testing import FlaskClient @@ -389,7 +390,11 @@ def test_received_get_filter_controls_render_defaults( 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) + status_option = BeautifulSoup(html, "html.parser").select_one( + 'select[name="status"] option[value=""][selected]' + ) + assert status_option is not None + assert status_option.get_text(strip=True) == "All statuses" def test_received_filters_source_or_target_as_literal_substring( @@ -773,6 +778,327 @@ def test_reverify_received_webmention( verify_webmention.assert_called_once_with(identifier) +def test_received_bulk_toolbar_and_shared_confirmation_modal( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + first = create_received_webmention(app) + second = create_received_webmention(app, source="https://other.example/post") + + response = client.get("/received") + + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + assert html.count('class="form-check-input received-select"') == 2 + assert f'value="{first}"' in html + assert f'value="{second}"' in html + assert 'id="received-select-all"' in html + assert 'id="received-selected-count"' in html + assert 'data-received-bulk-action="reverify"' in html + assert 'data-received-bulk-action="delete"' in html + assert 'data-title="Delete all declined webmentions?"' in html + assert html.count('id="confirmation-modal"') == 1 + assert 'id="confirmation-modal-form"' in html + assert 'received-bulk-actions.js' in html + + +def test_received_delete_all_declined_available_without_matching_rows( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + create_received_webmention(app, status="verified") + + response = client.get("/received", query_string={"status": "declined"}) + + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + assert 'data-title="Delete all declined webmentions?"' in html + assert 'id="confirmation-modal-form"' in html + assert 'data-received-bulk-action="delete"' not in html + assert 'id="received-select-all"' not in html + + +@pytest.mark.parametrize( + "path", + ["/received/bulk/reverify", "/received/bulk/delete", "/received/declined/delete"], +) +def test_received_bulk_actions_require_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( + "path", + ["/received/bulk/reverify", "/received/bulk/delete", "/received/declined/delete"], +) +def test_received_bulk_actions_reject_invalid_form( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + path: str, +) -> None: + log_in(app, client) + identifier = create_received_webmention(app, status="failed") + form = mock_admin_action_form(views_module, monkeypatch, valid=False) + + response = client.post(path, data={"webmention_uuids": str(identifier)}) + + assert response.status_code == HTTPStatus.BAD_REQUEST + form.validate_on_submit.assert_called_once_with() + with app.app_context(): + assert db.session.get(ReceivedWebmention, identifier).status == "failed" + + +@pytest.mark.parametrize( + "path", + ["/received/bulk/reverify", "/received/bulk/delete", "/received/declined/delete"], +) +def test_received_bulk_actions_reject_invalid_filters_before_mutating( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + path: str, +) -> None: + log_in(app, client) + identifier = create_received_webmention(app, status="failed") + mock_admin_action_form(views_module, monkeypatch, valid=True) + verify = Mock() + monkeypatch.setattr(views_module, "verify_webmention", verify) + + response = client.post( + path, + query_string={"sort": "invalid"}, + data={"webmention_uuids": str(identifier)}, + ) + + assert response.status_code == HTTPStatus.BAD_REQUEST + with app.app_context(): + assert db.session.get(ReceivedWebmention, identifier).status == "failed" + verify.assert_not_called() + + +@pytest.mark.parametrize("action", ["reverify", "delete"]) +@pytest.mark.parametrize("selection", [[], ["bad-uuid"]]) +def test_received_bulk_actions_reject_empty_or_malformed_selection( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + action: str, + selection: list[str], +) -> None: + log_in(app, client) + identifier = create_received_webmention(app, status="failed") + mock_admin_action_form(views_module, monkeypatch, valid=True) + verify = Mock() + monkeypatch.setattr(views_module, "verify_webmention", verify) + + response = client.post( + f"/received/bulk/{action}", data={"webmention_uuids": selection} + ) + + assert response.status_code == HTTPStatus.BAD_REQUEST + with app.app_context(): + assert db.session.get(ReceivedWebmention, identifier).status == "failed" + verify.assert_not_called() + + +@pytest.mark.parametrize("action", ["reverify", "delete"]) +def test_received_bulk_actions_reject_duplicate_oversized_or_stale_selection( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + action: str, +) -> None: + log_in(app, client) + first = create_received_webmention(app, status="failed") + second = create_received_webmention(app, source="https://other.example/post") + mock_admin_action_form(views_module, monkeypatch, valid=True) + verify = Mock() + monkeypatch.setattr(views_module, "verify_webmention", verify) + path = f"/received/bulk/{action}" + + duplicate = client.post(path, data={"webmention_uuids": [str(first)] * 2}) + assert duplicate.status_code == HTTPStatus.BAD_REQUEST + + stale = client.post( + path, data={"webmention_uuids": [str(first), str(uuid.uuid7())]} + ) + assert stale.status_code == HTTPStatus.NOT_FOUND + + app.config["RECEIVED_MAX_PAGE_SIZE"] = 1 + oversized = client.post( + path, data={"webmention_uuids": [str(first), str(second)]} + ) + assert oversized.status_code == HTTPStatus.BAD_REQUEST + + with app.app_context(): + assert db.session.get(ReceivedWebmention, first).status == "failed" + assert db.session.get(ReceivedWebmention, second) is not None + verify.assert_not_called() + + +def test_delete_selected_received_webmentions( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + first = create_received_webmention(app, status="failed") + second = create_received_webmention(app, source="https://other.example/post") + unselected = create_received_webmention(app, source="https://third.example/post") + mock_admin_action_form(views_module, monkeypatch, valid=True) + + response = client.post( + "/received/bulk/delete", + query_string={"page": 3, "sort": "source", "order": "asc", "per_page": 37}, + data={"webmention_uuids": [str(first), str(second)]}, + ) + + assert response.status_code == HTTPStatus.FOUND + assert parse_qs(urlsplit(response.headers["Location"]).query) == { + "page": ["3"], + "sort": ["source"], + "order": ["asc"], + "per_page": ["37"], + } + with app.app_context(): + assert db.session.get(ReceivedWebmention, first) is None + assert db.session.get(ReceivedWebmention, second) is None + assert db.session.get(ReceivedWebmention, unselected) is not None + + +def test_reverify_selected_received_webmentions( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + first = create_received_webmention(app, status="failed", failure_reason="Failure") + second = create_received_webmention( + app, source="https://other.example/post", status="source_removed" + ) + unselected = create_received_webmention( + app, source="https://third.example/post", status="failed" + ) + mock_admin_action_form(views_module, monkeypatch, valid=True) + verify = Mock() + monkeypatch.setattr(views_module, "verify_webmention", verify) + + response = client.post( + "/received/bulk/reverify", + query_string={"status": "declined", "page": 2}, + data={"webmention_uuids": [str(first), str(second)]}, + ) + + assert response.status_code == HTTPStatus.FOUND + assert parse_qs(urlsplit(response.headers["Location"]).query) == { + "status": ["declined"], + "page": ["2"], + } + with app.app_context(): + for identifier in (first, second): + webmention = db.session.get(ReceivedWebmention, identifier) + assert webmention is not None + assert webmention.status == "received" + assert webmention.failure_reason is None + assert webmention.recovery_attempts == 0 + assert db.session.get(ReceivedWebmention, unselected).status == "failed" + assert verify.call_count == 2 + assert {call.args[0] for call in verify.call_args_list} == {first, second} + + +def test_delete_all_declined_received_webmentions_ignores_age_and_filters( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + failed = create_received_webmention(app, status="failed", updated_at=datetime.now(UTC)) + removed = create_received_webmention( + app, + source="https://other.example/post", + status="source_removed", + updated_at=datetime.now(UTC) - timedelta(days=60), + ) + retained = [ + create_received_webmention( + app, source=f"https://{status}.example/post", status=status + ) + for status in ("received", "verifying", "verified") + ] + mock_admin_action_form(views_module, monkeypatch, valid=True) + + response = client.post( + "/received/declined/delete", + query_string={"page": 4, "status": "verified", "q": "no-match"}, + ) + + assert response.status_code == HTTPStatus.FOUND + assert parse_qs(urlsplit(response.headers["Location"]).query) == { + "page": ["4"], + "status": ["verified"], + "q": ["no-match"], + } + with app.app_context(): + assert db.session.get(ReceivedWebmention, failed) is None + assert db.session.get(ReceivedWebmention, removed) is None + for identifier in retained: + assert db.session.get(ReceivedWebmention, identifier) is not None + + +def test_delete_all_declined_received_webmentions_when_none_exist( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + identifier = create_received_webmention(app, status="verified") + mock_admin_action_form(views_module, monkeypatch, valid=True) + + response = client.post("/received/declined/delete") + + assert response.status_code == HTTPStatus.FOUND + with app.app_context(): + assert db.session.get(ReceivedWebmention, identifier) is not None + + +def test_received_bulk_delete_requires_real_csrf_token( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + identifier = create_received_webmention(app, status="failed") + app.config["WTF_CSRF_ENABLED"] = True + + invalid = client.post( + "/received/bulk/delete", data={"webmention_uuids": str(identifier)} + ) + assert invalid.status_code == HTTPStatus.BAD_REQUEST + + html = client.get("/received").get_data(as_text=True) + match = re.search( + r'<input\b(?=[^>]*name="csrf_token")(?=[^>]*value="([^"]+)")[^>]*>', + html, + ) + assert match is not None + valid = client.post( + "/received/bulk/delete", + data={"webmention_uuids": str(identifier), "csrf_token": match.group(1)}, + ) + assert valid.status_code == HTTPStatus.FOUND + with app.app_context(): + assert db.session.get(ReceivedWebmention, identifier) is None + + def test_rescan_sent_sources_requires_login(client: FlaskClient) -> None: response = client.post("/sources/rescan") |
