diff options
| author | Dennis Fink | 2026-11-17 17:09:59 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-17 17:09:59 +0100 |
| commit | 6d6d079a3a99d3d3a922a640b6925eb2adf044de (patch) | |
| tree | 33cc19241f4ce86ea43b2f194087c58e3fdc8a4d /tests/views/test_source_domains.py | |
| parent | 227092b57acc886469e16bcc4c0d3256e86d3d1e (diff) | |
| download | webmentions-ssg-6d6d079a3a99d3d3a922a640b6925eb2adf044de.tar.gz webmentions-ssg-6d6d079a3a99d3d3a922a640b6925eb2adf044de.zip | |
refactor(core)!: split forms and models by feature
Replace monolithic form and model modules with focused packages for
receiver, moderation, source, user, and shared validation concerns.
Rename source policy concepts to domain moderation, including routes,
models, tables, configuration, templates, and tests.
BREAKING CHANGE: form and model import paths, moderation routes, and
`SOURCE_ALLOWLIST_ENABLED` have changed; use `ALLOWLIST_ENABLED` and the
new feature-specific modules.
Diffstat (limited to 'tests/views/test_source_domains.py')
| -rw-r--r-- | tests/views/test_source_domains.py | 297 |
1 files changed, 0 insertions, 297 deletions
diff --git a/tests/views/test_source_domains.py b/tests/views/test_source_domains.py deleted file mode 100644 index c9e72b2..0000000 --- a/tests/views/test_source_domains.py +++ /dev/null @@ -1,297 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Authenticated source-domain management and early endpoint rejection.""" - -import uuid -from datetime import UTC, datetime -from http import HTTPStatus -from unittest.mock import Mock - -import pytest -import sqlalchemy as sa -from flask import Flask -from flask.testing import FlaskClient - -from webmentions_ssg import db -from webmentions_ssg.models import ( - AllowedSource, - BlockedSource, - ReceivedWebmention, - ReceivedWebmentionStatus, -) -from webmentions_ssg.tasks.verification import VerificationClaim - -from .helpers import SOURCE_URL, TARGET_URL, create_received_webmention, log_in - - -def test_source_rules_require_login(client: FlaskClient) -> None: - for method, path, data in ( - ("get", "/source-domains", None), - ("post", "/source-domains/blocked/add", {"domain": "bad.example"}), - ("post", "/source-domains/blocked/1/delete", None), - ): - response = client.open(path, method=method.upper(), data=data) - assert response.status_code == HTTPStatus.FOUND - assert "/login" in response.headers["Location"] - - -def test_add_and_unblock_source_domain(app: Flask, client: FlaskClient) -> None: - log_in(app, client) - response = client.post( - "/source-domains/blocked/add", data={"domain": "SPAM.Example."} - ) - assert response.status_code == HTTPStatus.FOUND - response = client.post( - "/source-domains/blocked/add", data={"domain": "spam.example"} - ) - assert response.status_code == HTTPStatus.FOUND - - with app.app_context(): - rows = db.session.scalars(sa.select(BlockedSource)).all() - assert len(rows) == 1 - assert rows[0].domain == "spam.example" - entry_id = rows[0].id - - response = client.get("/source-domains") - assert response.status_code == HTTPStatus.OK - assert b"spam.example" in response.data - assert b"Unblock" in response.data - assert b"Source rules" in response.data - - response = client.post(f"/source-domains/blocked/{entry_id}/delete") - assert response.status_code == HTTPStatus.FOUND - with app.app_context(): - assert db.session.scalar(sa.select(sa.func.count(BlockedSource.id))) == 0 - - -def test_allowlist_can_be_managed_while_mode_is_off( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - response = client.post( - "/source-domains/allowed/add", data={"domain": "friends.example"} - ) - assert response.status_code == HTTPStatus.FOUND - with app.app_context(): - row = db.session.scalar(sa.select(AllowedSource)) - assert row is not None - assert row.domain == "friends.example" - assert b"disabled" in client.get("/source-domains").data - - -@pytest.mark.parametrize( - "domain", ["https://spam.example", "*.example.com", "example.com/path", "bad host"] -) -def test_invalid_domains_are_rejected( - client: FlaskClient, app: Flask, domain: str -) -> None: - log_in(app, client) - response = client.post("/source-domains/blocked/add", data={"domain": domain}) - assert response.status_code == HTTPStatus.BAD_REQUEST - - -def test_invalid_list_kind_and_missing_entry(app: Flask, client: FlaskClient) -> None: - log_in(app, client) - assert ( - client.post("/source-domains/nope/add", data={"domain": "test.org"}).status_code - == 404 - ) - assert client.post("/source-domains/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(BlockedSource)) - 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: - from urllib.parse import parse_qs, urlsplit - - log_in(app, client) - first = create_received_webmention(app, source="https://SPAM.Example./article") - same_domain = create_received_webmention( - app, source="https://spam.example/another-article" - ) - second = create_received_webmention(app, source="https://second.example/article") - unselected = create_received_webmention(app, source="https://other.example/article") - with app.app_context(): - db.session.add(BlockedSource(domain="second.example")) - db.session.commit() - - response = client.post( - "/received/bulk/block-sources", - query_string={"page": 3, "status": "verified", "sort": "source"}, - data={"webmention_uuids": [str(first), str(same_domain), str(second)]}, - ) - - assert response.status_code == HTTPStatus.FOUND - assert parse_qs(urlsplit(response.headers["Location"]).query) == { - "page": ["3"], - "status": ["verified"], - "sort": ["source"], - } - with app.app_context(): - assert set(db.session.scalars(sa.select(BlockedSource.domain))) == { - "spam.example", - "second.example", - } - for identifier in (first, same_domain, second, unselected): - assert db.session.get(ReceivedWebmention, identifier) is not None - from webmentions_ssg.source_policy import source_rejection_reason - - assert ( - source_rejection_reason("https://child.spam.example/post") - == "Source domain is blocked" - ) - assert source_rejection_reason("https://other.example/post") is None - - # Repeating the action is idempotent. - response = client.post( - "/received/bulk/block-sources", - data={"webmention_uuids": [str(first), str(same_domain)]}, - ) - assert response.status_code == HTTPStatus.FOUND - with app.app_context(): - assert db.session.scalar(sa.select(sa.func.count(BlockedSource.id))) == 2 - - -def test_bulk_block_rejects_invalid_source_without_partial_changes( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - valid = create_received_webmention(app, source="https://valid.example/post") - invalid = create_received_webmention(app, source="not-a-url") - - response = client.post( - "/received/bulk/block-sources", - data={"webmention_uuids": [str(valid), str(invalid)]}, - ) - assert response.status_code == HTTPStatus.BAD_REQUEST - with app.app_context(): - assert db.session.scalar(sa.select(sa.func.count(BlockedSource.id))) == 0 - - -def test_bulk_block_action_enforces_csrf(app: Flask, client: FlaskClient) -> None: - log_in(app, client) - identifier = create_received_webmention(app) - app.config["WTF_CSRF_ENABLED"] = True - - response = client.post( - "/received/bulk/block-sources", data={"webmention_uuids": str(identifier)} - ) - assert response.status_code == HTTPStatus.BAD_REQUEST - with app.app_context(): - assert db.session.scalar(sa.select(sa.func.count(BlockedSource.id))) == 0 - - -def test_endpoint_rejects_blocked_without_queue_or_insert( - app: Flask, client: FlaskClient, monkeypatch: pytest.MonkeyPatch -) -> None: - from webmentions_ssg.views import endpoint - - enqueue = Mock() - monkeypatch.setattr(endpoint, "verify_webmention", enqueue) - with app.app_context(): - db.session.add(BlockedSource(domain="source.example")) - db.session.commit() - - response = client.post( - "/endpoint", data={"source": SOURCE_URL, "target": TARGET_URL} - ) - assert response.status_code == HTTPStatus.FORBIDDEN - assert response.json == {"error": "Source domain is blocked"} - enqueue.assert_not_called() - with app.app_context(): - assert db.session.scalar(sa.select(sa.func.count(ReceivedWebmention.uuid))) == 0 - - -def test_allowlist_mode_rejects_unlisted_and_allows_listed( - app: Flask, client: FlaskClient, monkeypatch: pytest.MonkeyPatch -) -> None: - from webmentions_ssg.views import endpoint - - app.config["SOURCE_ALLOWLIST_ENABLED"] = True - enqueue = Mock() - monkeypatch.setattr(endpoint, "verify_webmention", enqueue) - data = {"source": SOURCE_URL, "target": TARGET_URL} - assert client.post("/endpoint", data=data).status_code == HTTPStatus.FORBIDDEN - - with app.app_context(): - db.session.add(AllowedSource(domain="source.example")) - db.session.commit() - - assert client.post("/endpoint", data=data).status_code == HTTPStatus.CREATED - enqueue.assert_called_once() - - -def test_worker_checks_rules_before_fetch( - app: Flask, monkeypatch: pytest.MonkeyPatch -) -> None: - from webmentions_ssg.tasks import verification - - claim = VerificationClaim( - webmention_uuid=uuid.uuid7(), - source=SOURCE_URL, - target=TARGET_URL, - lease_started_at=datetime.now(UTC), - ) - fetch = Mock() - monkeypatch.setattr(verification, "source_mentions_target", fetch) - with app.app_context(): - db.session.add(BlockedSource(domain="source.example")) - db.session.commit() - assert verification.determine_verification_outcome(claim) == ( - ReceivedWebmentionStatus.FAILED, - "Source domain is blocked", - ) - fetch.assert_not_called() - - -def test_queued_worker_skips_fetch_after_source_is_blocked( - app: Flask, monkeypatch: pytest.MonkeyPatch -) -> None: - from webmentions_ssg.tasks import verification - - with app.app_context(): - mention = ReceivedWebmention( - uuid=uuid.uuid7(), source=SOURCE_URL, target=TARGET_URL - ) - db.session.add(mention) - db.session.commit() - identifier = mention.uuid - - # A user blocks the domain after the submission has already been queued. - db.session.add(BlockedSource(domain="source.example")) - db.session.commit() - - fetch = Mock() - monkeypatch.setattr(verification, "source_mentions_target", fetch) - verification.verify_webmention.call_local(identifier) - fetch.assert_not_called() - with app.app_context(): - mention = db.session.get(ReceivedWebmention, identifier) - assert mention is not None - assert mention.status == ReceivedWebmentionStatus.FAILED - assert mention.failure_reason == "Source domain is blocked" - - -def test_admin_actions_enforce_csrf(app: Flask, client: FlaskClient) -> None: - log_in(app, client) - app.config["WTF_CSRF_ENABLED"] = True - assert ( - client.post( - "/source-domains/blocked/add", data={"domain": "spam.example"} - ).status_code - == HTTPStatus.BAD_REQUEST - ) |
