From 4bd1d9e348918b2e55db47a1ac3489d035cf55e2 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Thu, 12 Nov 2026 19:57:08 +0100 Subject: refactor(core): split modules by responsibility Break up monolithic views, task helpers, networking, CLI, and application extension setup so each module owns a focused concern. Reorganize the test suite around the new boundaries while preserving existing Webmention behavior and coverage. --- tests/views/__init__.py | 3 + tests/views/helpers.py | 210 +++++++++++++++++++++ tests/views/test_admin.py | 362 ++++++++++++++++++++++++++++++++++++ tests/views/test_endpoint.py | 422 ++++++++++++++++++++++++++++++++++++++++++ tests/views/test_session.py | 431 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 1428 insertions(+) create mode 100644 tests/views/__init__.py create mode 100644 tests/views/helpers.py create mode 100644 tests/views/test_admin.py create mode 100644 tests/views/test_endpoint.py create mode 100644 tests/views/test_session.py (limited to 'tests/views') diff --git a/tests/views/__init__.py b/tests/views/__init__.py new file mode 100644 index 0000000..25d78d5 --- /dev/null +++ b/tests/views/__init__.py @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause diff --git a/tests/views/helpers.py b/tests/views/helpers.py new file mode 100644 index 0000000..b32a066 --- /dev/null +++ b/tests/views/helpers.py @@ -0,0 +1,210 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import uuid +from datetime import UTC, datetime +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, + SentWebmentionStatus, + Source, + User, +) + +SOURCE_URL = "https://source.example/post" +TARGET_URL = "https://dennisfink.me/blog/example/" + + +def mock_endpoint_form( + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + *, + valid: bool, + source: str = SOURCE_URL, + target: str = TARGET_URL, + errors: dict[str, list[str]] | None = None, +) -> Mock: + form = Mock() + form.validate_on_submit.return_value = valid + form.source.data = source + form.target.data = target + form.errors = errors or {} + + monkeypatch.setattr(views_module.forms, "EndpointForm", Mock(return_value=form)) + return form + + +def log_in(app: Flask, client: FlaskClient) -> None: + 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 + + +def create_sent_source(app: Flask) -> uuid.UUID: + now = datetime.now(UTC) + + with app.app_context(): + source = Source( + path="blog/example/index.html", + url="https://dennisfink.me/blog/example/", + content_hash="0" * 32, + revision=2, + last_seen_at=now, + revised_at=now, + ) + + source.sent_webmentions.extend( + [ + SentWebmention( + target="https://example.com/b", + active=True, + desired_revision=2, + processed_revision=2, + sent_revision=2, + status=SentWebmentionStatus.SENT, + endpoint="https://example.com/webmention", + response_status=202, + ), + SentWebmention( + target="https://example.com/a", + active=True, + desired_revision=2, + processed_revision=2, + sent_revision=1, + status=SentWebmentionStatus.FAILED, + failure_reason="Webmention endpoint returned HTTP 400", + endpoint="https://example.com/webmention", + response_status=400, + ), + ] + ) + + db.session.add(source) + db.session.commit() + + return source.uuid + + +def mock_login_form( + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + *, + valid: bool, + username: str = "admin", + password: str | None = "test", +) -> Mock: + form = Mock() + form.validate_on_submit.return_value = valid + form.username.data = username + form.password.data = password + + monkeypatch.setattr(views_module.forms, "LoginForm", Mock(return_value=form)) + return form + + +def mock_admin_action_form( + views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, *, valid: bool +) -> Mock: + form = Mock() + form.validate_on_submit.return_value = valid + + monkeypatch.setattr(views_module.forms, "AdminActionForm", Mock(return_value=form)) + return form + + +def create_received_webmention( + app: Flask, + *, + source: str = SOURCE_URL, + target: str = TARGET_URL, + status: str = "verified", + failure_reason: str | None = None, +) -> uuid.UUID: + with app.app_context(): + webmention = ReceivedWebmention( + uuid=uuid.uuid7(), + source=source, + target=target, + status=status, + failure_reason=failure_reason, + ) + + db.session.add(webmention) + db.session.commit() + + return webmention.uuid + + +def create_resendable_sent_webmention(app: Flask) -> tuple[uuid.UUID, uuid.UUID]: + now = datetime.now(UTC) + + with app.app_context(): + source = Source( + path="blog/resend/index.html", + url="https://dennisfink.me/blog/resend/", + content_hash="1" * 32, + revision=3, + last_seen_at=now, + revised_at=now, + ) + webmention = SentWebmention( + target="https://example.com/resend", + active=True, + desired_revision=3, + processed_revision=3, + sent_revision=2, + status=SentWebmentionStatus.FAILED, + failure_reason="Previous failure", + endpoint="https://example.com/webmention", + response_status=500, + status_url="https://example.com/webmention/status/123", + ) + source.sent_webmentions.append(webmention) + + db.session.add(source) + db.session.commit() + + return source.uuid, webmention.uuid + + +def create_outgoing( + app: Flask, *, status_url: str | None = None +) -> tuple[uuid.UUID, uuid.UUID]: + now = datetime.now(UTC) + with app.app_context(): + source = Source( + path="blog/post/index.html", + url="https://source.example/blog/post", + content_hash="0" * 32, + revision=1, + last_seen_at=now, + revised_at=now, + ) + mention = SentWebmention( + target="https://example.com/article", + active=True, + desired_revision=1, + status_url=status_url, + response_status=201 if status_url is not None else None, + ) + source.sent_webmentions.append(mention) + db.session.add(source) + db.session.commit() + return source.uuid, mention.uuid 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 +# +# 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 diff --git a/tests/views/test_endpoint.py b/tests/views/test_endpoint.py new file mode 100644 index 0000000..652250f --- /dev/null +++ b/tests/views/test_endpoint.py @@ -0,0 +1,422 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import uuid +from datetime import UTC, datetime, timedelta +from types import ModuleType +from unittest.mock import ANY, Mock + +import pytest +import sqlalchemy as sa +from flask import Flask +from flask.testing import FlaskClient +from sqlalchemy.exc import IntegrityError + +from webmentions_ssg import DATABASE as db +from webmentions_ssg.models import ReceivedWebmention + +from .helpers import ( + SOURCE_URL, + TARGET_URL, + create_received_webmention, + mock_endpoint_form, +) + + +@pytest.fixture +def views_module(app: Flask) -> ModuleType: + _ = app + from webmentions_ssg.views import endpoint as module + + return module + + +def test_endpoint_only_accepts_post(client: FlaskClient) -> None: + response = client.get("/endpoint") + assert response.status_code == 405 + + +def test_endpoint_returns_form_errors( + client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + errors = {"source": ["Invalid source"]} + mock_endpoint_form(views_module, monkeypatch, valid=False, errors=errors) + + response = client.post("/endpoint") + + assert response.status_code == 400 + assert response.get_json() == errors + + +def test_endpoint_creates_webmention( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + form = mock_endpoint_form(views_module, monkeypatch, valid=True) + verify_webmention = Mock() + monkeypatch.setattr(views_module, "verify_webmention", verify_webmention) + + response = client.post("/endpoint") + + assert response.status_code == 201 + form.validate_on_submit.assert_called_once_with() + + with app.app_context(): + webmention = db.session.scalar(sa.select(ReceivedWebmention)) + + assert webmention is not None + assert webmention.source == SOURCE_URL + assert webmention.target == TARGET_URL + assert webmention.status == "received" + assert webmention.failure_reason is None + + identifier = webmention.uuid + + assert response.headers["Location"].endswith(str(identifier)) + verify_webmention.assert_called_once_with(identifier) + + +def test_endpoint_is_idempotent( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + mock_endpoint_form(views_module, monkeypatch, valid=True) + verify_webmention = Mock() + monkeypatch.setattr(views_module, "verify_webmention", verify_webmention) + + first_response = client.post("/endpoint") + second_response = client.post("/endpoint") + + assert first_response.status_code == 201 + assert second_response.status_code == 201 + assert first_response.headers["Location"] == second_response.headers["Location"] + + with app.app_context(): + webmentions = db.session.scalars(sa.select(ReceivedWebmention)).all() + + assert len(webmentions) == 1 + + webmention = webmentions[0] + + assert webmention.source == SOURCE_URL + assert webmention.target == TARGET_URL + assert webmention.status == "received" + assert webmention.failure_reason is None + + identifier = webmention.uuid + + verify_webmention.assert_called_once_with(identifier) + + +def test_resending_resets_failure_state( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + mock_endpoint_form(views_module, monkeypatch, valid=True) + verify_webmention = Mock() + monkeypatch.setattr(views_module, "verify_webmention", verify_webmention) + + with app.app_context(): + existing = ReceivedWebmention( + uuid=uuid.uuid7(), + source=SOURCE_URL, + target=TARGET_URL, + status="failed", + failure_reason="Previous failure", + updated_at=datetime.now(UTC) - timedelta(minutes=11), + ) + + db.session.add(existing) + db.session.commit() + + identifier = existing.uuid + + response = client.post("/endpoint") + + assert response.status_code == 201 + + 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 + + verify_webmention.assert_called_once_with(identifier) + + +def test_status_returns_404_for_unknown_webmention(client: FlaskClient) -> None: + response = client.get(f"/status/{uuid.uuid7()}") + + assert response.status_code == 404 + + +def test_status_displays_webmention(app: Flask, client: FlaskClient) -> None: + identifier = create_received_webmention( + app, status="failed", failure_reason="Verification failed" + ) + + response = client.get(f"/status/{identifier}") + + assert response.status_code == 200 + html = response.get_data(as_text=True) + assert SOURCE_URL in html + assert TARGET_URL in html + assert "failed" in html + assert "Verification failed" in html + + +def test_endpoint_recovers_from_concurrent_insert( + client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + mock_endpoint_form(views_module, monkeypatch, valid=True) + + competing_identifier = uuid.uuid7() + competing_webmention = ReceivedWebmention( + uuid=competing_identifier, + source=SOURCE_URL, + target=TARGET_URL, + status="failed", + failure_reason="Competing request failed", + ) + + initial_result = Mock() + initial_result.scalar_one_or_none.return_value = None + competing_result = Mock() + competing_result.scalar_one_or_none.return_value = competing_webmention + + execute = Mock(side_effect=[initial_result, competing_result]) + add = Mock() + rollback = Mock() + commit = Mock( + side_effect=[ + IntegrityError( + "INSERT INTO received_webmention ...", + {}, + Exception("duplicate source/target"), + ), + None, + ] + ) + verify_webmention = Mock() + + monkeypatch.setattr(views_module.db.session, "execute", execute) + monkeypatch.setattr(views_module.db.session, "add", add) + monkeypatch.setattr(views_module.db.session, "rollback", rollback) + monkeypatch.setattr(views_module.db.session, "commit", commit) + monkeypatch.setattr(views_module, "verify_webmention", verify_webmention) + + response = client.post("/endpoint") + + assert response.status_code == 201 + assert response.headers["Location"].endswith(f"/status/{competing_identifier}") + + assert execute.call_count == 2 + add.assert_called_once_with(ANY) + rollback.assert_called_once_with() + assert commit.call_count == 1 + + assert competing_webmention.status == "failed" + assert competing_webmention.failure_reason == "Competing request failed" + verify_webmention.assert_not_called() + + +def test_endpoint_rate_limits_requests_per_ip( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + app.config["ENDPOINT_RATE_LIMIT"] = "2/minute" + + form = mock_endpoint_form(views_module, monkeypatch, valid=True) + verify = Mock() + monkeypatch.setattr(views_module, "verify_webmention", verify) + + responses = [] + + for index in range(3): + form.source.data = f"https://source.example/post-{index}" + responses.append( + client.post("/endpoint", environ_overrides={"REMOTE_ADDR": "192.0.2.10"}) + ) + + assert [r.status_code for r in responses] == [201, 201, 429] + + assert verify.call_count == 2 + + with app.app_context(): + count = db.session.scalar( + sa.select(sa.func.count()).select_from(ReceivedWebmention) + ) + assert count == 2 + + +def test_endpoint_rate_limit_distinguishes_clients_behind_trusted_proxy( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + from werkzeug.middleware.proxy_fix import ProxyFix + + app.config["ENDPOINT_RATE_LIMIT"] = "1/minute" + app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1) + form = mock_endpoint_form(views_module, monkeypatch, valid=True) + monkeypatch.setattr(views_module, "verify_webmention", Mock()) + + def attempt(client_ip: str, index: int) -> int: + form.source.data = f"https://source.example/proxy-post-{index}" + return client.post( + "/endpoint", + environ_overrides={"REMOTE_ADDR": "192.0.2.200"}, + headers={"X-Forwarded-For": client_ip}, + ).status_code + + assert attempt("198.51.100.11", 0) == 201 + assert attempt("198.51.100.11", 1) == 429 + assert attempt("198.51.100.12", 2) == 201 + + +def test_endpoint_global_rate_limit( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + app.config["ENDPOINT_GLOBAL_RATE_LIMIT"] = "2/minute" + + form = mock_endpoint_form(views_module, monkeypatch, valid=True) + verify = Mock() + monkeypatch.setattr(views_module, "verify_webmention", verify) + + responses = [] + + for index in range(3): + form.source.data = f"https://source.example/post-{index}" + + responses.append( + client.post( + "/endpoint", environ_overrides={"REMOTE_ADDR": f"192.0.2.{index + 1}"} + ) + ) + + assert [r.status_code for r in responses] == [201, 201, 429] + assert verify.call_count == 2 + + +def test_endpoint_recent_duplicate_does_not_reverify( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + mock_endpoint_form(views_module, monkeypatch, valid=True) + + verify = Mock() + monkeypatch.setattr(views_module, "verify_webmention", verify) + + with app.app_context(): + webmention = ReceivedWebmention( + uuid=uuid.uuid7(), + source=SOURCE_URL, + target=TARGET_URL, + status="failed", + failure_reason="Previous failure", + ) + db.session.add(webmention) + db.session.commit() + identifier = webmention.uuid + + response = client.post("/endpoint") + + assert response.status_code == 201 + assert response.headers["Location"].endswith(str(identifier)) + + with app.app_context(): + stored = db.session.get(ReceivedWebmention, identifier) + assert stored is not None + assert stored.status == "failed" + assert stored.failure_reason == "Previous failure" + + verify.assert_not_called() + + +def test_endpoint_reverifies_after_cooldown( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + mock_endpoint_form(views_module, monkeypatch, valid=True) + + verify = Mock() + monkeypatch.setattr(views_module, "verify_webmention", verify) + + with app.app_context(): + webmention = ReceivedWebmention( + uuid=uuid.uuid7(), + source=SOURCE_URL, + target=TARGET_URL, + status="failed", + failure_reason="Previous failure", + recovery_attempts=3, + updated_at=datetime.now(UTC) - timedelta(minutes=11), + ) + db.session.add(webmention) + db.session.commit() + identifier = webmention.uuid + + response = client.post("/endpoint") + + assert response.status_code == 201 + + with app.app_context(): + stored = db.session.get(ReceivedWebmention, identifier) + assert stored is not None + assert stored.status == "received" + assert stored.failure_reason is None + assert stored.recovery_attempts == 0 + + verify.assert_called_once_with(identifier) + + +def test_endpoint_rejects_request_bodies_over_configured_maximum( + app: Flask, client: FlaskClient +) -> None: + app.config["MAX_CONTENT_LENGTH"] = 128 + response = client.post( + "/endpoint", data={"source": SOURCE_URL + "x" * 150, "target": TARGET_URL} + ) + assert response.status_code == 413 + + +def test_endpoint_rejects_bad_url_before_inserting_row( + app: Flask, client: FlaskClient, monkeypatch: pytest.MonkeyPatch +) -> None: + from webmentions_ssg.views import endpoint as views + + verify_webmention = Mock() + monkeypatch.setattr(views, "verify_webmention", verify_webmention) + + response = client.post( + "/endpoint", data={"source": "http://ex☃mple.test/", "target": TARGET_URL} + ) + + assert response.status_code == 400 + assert "source" in response.get_json() + verify_webmention.assert_not_called() + with app.app_context(): + assert ( + db.session.scalar( + sa.select(sa.func.count()).select_from(ReceivedWebmention) + ) + == 0 + ) diff --git a/tests/views/test_session.py b/tests/views/test_session.py new file mode 100644 index 0000000..6a96032 --- /dev/null +++ b/tests/views/test_session.py @@ -0,0 +1,431 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +from types import ModuleType + +import pytest +from flask import Flask +from flask.testing import FlaskClient + +from webmentions_ssg import DATABASE as db +from webmentions_ssg.models import User + +from .helpers import log_in, mock_login_form + + +@pytest.fixture +def views_module(app: Flask) -> ModuleType: + _ = app + from webmentions_ssg.views import session as module + + return module + + +def test_login_renders_form(client: FlaskClient) -> None: + response = client.get("/login") + + assert response.status_code == 200 + assert "login" in response.get_data(as_text=True).lower() + + +@pytest.mark.parametrize("password", ("wrong-password", None)) +def test_login_rejects_invalid_credentials( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + password: str | None, +) -> None: + with app.app_context(): + db.session.add(User(username="admin", password="test")) + db.session.commit() + + mock_login_form( + views_module, monkeypatch, valid=True, username="admin", password=password + ) + + response = client.post("/login") + + assert response.status_code == 302 + assert response.headers["Location"].endswith("/login") + + with client.session_transaction() as session: + assert "_user_id" not in session + assert ("danger", "Invalid username or password") in session["_flashes"] + + +@pytest.mark.parametrize( + "next_url", + [ + pytest.param("/", id="root"), + pytest.param("/received", id="local-path"), + pytest.param("/received?page=2", id="query-string"), + pytest.param("/received#details", id="fragment"), + ], +) +def test_login_redirects_to_local_next_page( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + next_url: str, +) -> None: + with app.app_context(): + user = User(username="admin", password="test") + db.session.add(user) + db.session.commit() + user_id = user.get_id() + + mock_login_form(views_module, monkeypatch, valid=True) + + response = client.post("/login", query_string={"next": next_url}) + + assert response.status_code == 302 + assert response.headers["Location"] == next_url + + with client.session_transaction() as session: + assert session["_user_id"] == str(user_id) + + +@pytest.mark.parametrize( + "next_url", + [ + pytest.param(None, id="missing"), + pytest.param("", id="empty"), + pytest.param("received", id="relative-path"), + pytest.param("https://evil.example/phishing", id="absolute-https"), + pytest.param("http://evil.example", id="absolute-http"), + pytest.param("javascript:alert(1)", id="non-http-scheme"), + pytest.param("//evil.example/phishing", id="scheme-relative"), + pytest.param("///evil.example/phishing", id="triple-slash"), + pytest.param("////evil.example", id="four-slashes"), + pytest.param(r"/\evil.example", id="slash-backslash-host"), + pytest.param(r"/received\evil.example", id="backslash-in-path"), + pytest.param(r"\/evil.example", id="backslash-slash-host"), + ], +) +def test_login_rejects_unsafe_next_page( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + next_url: str | None, +) -> None: + with app.app_context(): + user = User(username="admin", password="test") + db.session.add(user) + db.session.commit() + user_id = user.get_id() + + mock_login_form(views_module, monkeypatch, valid=True) + + query_string = {} if next_url is None else {"next": next_url} + response = client.post("/login", query_string=query_string) + + assert response.status_code == 302 + assert response.headers["Location"] == "/" + + with client.session_transaction() as session: + assert session["_user_id"] == str(user_id) + + +def test_login_redirects_authenticated_user(app: Flask, client: FlaskClient) -> None: + log_in(app, client) + + response = client.get("/login") + + assert response.status_code == 302 + assert response.headers["Location"].endswith("/") + + +@pytest.mark.parametrize("username", ["admin", "unknown-user"]) +def test_login_always_checks_password( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + username: str, +) -> None: + with app.app_context(): + db.session.add(User(username="admin", password="test")) + db.session.commit() + + mock_login_form( + views_module, + monkeypatch, + valid=True, + username=username, + password="wrong-password", + ) + + original_check_password = User.check_password + checked_users: list[User] = [] + + def spy_check_password(self: User, password: str) -> bool: + checked_users.append(self) + return original_check_password(self, password) + + monkeypatch.setattr(User, "check_password", spy_check_password) + + response = client.post("/login") + + assert response.status_code == 302 + assert response.headers["Location"].endswith("/login") + assert len(checked_users) == 1 + + if username == "admin": + assert checked_users[0].username == "admin" + else: + assert checked_users[0] is views_module.DUMMY_USER + + with client.session_transaction() as session: + assert "_user_id" not in session + assert ("danger", "Invalid username or password") in session["_flashes"] + + +def test_login_rejects_dummy_user_even_if_password_matches( + client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + mock_login_form( + views_module, + monkeypatch, + valid=True, + username="unknown-user", + password="anything", + ) + + checked_users: list[User] = [] + + def mock_check_password(self: User, password: str) -> bool: + checked_users.append(self) + return True + + monkeypatch.setattr(User, "check_password", mock_check_password) + + response = client.post("/login") + + assert response.status_code == 302 + assert response.headers["Location"].endswith("/login") + assert checked_users == [views_module.DUMMY_USER] + + with client.session_transaction() as session: + assert "_user_id" not in session + + +def test_login_rate_limit_is_independent_per_ip( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + app.config["LOGIN_RATE_LIMIT"] = "1/minute" + + mock_login_form( + views_module, + monkeypatch, + valid=True, + username="unknown-user", + password="wrong-password", + ) + + def attempt(ip: str) -> int: + response = client.post("/login", environ_overrides={"REMOTE_ADDR": ip}) + return response.status_code + + assert attempt("192.0.2.10") == 302 + assert attempt("192.0.2.10") == 429 + + # Another IP still has its own allowance. + assert attempt("192.0.2.11") == 302 + assert attempt("192.0.2.11") == 429 + + +def test_login_rate_limit_uses_forwarded_client_ip_when_proxy_is_trusted( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + from werkzeug.middleware.proxy_fix import ProxyFix + + app.config["LOGIN_RATE_LIMIT"] = "1/minute" + app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1) + mock_login_form( + views_module, + monkeypatch, + valid=True, + username="unknown-user", + password="wrong-password", + ) + + def attempt(client_ip: str) -> int: + response = client.post( + "/login", + environ_overrides={"REMOTE_ADDR": "192.0.2.200"}, + headers={"X-Forwarded-For": client_ip}, + ) + return response.status_code + + assert attempt("198.51.100.11") == 302 + # A forged earlier X-Forwarded-For entry cannot change the client bucket. + assert attempt("203.0.113.99, 198.51.100.11") == 429 + assert attempt("198.51.100.12") == 302 + + +def test_login_username_limit_throttles_across_ips( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + app.config["LOGIN_RATE_LIMIT"] = "100/minute" + app.config["LOGIN_USERNAME_RATE_LIMIT"] = "2/minute" + + with monkeypatch.context() as patcher: + mock_login_form( + views_module, + patcher, + valid=True, + username="unknown-user", + password="wrong-password", + ) + + def attempt(ip: str, username: str) -> int: + return client.post( + "/login", + data={"username": username, "password": "wrong-password"}, + environ_overrides={"REMOTE_ADDR": ip}, + ).status_code + + assert attempt("192.0.2.10", "Admin") == 302 + assert attempt("192.0.2.11", " admin ") == 302 + assert attempt("192.0.2.12", "ADMIN") == 429 + # An unrelated username is not locked by attempts against Admin. + assert attempt("192.0.2.12", "another-user") == 302 + + # Restore the real form: the POST-only limits must not block a GET, + # even from the client IP that submitted the throttled attempt. + response = client.get("/login", environ_overrides={"REMOTE_ADDR": "192.0.2.12"}) + assert response.status_code == 200 + + +def test_login_missing_username_uses_per_ip_bucket( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + app.config["LOGIN_RATE_LIMIT"] = "100/minute" + app.config["LOGIN_USERNAME_RATE_LIMIT"] = "1/minute" + mock_login_form( + views_module, + monkeypatch, + valid=True, + username="unknown-user", + password="wrong-password", + ) + + def attempt(ip: str) -> int: + return client.post("/login", environ_overrides={"REMOTE_ADDR": ip}).status_code + + assert attempt("192.0.2.10") == 302 + assert attempt("192.0.2.10") == 429 + assert attempt("192.0.2.11") == 302 + + +def test_login_rate_limits_requests_per_ip( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + app.config["LOGIN_RATE_LIMIT"] = "2/minute" + + with monkeypatch.context() as patcher: + form = mock_login_form( + views_module, + patcher, + valid=True, + username="unknown-user", + password="wrong-password", + ) + + responses = [ + client.post("/login", environ_overrides={"REMOTE_ADDR": "192.0.2.10"}) + for _ in range(3) + ] + + assert [response.status_code for response in responses] == [302, 302, 429] + assert form.validate_on_submit.call_count == 2 + assert "Too many login attempts" in responses[-1].get_data(as_text=True) + + # The real LoginForm is restored here. + # GET requests must remain accessible after throttling. + response = client.get("/login", environ_overrides={"REMOTE_ADDR": "192.0.2.10"}) + assert response.status_code == 200 + + with client.session_transaction() as session: + assert "_user_id" not in session + + +def test_logout_rejects_get(app: Flask, client: FlaskClient) -> None: + log_in(app, client) + + response = client.get("/logout") + + assert response.status_code == 405 + + with client.session_transaction() as session: + assert "_user_id" in session + + +@pytest.mark.parametrize( + "data", + [ + pytest.param({}, id="missing"), + pytest.param({"csrf_token": "invalid"}, id="invalid"), + ], +) +def test_logout_rejects_invalid_csrf( + app: Flask, client: FlaskClient, data: dict[str, str] +) -> None: + app.config["WTF_CSRF_ENABLED"] = True + log_in(app, client) + + response = client.post("/logout", data=data) + + assert response.status_code == 400 + + with client.session_transaction() as session: + assert "_user_id" in session + + +def test_logout_ends_session(app: Flask, client: FlaskClient) -> None: + import re + + app.config["WTF_CSRF_ENABLED"] = True + log_in(app, client) + + page = client.get("/received") + assert page.status_code == 200 + + html = page.get_data(as_text=True) + + assert 'action="/logout"' in html + assert 'method="post"' in html + + match = re.search(r'name="csrf_token"\s+value="([^"]+)"', html) + assert match is not None + + response = client.post("/logout", data={"csrf_token": match.group(1)}) + + assert response.status_code == 302 + assert response.headers["Location"].endswith("/") + + with client.session_transaction() as session: + assert "_user_id" not in session + + protected_response = client.get("/received") + assert protected_response.status_code == 302 + assert "/login" in protected_response.headers["Location"] -- cgit v1.3.1