aboutsummaryrefslogtreecommitdiff
path: root/tests/views/test_admin.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/views/test_admin.py362
1 files changed, 362 insertions, 0 deletions
diff --git a/tests/views/test_admin.py b/tests/views/test_admin.py
new file mode 100644
index 0000000..26a13f8
--- /dev/null
+++ b/tests/views/test_admin.py
@@ -0,0 +1,362 @@
+# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+import uuid
+from datetime import UTC, datetime, timedelta
+from types import ModuleType
+from unittest.mock import Mock
+
+import pytest
+from flask import Flask
+from flask.testing import FlaskClient
+
+from webmentions_ssg import DATABASE as db
+from webmentions_ssg.models import ReceivedWebmention, SentWebmention, User
+
+from .helpers import (
+ SOURCE_URL,
+ TARGET_URL,
+ create_outgoing,
+ create_received_webmention,
+ create_resendable_sent_webmention,
+ create_sent_source,
+ log_in,
+ mock_admin_action_form,
+)
+
+
+@pytest.fixture
+def views_module(app: Flask) -> ModuleType:
+ _ = app
+ from webmentions_ssg.views import admin as module
+
+ return module
+
+
+@pytest.mark.parametrize("path", ["/sent", f"/sent/{uuid.uuid7()}"])
+def test_sent_views_require_login(client: FlaskClient, path: str) -> None:
+ response = client.get(path)
+
+ assert response.status_code == 302
+ assert "/login" in response.headers["Location"]
+
+
+def test_sent_lists_sources(app: Flask, client: FlaskClient) -> None:
+ log_in(app, client)
+ identifier = create_sent_source(app)
+
+ response = client.get("/sent")
+
+ assert response.status_code == 200
+
+ html = response.get_data(as_text=True)
+
+ assert "blog/example/index.html" in html
+ assert "https://dennisfink.me/blog/example/" in html
+ assert f"/sent/{identifier}" in html
+
+
+def test_sent_source_lists_webmentions(app: Flask, client: FlaskClient) -> None:
+ log_in(app, client)
+ identifier = create_sent_source(app)
+
+ response = client.get(f"/sent/{identifier}")
+
+ assert response.status_code == 200
+
+ html = response.get_data(as_text=True)
+
+ assert "blog/example/index.html" in html
+ assert "https://dennisfink.me/blog/example/" in html
+
+ first_target = "https://example.com/a"
+ second_target = "https://example.com/b"
+
+ assert first_target in html
+ assert second_target in html
+ assert html.index(first_target) < html.index(second_target)
+
+ assert "Webmention endpoint returned HTTP 400" in html
+ assert "https://example.com/webmention" in html
+
+
+def test_sent_source_returns_404_for_unknown_source(
+ app: Flask, client: FlaskClient
+) -> None:
+ log_in(app, client)
+ response = client.get(f"/sent/{uuid.uuid7()}")
+ assert response.status_code == 404
+
+
+def test_received_requires_login(client: FlaskClient) -> None:
+ response = client.get("/received")
+
+ assert response.status_code == 302
+ assert "/login" in response.headers["Location"]
+
+
+def test_received_lists_webmentions(app: Flask, client: FlaskClient) -> None:
+ log_in(app, client)
+ create_received_webmention(app)
+
+ response = client.get("/received")
+
+ assert response.status_code == 200
+ html = response.get_data(as_text=True)
+ assert SOURCE_URL in html
+ assert TARGET_URL in html
+
+
+@pytest.mark.parametrize("action", ["delete", "reverify"])
+def test_received_admin_actions_require_login(client: FlaskClient, action: str) -> None:
+ response = client.post(f"/received/{uuid.uuid7()}/{action}")
+
+ assert response.status_code == 302
+ assert "/login" in response.headers["Location"]
+
+
+@pytest.mark.parametrize("action", ["delete", "reverify"])
+def test_received_admin_actions_reject_invalid_form(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+ action: str,
+) -> None:
+ log_in(app, client)
+ identifier = create_received_webmention(app)
+ form = mock_admin_action_form(views_module, monkeypatch, valid=False)
+
+ response = client.post(f"/received/{identifier}/{action}")
+
+ assert response.status_code == 400
+ form.validate_on_submit.assert_called_once_with()
+
+
+@pytest.mark.parametrize("action", ["delete", "reverify"])
+def test_received_admin_actions_return_404_for_unknown_webmention(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+ action: str,
+) -> None:
+ log_in(app, client)
+ mock_admin_action_form(views_module, monkeypatch, valid=True)
+
+ response = client.post(f"/received/{uuid.uuid7()}/{action}")
+
+ assert response.status_code == 404
+
+
+def test_delete_received_webmention(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ log_in(app, client)
+ identifier = create_received_webmention(app)
+ mock_admin_action_form(views_module, monkeypatch, valid=True)
+
+ response = client.post(f"/received/{identifier}/delete?page=3")
+
+ assert response.status_code == 302
+ assert response.headers["Location"].endswith("/received?page=3")
+
+ with app.app_context():
+ assert db.session.get(ReceivedWebmention, identifier) is None
+
+
+def test_reverify_received_webmention(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ log_in(app, client)
+ identifier = create_received_webmention(
+ app, status="failed", failure_reason="Previous failure"
+ )
+ with app.app_context():
+ row = db.session.get(ReceivedWebmention, identifier)
+ assert row is not None
+ row.recovery_attempts = 3
+ db.session.commit()
+ mock_admin_action_form(views_module, monkeypatch, valid=True)
+ verify_webmention = Mock()
+ monkeypatch.setattr(views_module, "verify_webmention", verify_webmention)
+
+ response = client.post(f"/received/{identifier}/reverify?page=2")
+
+ assert response.status_code == 302
+ assert response.headers["Location"].endswith("/received?page=2")
+
+ with app.app_context():
+ 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
+
+ verify_webmention.assert_called_once_with(identifier)
+
+
+def test_rescan_sent_sources_requires_login(client: FlaskClient) -> None:
+ response = client.post("/sent/rescan")
+
+ assert response.status_code == 302
+ assert "/login" in response.headers["Location"]
+
+
+@pytest.mark.parametrize("valid", [False, True])
+def test_rescan_sent_sources(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+ valid: bool,
+) -> None:
+ log_in(app, client)
+ form = mock_admin_action_form(views_module, monkeypatch, valid=valid)
+ manual_scan_sources = Mock()
+ monkeypatch.setattr(views_module, "manual_scan_sources", manual_scan_sources)
+
+ response = client.post("/sent/rescan")
+
+ assert response.status_code == 302
+ assert response.headers["Location"].endswith("/sent")
+ form.validate_on_submit.assert_called_once_with()
+
+ if valid:
+ manual_scan_sources.assert_called_once_with()
+ else:
+ manual_scan_sources.assert_not_called()
+
+
+def test_resend_sent_webmention_requires_login(client: FlaskClient) -> None:
+ response = client.post(f"/sent/{uuid.uuid7()}/{uuid.uuid7()}/resend")
+
+ assert response.status_code == 302
+ assert "/login" in response.headers["Location"]
+
+
+def test_resend_sent_webmention_rejects_invalid_form(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ log_in(app, client)
+ source_identifier, webmention_identifier = create_resendable_sent_webmention(app)
+ mock_admin_action_form(views_module, monkeypatch, valid=False)
+
+ response = client.post(f"/sent/{source_identifier}/{webmention_identifier}/resend")
+
+ assert response.status_code == 400
+
+
+def test_resend_sent_webmention_returns_404_for_unknown_webmention(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ log_in(app, client)
+ source_identifier = create_sent_source(app)
+ mock_admin_action_form(views_module, monkeypatch, valid=True)
+
+ response = client.post(f"/sent/{source_identifier}/{uuid.uuid7()}/resend")
+
+ assert response.status_code == 404
+
+
+def test_resend_sent_webmention_rejects_wrong_source(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ log_in(app, client)
+ _, webmention_identifier = create_resendable_sent_webmention(app)
+ wrong_source_identifier = create_sent_source(app)
+ mock_admin_action_form(views_module, monkeypatch, valid=True)
+
+ response = client.post(
+ f"/sent/{wrong_source_identifier}/{webmention_identifier}/resend"
+ )
+
+ assert response.status_code == 404
+
+
+def test_resend_sent_webmention_reopens_current_revision(
+ app: Flask,
+ client: FlaskClient,
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ log_in(app, client)
+ source_identifier, webmention_identifier = create_resendable_sent_webmention(app)
+ mock_admin_action_form(views_module, monkeypatch, valid=True)
+ send_webmention = Mock()
+ monkeypatch.setattr(views_module, "send_webmention", send_webmention)
+
+ with app.app_context():
+ webmention = db.session.get(SentWebmention, webmention_identifier)
+ assert webmention is not None
+ webmention.attempt_count = 6
+ webmention.next_retry_at = datetime.now(UTC) + timedelta(hours=24)
+ db.session.commit()
+
+ response = client.post(f"/sent/{source_identifier}/{webmention_identifier}/resend")
+
+ assert response.status_code == 302
+ assert response.headers["Location"].endswith(f"/sent/{source_identifier}")
+
+ with app.app_context():
+ webmention = db.session.get(SentWebmention, webmention_identifier)
+ assert webmention is not None
+ assert webmention.desired_revision == 3
+ assert webmention.processed_revision is None
+ assert webmention.attempt_count == 0
+ assert webmention.next_retry_at is None
+ assert webmention.sent_revision == 2
+ assert webmention.status is None
+ assert webmention.failure_reason is None
+ assert webmention.endpoint is None
+ assert webmention.response_status is None
+ assert webmention.status_url is None
+
+ send_webmention.assert_called_once_with(webmention_identifier)
+
+
+@pytest.mark.parametrize(
+ ("status_url", "safe"),
+ [
+ ("https://example.com/status", True),
+ ("http://example.com/status", True),
+ ("javascript:alert(1)", False),
+ ("data:text/html,evil", False),
+ ("ftp://example.com/status", False),
+ ],
+)
+def test_admin_only_links_safe_existing_status_urls(
+ app: Flask, client: FlaskClient, status_url: str, safe: bool
+) -> None:
+ source_identifier, _ = create_outgoing(app, status_url=status_url)
+ with app.app_context():
+ user = User(username="admin", password="test")
+ db.session.add(user)
+ db.session.commit()
+ user_id = user.get_id()
+
+ with client.session_transaction() as session:
+ session["_user_id"] = str(user_id)
+ session["_fresh"] = True
+
+ response = client.get(f"/sent/{source_identifier}")
+ assert response.status_code == 200
+ html = response.get_data(as_text=True)
+ assert (f'href="{status_url}"' in html) is safe
+ assert status_url in html