From 6d6d079a3a99d3d3a922a640b6925eb2adf044de Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Tue, 17 Nov 2026 17:09:59 +0100 Subject: 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. --- tests/forms/__init__.py | 3 + tests/forms/test_admin.py | 13 + tests/forms/test_endpoint.py | 228 +++++++ tests/forms/test_received.py | 93 +++ tests/forms/test_user.py | 35 + tests/forms/test_validators.py | 88 +++ tests/models/__init__.py | 3 + tests/models/test_received.py | 130 ++++ tests/models/test_sources.py | 201 ++++++ tests/models/test_user.py | 41 ++ tests/models/test_utils.py | 95 +++ tests/tasks/test_notifications.py | 2 +- tests/tasks/test_retention.py | 2 +- tests/tasks/test_scanner.py | 6 +- tests/tasks/test_sender.py | 6 +- tests/tasks/test_verification.py | 5 +- tests/test_auth.py | 2 +- tests/test_cli.py | 25 +- tests/test_config.py | 6 +- tests/test_forms.py | 427 ------------ tests/test_models.py | 439 ------------- tests/test_moderation_policy.py | 62 ++ tests/test_source_policy.py | 67 -- tests/views/helpers.py | 14 +- tests/views/test_admin.py | 1280 ------------------------------------ tests/views/test_endpoint.py | 2 +- tests/views/test_moderation.py | 290 ++++++++ tests/views/test_received.py | 1063 ++++++++++++++++++++++++++++++ tests/views/test_session.py | 2 +- tests/views/test_source_domains.py | 297 --------- tests/views/test_sources.py | 253 +++++++ 31 files changed, 2647 insertions(+), 2533 deletions(-) create mode 100644 tests/forms/__init__.py create mode 100644 tests/forms/test_admin.py create mode 100644 tests/forms/test_endpoint.py create mode 100644 tests/forms/test_received.py create mode 100644 tests/forms/test_user.py create mode 100644 tests/forms/test_validators.py create mode 100644 tests/models/__init__.py create mode 100644 tests/models/test_received.py create mode 100644 tests/models/test_sources.py create mode 100644 tests/models/test_user.py create mode 100644 tests/models/test_utils.py delete mode 100644 tests/test_forms.py delete mode 100644 tests/test_models.py create mode 100644 tests/test_moderation_policy.py delete mode 100644 tests/test_source_policy.py delete mode 100644 tests/views/test_admin.py create mode 100644 tests/views/test_moderation.py create mode 100644 tests/views/test_received.py delete mode 100644 tests/views/test_source_domains.py create mode 100644 tests/views/test_sources.py (limited to 'tests') diff --git a/tests/forms/__init__.py b/tests/forms/__init__.py new file mode 100644 index 0000000..25d78d5 --- /dev/null +++ b/tests/forms/__init__.py @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause diff --git a/tests/forms/test_admin.py b/tests/forms/test_admin.py new file mode 100644 index 0000000..cb011ee --- /dev/null +++ b/tests/forms/test_admin.py @@ -0,0 +1,13 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +from flask import Flask + +from webmentions_ssg.forms.admin import ConfirmActionForm + + +def test_admin_action_form_accepts_submission(app: Flask) -> None: + with app.test_request_context(method="POST"): + form = ConfirmActionForm(meta={"csrf": False}) + assert form.validate() diff --git a/tests/forms/test_endpoint.py b/tests/forms/test_endpoint.py new file mode 100644 index 0000000..11f233d --- /dev/null +++ b/tests/forms/test_endpoint.py @@ -0,0 +1,228 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import pytest +from flask import Flask +from werkzeug.datastructures import MultiDict + +from webmentions_ssg.forms.endpoint import WebmentionSubmissionForm + +VALID_SOURCE = "https://source.example/post" +VALID_TARGET = "https://dennisfink.me/blog/example/" + + +@pytest.mark.parametrize( + ("form_data", "invalid_field", "expected_error"), + [ + pytest.param( + {"target": VALID_TARGET}, + "source", + "This field is required.", + id="source-required", + ), + pytest.param( + {"source": "not a URL", "target": VALID_TARGET}, + "source", + "Invalid URL.", + id="source-url", + ), + pytest.param( + {"source": "ftp://source.example/post", "target": VALID_TARGET}, + "source", + "source must begin with http or https", + id="source-scheme", + ), + pytest.param( + {"source": VALID_SOURCE}, + "target", + "This field is required.", + id="target-required", + ), + pytest.param( + {"source": VALID_SOURCE, "target": "not a URL"}, + "target", + "Invalid URL.", + id="target-url", + ), + pytest.param( + {"source": VALID_SOURCE, "target": "ftp://dennisfink.me/blog/example/"}, + "target", + "target must begin with http or https", + id="target-scheme", + ), + pytest.param( + {"source": "http:///source.example/post", "target": VALID_TARGET}, + "source", + "Invalid URL.", + id="source-missing-host", + ), + pytest.param( + {"source": VALID_SOURCE, "target": "https://dennisfink.me:invalid/"}, + "target", + "Invalid URL.", + id="target-invalid-port", + ), + ], +) +def test_endpoint_form_rejects_invalid_field_syntax( + app: Flask, form_data: dict[str, str], invalid_field: str, expected_error: str +) -> None: + with app.test_request_context("/endpoint", method="POST"): + form = WebmentionSubmissionForm( + formdata=MultiDict(form_data), meta={"csrf": False} + ) + + assert not form.validate() + assert expected_error in form.errors[invalid_field] + + +@pytest.mark.parametrize("scheme", ["http", "https", "HTTP", "HTTPS"]) +def test_endpoint_form_accepts_http_schemes_case_insensitively( + app: Flask, scheme: str +) -> None: + with app.test_request_context("/endpoint", method="POST"): + form = WebmentionSubmissionForm( + formdata=MultiDict( + { + "source": f"{scheme}://source.example/post", + "target": f"{scheme}://dennisfink.me/blog/example/", + } + ), + meta={"csrf": False}, + ) + assert form.validate() + + +def test_endpoint_form_accepts_valid_data(app: Flask) -> None: + with app.test_request_context("/endpoint", method="POST"): + form = WebmentionSubmissionForm( + formdata=MultiDict({"source": VALID_SOURCE, "target": VALID_TARGET}), + meta={"csrf": False}, + ) + + assert form.validate() + assert form.errors == {} + + +def test_endpoint_does_not_do_dns_in_web_request( + app: Flask, monkeypatch: pytest.MonkeyPatch +) -> None: + def unexpected_dns_lookup(*args: object, **kwargs: object) -> None: + raise AssertionError("No DNS lookup belongs in form validation") + + monkeypatch.setattr( + "webmentions_ssg.net.dns.resolver.resolve_name", unexpected_dns_lookup + ) + with app.test_request_context("/endpoint", method="POST"): + for source in (VALID_SOURCE, "ftp://source.example/post", "not a URL"): + form = WebmentionSubmissionForm( + formdata=MultiDict({"source": source, "target": VALID_TARGET}), + meta={"csrf": False}, + ) + assert form.validate() is (source == VALID_SOURCE) + + +@pytest.mark.parametrize( + "source", ["http://127.0.0.1/", "http://[::1]/", "http://169.254.169.254/"] +) +def test_endpoint_rejects_private_ip_literals(app: Flask, source: str) -> None: + with app.test_request_context("/endpoint", method="POST"): + form = WebmentionSubmissionForm( + formdata=MultiDict({"source": source, "target": VALID_TARGET}), + meta={"csrf": False}, + ) + assert not form.validate() + assert "URL must use a public address." in form.errors["source"] + + +def test_endpoint_rejects_private_ip_literal_with_trailing_dot(app: Flask) -> None: + with app.test_request_context("/endpoint", method="POST"): + form = WebmentionSubmissionForm( + formdata=MultiDict( + {"source": "http://127.0.0.1./", "target": VALID_TARGET} + ), + meta={"csrf": False}, + ) + assert not form.validate() + assert "URL must use a public address." in form.errors["source"] + + +def test_endpoint_handles_malformed_ipv6_without_crashing(app: Flask) -> None: + with app.test_request_context("/endpoint", method="POST"): + form = WebmentionSubmissionForm( + formdata=MultiDict({"source": VALID_SOURCE, "target": "http://[::1"}), + meta={"csrf": False}, + ) + assert not form.validate() + assert "Invalid URL." in form.errors["target"] + + +def test_endpoint_form_applies_configurable_url_byte_limit_to_both_fields( + app: Flask, +) -> None: + with app.test_request_context("/endpoint", method="POST"): + app.config["MAX_WEBMENTION_URL_BYTES"] = len(VALID_SOURCE.encode("utf-8")) + form = WebmentionSubmissionForm( + formdata=MultiDict({"source": VALID_SOURCE + "x", "target": VALID_TARGET}), + meta={"csrf": False}, + ) + assert not form.validate() + assert "source" in form.errors + assert any("bytes" in message for message in form.errors["source"]) + + app.config["MAX_WEBMENTION_URL_BYTES"] = len(VALID_TARGET.encode("utf-8")) + form = WebmentionSubmissionForm( + formdata=MultiDict({"source": VALID_SOURCE, "target": VALID_TARGET + "x"}), + meta={"csrf": False}, + ) + assert not form.validate() + assert "target" in form.errors + assert any("bytes" in message for message in form.errors["target"]) + + +@pytest.mark.parametrize("field_name", ["source", "target"]) +@pytest.mark.parametrize( + "invalid_url", + [ + pytest.param("http://ex☃mple.test/", id="invalid-idn"), + pytest.param("https://example.com/a\nb", id="newline"), + pytest.param("https://example.com/a\rb", id="carriage-return"), + pytest.param("https://example.com/\x00b", id="nul"), + pytest.param("https://example.com/a b", id="ascii-space"), + pytest.param("https://example.com/a\u00a0b", id="unicode-whitespace"), + pytest.param("https://example.com/\x7fb", id="del"), + pytest.param("https://example.com/a\x80b", id="c1-control"), + ], +) +def test_endpoint_rejects_invalid_httpx_urls_and_whitespace( + app: Flask, field_name: str, invalid_url: str +) -> None: + data = {"source": VALID_SOURCE, "target": VALID_TARGET} + data[field_name] = invalid_url + with app.test_request_context("/endpoint", method="POST"): + form = WebmentionSubmissionForm(formdata=MultiDict(data), meta={"csrf": False}) + assert not form.validate() + assert "Invalid URL." in form.errors[field_name] + + +def test_endpoint_accepts_encoded_path_space(app: Flask) -> None: + with app.test_request_context("/endpoint", method="POST"): + form = WebmentionSubmissionForm( + formdata=MultiDict( + {"source": "https://source.example/a%20b", "target": VALID_TARGET} + ), + meta={"csrf": False}, + ) + assert form.validate() + + +def test_endpoint_form_rejects_matching_source_and_target(app: Flask) -> None: + with app.test_request_context("/endpoint", method="POST"): + form = WebmentionSubmissionForm( + formdata=MultiDict({"source": VALID_TARGET, "target": VALID_TARGET}), + meta={"csrf": False}, + ) + + assert not form.validate() + assert "Field must not be equal to target." in form.errors["source"] diff --git a/tests/forms/test_received.py b/tests/forms/test_received.py new file mode 100644 index 0000000..4ef9ed3 --- /dev/null +++ b/tests/forms/test_received.py @@ -0,0 +1,93 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import pytest +from flask import Flask +from werkzeug.datastructures import MultiDict + +from webmentions_ssg.forms.received import ReceivedWebmentionFilterForm + + +def test_received_filter_form_accepts_valid_get_params(app: Flask) -> None: + with app.test_request_context("/received", method="GET"): + form = ReceivedWebmentionFilterForm( + formdata=MultiDict( + { + "status": "declined", + "q": "example.org", + "date_from": "2026-11-08", + "date_to": "2026-11-14", + } + ), + meta={"csrf": False}, + ) + assert form.validate() + assert form.status.data == "declined" + assert form.q.data == "example.org" + assert form.date_from.data.isoformat() == "2026-11-08" + assert form.date_to.data.isoformat() == "2026-11-14" + assert "csrf_token" not in form._fields + + +@pytest.mark.parametrize( + "data", + [ + {"status": "unknown"}, + {"date_from": "not-a-date"}, + {"date_from": "2026-11-8"}, + {"date_to": "2026-02-30"}, + {"date_from": "2026-11-15", "date_to": "2026-11-08"}, + ], +) +def test_received_filter_form_rejects_invalid_get_params( + app: Flask, data: dict[str, str] +) -> None: + with app.test_request_context("/received", method="GET"): + form = ReceivedWebmentionFilterForm( + formdata=MultiDict(data), meta={"csrf": False} + ) + assert not form.validate() + assert form.errors + + +def test_received_filter_form_validates_sort_and_page_size(app: Flask) -> None: + with app.test_request_context("/received", method="GET"): + form = ReceivedWebmentionFilterForm( + formdata=MultiDict({"sort": "updated", "order": "asc", "per_page": "37"}), + meta={"csrf": False}, + ) + assert form.validate() + assert form.sort.data == "updated" + assert form.order.data == "asc" + assert form.per_page.data == 37 + + with app.test_request_context("/received", method="GET"): + form = ReceivedWebmentionFilterForm(formdata=MultiDict(), meta={"csrf": False}) + assert form.validate() + assert (form.sort.data, form.order.data, form.per_page.data) == ( + "received", + "desc", + 25, + ) + + +@pytest.mark.parametrize( + "data", + [ + {"sort": "invalid"}, + {"order": "invalid"}, + {"per_page": "0"}, + {"per_page": "101"}, + {"per_page": "invalid"}, + ], +) +def test_received_filter_form_rejects_invalid_sort_and_page_size( + app: Flask, data: dict[str, str] +) -> None: + with app.test_request_context("/received", method="GET"): + form = ReceivedWebmentionFilterForm( + formdata=MultiDict(data), meta={"csrf": False} + ) + assert not form.validate() + assert form.errors diff --git a/tests/forms/test_user.py b/tests/forms/test_user.py new file mode 100644 index 0000000..4b35a9a --- /dev/null +++ b/tests/forms/test_user.py @@ -0,0 +1,35 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import pytest +from flask import Flask +from werkzeug.datastructures import MultiDict + +from webmentions_ssg.forms.user import LoginForm + + +@pytest.mark.parametrize( + ("form_data", "invalid_field"), + [ + pytest.param({"password": "secret"}, "username", id="username-required"), + pytest.param({"username": "admin"}, "password", id="password-required"), + ], +) +def test_login_form_requires_credentials( + app: Flask, form_data: dict[str, str], invalid_field: str +) -> None: + with app.test_request_context("/login", method="POST"): + form = LoginForm(formdata=MultiDict(form_data), meta={"csrf": False}) + + assert not form.validate() + assert "This field is required." in form.errors[invalid_field] + + +def test_login_form_accepts_credentials(app: Flask) -> None: + with app.test_request_context("/login", method="POST"): + form = LoginForm( + formdata=MultiDict({"username": "admin", "password": "secret"}), + meta={"csrf": False}, + ) + assert form.validate() diff --git a/tests/forms/test_validators.py b/tests/forms/test_validators.py new file mode 100644 index 0000000..4234552 --- /dev/null +++ b/tests/forms/test_validators.py @@ -0,0 +1,88 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import pytest +from flask import Flask +from werkzeug.datastructures import MultiDict +from wtforms import Form, StringField, ValidationError + +from webmentions_ssg.forms.validators import ( + AllowedTargetHostname, + MaxURLBytes, + RejectNonPublicIPLiteral, +) + + +class URLForm(Form): + url = StringField("URL") + + +def test_allowed_hostname_accepts_configured_hostname(app: Flask) -> None: + app.config["ALLOWED_TARGET_HOSTNAMES"] = ["dennisfink.me"] + form = URLForm(MultiDict({"url": "https://dennisfink.me/blog/example/"})) + with app.app_context(): + AllowedTargetHostname()(form, form.url) + + +def test_allowed_hostname_rejects_unconfigured_hostname(app: Flask) -> None: + app.config["ALLOWED_TARGET_HOSTNAMES"] = ["dennisfink.me"] + form = URLForm(MultiDict({"url": "https://example.com/post"})) + with app.app_context(), pytest.raises(ValidationError, match="Invalid input"): + AllowedTargetHostname()(form, form.url) + + +def test_allowed_hostname_uses_custom_message(app: Flask) -> None: + app.config["ALLOWED_TARGET_HOSTNAMES"] = ["dennisfink.me"] + form = URLForm(MultiDict({"url": "https://example.com/post"})) + with ( + app.app_context(), + pytest.raises(ValidationError, match="Hostname is not allowed"), + ): + AllowedTargetHostname("Hostname is not allowed")(form, form.url) + + +@pytest.mark.parametrize( + "address", + ["64:ff9b::a9fe:a9fe", "::10.0.0.1", "2002:a9fe:a9fe::1", "::ffff:169.254.169.254"], +) +def test_public_ip_literal_rejects_ipv6_transition_addresses(address: str) -> None: + form = URLForm(MultiDict({"url": f"https://[{address}]/"})) + with pytest.raises(ValidationError, match="URL must use a public address"): + RejectNonPublicIPLiteral()(form, form.url) + + +def test_public_ip_literal_accepts_public_ipv6_address() -> None: + form = URLForm(MultiDict({"url": "https://[2606:2800:220:1:248:1893:25c8:1946]/"})) + RejectNonPublicIPLiteral()(form, form.url) + + +def test_allowed_hostname_handles_invalid_ipv6(app: Flask) -> None: + form = URLForm(MultiDict({"url": "http://[::1"})) + with app.app_context(), pytest.raises(ValidationError, match="Invalid input"): + AllowedTargetHostname()(form, form.url) + + +def test_url_max_bytes_respects_utf8_byte_length_and_current_config(app: Flask) -> None: + validator = MaxURLBytes() + form = URLForm(MultiDict({"url": "é" * 3})) + with app.app_context(): + app.config["MAX_WEBMENTION_URL_BYTES"] = 6 + validator(form, form.url) + app.config["MAX_WEBMENTION_URL_BYTES"] = 5 + with pytest.raises(ValidationError, match="5 bytes"): + validator(form, form.url) + + +def test_url_max_bytes_custom_message(app: Flask) -> None: + form = URLForm(MultiDict({"url": "too-long"})) + with app.app_context(): + app.config["MAX_WEBMENTION_URL_BYTES"] = 2 + with pytest.raises(ValidationError, match="URL too long"): + MaxURLBytes(message="URL too long")(form, form.url) + + +def test_url_max_bytes_rejects_malformed_unicode(app: Flask) -> None: + form = URLForm(MultiDict({"url": "\ud800"})) + with app.app_context(), pytest.raises(ValidationError, match="valid UTF-8"): + MaxURLBytes()(form, form.url) diff --git a/tests/models/__init__.py b/tests/models/__init__.py new file mode 100644 index 0000000..25d78d5 --- /dev/null +++ b/tests/models/__init__.py @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause diff --git a/tests/models/test_received.py b/tests/models/test_received.py new file mode 100644 index 0000000..6f74d2d --- /dev/null +++ b/tests/models/test_received.py @@ -0,0 +1,130 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import uuid + +import pytest +import sqlalchemy as sa +from flask import Flask +from sqlalchemy.exc import IntegrityError + +from webmentions_ssg import db +from webmentions_ssg.models.received import ReceivedWebmention, ReceivedWebmentionStatus +from webmentions_ssg.models.utils import source_target_digest + + +@pytest.mark.parametrize( + ("status", "expected"), + [ + (ReceivedWebmentionStatus.RECEIVED, False), + (ReceivedWebmentionStatus.VERIFYING, False), + (ReceivedWebmentionStatus.VERIFIED, True), + (ReceivedWebmentionStatus.FAILED, False), + (ReceivedWebmentionStatus.SOURCE_REMOVED, False), + ], +) +def test_received_webmention_verified( + status: ReceivedWebmentionStatus, expected: bool +) -> None: + webmention = ReceivedWebmention( + uuid=uuid.uuid7(), + source="https://source.example/post", + target="https://dennisfink.me/blog/example/", + status=status, + ) + + assert webmention.verified is expected + + +def test_received_webmention_repr() -> None: + webmention = ReceivedWebmention( + uuid=uuid.uuid7(), + source="https://source.example/post", + target="https://dennisfink.me/blog/example/", + ) + + assert repr(webmention) == ( + " " + "'https://dennisfink.me/blog/example/'>" + ) + + +def test_received_status_uses_non_native_enum() -> None: + status_type = ReceivedWebmention.__table__.c.status.type + + assert isinstance(status_type, sa.Enum) + assert status_type.enum_class is ReceivedWebmentionStatus + assert status_type.native_enum is False + assert status_type.enums == [status.value for status in ReceivedWebmentionStatus] + + +def test_received_status_round_trips_as_enum(app: Flask) -> None: + with app.app_context(): + identifier = uuid.uuid7() + mention = ReceivedWebmention( + uuid=identifier, + source="https://source.example/post", + target="https://dennisfink.me/blog/example/", + status=ReceivedWebmentionStatus.VERIFIED, + ) + db.session.add(mention) + db.session.commit() + + assert ( + db.session.scalar( + sa.text("SELECT status FROM received_webmentions WHERE uuid = :uuid"), + {"uuid": identifier.hex}, + ) + == "verified" + ) + + db.session.expunge_all() + loaded = db.session.get(ReceivedWebmention, identifier) + assert loaded is not None + assert loaded.status is ReceivedWebmentionStatus.VERIFIED + + +def test_received_verification_reset_is_reusable_for_atomic_updates() -> None: + mention = ReceivedWebmention( + uuid=uuid.uuid7(), + source="https://source.example/post", + target="https://dennisfink.me/post", + status=ReceivedWebmentionStatus.FAILED, + failure_reason="Old error", + recovery_attempts=3, + ) + assert mention.reopen_verification() is None + assert mention.status == ReceivedWebmentionStatus.RECEIVED + assert mention.failure_reason is None + assert mention.recovery_attempts == 0 + assert mention.updated_at is not None + + +def test_long_received_pair_can_be_stored_with_unique_digest(app: Flask) -> None: + long_source = "https://source.example/" + "a" * 4000 + long_target = "https://dennisfink.me/" + "b" * 4000 + with app.app_context(): + db.session.add( + ReceivedWebmention( + uuid=uuid.uuid7(), source=long_source, target=long_target + ) + ) + db.session.commit() + match = db.session.scalar( + sa.select(ReceivedWebmention).where( + ReceivedWebmention.source_target_digest + == source_target_digest(long_source, long_target) + ) + ) + assert match is not None + assert match.source == long_source + assert match.target == long_target + db.session.add( + ReceivedWebmention( + uuid=uuid.uuid7(), source=long_source, target=long_target + ) + ) + with pytest.raises(IntegrityError): + db.session.commit() + db.session.rollback() diff --git a/tests/models/test_sources.py b/tests/models/test_sources.py new file mode 100644 index 0000000..8d01d79 --- /dev/null +++ b/tests/models/test_sources.py @@ -0,0 +1,201 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import uuid +from datetime import UTC, datetime +from http import HTTPStatus +from typing import get_type_hints + +import pytest +import sqlalchemy as sa +from flask import Flask +from sqlalchemy.exc import IntegrityError +from sqlalchemy.orm import Mapped + +from webmentions_ssg import db +from webmentions_ssg.models.received import ReceivedWebmention +from webmentions_ssg.models.sources import ( + SentWebmention, + SentWebmentionStatus, + SourceDocument, +) +from webmentions_ssg.models.utils import target_digest + + +def test_uuid_annotations_and_relationships_resolve() -> None: + """Verify that UUID columns and forward relationships map correctly.""" + for model in (ReceivedWebmention, SourceDocument, SentWebmention): + assert get_type_hints(model)["uuid"] == Mapped[uuid.UUID] + assert sa.inspect(model).columns["uuid"].primary_key + + assert ( + sa.inspect(SourceDocument).relationships["sent_webmentions"].mapper.class_ + is SentWebmention + ) + assert ( + sa.inspect(SentWebmention).relationships["source"].mapper.class_ + is SourceDocument + ) + + +@pytest.mark.parametrize( + ("desired_revision", "processed_revision", "expected"), + [(1, None, True), (1, 0, True), (2, 1, True), (1, 1, False), (1, 2, False)], +) +def test_sent_webmention_pending( + desired_revision: int, processed_revision: int | None, expected: bool +) -> None: + webmention = SentWebmention( + target="https://example.com/post", + desired_revision=desired_revision, + processed_revision=processed_revision, + ) + assert webmention.awaiting_processing is expected + + +def test_sent_webmention_pending_sql_matches_python(app: Flask) -> None: + cases = [(1, None, True), (1, 0, True), (2, 1, True), (1, 1, False), (1, 2, False)] + + with app.app_context(): + now = datetime.now(UTC) + source = SourceDocument( + path="pending/index.html", + url="https://site.example/pending/", + content_hash="0" * 32, + last_seen_at=now, + revised_at=now, + ) + for i, (desired, processed, _) in enumerate(cases): + source.sent_webmentions.append( + SentWebmention( + target=f"https://example.com/post/{i}", + desired_revision=desired, + processed_revision=processed, + ) + ) + db.session.add(source) + db.session.commit() + + pending_ids = set( + db.session.scalars( + sa.select(SentWebmention.uuid).where(SentWebmention.awaiting_processing) + ) + ) + for webmention, (_, _, expected) in zip( + source.sent_webmentions, cases, strict=True + ): + assert webmention.awaiting_processing is expected + assert (webmention.uuid in pending_ids) is expected + + result = db.session.execute( + sa.update(SentWebmention) + .where(SentWebmention.awaiting_processing) + .values(attempt_count=1) + ) + assert result.rowcount == 3 + + +def test_source_repr() -> None: + source = SourceDocument( + path="example/index.html", + url="https://dennisfink.me/blog/example/", + content_hash="0" * 32, + revision=1, + last_seen_at=datetime.now(UTC), + revised_at=datetime.now(UTC), + ) + assert repr(source) == "" + + +def test_sent_webmention_repr() -> None: + webmention = SentWebmention(target="https://example.com/post", desired_revision=1) + assert repr(webmention) == "" + + +def test_reopen_preserves_delivery_history() -> None: + sent_at = datetime.now(UTC) + mention = SentWebmention( + target="https://example.com/post", + linked=True, + desired_revision=2, + processed_revision=2, + sent_revision=2, + status=SentWebmentionStatus.SENT, + attempt_count=3, + next_retry_at=sent_at, + last_sent_at=sent_at, + ) + assert mention.reset_for_revision(3, False) is None + assert mention.desired_revision == 3 + assert mention.linked is False + assert mention.attempt_count == 0 + assert mention.next_retry_at is None + assert mention.status == SentWebmentionStatus.SENT + assert mention.sent_revision == 2 + + +def test_resend_requeues_current_revision_without_losing_send_history() -> None: + sent_at = datetime.now(UTC) + mention = SentWebmention( + target="https://example.com/post", + linked=True, + desired_revision=3, + processed_revision=3, + sent_revision=2, + status=SentWebmentionStatus.FAILED, + failure_reason="Previous failure", + webmention_endpoint="https://example.com/webmention", + endpoint_response_status=HTTPStatus.SERVICE_UNAVAILABLE, + status_url="https://example.com/status", + attempt_count=3, + next_retry_at=sent_at, + last_attempted_at=sent_at, + last_sent_at=sent_at, + ) + assert not mention.awaiting_processing + + assert mention.clear_result_for_resend() is None + assert mention.desired_revision == 3 + assert mention.linked is True + assert mention.awaiting_processing + assert mention.processed_revision is None + assert mention.status is None + assert mention.failure_reason is None + assert mention.webmention_endpoint is None + assert mention.endpoint_response_status is None + assert mention.status_url is None + assert mention.attempt_count == 0 + assert mention.next_retry_at is None + assert mention.sent_revision == 2 + assert mention.last_attempted_at == sent_at + assert mention.last_sent_at == sent_at + + +def test_long_sent_targets_enforce_uniqueness_without_indexing_url_text( + app: Flask, +) -> None: + now = datetime.now(UTC) + long_target = "https://destination.example/" + "a" * 4000 + with app.app_context(): + source = SourceDocument( + path="long/index.html", + url="https://example.com/long", + content_hash="0" * 32, + revision=1, + last_seen_at=now, + revised_at=now, + ) + source.sent_webmentions.append( + SentWebmention(target=long_target, desired_revision=1) + ) + db.session.add(source) + db.session.commit() + assert source.sent_webmentions[0].target_digest == target_digest(long_target) + + source.sent_webmentions.append( + SentWebmention(target=long_target, desired_revision=1) + ) + with pytest.raises(IntegrityError): + db.session.commit() + db.session.rollback() diff --git a/tests/models/test_user.py b/tests/models/test_user.py new file mode 100644 index 0000000..a3f95ae --- /dev/null +++ b/tests/models/test_user.py @@ -0,0 +1,41 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import pytest + +from webmentions_ssg.models.user import User + + +def test_user_repr() -> None: + user = User(username="admin", password="secret") + assert repr(user) == "" + + +def test_user_password_is_write_only() -> None: + user = User(username="admin", password="secret") + with pytest.raises(AttributeError, match="Password is write-only"): + _ = user.password + + +def test_user_password_is_hashed_and_can_be_checked() -> None: + user = User(username="admin", password="secret") + assert user.password_hash != "secret" + assert user.check_password("secret") + assert not user.check_password("wrong") + + +def test_user_session_id_changes_when_password_hash_changes() -> None: + user = User(username="admin", password="a strong passphrase", id=42) + original_hash = user.password_hash + original_id = user.get_id() + + assert original_id.startswith("42:") + assert original_hash not in original_id + assert len(original_id.split(":", 1)[1]) == 64 + + # Even a reset to the same password rehashes with a new salt, revoking + # any old session token. + user.password = "a strong passphrase" + assert user.password_hash != original_hash + assert user.get_id() != original_id diff --git a/tests/models/test_utils.py b/tests/models/test_utils.py new file mode 100644 index 0000000..8722a02 --- /dev/null +++ b/tests/models/test_utils.py @@ -0,0 +1,95 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import uuid +from collections.abc import Callable +from datetime import UTC, datetime + +import pytest + +from webmentions_ssg.models.received import ReceivedWebmention +from webmentions_ssg.models.sources import SentWebmention, SourceDocument +from webmentions_ssg.models.utils import ( + source_target_digest, + target_digest, + uuid7_to_datetime, +) + + +def test_uuid7_to_datetime() -> None: + identifier = uuid.uuid7() + timestamp = uuid7_to_datetime(identifier) + + assert timestamp.tzinfo is UTC + assert timestamp.timestamp() == pytest.approx(identifier.time / 1000) + + +@pytest.mark.parametrize( + "factory", + [ + pytest.param( + lambda identifier: ReceivedWebmention( + uuid=identifier, + source="https://source.example/post", + target="https://dennisfink.me/blog/example/", + ), + id="received-webmention", + ), + pytest.param( + lambda identifier: SourceDocument( + uuid=identifier, + path="example/index.html", + url="https://dennisfink.me/blog/example/", + content_hash="0" * 32, + revision=1, + last_seen_at=datetime.now(UTC), + revised_at=datetime.now(UTC), + ), + id="source", + ), + pytest.param( + lambda identifier: SentWebmention( + uuid=identifier, target="https://example.com/post", desired_revision=1 + ), + id="sent-webmention", + ), + ], +) +def test_created_at_comes_from_uuid7_timestamp( + factory: Callable[ + [uuid.UUID], ReceivedWebmention | SourceDocument | SentWebmention + ], +) -> None: + identifier = uuid.uuid7() + model = factory(identifier) + assert model.created_at == uuid7_to_datetime(identifier) + + +def test_received_pair_fingerprint_is_unambiguous() -> None: + assert source_target_digest("a", "bc") != source_target_digest("ab", "c") + assert source_target_digest("a", "b") != source_target_digest("b", "a") + + +def test_models_hash_url_fields_when_created_or_changed() -> None: + received = ReceivedWebmention( + uuid=uuid.uuid7(), + source="https://source.example/a", + target="https://site.example/b", + ) + assert received.source_target_digest == source_target_digest( + received.source, received.target + ) + received.source = "https://source.example/c" + assert received.source_target_digest == source_target_digest( + received.source, received.target + ) + received.target = "https://site.example/d" + assert received.source_target_digest == source_target_digest( + received.source, received.target + ) + + sent = SentWebmention(target="https://site.example/a", desired_revision=1) + assert sent.target_digest == target_digest(sent.target) + sent.target = "https://site.example/b" + assert sent.target_digest == target_digest(sent.target) diff --git a/tests/tasks/test_notifications.py b/tests/tasks/test_notifications.py index ca89a7a..dadec3e 100644 --- a/tests/tasks/test_notifications.py +++ b/tests/tasks/test_notifications.py @@ -13,7 +13,7 @@ from huey.exceptions import TaskLockedException from webmentions_ssg import db from webmentions_ssg.extensions import huey -from webmentions_ssg.models import ReceivedWebmention +from webmentions_ssg.models.received import ReceivedWebmention @pytest.fixture diff --git a/tests/tasks/test_retention.py b/tests/tasks/test_retention.py index 70ab0e0..a3891a9 100644 --- a/tests/tasks/test_retention.py +++ b/tests/tasks/test_retention.py @@ -11,7 +11,7 @@ import sqlalchemy as sa from flask import Flask from webmentions_ssg import db -from webmentions_ssg.models import ReceivedWebmention +from webmentions_ssg.models.received import ReceivedWebmention from webmentions_ssg.tasks.retention import purge_old_received_webmentions diff --git a/tests/tasks/test_scanner.py b/tests/tasks/test_scanner.py index 4cf2a67..dbeeac9 100644 --- a/tests/tasks/test_scanner.py +++ b/tests/tasks/test_scanner.py @@ -17,7 +17,11 @@ from huey.exceptions import RetryTask from pytest import MonkeyPatch from webmentions_ssg import db -from webmentions_ssg.models import SentWebmention, SentWebmentionStatus, SourceDocument +from webmentions_ssg.models.sources import ( + SentWebmention, + SentWebmentionStatus, + SourceDocument, +) from webmentions_ssg.tasks import source_parser from .scanner_helpers import BASE_URL, SOURCE_URL, configure_scanner, write_post diff --git a/tests/tasks/test_sender.py b/tests/tasks/test_sender.py index 9a0430c..0d68933 100644 --- a/tests/tasks/test_sender.py +++ b/tests/tasks/test_sender.py @@ -14,7 +14,11 @@ import pytest from flask import Flask from webmentions_ssg import db -from webmentions_ssg.models import SentWebmention, SentWebmentionStatus, SourceDocument +from webmentions_ssg.models.sources import ( + SentWebmention, + SentWebmentionStatus, + SourceDocument, +) SOURCE_URL = "https://dennisfink.me/blog/example/" TARGET_URL = "https://example.com/post" diff --git a/tests/tasks/test_verification.py b/tests/tasks/test_verification.py index c5550c8..3547464 100644 --- a/tests/tasks/test_verification.py +++ b/tests/tasks/test_verification.py @@ -15,7 +15,8 @@ import sqlalchemy as sa from flask import Flask from webmentions_ssg import db -from webmentions_ssg.models import BlockedSource, ReceivedWebmention +from webmentions_ssg.models.moderation import BlockedDomain +from webmentions_ssg.models.received import ReceivedWebmention from webmentions_ssg.net import AddressResolutionError SOURCE_URL = "https://source.example/article" @@ -363,7 +364,7 @@ def test_verify_webmention_refuses_domain_blocked_after_enqueue( ) -> None: identifier = create_webmention(app) with app.app_context(): - db.session.add(BlockedSource(domain="source.example")) + db.session.add(BlockedDomain(domain="source.example")) db.session.commit() fetch = Mock(return_value=True) diff --git a/tests/test_auth.py b/tests/test_auth.py index 8219a07..27e8fb0 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -16,7 +16,7 @@ from webmentions_ssg.auth import ( login_manager, validate_new_password, ) -from webmentions_ssg.models import User +from webmentions_ssg.models.user import User @pytest.mark.parametrize("character", ["\x00", "\x1f", "\x7f", "\x80", "\x9f"]) diff --git a/tests/test_cli.py b/tests/test_cli.py index f5535d2..5e71e2a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -19,7 +19,8 @@ from sqlalchemy.exc import IntegrityError from webmentions_ssg import create_app, db, migrate from webmentions_ssg.config import DevelopmentConfig, TestingConfig -from webmentions_ssg.models import ReceivedWebmention, ReceivedWebmentionStatus, User +from webmentions_ssg.models.received import ReceivedWebmention, ReceivedWebmentionStatus +from webmentions_ssg.models.user import User def test_cli_groups_are_registered(app: Flask) -> None: @@ -158,6 +159,8 @@ def test_create_db_builds_schema_and_records_latest_migration( assert "source_target_digest" in received_columns assert {"attempt_count", "next_retry_at"} <= columns assert "recovery_attempts" in received_columns + assert {"blocked_domains", "allowed_domains"} <= table_names + assert not {"blocked_sources", "allowed_sources"} & table_names assert "ix_received_webmentions_status_updated_at" in received_indexes @@ -250,11 +253,27 @@ def test_upgrade_merges_existing_migration_branches( """Existing installations on either branch can upgrade to a single head.""" with database_app.app_context(): migrate_upgrade(revision=existing_head) + if existing_head == "3e8c87a96b2d": + with db.engine.begin() as connection: + connection.execute( + sa.text("INSERT INTO blocked_sources (domain) VALUES (:domain)"), + {"domain": "existing.example"}, + ) migrate_upgrade() assert _database_revision(database_app) == _head_revision(database_app) tables = set(inspect(db.engine).get_table_names()) - assert {"blocked_sources", "allowed_sources", "source_documents"} <= tables + assert {"blocked_domains", "allowed_domains", "source_documents"} <= tables + assert "blocked_sources" not in tables + assert "allowed_sources" not in tables + if existing_head == "3e8c87a96b2d": + with db.engine.connect() as connection: + assert ( + connection.execute( + sa.text("SELECT domain FROM blocked_domains") + ).scalar_one() + == "existing.example" + ) def test_upgrade_is_safe_to_run_when_already_current(database_app: Flask) -> None: @@ -304,7 +323,7 @@ def test_upgrade_backfills_portable_digest_for_existing_received_rows( :param database_app: Flask application connected to the legacy SQLite schema. """ - from webmentions_ssg.models import source_target_digest + from webmentions_ssg.models.utils import source_target_digest with database_app.app_context(): migrate_upgrade(revision="c0272f25ce17") diff --git a/tests/test_config.py b/tests/test_config.py index 4a5f4cb..81ff498 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -19,7 +19,7 @@ from webmentions_ssg.config import ( ProductionConfig, TestingConfig, ) -from webmentions_ssg.forms import AllowedTargetHostname +from webmentions_ssg.forms.validators import AllowedTargetHostname @pytest.mark.parametrize( @@ -42,9 +42,9 @@ def test_session_cookie_defaults( def test_source_allowlist_mode_requires_boolean_configuration(tmp_path: Path) -> None: class BadSourceModeConfig(TestingConfig): LOG_FILE = str(tmp_path / "mode-test.log") - SOURCE_ALLOWLIST_ENABLED = "yes" + ALLOWLIST_ENABLED = "yes" - with pytest.raises(ValueError, match="SOURCE_ALLOWLIST_ENABLED"): + with pytest.raises(ValueError, match="ALLOWLIST_ENABLED"): create_app(BadSourceModeConfig) diff --git a/tests/test_forms.py b/tests/test_forms.py deleted file mode 100644 index c6a8b25..0000000 --- a/tests/test_forms.py +++ /dev/null @@ -1,427 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause - -import pytest -from flask import Flask -from werkzeug.datastructures import MultiDict -from wtforms import Form, StringField, ValidationError - -from webmentions_ssg.forms import ( - AllowedTargetHostname, - ConfirmActionForm, - LoginForm, - MaxURLBytes, - ReceivedWebmentionFilterForm, - RejectNonPublicIPLiteral, - WebmentionSubmissionForm, -) - -VALID_SOURCE = "https://source.example/post" -VALID_TARGET = "https://dennisfink.me/blog/example/" - - -class URLForm(Form): - url = StringField("URL") - - -@pytest.mark.parametrize( - ("form_data", "invalid_field", "expected_error"), - [ - pytest.param( - {"target": VALID_TARGET}, - "source", - "This field is required.", - id="source-required", - ), - pytest.param( - {"source": "not a URL", "target": VALID_TARGET}, - "source", - "Invalid URL.", - id="source-url", - ), - pytest.param( - {"source": "ftp://source.example/post", "target": VALID_TARGET}, - "source", - "source must begin with http or https", - id="source-scheme", - ), - pytest.param( - {"source": VALID_SOURCE}, - "target", - "This field is required.", - id="target-required", - ), - pytest.param( - {"source": VALID_SOURCE, "target": "not a URL"}, - "target", - "Invalid URL.", - id="target-url", - ), - pytest.param( - {"source": VALID_SOURCE, "target": "ftp://dennisfink.me/blog/example/"}, - "target", - "target must begin with http or https", - id="target-scheme", - ), - pytest.param( - {"source": "http:///source.example/post", "target": VALID_TARGET}, - "source", - "Invalid URL.", - id="source-missing-host", - ), - pytest.param( - {"source": VALID_SOURCE, "target": "https://dennisfink.me:invalid/"}, - "target", - "Invalid URL.", - id="target-invalid-port", - ), - ], -) -def test_endpoint_form_rejects_invalid_field_syntax( - app: Flask, form_data: dict[str, str], invalid_field: str, expected_error: str -) -> None: - with app.test_request_context("/endpoint", method="POST"): - form = WebmentionSubmissionForm( - formdata=MultiDict(form_data), meta={"csrf": False} - ) - - assert not form.validate() - assert expected_error in form.errors[invalid_field] - - -@pytest.mark.parametrize("scheme", ["http", "https", "HTTP", "HTTPS"]) -def test_endpoint_form_accepts_http_schemes_case_insensitively( - app: Flask, scheme: str -) -> None: - with app.test_request_context("/endpoint", method="POST"): - form = WebmentionSubmissionForm( - formdata=MultiDict( - { - "source": f"{scheme}://source.example/post", - "target": f"{scheme}://dennisfink.me/blog/example/", - } - ), - meta={"csrf": False}, - ) - assert form.validate() - - -def test_endpoint_form_accepts_valid_data(app: Flask) -> None: - with app.test_request_context("/endpoint", method="POST"): - form = WebmentionSubmissionForm( - formdata=MultiDict({"source": VALID_SOURCE, "target": VALID_TARGET}), - meta={"csrf": False}, - ) - - assert form.validate() - assert form.errors == {} - - -@pytest.mark.parametrize( - ("form_data", "invalid_field"), - [ - pytest.param({"password": "secret"}, "username", id="username-required"), - pytest.param({"username": "admin"}, "password", id="password-required"), - ], -) -def test_login_form_requires_credentials( - app: Flask, form_data: dict[str, str], invalid_field: str -) -> None: - with app.test_request_context("/login", method="POST"): - form = LoginForm(formdata=MultiDict(form_data), meta={"csrf": False}) - - assert not form.validate() - assert "This field is required." in form.errors[invalid_field] - - -def test_login_form_accepts_credentials(app: Flask) -> None: - with app.test_request_context("/login", method="POST"): - form = LoginForm( - formdata=MultiDict({"username": "admin", "password": "secret"}), - meta={"csrf": False}, - ) - assert form.validate() - - -def test_received_filter_form_accepts_valid_get_params(app: Flask) -> None: - with app.test_request_context("/received", method="GET"): - form = ReceivedWebmentionFilterForm( - formdata=MultiDict( - { - "status": "declined", - "q": "example.org", - "date_from": "2026-11-08", - "date_to": "2026-11-14", - } - ), - meta={"csrf": False}, - ) - assert form.validate() - assert form.status.data == "declined" - assert form.q.data == "example.org" - assert form.date_from.data.isoformat() == "2026-11-08" - assert form.date_to.data.isoformat() == "2026-11-14" - assert "csrf_token" not in form._fields - - -@pytest.mark.parametrize( - "data", - [ - {"status": "unknown"}, - {"date_from": "not-a-date"}, - {"date_from": "2026-11-8"}, - {"date_to": "2026-02-30"}, - {"date_from": "2026-11-15", "date_to": "2026-11-08"}, - ], -) -def test_received_filter_form_rejects_invalid_get_params( - app: Flask, data: dict[str, str] -) -> None: - with app.test_request_context("/received", method="GET"): - form = ReceivedWebmentionFilterForm( - formdata=MultiDict(data), meta={"csrf": False} - ) - assert not form.validate() - assert form.errors - - -def test_received_filter_form_validates_sort_and_page_size(app: Flask) -> None: - with app.test_request_context("/received", method="GET"): - form = ReceivedWebmentionFilterForm( - formdata=MultiDict({"sort": "updated", "order": "asc", "per_page": "37"}), - meta={"csrf": False}, - ) - assert form.validate() - assert form.sort.data == "updated" - assert form.order.data == "asc" - assert form.per_page.data == 37 - - with app.test_request_context("/received", method="GET"): - form = ReceivedWebmentionFilterForm(formdata=MultiDict(), meta={"csrf": False}) - assert form.validate() - assert (form.sort.data, form.order.data, form.per_page.data) == ( - "received", - "desc", - 25, - ) - - -@pytest.mark.parametrize( - "data", - [ - {"sort": "invalid"}, - {"order": "invalid"}, - {"per_page": "0"}, - {"per_page": "101"}, - {"per_page": "invalid"}, - ], -) -def test_received_filter_form_rejects_invalid_sort_and_page_size( - app: Flask, data: dict[str, str] -) -> None: - with app.test_request_context("/received", method="GET"): - form = ReceivedWebmentionFilterForm( - formdata=MultiDict(data), meta={"csrf": False} - ) - assert not form.validate() - assert form.errors - - -def test_admin_action_form_accepts_submission(app: Flask) -> None: - with app.test_request_context(method="POST"): - form = ConfirmActionForm(meta={"csrf": False}) - assert form.validate() - - -def test_endpoint_does_not_do_dns_in_web_request( - app: Flask, monkeypatch: pytest.MonkeyPatch -) -> None: - def unexpected_dns_lookup(*args: object, **kwargs: object) -> None: - raise AssertionError("No DNS lookup belongs in form validation") - - monkeypatch.setattr( - "webmentions_ssg.net.dns.resolver.resolve_name", unexpected_dns_lookup - ) - with app.test_request_context("/endpoint", method="POST"): - for source in (VALID_SOURCE, "ftp://source.example/post", "not a URL"): - form = WebmentionSubmissionForm( - formdata=MultiDict({"source": source, "target": VALID_TARGET}), - meta={"csrf": False}, - ) - assert form.validate() is (source == VALID_SOURCE) - - -@pytest.mark.parametrize( - "source", ["http://127.0.0.1/", "http://[::1]/", "http://169.254.169.254/"] -) -def test_endpoint_rejects_private_ip_literals(app: Flask, source: str) -> None: - with app.test_request_context("/endpoint", method="POST"): - form = WebmentionSubmissionForm( - formdata=MultiDict({"source": source, "target": VALID_TARGET}), - meta={"csrf": False}, - ) - assert not form.validate() - assert "URL must use a public address." in form.errors["source"] - - -def test_endpoint_rejects_private_ip_literal_with_trailing_dot(app: Flask) -> None: - with app.test_request_context("/endpoint", method="POST"): - form = WebmentionSubmissionForm( - formdata=MultiDict( - {"source": "http://127.0.0.1./", "target": VALID_TARGET} - ), - meta={"csrf": False}, - ) - assert not form.validate() - assert "URL must use a public address." in form.errors["source"] - - -def test_endpoint_handles_malformed_ipv6_without_crashing(app: Flask) -> None: - with app.test_request_context("/endpoint", method="POST"): - form = WebmentionSubmissionForm( - formdata=MultiDict({"source": VALID_SOURCE, "target": "http://[::1"}), - meta={"csrf": False}, - ) - assert not form.validate() - assert "Invalid URL." in form.errors["target"] - - -def test_endpoint_form_applies_configurable_url_byte_limit_to_both_fields( - app: Flask, -) -> None: - with app.test_request_context("/endpoint", method="POST"): - app.config["MAX_WEBMENTION_URL_BYTES"] = len(VALID_SOURCE.encode("utf-8")) - form = WebmentionSubmissionForm( - formdata=MultiDict({"source": VALID_SOURCE + "x", "target": VALID_TARGET}), - meta={"csrf": False}, - ) - assert not form.validate() - assert "source" in form.errors - assert any("bytes" in message for message in form.errors["source"]) - - app.config["MAX_WEBMENTION_URL_BYTES"] = len(VALID_TARGET.encode("utf-8")) - form = WebmentionSubmissionForm( - formdata=MultiDict({"source": VALID_SOURCE, "target": VALID_TARGET + "x"}), - meta={"csrf": False}, - ) - assert not form.validate() - assert "target" in form.errors - assert any("bytes" in message for message in form.errors["target"]) - - -@pytest.mark.parametrize("field_name", ["source", "target"]) -@pytest.mark.parametrize( - "invalid_url", - [ - pytest.param("http://ex☃mple.test/", id="invalid-idn"), - pytest.param("https://example.com/a\nb", id="newline"), - pytest.param("https://example.com/a\rb", id="carriage-return"), - pytest.param("https://example.com/\x00b", id="nul"), - pytest.param("https://example.com/a b", id="ascii-space"), - pytest.param("https://example.com/a\u00a0b", id="unicode-whitespace"), - pytest.param("https://example.com/\x7fb", id="del"), - pytest.param("https://example.com/a\x80b", id="c1-control"), - ], -) -def test_endpoint_rejects_invalid_httpx_urls_and_whitespace( - app: Flask, field_name: str, invalid_url: str -) -> None: - data = {"source": VALID_SOURCE, "target": VALID_TARGET} - data[field_name] = invalid_url - with app.test_request_context("/endpoint", method="POST"): - form = WebmentionSubmissionForm(formdata=MultiDict(data), meta={"csrf": False}) - assert not form.validate() - assert "Invalid URL." in form.errors[field_name] - - -def test_endpoint_accepts_encoded_path_space(app: Flask) -> None: - with app.test_request_context("/endpoint", method="POST"): - form = WebmentionSubmissionForm( - formdata=MultiDict( - {"source": "https://source.example/a%20b", "target": VALID_TARGET} - ), - meta={"csrf": False}, - ) - assert form.validate() - - -def test_endpoint_form_rejects_matching_source_and_target(app: Flask) -> None: - with app.test_request_context("/endpoint", method="POST"): - form = WebmentionSubmissionForm( - formdata=MultiDict({"source": VALID_TARGET, "target": VALID_TARGET}), - meta={"csrf": False}, - ) - - assert not form.validate() - assert "Field must not be equal to target." in form.errors["source"] - - -def test_allowed_hostname_accepts_configured_hostname(app: Flask) -> None: - app.config["ALLOWED_TARGET_HOSTNAMES"] = ["dennisfink.me"] - form = URLForm(MultiDict({"url": "https://dennisfink.me/blog/example/"})) - with app.app_context(): - AllowedTargetHostname()(form, form.url) - - -def test_allowed_hostname_rejects_unconfigured_hostname(app: Flask) -> None: - app.config["ALLOWED_TARGET_HOSTNAMES"] = ["dennisfink.me"] - form = URLForm(MultiDict({"url": "https://example.com/post"})) - with app.app_context(), pytest.raises(ValidationError, match="Invalid input"): - AllowedTargetHostname()(form, form.url) - - -def test_allowed_hostname_uses_custom_message(app: Flask) -> None: - app.config["ALLOWED_TARGET_HOSTNAMES"] = ["dennisfink.me"] - form = URLForm(MultiDict({"url": "https://example.com/post"})) - with ( - app.app_context(), - pytest.raises(ValidationError, match="Hostname is not allowed"), - ): - AllowedTargetHostname("Hostname is not allowed")(form, form.url) - - -@pytest.mark.parametrize( - "address", - ["64:ff9b::a9fe:a9fe", "::10.0.0.1", "2002:a9fe:a9fe::1", "::ffff:169.254.169.254"], -) -def test_public_ip_literal_rejects_ipv6_transition_addresses(address: str) -> None: - form = URLForm(MultiDict({"url": f"https://[{address}]/"})) - with pytest.raises(ValidationError, match="URL must use a public address"): - RejectNonPublicIPLiteral()(form, form.url) - - -def test_public_ip_literal_accepts_public_ipv6_address() -> None: - form = URLForm(MultiDict({"url": "https://[2606:2800:220:1:248:1893:25c8:1946]/"})) - RejectNonPublicIPLiteral()(form, form.url) - - -def test_allowed_hostname_handles_invalid_ipv6(app: Flask) -> None: - form = URLForm(MultiDict({"url": "http://[::1"})) - with app.app_context(), pytest.raises(ValidationError, match="Invalid input"): - AllowedTargetHostname()(form, form.url) - - -def test_url_max_bytes_respects_utf8_byte_length_and_current_config(app: Flask) -> None: - validator = MaxURLBytes() - form = URLForm(MultiDict({"url": "é" * 3})) - with app.app_context(): - app.config["MAX_WEBMENTION_URL_BYTES"] = 6 - validator(form, form.url) - app.config["MAX_WEBMENTION_URL_BYTES"] = 5 - with pytest.raises(ValidationError, match="5 bytes"): - validator(form, form.url) - - -def test_url_max_bytes_custom_message(app: Flask) -> None: - form = URLForm(MultiDict({"url": "too-long"})) - with app.app_context(): - app.config["MAX_WEBMENTION_URL_BYTES"] = 2 - with pytest.raises(ValidationError, match="URL too long"): - MaxURLBytes(message="URL too long")(form, form.url) - - -def test_url_max_bytes_rejects_malformed_unicode(app: Flask) -> None: - form = URLForm(MultiDict({"url": "\ud800"})) - with app.app_context(), pytest.raises(ValidationError, match="valid UTF-8"): - MaxURLBytes()(form, form.url) diff --git a/tests/test_models.py b/tests/test_models.py deleted file mode 100644 index 4e785c7..0000000 --- a/tests/test_models.py +++ /dev/null @@ -1,439 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause - -import uuid -from collections.abc import Callable -from datetime import UTC, datetime -from http import HTTPStatus -from typing import get_type_hints - -import pytest -import sqlalchemy as sa -from flask import Flask -from sqlalchemy.exc import IntegrityError -from sqlalchemy.orm import Mapped - -from webmentions_ssg import db -from webmentions_ssg.models import ( - ReceivedWebmention, - ReceivedWebmentionStatus, - SentWebmention, - SourceDocument, - User, - source_target_digest, - target_digest, - uuid7_to_datetime, -) - - -def test_uuid_annotations_and_relationships_resolve() -> None: - """Verify that UUID columns and forward relationships map correctly.""" - for model in (ReceivedWebmention, SourceDocument, SentWebmention): - assert get_type_hints(model)["uuid"] == Mapped[uuid.UUID] - assert sa.inspect(model).columns["uuid"].primary_key - - assert ( - sa.inspect(SourceDocument).relationships["sent_webmentions"].mapper.class_ - is SentWebmention - ) - assert ( - sa.inspect(SentWebmention).relationships["source"].mapper.class_ - is SourceDocument - ) - - -def test_user_repr() -> None: - user = User(username="admin", password="secret") - assert repr(user) == "" - - -def test_user_password_is_write_only() -> None: - user = User(username="admin", password="secret") - with pytest.raises(AttributeError, match="Password is write-only"): - _ = user.password - - -def test_user_password_is_hashed_and_can_be_checked() -> None: - user = User(username="admin", password="secret") - assert user.password_hash != "secret" - assert user.check_password("secret") - assert not user.check_password("wrong") - - -@pytest.mark.parametrize( - ("status", "expected"), - [ - (ReceivedWebmentionStatus.RECEIVED, False), - (ReceivedWebmentionStatus.VERIFYING, False), - (ReceivedWebmentionStatus.VERIFIED, True), - (ReceivedWebmentionStatus.FAILED, False), - (ReceivedWebmentionStatus.SOURCE_REMOVED, False), - ], -) -def test_received_webmention_verified( - status: ReceivedWebmentionStatus, expected: bool -) -> None: - webmention = ReceivedWebmention( - uuid=uuid.uuid7(), - source="https://source.example/post", - target="https://dennisfink.me/blog/example/", - status=status, - ) - - assert webmention.verified is expected - - -@pytest.mark.parametrize( - ("desired_revision", "processed_revision", "expected"), - [(1, None, True), (1, 0, True), (2, 1, True), (1, 1, False), (1, 2, False)], -) -def test_sent_webmention_pending( - desired_revision: int, processed_revision: int | None, expected: bool -) -> None: - webmention = SentWebmention( - target="https://example.com/post", - desired_revision=desired_revision, - processed_revision=processed_revision, - ) - assert webmention.awaiting_processing is expected - - -def test_sent_webmention_pending_sql_matches_python(app: Flask) -> None: - cases = [(1, None, True), (1, 0, True), (2, 1, True), (1, 1, False), (1, 2, False)] - - with app.app_context(): - now = datetime.now(UTC) - source = SourceDocument( - path="pending/index.html", - url="https://site.example/pending/", - content_hash="0" * 32, - last_seen_at=now, - revised_at=now, - ) - for i, (desired, processed, _) in enumerate(cases): - source.sent_webmentions.append( - SentWebmention( - target=f"https://example.com/post/{i}", - desired_revision=desired, - processed_revision=processed, - ) - ) - db.session.add(source) - db.session.commit() - - pending_ids = set( - db.session.scalars( - sa.select(SentWebmention.uuid).where(SentWebmention.awaiting_processing) - ) - ) - for webmention, (_, _, expected) in zip( - source.sent_webmentions, cases, strict=True - ): - assert webmention.awaiting_processing is expected - assert (webmention.uuid in pending_ids) is expected - - result = db.session.execute( - sa.update(SentWebmention) - .where(SentWebmention.awaiting_processing) - .values(attempt_count=1) - ) - assert result.rowcount == 3 - - -def test_uuid7_to_datetime() -> None: - identifier = uuid.uuid7() - timestamp = uuid7_to_datetime(identifier) - - assert timestamp.tzinfo is UTC - assert timestamp.timestamp() == pytest.approx(identifier.time / 1000) - - -@pytest.mark.parametrize( - "factory", - [ - pytest.param( - lambda identifier: ReceivedWebmention( - uuid=identifier, - source="https://source.example/post", - target="https://dennisfink.me/blog/example/", - ), - id="received-webmention", - ), - pytest.param( - lambda identifier: SourceDocument( - uuid=identifier, - path="example/index.html", - url="https://dennisfink.me/blog/example/", - content_hash="0" * 32, - revision=1, - last_seen_at=datetime.now(UTC), - revised_at=datetime.now(UTC), - ), - id="source", - ), - pytest.param( - lambda identifier: SentWebmention( - uuid=identifier, target="https://example.com/post", desired_revision=1 - ), - id="sent-webmention", - ), - ], -) -def test_created_at_comes_from_uuid7_timestamp( - factory: Callable[ - [uuid.UUID], ReceivedWebmention | SourceDocument | SentWebmention - ], -) -> None: - identifier = uuid.uuid7() - model = factory(identifier) - assert model.created_at == uuid7_to_datetime(identifier) - - -def test_received_webmention_repr() -> None: - webmention = ReceivedWebmention( - uuid=uuid.uuid7(), - source="https://source.example/post", - target="https://dennisfink.me/blog/example/", - ) - - assert repr(webmention) == ( - " " - "'https://dennisfink.me/blog/example/'>" - ) - - -def test_source_repr() -> None: - source = SourceDocument( - path="example/index.html", - url="https://dennisfink.me/blog/example/", - content_hash="0" * 32, - revision=1, - last_seen_at=datetime.now(UTC), - revised_at=datetime.now(UTC), - ) - assert repr(source) == "" - - -def test_sent_webmention_repr() -> None: - webmention = SentWebmention(target="https://example.com/post", desired_revision=1) - assert repr(webmention) == "" - - -def test_user_session_id_changes_when_password_hash_changes() -> None: - user = User(username="admin", password="a strong passphrase", id=42) - original_hash = user.password_hash - original_id = user.get_id() - - assert original_id.startswith("42:") - assert original_hash not in original_id - assert len(original_id.split(":", 1)[1]) == 64 - - # Even a reset to the same password rehashes with a new salt, revoking - # any old session token. - user.password = "a strong passphrase" - assert user.password_hash != original_hash - assert user.get_id() != original_id - - -def test_received_status_uses_non_native_enum() -> None: - status_type = ReceivedWebmention.__table__.c.status.type - - assert isinstance(status_type, sa.Enum) - assert status_type.enum_class is ReceivedWebmentionStatus - assert status_type.native_enum is False - assert status_type.enums == [status.value for status in ReceivedWebmentionStatus] - - -def test_received_status_round_trips_as_enum(app: Flask) -> None: - with app.app_context(): - identifier = uuid.uuid7() - mention = ReceivedWebmention( - uuid=identifier, - source="https://source.example/post", - target="https://dennisfink.me/blog/example/", - status=ReceivedWebmentionStatus.VERIFIED, - ) - db.session.add(mention) - db.session.commit() - - assert ( - db.session.scalar( - sa.text("SELECT status FROM received_webmentions WHERE uuid = :uuid"), - {"uuid": identifier.hex}, - ) - == "verified" - ) - - db.session.expunge_all() - loaded = db.session.get(ReceivedWebmention, identifier) - assert loaded is not None - assert loaded.status is ReceivedWebmentionStatus.VERIFIED - - -def test_received_verification_reset_is_reusable_for_atomic_updates() -> None: - from webmentions_ssg.models import ReceivedWebmentionStatus - - mention = ReceivedWebmention( - uuid=uuid.uuid7(), - source="https://source.example/post", - target="https://dennisfink.me/post", - status=ReceivedWebmentionStatus.FAILED, - failure_reason="Old error", - recovery_attempts=3, - ) - assert mention.reopen_verification() is None - assert mention.status == ReceivedWebmentionStatus.RECEIVED - assert mention.failure_reason is None - assert mention.recovery_attempts == 0 - assert mention.updated_at is not None - - -def test_reopen_preserves_delivery_history() -> None: - from webmentions_ssg.models import SentWebmentionStatus - - sent_at = datetime.now(UTC) - mention = SentWebmention( - target="https://example.com/post", - linked=True, - desired_revision=2, - processed_revision=2, - sent_revision=2, - status=SentWebmentionStatus.SENT, - attempt_count=3, - next_retry_at=sent_at, - last_sent_at=sent_at, - ) - assert mention.reset_for_revision(3, False) is None - assert mention.desired_revision == 3 - assert mention.linked is False - assert mention.attempt_count == 0 - assert mention.next_retry_at is None - assert mention.status == SentWebmentionStatus.SENT - assert mention.sent_revision == 2 - - -def test_resend_requeues_current_revision_without_losing_send_history() -> None: - from webmentions_ssg.models import SentWebmentionStatus - - sent_at = datetime.now(UTC) - mention = SentWebmention( - target="https://example.com/post", - linked=True, - desired_revision=3, - processed_revision=3, - sent_revision=2, - status=SentWebmentionStatus.FAILED, - failure_reason="Previous failure", - webmention_endpoint="https://example.com/webmention", - endpoint_response_status=HTTPStatus.SERVICE_UNAVAILABLE, - status_url="https://example.com/status", - attempt_count=3, - next_retry_at=sent_at, - last_attempted_at=sent_at, - last_sent_at=sent_at, - ) - assert not mention.awaiting_processing - - assert mention.clear_result_for_resend() is None - assert mention.desired_revision == 3 - assert mention.linked is True - assert mention.awaiting_processing - assert mention.processed_revision is None - assert mention.status is None - assert mention.failure_reason is None - assert mention.webmention_endpoint is None - assert mention.endpoint_response_status is None - assert mention.status_url is None - assert mention.attempt_count == 0 - assert mention.next_retry_at is None - assert mention.sent_revision == 2 - assert mention.last_attempted_at == sent_at - assert mention.last_sent_at == sent_at - - -def test_received_pair_fingerprint_is_unambiguous() -> None: - assert source_target_digest("a", "bc") != source_target_digest("ab", "c") - assert source_target_digest("a", "b") != source_target_digest("b", "a") - - -def test_models_hash_url_fields_when_created_or_changed() -> None: - received = ReceivedWebmention( - uuid=uuid.uuid7(), - source="https://source.example/a", - target="https://site.example/b", - ) - assert received.source_target_digest == source_target_digest( - received.source, received.target - ) - received.source = "https://source.example/c" - assert received.source_target_digest == source_target_digest( - received.source, received.target - ) - received.target = "https://site.example/d" - assert received.source_target_digest == source_target_digest( - received.source, received.target - ) - - sent = SentWebmention(target="https://site.example/a", desired_revision=1) - assert sent.target_digest == target_digest(sent.target) - sent.target = "https://site.example/b" - assert sent.target_digest == target_digest(sent.target) - - -def test_long_received_pair_can_be_stored_with_unique_digest(app: Flask) -> None: - long_source = "https://source.example/" + "a" * 4000 - long_target = "https://dennisfink.me/" + "b" * 4000 - with app.app_context(): - db.session.add( - ReceivedWebmention( - uuid=uuid.uuid7(), source=long_source, target=long_target - ) - ) - db.session.commit() - match = db.session.scalar( - sa.select(ReceivedWebmention).where( - ReceivedWebmention.source_target_digest - == source_target_digest(long_source, long_target) - ) - ) - assert match is not None - assert match.source == long_source - assert match.target == long_target - db.session.add( - ReceivedWebmention( - uuid=uuid.uuid7(), source=long_source, target=long_target - ) - ) - with pytest.raises(IntegrityError): - db.session.commit() - db.session.rollback() - - -def test_long_sent_targets_enforce_uniqueness_without_indexing_url_text( - app: Flask, -) -> None: - now = datetime.now(UTC) - long_target = "https://destination.example/" + "a" * 4000 - with app.app_context(): - source = SourceDocument( - path="long/index.html", - url="https://example.com/long", - content_hash="0" * 32, - revision=1, - last_seen_at=now, - revised_at=now, - ) - source.sent_webmentions.append( - SentWebmention(target=long_target, desired_revision=1) - ) - db.session.add(source) - db.session.commit() - assert source.sent_webmentions[0].target_digest == target_digest(long_target) - - source.sent_webmentions.append( - SentWebmention(target=long_target, desired_revision=1) - ) - with pytest.raises(IntegrityError): - db.session.commit() - db.session.rollback() diff --git a/tests/test_moderation_policy.py b/tests/test_moderation_policy.py new file mode 100644 index 0000000..48a34ac --- /dev/null +++ b/tests/test_moderation_policy.py @@ -0,0 +1,62 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Domain normalization and receiver admission policy.""" + +from flask import Flask + +from webmentions_ssg import db +from webmentions_ssg.models.moderation import AllowedDomain, BlockedDomain +from webmentions_ssg.moderation_policy import domain_from_url, rejection_reason + + +def test_domain_from_url() -> None: + assert ( + domain_from_url("https://BÜCHER.example.:8443/post") == "xn--bcher-kva.example" + ) + assert domain_from_url("https://[2001:db8::1]/post") == "2001:db8::1" + assert domain_from_url("invalid") is None + + +def test_blocked_domain_matches_itself_and_subdomains_but_not_suffixes( + app: Flask, +) -> None: + with app.app_context(): + db.session.add(BlockedDomain(domain="example.org")) + db.session.commit() + for source in ( + "https://example.org/post", + "https://blog.example.org/post", + "https://A.blog.Example.ORG./post", + ): + assert rejection_reason(source) == "Source domain is blocked" + for source in ( + "https://notexample.org/post", + "https://example.org.evil/post", + "https://other.net/post", + ): + assert rejection_reason(source) is None + + +def test_allowlist_is_fail_closed_and_blocklist_takes_precedence(app: Flask) -> None: + app.config["ALLOWLIST_ENABLED"] = True + with app.app_context(): + assert ( + rejection_reason("https://example.org/post") + == "Source domain is not allowlisted" + ) + db.session.add(AllowedDomain(domain="example.org")) + db.session.commit() + assert rejection_reason("https://blog.example.org/post") is None + assert ( + rejection_reason("https://notexample.org/post") + == "Source domain is not allowlisted" + ) + db.session.add(BlockedDomain(domain="blog.example.org")) + db.session.commit() + assert ( + rejection_reason("https://blog.example.org/post") + == "Source domain is blocked" + ) + assert rejection_reason("https://example.org/post") is None diff --git a/tests/test_source_policy.py b/tests/test_source_policy.py deleted file mode 100644 index cddfa3a..0000000 --- a/tests/test_source_policy.py +++ /dev/null @@ -1,67 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Domain normalization and receiver admission policy.""" - -import pytest -from flask import Flask - -from webmentions_ssg import db -from webmentions_ssg.models import AllowedSource, BlockedSource -from webmentions_ssg.source_policy import ( - source_domain_from_url, - source_rejection_reason, -) - - -def test_source_domain_from_url() -> None: - assert ( - source_domain_from_url("https://BÜCHER.example.:8443/post") - == "xn--bcher-kva.example" - ) - assert source_domain_from_url("https://[2001:db8::1]/post") == "2001:db8::1" - assert source_domain_from_url("invalid") is None - - -def test_blocked_domain_matches_itself_and_subdomains_but_not_suffixes( - app: Flask, -) -> None: - with app.app_context(): - db.session.add(BlockedSource(domain="example.org")) - db.session.commit() - for source in ( - "https://example.org/post", - "https://blog.example.org/post", - "https://A.blog.Example.ORG./post", - ): - assert source_rejection_reason(source) == "Source domain is blocked" - for source in ( - "https://notexample.org/post", - "https://example.org.evil/post", - "https://other.net/post", - ): - assert source_rejection_reason(source) is None - - -def test_allowlist_is_fail_closed_and_blocklist_takes_precedence(app: Flask) -> None: - app.config["SOURCE_ALLOWLIST_ENABLED"] = True - with app.app_context(): - assert ( - source_rejection_reason("https://example.org/post") - == "Source domain is not allowlisted" - ) - db.session.add(AllowedSource(domain="example.org")) - db.session.commit() - assert source_rejection_reason("https://blog.example.org/post") is None - assert ( - source_rejection_reason("https://notexample.org/post") - == "Source domain is not allowlisted" - ) - db.session.add(BlockedSource(domain="blog.example.org")) - db.session.commit() - assert ( - source_rejection_reason("https://blog.example.org/post") - == "Source domain is blocked" - ) - assert source_rejection_reason("https://example.org/post") is None diff --git a/tests/views/helpers.py b/tests/views/helpers.py index 354102b..2bea73e 100644 --- a/tests/views/helpers.py +++ b/tests/views/helpers.py @@ -13,13 +13,13 @@ from flask import Flask from flask.testing import FlaskClient from webmentions_ssg import db -from webmentions_ssg.models import ( - ReceivedWebmention, +from webmentions_ssg.models.received import ReceivedWebmention +from webmentions_ssg.models.sources import ( SentWebmention, SentWebmentionStatus, SourceDocument, - User, ) +from webmentions_ssg.models.user import User SOURCE_URL = "https://source.example/post" TARGET_URL = "https://dennisfink.me/blog/example/" @@ -41,7 +41,7 @@ def mock_endpoint_form( form.errors = errors or {} monkeypatch.setattr( - views_module.forms, "WebmentionSubmissionForm", Mock(return_value=form) + views_module, "WebmentionSubmissionForm", Mock(return_value=form) ) return form @@ -118,7 +118,7 @@ def mock_login_form( form.username.data = username form.password.data = password - monkeypatch.setattr(views_module.forms, "LoginForm", Mock(return_value=form)) + monkeypatch.setattr(views_module, "LoginForm", Mock(return_value=form)) return form @@ -128,9 +128,7 @@ def mock_admin_action_form( form = Mock() form.validate_on_submit.return_value = valid - monkeypatch.setattr( - views_module.forms, "ConfirmActionForm", Mock(return_value=form) - ) + monkeypatch.setattr(views_module, "ConfirmActionForm", Mock(return_value=form)) return form diff --git a/tests/views/test_admin.py b/tests/views/test_admin.py deleted file mode 100644 index 175bcfd..0000000 --- a/tests/views/test_admin.py +++ /dev/null @@ -1,1280 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause - -import re -import uuid -from datetime import UTC, datetime, timedelta -from html import unescape -from http import HTTPStatus -from types import ModuleType -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 - -from webmentions_ssg import 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", ["/sources", f"/sources/{uuid.uuid7()}"]) -def test_sent_views_require_login(client: FlaskClient, path: str) -> None: - response = client.get(path) - - assert response.status_code == HTTPStatus.FOUND - 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("/sources") - - assert response.status_code == HTTPStatus.OK - - html = response.get_data(as_text=True) - - assert "blog/example/index.html" in html - assert "https://dennisfink.me/blog/example/" in html - assert f"/sources/{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"/sources/{identifier}") - - assert response.status_code == HTTPStatus.OK - - 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"/sources/{uuid.uuid7()}") - assert response.status_code == HTTPStatus.NOT_FOUND - - -def test_received_requires_login(client: FlaskClient) -> None: - response = client.get("/received") - - assert response.status_code == HTTPStatus.FOUND - 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 == HTTPStatus.OK - html = response.get_data(as_text=True) - assert SOURCE_URL in html - assert TARGET_URL in html - - -@pytest.mark.parametrize( - ("filter_status", "expected_statuses"), - [ - ("pending", {"received"}), - ("verifying", {"verifying"}), - ("verified", {"verified"}), - ("declined", {"failed", "source_removed"}), - ], -) -def test_received_filters_by_display_status( - app: Flask, client: FlaskClient, filter_status: str, expected_statuses: set[str] -) -> None: - log_in(app, client) - statuses = ("received", "verifying", "verified", "failed", "source_removed") - for status in statuses: - create_received_webmention( - app, source=f"https://{status}.example/post", status=status - ) - - response = client.get("/received", query_string={"status": filter_status}) - - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - for status in statuses: - assert (f"https://{status}.example/post" in html) == ( - status in expected_statuses - ) - assert re.search( - rf']*\bvalue="{filter_status}")(?=[^>]*\bselected\b)[^>]*>', html - ) - - -def test_received_filter_args_uses_typed_dict_fields( - app: Flask, views_module: ModuleType -) -> None: - with app.test_request_context( - "/received", - query_string={ - "status": "declined", - "q": " example.org ", - "date_from": "2026-11-08", - "date_to": "2026-11-14", - "sort": "updated", - "order": "asc", - "per_page": "37", - }, - ): - form = views_module.received_filter_form() - assert set(views_module.ReceivedFilterArgs.__annotations__) == set(form._fields) - assert views_module.received_filter_args(form) == { - "status": "declined", - "q": "example.org", - "date_from": "2026-11-08", - "date_to": "2026-11-14", - "sort": "updated", - "order": "asc", - "per_page": "37", - } - - -@pytest.mark.parametrize("max_page_size", [10, 100]) -def test_received_filter_args_omits_defaults_for_configured_page_size( - app: Flask, views_module: ModuleType, max_page_size: int -) -> None: - app.config["RECEIVED_MAX_PAGE_SIZE"] = max_page_size - with app.test_request_context("/received"): - form = views_module.received_filter_form() - assert form.per_page.data == min(25, max_page_size) - assert views_module.received_filter_args(form) == {} - - -@pytest.mark.parametrize( - ("sort", "order", "expected"), - [ - ("received", "desc", ("removed", "failed", "verified", "pending")), - ("received", "asc", ("pending", "verified", "failed", "removed")), - ("updated", "desc", ("verified", "failed", "pending", "removed")), - ("updated", "asc", ("removed", "pending", "failed", "verified")), - ("status", "asc", ("pending", "verified", "removed", "failed")), - ("status", "desc", ("removed", "failed", "verified", "pending")), - ], -) -def test_received_sorting( - app: Flask, client: FlaskClient, sort: str, order: str, expected: tuple[str, ...] -) -> None: - log_in(app, client) - for label, status, day in ( - ("pending", "received", 12), - ("verified", "verified", 14), - ("failed", "failed", 13), - ("removed", "source_removed", 11), - ): - create_received_webmention( - app, - source=f"https://{label}.example/post", - status=status, - updated_at=datetime(2026, 11, day, tzinfo=UTC), - ) - - response = client.get("/received", query_string={"sort": sort, "order": order}) - - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - indices = [html.index(f"https://{label}.example/post") for label in expected] - assert indices == sorted(indices) - assert f'aria-sort="{"ascending" if order == "asc" else "descending"}"' in html - - -@pytest.mark.parametrize("column", ["source", "target"]) -@pytest.mark.parametrize("order", ["asc", "desc"]) -def test_received_sorts_by_source_or_target( - app: Flask, client: FlaskClient, column: str, order: str -) -> None: - log_in(app, client) - # Deliberately create rows out of lexical order for both URL columns. - for source_name, target_name in ( - ("beta", "alpha"), - ("alpha", "gamma"), - ("gamma", "beta"), - ): - create_received_webmention( - app, - source=f"https://source.example/{source_name}", - target=f"https://target.example/{target_name}", - ) - - response = client.get("/received", query_string={"sort": column, "order": order}) - - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - rows = re.findall(r"]*>.*?", html, flags=re.DOTALL) - urls = re.findall(rf'', "".join(rows)) - assert urls == sorted(urls, reverse=(order == "desc")) - assert len(urls) == 3 - assert f'aria-sort="{"ascending" if order == "asc" else "descending"}"' in html - - -def test_received_sort_links_preserve_filters_and_page_size( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - create_received_webmention(app, source="https://some.example/post", status="failed") - response = client.get( - "/received", - query_string={ - "status": "declined", - "q": "some.example", - "sort": "updated", - "order": "desc", - "per_page": "37", - }, - ) - - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - links = [ - parse_qs(urlsplit(unescape(link)).query) - for link in re.findall(r'href="([^"]+)"', html) - if urlsplit(unescape(link)).path == "/received" - ] - for sort, order in ( - ("received", "desc"), - ("updated", "asc"), - ("status", "asc"), - ("source", "asc"), - ("target", "asc"), - ): - assert { - "status": ["declined"], - "q": ["some.example"], - "sort": [sort], - "order": [order], - "per_page": ["37"], - } in links - assert 'name="sort"' in html - assert 'name="order"' in html - assert 'name="per_page"' in html - assert 'value="37"' in html - - -def test_received_page_size_is_free_form_and_controls_pagination( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - for index in range(52): - create_received_webmention(app, source=f"https://source.example/{index}") - - for page_size, expected_rows in ((25, 25), (37, 37), (50, 50), (100, 52)): - response = client.get("/received", query_string={"per_page": page_size}) - 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 ("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 - ) - - -def test_received_default_page_size_is_capped_when_max_is_below_25( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - app.config["RECEIVED_MAX_PAGE_SIZE"] = 10 - for index in range(11): - create_received_webmention(app, source=f"https://source.example/{index}") - - response = client.get("/received") - - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - assert html.count('data-title="Reverify webmention?"') == 10 - assert 'name="per_page"' in html - assert 'value="10"' in html - assert "page=2" in html - - -def test_received_page_size_respects_configured_maximum( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - app.config["RECEIVED_MAX_PAGE_SIZE"] = 50 - response = client.get("/received", query_string={"per_page": 50}) - assert response.status_code == HTTPStatus.OK - assert re.search( - r']*name="per_page")(?=[^>]*max="50")', - response.get_data(as_text=True), - ) - assert client.get("/received?per_page=51").status_code == HTTPStatus.BAD_REQUEST - - -def test_received_get_filter_controls_render_serialized_values( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - response = client.get( - "/received", - query_string={ - "status": "pending", - "q": "example.org", - "date_from": "2026-11-08", - "date_to": "2026-11-14", - "sort": "updated", - "order": "asc", - "per_page": "37", - }, - ) - - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - for field in ("status", "q", "date_from", "date_to", "sort", "order", "per_page"): - assert f'name="{field}"' in html - assert 'type="search"' in html - assert html.count('type="date"') == 2 - assert 'value="example.org"' in html - assert 'value="2026-11-08"' in html - assert 'value="2026-11-14"' in html - assert 'name="sort" type="hidden" value="updated"' in html - assert 'name="order" type="hidden" value="asc"' in html - assert 'value="37"' in html - assert re.search(r']*method="get"[^>]*role="search"', html) - - -def test_received_get_filter_controls_render_defaults( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - response = client.get("/received") - - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - assert 'name="sort" type="hidden" value="received"' in html - assert 'name="order" type="hidden" value="desc"' in html - assert 'value="25"' in 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( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - create_received_webmention( - app, source="https://site.example/Alpha_10%", target="https://other.example/a" - ) - create_received_webmention( - app, source="https://other.example/b", target="https://site.example/alpha_10%" - ) - create_received_webmention( - app, source="https://site.example/alphaX100", target="https://other.example/c" - ) - - response = client.get("/received", query_string={"q": "ALPHA_10%"}) - - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - assert "https://other.example/a" in html - assert "https://other.example/b" in html - assert "https://other.example/c" not in html - - -def test_received_filters_by_inclusive_updated_date_range( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - dates = { - "before": datetime(2026, 11, 7, 23, 59, 59, tzinfo=UTC), - "first": datetime(2026, 11, 8, tzinfo=UTC), - "last": datetime(2026, 11, 14, 23, 59, 59, 999999, tzinfo=UTC), - "after": datetime(2026, 11, 15, tzinfo=UTC), - } - for label, updated_at in dates.items(): - create_received_webmention( - app, - source=f"https://{label}.example/post", - status="failed", - updated_at=updated_at, - ) - - response = client.get( - "/received", - query_string={ - "status": "declined", - "date_from": "2026-11-08", - "date_to": "2026-11-14", - }, - ) - - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - for label in ("first", "last"): - assert f"https://{label}.example/post" in html - for label in ("before", "after"): - assert f"https://{label}.example/post" not in html - - -def test_received_filters_are_combined(app: Flask, client: FlaskClient) -> None: - log_in(app, client) - for status, source, day in ( - ("failed", "https://match.example/a", 12), - ("verified", "https://match.example/b", 12), - ("failed", "https://other.example/c", 12), - ("failed", "https://match.example/d", 1), - ): - create_received_webmention( - app, - source=source, - status=status, - updated_at=datetime(2026, 11, day, tzinfo=UTC), - ) - - response = client.get( - "/received?status=declined&q=match.example&date_from=2026-11-08&date_to=2026-11-14" - ) - - html = response.get_data(as_text=True) - assert "https://match.example/a" in html - for suffix in ("b", "c", "d"): - assert f"/{suffix}" not in html - assert "1 total" in html - - -@pytest.mark.parametrize( - "query_string", - [ - "status=unknown", - "date_from=not-a-date", - "date_from=2026-11-8", - "date_to=2026-02-30", - "date_from=2026-11-15&date_to=2026-11-08", - "sort=unknown", - "order=sideways", - "per_page=0", - "per_page=-1", - "per_page=101", - "per_page=abc", - "per_page=1.5", - ], -) -def test_received_rejects_invalid_filters( - app: Flask, client: FlaskClient, query_string: str -) -> None: - log_in(app, client) - assert client.get(f"/received?{query_string}").status_code == HTTPStatus.BAD_REQUEST - - -def test_received_pagination_and_actions_keep_filters( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - for index in range(26): - create_received_webmention( - app, - source=f"https://match.example/{index}", - status="failed", - updated_at=datetime(2026, 11, 12, tzinfo=UTC), - ) - - filters = { - "status": "declined", - "q": "match.example", - "date_from": "2026-11-08", - "date_to": "2026-11-14", - } - response = client.get("/received", query_string=filters) - - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - assert "26 total" in html - pagination_links = re.findall(r'href="([^"]+)"', html) - action_links = re.findall(r'data-action="([^"]+)"', html) - assert action_links - expected_filters = {key: [value] for key, value in filters.items()} - assert any( - urlsplit(unescape(link)).path == "/received" - and parse_qs(urlsplit(unescape(link)).query) - == {**expected_filters, "page": ["2"]} - for link in pagination_links - ) - for link in action_links: - assert parse_qs(urlsplit(unescape(link)).query) == { - **expected_filters, - "page": ["1"], - } - - -def test_received_pagination_and_actions_keep_sort_and_page_size( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - for index in range(38): - create_received_webmention(app, source=f"https://source.example/{index}") - - response = client.get( - "/received", query_string={"sort": "updated", "order": "asc", "per_page": 37} - ) - - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - expected = {"sort": ["updated"], "order": ["asc"], "per_page": ["37"]} - assert any( - urlsplit(unescape(link)).path == "/received" - and parse_qs(urlsplit(unescape(link)).query) == {**expected, "page": ["2"]} - for link in re.findall(r'href="([^"]+)"', html) - ) - for link in re.findall(r'data-action="([^"]+)"', html): - assert parse_qs(urlsplit(unescape(link)).query) == {**expected, "page": ["1"]} - - -@pytest.mark.parametrize("action", ["delete", "reverify"]) -def test_received_action_redirect_keeps_sort_and_page_size( - app: Flask, - client: FlaskClient, - views_module: ModuleType, - monkeypatch: pytest.MonkeyPatch, - action: str, -) -> None: - log_in(app, client) - identifier = create_received_webmention(app) - mock_admin_action_form(views_module, monkeypatch, valid=True) - monkeypatch.setattr(views_module, "verify_webmention", Mock()) - - response = client.post( - f"/received/{identifier}/{action}", - query_string={"page": 3, "sort": "updated", "order": "asc", "per_page": 37}, - ) - - assert response.status_code == HTTPStatus.FOUND - assert parse_qs(urlsplit(response.headers["Location"]).query) == { - "page": ["3"], - "sort": ["updated"], - "order": ["asc"], - "per_page": ["37"], - } - - -@pytest.mark.parametrize("action", ["delete", "reverify"]) -def test_received_action_redirect_keeps_filters( - app: Flask, - client: FlaskClient, - views_module: ModuleType, - monkeypatch: pytest.MonkeyPatch, - action: str, -) -> None: - log_in(app, client) - identifier = create_received_webmention(app) - mock_admin_action_form(views_module, monkeypatch, valid=True) - monkeypatch.setattr(views_module, "verify_webmention", Mock()) - - response = client.post( - f"/received/{identifier}/{action}", - query_string={ - "page": 3, - "status": "declined", - "q": "example.org/a&b", - "date_from": "2026-11-08", - "date_to": "2026-11-14", - }, - ) - - assert response.status_code == HTTPStatus.FOUND - redirect_url = urlsplit(response.headers["Location"]) - assert redirect_url.path == "/received" - assert parse_qs(redirect_url.query) == { - "page": ["3"], - "status": ["declined"], - "q": ["example.org/a&b"], - "date_from": ["2026-11-08"], - "date_to": ["2026-11-14"], - } - - -@pytest.mark.parametrize("action", ["delete", "reverify"]) -def test_received_action_rejects_invalid_filters_before_mutating( - app: Flask, - client: FlaskClient, - views_module: ModuleType, - monkeypatch: pytest.MonkeyPatch, - action: str, -) -> None: - log_in(app, client) - identifier = create_received_webmention(app, status="received") - mock_admin_action_form(views_module, monkeypatch, valid=True) - verify = Mock() - monkeypatch.setattr(views_module, "verify_webmention", verify) - - response = client.post( - f"/received/{identifier}/{action}", query_string={"status": "invalid"} - ) - - assert response.status_code == HTTPStatus.BAD_REQUEST - with app.app_context(): - row = db.session.get(ReceivedWebmention, identifier) - assert row is not None - assert row.status == "received" - verify.assert_not_called() - - -@pytest.mark.parametrize("action", ["delete", "reverify"]) -@pytest.mark.parametrize("invalid", ["sort=invalid", "order=invalid", "per_page=101"]) -def test_received_action_rejects_invalid_sort_and_page_size_before_mutating( - app: Flask, - client: FlaskClient, - views_module: ModuleType, - monkeypatch: pytest.MonkeyPatch, - action: str, - invalid: str, -) -> None: - log_in(app, client) - identifier = create_received_webmention(app, status="received") - mock_admin_action_form(views_module, monkeypatch, valid=True) - verify = Mock() - monkeypatch.setattr(views_module, "verify_webmention", verify) - - response = client.post(f"/received/{identifier}/{action}?{invalid}") - - assert response.status_code == HTTPStatus.BAD_REQUEST - with app.app_context(): - row = db.session.get(ReceivedWebmention, identifier) - assert row is not None - assert row.status == "received" - verify.assert_not_called() - - -@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 == HTTPStatus.FOUND - 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 == HTTPStatus.BAD_REQUEST - 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 == HTTPStatus.NOT_FOUND - - -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 == HTTPStatus.FOUND - 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 == HTTPStatus.FOUND - 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_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="block-sources"' 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 'data-received-bulk-action="block-sources"' not in html - assert 'id="received-select-all"' not in html - - -@pytest.mark.parametrize( - "path", - [ - "/received/bulk/reverify", - "/received/bulk/block-sources", - "/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/block-sources", - "/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/block-sources", - "/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", "block-sources", "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", "block-sources", "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']*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") - - assert response.status_code == HTTPStatus.FOUND - 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("/sources/rescan") - - assert response.status_code == ( - HTTPStatus.FOUND if valid else HTTPStatus.BAD_REQUEST - ) - form.validate_on_submit.assert_called_once_with() - - if valid: - assert response.headers["Location"].endswith("/sources") - 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"/sources/{uuid.uuid7()}/{uuid.uuid7()}/resend") - - assert response.status_code == HTTPStatus.FOUND - 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"/sources/{source_identifier}/{webmention_identifier}/resend" - ) - - assert response.status_code == HTTPStatus.BAD_REQUEST - - -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"/sources/{source_identifier}/{uuid.uuid7()}/resend") - - assert response.status_code == HTTPStatus.NOT_FOUND - - -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"/sources/{wrong_source_identifier}/{webmention_identifier}/resend" - ) - - assert response.status_code == HTTPStatus.NOT_FOUND - - -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"/sources/{source_identifier}/{webmention_identifier}/resend" - ) - - assert response.status_code == HTTPStatus.FOUND - assert response.headers["Location"].endswith(f"/sources/{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.webmention_endpoint is None - assert webmention.endpoint_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"/sources/{source_identifier}") - assert response.status_code == HTTPStatus.OK - 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 index 88b665f..23643d9 100644 --- a/tests/views/test_endpoint.py +++ b/tests/views/test_endpoint.py @@ -15,7 +15,7 @@ from flask.testing import FlaskClient from sqlalchemy.exc import IntegrityError from webmentions_ssg import db -from webmentions_ssg.models import ReceivedWebmention +from webmentions_ssg.models.received import ReceivedWebmention from .helpers import ( SOURCE_URL, diff --git a/tests/views/test_moderation.py b/tests/views/test_moderation.py new file mode 100644 index 0000000..7f683d4 --- /dev/null +++ b/tests/views/test_moderation.py @@ -0,0 +1,290 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# 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.moderation import AllowedDomain, BlockedDomain +from webmentions_ssg.models.received import ReceivedWebmention, ReceivedWebmentionStatus +from webmentions_ssg.moderation_policy import rejection_reason +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", "/moderation", None), + ("post", "/moderation/blocked/add", {"domain": "bad.example"}), + ("post", "/moderation/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("/moderation/blocked/add", data={"domain": "SPAM.Example."}) + assert response.status_code == HTTPStatus.FOUND + assert response.headers["Location"].endswith("/moderation") + response = client.post("/moderation/blocked/add", data={"domain": "spam.example"}) + assert response.status_code == HTTPStatus.FOUND + + with app.app_context(): + rows = db.session.scalars(sa.select(BlockedDomain)).all() + assert len(rows) == 1 + assert rows[0].domain == "spam.example" + entry_id = rows[0].id + + response = client.get("/moderation") + 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"/moderation/blocked/{entry_id}/delete") + assert response.status_code == HTTPStatus.FOUND + assert response.headers["Location"].endswith("/moderation") + with app.app_context(): + assert db.session.scalar(sa.select(sa.func.count(BlockedDomain.id))) == 0 + + +def test_allowlist_can_be_managed_while_mode_is_off( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + response = client.post( + "/moderation/allowed/add", data={"domain": "friends.example"} + ) + assert response.status_code == HTTPStatus.FOUND + with app.app_context(): + row = db.session.scalar(sa.select(AllowedDomain)) + assert row is not None + assert row.domain == "friends.example" + assert b"disabled" in client.get("/moderation").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("/moderation/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("/moderation/nope/add", data={"domain": "test.org"}).status_code + == 404 + ) + assert client.post("/moderation/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(BlockedDomain)) + 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(BlockedDomain(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(BlockedDomain.domain))) == { + "spam.example", + "second.example", + } + for identifier in (first, same_domain, second, unselected): + assert db.session.get(ReceivedWebmention, identifier) is not None + assert ( + rejection_reason("https://child.spam.example/post") + == "Source domain is blocked" + ) + assert 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(BlockedDomain.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(BlockedDomain.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(BlockedDomain.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(BlockedDomain(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["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(AllowedDomain(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(BlockedDomain(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(BlockedDomain(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( + "/moderation/blocked/add", data={"domain": "spam.example"} + ).status_code + == HTTPStatus.BAD_REQUEST + ) diff --git a/tests/views/test_received.py b/tests/views/test_received.py new file mode 100644 index 0000000..ec00e05 --- /dev/null +++ b/tests/views/test_received.py @@ -0,0 +1,1063 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import re +import uuid +from datetime import UTC, datetime, timedelta +from html import unescape +from http import HTTPStatus +from types import ModuleType +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 + +from webmentions_ssg import db +from webmentions_ssg.models.received import ReceivedWebmention + +from .helpers import ( + SOURCE_URL, + TARGET_URL, + create_received_webmention, + log_in, + mock_admin_action_form, +) + + +@pytest.fixture +def views_module(app: Flask) -> ModuleType: + _ = app + from webmentions_ssg.views import received as module + + return module + + +def test_received_requires_login(client: FlaskClient) -> None: + response = client.get("/received") + + assert response.status_code == HTTPStatus.FOUND + 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 == HTTPStatus.OK + html = response.get_data(as_text=True) + assert SOURCE_URL in html + assert TARGET_URL in html + + +@pytest.mark.parametrize( + ("filter_status", "expected_statuses"), + [ + ("pending", {"received"}), + ("verifying", {"verifying"}), + ("verified", {"verified"}), + ("declined", {"failed", "source_removed"}), + ], +) +def test_received_filters_by_display_status( + app: Flask, client: FlaskClient, filter_status: str, expected_statuses: set[str] +) -> None: + log_in(app, client) + statuses = ("received", "verifying", "verified", "failed", "source_removed") + for status in statuses: + create_received_webmention( + app, source=f"https://{status}.example/post", status=status + ) + + response = client.get("/received", query_string={"status": filter_status}) + + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + for status in statuses: + assert (f"https://{status}.example/post" in html) == ( + status in expected_statuses + ) + assert re.search( + rf']*\bvalue="{filter_status}")(?=[^>]*\bselected\b)[^>]*>', html + ) + + +def test_received_filter_args_uses_typed_dict_fields( + app: Flask, views_module: ModuleType +) -> None: + with app.test_request_context( + "/received", + query_string={ + "status": "declined", + "q": " example.org ", + "date_from": "2026-11-08", + "date_to": "2026-11-14", + "sort": "updated", + "order": "asc", + "per_page": "37", + }, + ): + form = views_module.received_filter_form() + assert set(views_module.ReceivedFilterArgs.__annotations__) == set(form._fields) + assert views_module.received_filter_args(form) == { + "status": "declined", + "q": "example.org", + "date_from": "2026-11-08", + "date_to": "2026-11-14", + "sort": "updated", + "order": "asc", + "per_page": "37", + } + + +@pytest.mark.parametrize("max_page_size", [10, 100]) +def test_received_filter_args_omits_defaults_for_configured_page_size( + app: Flask, views_module: ModuleType, max_page_size: int +) -> None: + app.config["RECEIVED_MAX_PAGE_SIZE"] = max_page_size + with app.test_request_context("/received"): + form = views_module.received_filter_form() + assert form.per_page.data == min(25, max_page_size) + assert views_module.received_filter_args(form) == {} + + +@pytest.mark.parametrize( + ("sort", "order", "expected"), + [ + ("received", "desc", ("removed", "failed", "verified", "pending")), + ("received", "asc", ("pending", "verified", "failed", "removed")), + ("updated", "desc", ("verified", "failed", "pending", "removed")), + ("updated", "asc", ("removed", "pending", "failed", "verified")), + ("status", "asc", ("pending", "verified", "removed", "failed")), + ("status", "desc", ("removed", "failed", "verified", "pending")), + ], +) +def test_received_sorting( + app: Flask, client: FlaskClient, sort: str, order: str, expected: tuple[str, ...] +) -> None: + log_in(app, client) + for label, status, day in ( + ("pending", "received", 12), + ("verified", "verified", 14), + ("failed", "failed", 13), + ("removed", "source_removed", 11), + ): + create_received_webmention( + app, + source=f"https://{label}.example/post", + status=status, + updated_at=datetime(2026, 11, day, tzinfo=UTC), + ) + + response = client.get("/received", query_string={"sort": sort, "order": order}) + + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + indices = [html.index(f"https://{label}.example/post") for label in expected] + assert indices == sorted(indices) + assert f'aria-sort="{"ascending" if order == "asc" else "descending"}"' in html + + +@pytest.mark.parametrize("column", ["source", "target"]) +@pytest.mark.parametrize("order", ["asc", "desc"]) +def test_received_sorts_by_source_or_target( + app: Flask, client: FlaskClient, column: str, order: str +) -> None: + log_in(app, client) + # Deliberately create rows out of lexical order for both URL columns. + for source_name, target_name in ( + ("beta", "alpha"), + ("alpha", "gamma"), + ("gamma", "beta"), + ): + create_received_webmention( + app, + source=f"https://source.example/{source_name}", + target=f"https://target.example/{target_name}", + ) + + response = client.get("/received", query_string={"sort": column, "order": order}) + + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + rows = re.findall(r"]*>.*?", html, flags=re.DOTALL) + urls = re.findall(rf'', "".join(rows)) + assert urls == sorted(urls, reverse=(order == "desc")) + assert len(urls) == 3 + assert f'aria-sort="{"ascending" if order == "asc" else "descending"}"' in html + + +def test_received_sort_links_preserve_filters_and_page_size( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + create_received_webmention(app, source="https://some.example/post", status="failed") + response = client.get( + "/received", + query_string={ + "status": "declined", + "q": "some.example", + "sort": "updated", + "order": "desc", + "per_page": "37", + }, + ) + + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + links = [ + parse_qs(urlsplit(unescape(link)).query) + for link in re.findall(r'href="([^"]+)"', html) + if urlsplit(unescape(link)).path == "/received" + ] + for sort, order in ( + ("received", "desc"), + ("updated", "asc"), + ("status", "asc"), + ("source", "asc"), + ("target", "asc"), + ): + assert { + "status": ["declined"], + "q": ["some.example"], + "sort": [sort], + "order": [order], + "per_page": ["37"], + } in links + assert 'name="sort"' in html + assert 'name="order"' in html + assert 'name="per_page"' in html + assert 'value="37"' in html + + +def test_received_page_size_is_free_form_and_controls_pagination( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + for index in range(52): + create_received_webmention(app, source=f"https://source.example/{index}") + + for page_size, expected_rows in ((25, 25), (37, 37), (50, 50), (100, 52)): + response = client.get("/received", query_string={"per_page": page_size}) + 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 ("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 + ) + + +def test_received_default_page_size_is_capped_when_max_is_below_25( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + app.config["RECEIVED_MAX_PAGE_SIZE"] = 10 + for index in range(11): + create_received_webmention(app, source=f"https://source.example/{index}") + + response = client.get("/received") + + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + assert html.count('data-title="Reverify webmention?"') == 10 + assert 'name="per_page"' in html + assert 'value="10"' in html + assert "page=2" in html + + +def test_received_page_size_respects_configured_maximum( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + app.config["RECEIVED_MAX_PAGE_SIZE"] = 50 + response = client.get("/received", query_string={"per_page": 50}) + assert response.status_code == HTTPStatus.OK + assert re.search( + r']*name="per_page")(?=[^>]*max="50")', + response.get_data(as_text=True), + ) + assert client.get("/received?per_page=51").status_code == HTTPStatus.BAD_REQUEST + + +def test_received_get_filter_controls_render_serialized_values( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + response = client.get( + "/received", + query_string={ + "status": "pending", + "q": "example.org", + "date_from": "2026-11-08", + "date_to": "2026-11-14", + "sort": "updated", + "order": "asc", + "per_page": "37", + }, + ) + + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + for field in ("status", "q", "date_from", "date_to", "sort", "order", "per_page"): + assert f'name="{field}"' in html + assert 'type="search"' in html + assert html.count('type="date"') == 2 + assert 'value="example.org"' in html + assert 'value="2026-11-08"' in html + assert 'value="2026-11-14"' in html + assert 'name="sort" type="hidden" value="updated"' in html + assert 'name="order" type="hidden" value="asc"' in html + assert 'value="37"' in html + assert re.search(r']*method="get"[^>]*role="search"', html) + + +def test_received_get_filter_controls_render_defaults( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + response = client.get("/received") + + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + assert 'name="sort" type="hidden" value="received"' in html + assert 'name="order" type="hidden" value="desc"' in html + assert 'value="25"' in 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( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + create_received_webmention( + app, source="https://site.example/Alpha_10%", target="https://other.example/a" + ) + create_received_webmention( + app, source="https://other.example/b", target="https://site.example/alpha_10%" + ) + create_received_webmention( + app, source="https://site.example/alphaX100", target="https://other.example/c" + ) + + response = client.get("/received", query_string={"q": "ALPHA_10%"}) + + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + assert "https://other.example/a" in html + assert "https://other.example/b" in html + assert "https://other.example/c" not in html + + +def test_received_filters_by_inclusive_updated_date_range( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + dates = { + "before": datetime(2026, 11, 7, 23, 59, 59, tzinfo=UTC), + "first": datetime(2026, 11, 8, tzinfo=UTC), + "last": datetime(2026, 11, 14, 23, 59, 59, 999999, tzinfo=UTC), + "after": datetime(2026, 11, 15, tzinfo=UTC), + } + for label, updated_at in dates.items(): + create_received_webmention( + app, + source=f"https://{label}.example/post", + status="failed", + updated_at=updated_at, + ) + + response = client.get( + "/received", + query_string={ + "status": "declined", + "date_from": "2026-11-08", + "date_to": "2026-11-14", + }, + ) + + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + for label in ("first", "last"): + assert f"https://{label}.example/post" in html + for label in ("before", "after"): + assert f"https://{label}.example/post" not in html + + +def test_received_filters_are_combined(app: Flask, client: FlaskClient) -> None: + log_in(app, client) + for status, source, day in ( + ("failed", "https://match.example/a", 12), + ("verified", "https://match.example/b", 12), + ("failed", "https://other.example/c", 12), + ("failed", "https://match.example/d", 1), + ): + create_received_webmention( + app, + source=source, + status=status, + updated_at=datetime(2026, 11, day, tzinfo=UTC), + ) + + response = client.get( + "/received", + query_string={ + "status": "declined", + "q": "match.example", + "date_from": "2026-11-08", + "date_to": "2026-11-14", + }, + ) + + html = response.get_data(as_text=True) + assert "https://match.example/a" in html + for suffix in ("b", "c", "d"): + assert f"/{suffix}" not in html + assert "1 total" in html + + +@pytest.mark.parametrize( + "query_string", + [ + "status=unknown", + "date_from=not-a-date", + "date_from=2026-11-8", + "date_to=2026-02-30", + "date_from=2026-11-15&date_to=2026-11-08", + "sort=unknown", + "order=sideways", + "per_page=0", + "per_page=-1", + "per_page=101", + "per_page=abc", + "per_page=1.5", + ], +) +def test_received_rejects_invalid_filters( + app: Flask, client: FlaskClient, query_string: str +) -> None: + log_in(app, client) + assert client.get(f"/received?{query_string}").status_code == HTTPStatus.BAD_REQUEST + + +def test_received_pagination_and_actions_keep_filters( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + for index in range(26): + create_received_webmention( + app, + source=f"https://match.example/{index}", + status="failed", + updated_at=datetime(2026, 11, 12, tzinfo=UTC), + ) + + filters = { + "status": "declined", + "q": "match.example", + "date_from": "2026-11-08", + "date_to": "2026-11-14", + } + response = client.get("/received", query_string=filters) + + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + assert "26 total" in html + pagination_links = re.findall(r'href="([^"]+)"', html) + action_links = re.findall(r'data-action="([^"]+)"', html) + assert action_links + expected_filters = {key: [value] for key, value in filters.items()} + assert any( + urlsplit(unescape(link)).path == "/received" + and parse_qs(urlsplit(unescape(link)).query) + == {**expected_filters, "page": ["2"]} + for link in pagination_links + ) + for link in action_links: + assert parse_qs(urlsplit(unescape(link)).query) == { + **expected_filters, + "page": ["1"], + } + + +def test_received_pagination_and_actions_keep_sort_and_page_size( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + for index in range(38): + create_received_webmention(app, source=f"https://source.example/{index}") + + response = client.get( + "/received", query_string={"sort": "updated", "order": "asc", "per_page": 37} + ) + + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + expected = {"sort": ["updated"], "order": ["asc"], "per_page": ["37"]} + assert any( + urlsplit(unescape(link)).path == "/received" + and parse_qs(urlsplit(unescape(link)).query) == {**expected, "page": ["2"]} + for link in re.findall(r'href="([^"]+)"', html) + ) + for link in re.findall(r'data-action="([^"]+)"', html): + assert parse_qs(urlsplit(unescape(link)).query) == {**expected, "page": ["1"]} + + +@pytest.mark.parametrize("action", ["delete", "reverify"]) +def test_received_action_redirect_keeps_sort_and_page_size( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + action: str, +) -> None: + log_in(app, client) + identifier = create_received_webmention(app) + mock_admin_action_form(views_module, monkeypatch, valid=True) + monkeypatch.setattr(views_module, "verify_webmention", Mock()) + + response = client.post( + f"/received/{identifier}/{action}", + query_string={"page": 3, "sort": "updated", "order": "asc", "per_page": 37}, + ) + + assert response.status_code == HTTPStatus.FOUND + assert parse_qs(urlsplit(response.headers["Location"]).query) == { + "page": ["3"], + "sort": ["updated"], + "order": ["asc"], + "per_page": ["37"], + } + + +@pytest.mark.parametrize("action", ["delete", "reverify"]) +def test_received_action_redirect_keeps_filters( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + action: str, +) -> None: + log_in(app, client) + identifier = create_received_webmention(app) + mock_admin_action_form(views_module, monkeypatch, valid=True) + monkeypatch.setattr(views_module, "verify_webmention", Mock()) + + response = client.post( + f"/received/{identifier}/{action}", + query_string={ + "page": 3, + "status": "declined", + "q": "example.org/a&b", + "date_from": "2026-11-08", + "date_to": "2026-11-14", + }, + ) + + assert response.status_code == HTTPStatus.FOUND + redirect_url = urlsplit(response.headers["Location"]) + assert redirect_url.path == "/received" + assert parse_qs(redirect_url.query) == { + "page": ["3"], + "status": ["declined"], + "q": ["example.org/a&b"], + "date_from": ["2026-11-08"], + "date_to": ["2026-11-14"], + } + + +@pytest.mark.parametrize("action", ["delete", "reverify"]) +def test_received_action_rejects_invalid_filters_before_mutating( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + action: str, +) -> None: + log_in(app, client) + identifier = create_received_webmention(app, status="received") + mock_admin_action_form(views_module, monkeypatch, valid=True) + verify = Mock() + monkeypatch.setattr(views_module, "verify_webmention", verify) + + response = client.post( + f"/received/{identifier}/{action}", query_string={"status": "invalid"} + ) + + assert response.status_code == HTTPStatus.BAD_REQUEST + with app.app_context(): + row = db.session.get(ReceivedWebmention, identifier) + assert row is not None + assert row.status == "received" + verify.assert_not_called() + + +@pytest.mark.parametrize("action", ["delete", "reverify"]) +@pytest.mark.parametrize("invalid", ["sort=invalid", "order=invalid", "per_page=101"]) +def test_received_action_rejects_invalid_sort_and_page_size_before_mutating( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + action: str, + invalid: str, +) -> None: + log_in(app, client) + identifier = create_received_webmention(app, status="received") + mock_admin_action_form(views_module, monkeypatch, valid=True) + verify = Mock() + monkeypatch.setattr(views_module, "verify_webmention", verify) + + response = client.post(f"/received/{identifier}/{action}?{invalid}") + + assert response.status_code == HTTPStatus.BAD_REQUEST + with app.app_context(): + row = db.session.get(ReceivedWebmention, identifier) + assert row is not None + assert row.status == "received" + verify.assert_not_called() + + +@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 == HTTPStatus.FOUND + 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 == HTTPStatus.BAD_REQUEST + 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 == HTTPStatus.NOT_FOUND + + +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 == HTTPStatus.FOUND + 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 == HTTPStatus.FOUND + 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_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="block-sources"' 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 'data-received-bulk-action="block-sources"' not in html + assert 'id="received-select-all"' not in html + + +@pytest.mark.parametrize( + "path", + [ + "/received/bulk/reverify", + "/received/bulk/block-sources", + "/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/block-sources", + "/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/block-sources", + "/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", "block-sources", "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", "block-sources", "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']*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 diff --git a/tests/views/test_session.py b/tests/views/test_session.py index a2d3268..7edee71 100644 --- a/tests/views/test_session.py +++ b/tests/views/test_session.py @@ -10,7 +10,7 @@ from flask import Flask from flask.testing import FlaskClient from webmentions_ssg import db -from webmentions_ssg.models import User +from webmentions_ssg.models.user import User from .helpers import log_in, mock_login_form 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 -# -# 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 - ) diff --git a/tests/views/test_sources.py b/tests/views/test_sources.py new file mode 100644 index 0000000..012347e --- /dev/null +++ b/tests/views/test_sources.py @@ -0,0 +1,253 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import uuid +from datetime import UTC, datetime, timedelta +from http import HTTPStatus +from types import ModuleType +from unittest.mock import Mock + +import pytest +from flask import Flask +from flask.testing import FlaskClient + +from webmentions_ssg import db +from webmentions_ssg.models.sources import SentWebmention +from webmentions_ssg.models.user import User + +from .helpers import ( + create_outgoing, + create_resendable_sent_webmention, + create_sent_source, + log_in, + mock_admin_action_form, +) + + +@pytest.fixture +def sources_module(app: Flask) -> ModuleType: + _ = app + from webmentions_ssg.views import sources as module + + return module + + +@pytest.mark.parametrize("path", ["/sources", f"/sources/{uuid.uuid7()}"]) +def test_sent_views_require_login(client: FlaskClient, path: str) -> None: + response = client.get(path) + + assert response.status_code == HTTPStatus.FOUND + 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("/sources") + + assert response.status_code == HTTPStatus.OK + + html = response.get_data(as_text=True) + + assert "blog/example/index.html" in html + assert "https://dennisfink.me/blog/example/" in html + assert f"/sources/{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"/sources/{identifier}") + + assert response.status_code == HTTPStatus.OK + + 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"/sources/{uuid.uuid7()}") + assert response.status_code == HTTPStatus.NOT_FOUND + + +def test_rescan_sent_sources_requires_login(client: FlaskClient) -> None: + response = client.post("/sources/rescan") + + assert response.status_code == HTTPStatus.FOUND + assert "/login" in response.headers["Location"] + + +@pytest.mark.parametrize("valid", [False, True]) +def test_rescan_sent_sources( + app: Flask, + client: FlaskClient, + sources_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + valid: bool, +) -> None: + log_in(app, client) + form = mock_admin_action_form(sources_module, monkeypatch, valid=valid) + manual_scan_sources = Mock() + monkeypatch.setattr(sources_module, "manual_scan_sources", manual_scan_sources) + + response = client.post("/sources/rescan") + + assert response.status_code == ( + HTTPStatus.FOUND if valid else HTTPStatus.BAD_REQUEST + ) + form.validate_on_submit.assert_called_once_with() + + if valid: + assert response.headers["Location"].endswith("/sources") + 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"/sources/{uuid.uuid7()}/{uuid.uuid7()}/resend") + + assert response.status_code == HTTPStatus.FOUND + assert "/login" in response.headers["Location"] + + +def test_resend_sent_webmention_rejects_invalid_form( + app: Flask, + client: FlaskClient, + sources_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + source_identifier, webmention_identifier = create_resendable_sent_webmention(app) + mock_admin_action_form(sources_module, monkeypatch, valid=False) + + response = client.post( + f"/sources/{source_identifier}/{webmention_identifier}/resend" + ) + + assert response.status_code == HTTPStatus.BAD_REQUEST + + +def test_resend_sent_webmention_returns_404_for_unknown_webmention( + app: Flask, + client: FlaskClient, + sources_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + source_identifier = create_sent_source(app) + mock_admin_action_form(sources_module, monkeypatch, valid=True) + + response = client.post(f"/sources/{source_identifier}/{uuid.uuid7()}/resend") + + assert response.status_code == HTTPStatus.NOT_FOUND + + +def test_resend_sent_webmention_rejects_wrong_source( + app: Flask, + client: FlaskClient, + sources_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(sources_module, monkeypatch, valid=True) + + response = client.post( + f"/sources/{wrong_source_identifier}/{webmention_identifier}/resend" + ) + + assert response.status_code == HTTPStatus.NOT_FOUND + + +def test_resend_sent_webmention_reopens_current_revision( + app: Flask, + client: FlaskClient, + sources_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + source_identifier, webmention_identifier = create_resendable_sent_webmention(app) + mock_admin_action_form(sources_module, monkeypatch, valid=True) + send_webmention = Mock() + monkeypatch.setattr(sources_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"/sources/{source_identifier}/{webmention_identifier}/resend" + ) + + assert response.status_code == HTTPStatus.FOUND + assert response.headers["Location"].endswith(f"/sources/{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.webmention_endpoint is None + assert webmention.endpoint_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"/sources/{source_identifier}") + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + assert (f'href="{status_url}"' in html) is safe + assert status_url in html -- cgit v1.3.1