From 509d93b2976e353d29441a7a7b7e78ca466dc4fd Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sun, 15 Nov 2026 20:42:20 +0100 Subject: refactor(core)!: standardize application terminology Rename configuration keys, task helpers, forms, network utilities, and view endpoints to make their responsibilities and domains more explicit. Align source-management routes and templates with the new terminology and rename the target-matching module and related task abstractions consistently. BREAKING CHANGE: configuration keys have been renamed and sent-source routes move from `/sent` to `/sources`, with corresponding blueprint endpoint changes. --- tests/tasks/scanner_helpers.py | 4 +- tests/tasks/test_consumer.py | 8 +- tests/tasks/test_discovery.py | 46 +++++----- tests/tasks/test_json_serializer.py | 10 +- tests/tasks/test_mentions.py | 176 ------------------------------------ tests/tasks/test_notifications.py | 8 +- tests/tasks/test_receiver.py | 113 ++++++++++++----------- tests/tasks/test_scanner.py | 8 +- tests/tasks/test_sender.py | 24 ++--- tests/tasks/test_source_parser.py | 88 ++++++++++-------- tests/tasks/test_target_matching.py | 176 ++++++++++++++++++++++++++++++++++++ tests/test_auth.py | 23 +++-- tests/test_cli.py | 12 +-- tests/test_config.py | 54 +++++------ tests/test_forms.py | 56 ++++++------ tests/test_init.py | 6 +- tests/test_net.py | 50 +++++----- tests/views/helpers.py | 8 +- tests/views/test_admin.py | 34 ++++--- tests/views/test_session.py | 7 ++ 20 files changed, 469 insertions(+), 442 deletions(-) delete mode 100644 tests/tasks/test_mentions.py create mode 100644 tests/tasks/test_target_matching.py (limited to 'tests') diff --git a/tests/tasks/scanner_helpers.py b/tests/tasks/scanner_helpers.py index 4100028..36d9bb5 100644 --- a/tests/tasks/scanner_helpers.py +++ b/tests/tasks/scanner_helpers.py @@ -68,5 +68,5 @@ def write_post( def configure_scanner(app: Flask, root: Path, *, base_url: str | None = None) -> None: - app.config["SOURCE_DIRECTORY"] = str(root) - app.config["SOURCE_BASE_URL"] = base_url + app.config["SITE_OUTPUT_DIRECTORY"] = str(root) + app.config["SITE_BASE_URL"] = base_url diff --git a/tests/tasks/test_consumer.py b/tests/tasks/test_consumer.py index 1563cdd..78f1b6b 100644 --- a/tests/tasks/test_consumer.py +++ b/tests/tasks/test_consumer.py @@ -18,9 +18,9 @@ def test_consumer_registers_scanner_digest_reaper_and_retention( ) -> None: import webmentions_ssg - app.config["SCANNER_SCHEDULE"] = "*/15 * * * *" - app.config["NOTIFICATION_SCHEDULE"] = "0 9 * * *" - app.config["VERIFICATION_REAPER_SCHEDULE"] = "*/3 * * * *" + app.config["SOURCE_SCAN_SCHEDULE"] = "*/15 * * * *" + app.config["DIGEST_SCHEDULE"] = "0 9 * * *" + app.config["VERIFICATION_RECOVERY_SCHEDULE"] = "*/3 * * * *" app.config["RECEIVED_RETENTION_SCHEDULE"] = "0 4 * * *" registered_tasks: list[object] = [] @@ -43,7 +43,7 @@ def test_consumer_registers_scanner_digest_reaper_and_retention( assert periodic_task.call_count == 4 assert registered_tasks == [ scanner.scan_sources, - receiver.recover_stale_webmentions, + receiver.recover_stale_verifications, notifications.send_webmention_digest, retention.purge_old_received_webmentions, ] diff --git a/tests/tasks/test_discovery.py b/tests/tasks/test_discovery.py index ab621e7..4616a53 100644 --- a/tests/tasks/test_discovery.py +++ b/tests/tasks/test_discovery.py @@ -58,19 +58,19 @@ def sender_module(app: Flask) -> ModuleType: ), ), ) -def test_parse_link_value( +def test_parse_link_header_value( sender_module: ModuleType, value: str, expected: tuple[str, set[str]] ) -> None: - assert sender_module.parse_link_value(value) == expected + assert sender_module.parse_link_header_value(value) == expected @pytest.mark.parametrize( "value", ("", "https://example.com/webmention", " None: - assert sender_module.parse_link_value(value) is None + assert sender_module.parse_link_header_value(value) is None @pytest.mark.parametrize( @@ -583,7 +583,7 @@ def test_discovery_does_not_suppress_unexpected_head_error( def request_hook(request: httpx.Request) -> None: if request.method == "HEAD": - raise sender_module.TemporarySenderError("HEAD DNS lookup failed") + raise sender_module.RetryableDeliveryError("HEAD DNS lookup failed") def handler(request: httpx.Request) -> httpx.Response: methods.append(request.method) @@ -598,7 +598,7 @@ def test_discovery_does_not_suppress_unexpected_head_error( event_hooks={"request": [request_hook]}, ) as client, pytest.raises( - sender_module.TemporarySenderError, match="HEAD DNS lookup failed" + sender_module.RetryableDeliveryError, match="HEAD DNS lookup failed" ), ): sender_module.discover_webmention_endpoint(client, TARGET_URL) @@ -703,20 +703,20 @@ def test_discovery_returns_none_when_all_html_candidates_are_invalid( assert endpoint is None -def test_resolve_endpoint_rejects_invalid_url( +def test_resolve_endpoint_url_rejects_invalid_url( sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: is_http_url = Mock(return_value=False) monkeypatch.setattr(discovery, "is_http_url", is_http_url) response = httpx.Response(HTTPStatus.OK, request=httpx.Request("GET", TARGET_URL)) with pytest.raises( - sender_module.PermanentSenderError, match="Invalid Webmention endpoint" + sender_module.PermanentDeliveryError, match="Invalid Webmention endpoint" ): - sender_module.resolve_endpoint(response, "/webmention") + sender_module.resolve_endpoint_url(response, "/webmention") is_http_url.assert_called_once_with(ENDPOINT_URL) -def test_endpoint_from_headers_ignores_malformed_link( +def test_find_endpoint_in_headers_ignores_malformed_link( sender_module: ModuleType, ) -> None: response = httpx.Response( @@ -724,36 +724,36 @@ def test_endpoint_from_headers_ignores_malformed_link( headers=[("Link", "not-a-link"), ("Link", "; rel=webmention")], request=httpx.Request("GET", TARGET_URL), ) - assert sender_module.endpoint_from_headers(response) == ENDPOINT_URL + assert sender_module.find_endpoint_in_headers(response) == ENDPOINT_URL -def test_endpoint_from_html_accepts_string_rel(sender_module: ModuleType) -> None: +def test_find_endpoint_in_html_accepts_string_rel(sender_module: ModuleType) -> None: response = httpx.Response(HTTPStatus.OK, request=httpx.Request("GET", TARGET_URL)) body = b'' - assert sender_module.endpoint_from_html(response, body) == ENDPOINT_URL + assert sender_module.find_endpoint_in_html(response, body) == ENDPOINT_URL -def test_endpoint_from_html_accepts_uppercase_rel(sender_module: ModuleType) -> None: +def test_find_endpoint_in_html_accepts_uppercase_rel(sender_module: ModuleType) -> None: response = httpx.Response(HTTPStatus.OK, request=httpx.Request("GET", TARGET_URL)) body = b'' - assert sender_module.endpoint_from_html(response, body) == ENDPOINT_URL + assert sender_module.find_endpoint_in_html(response, body) == ENDPOINT_URL -def test_endpoint_from_html_ignores_elements_without_matching_rel_or_href( +def test_find_endpoint_in_html_ignores_elements_without_matching_rel_or_href( sender_module: ModuleType, ) -> None: response = httpx.Response(HTTPStatus.OK, request=httpx.Request("GET", TARGET_URL)) body = b'No endpoint' - assert sender_module.endpoint_from_html(response, body) is None + assert sender_module.find_endpoint_in_html(response, body) is None def test_read_target_body_rejects_declared_oversized_body( app: Flask, sender_module: ModuleType ) -> None: - app.config["MAX_TARGET_BYTES"] = 10 + app.config["MAX_DISCOVERY_TARGET_BYTES"] = 10 response = httpx.Response( HTTPStatus.OK, headers={"Content-Length": "11"}, content=b"x" * 11 @@ -762,7 +762,7 @@ def test_read_target_body_rejects_declared_oversized_body( with ( app.app_context(), pytest.raises( - sender_module.PermanentSenderError, match="Target document is too large" + sender_module.PermanentDeliveryError, match="Target document is too large" ), ): sender_module.read_target_body(response) @@ -800,7 +800,7 @@ def test_read_target_body_ignores_invalid_content_length( def test_read_target_body_rejects_streamed_oversized_body( app: Flask, sender_module: ModuleType ) -> None: - app.config["MAX_TARGET_BYTES"] = 10 + app.config["MAX_DISCOVERY_TARGET_BYTES"] = 10 class BodyStream(httpx.SyncByteStream): def __iter__(self): @@ -812,7 +812,7 @@ def test_read_target_body_rejects_streamed_oversized_body( with ( app.app_context(), pytest.raises( - sender_module.PermanentSenderError, match="Target document is too large" + sender_module.PermanentDeliveryError, match="Target document is too large" ), ): sender_module.read_target_body(response) @@ -849,9 +849,9 @@ def test_discovery_uses_link_header_from_get( ("status_code", "exception_name"), [ pytest.param( - HTTPStatus.SERVICE_UNAVAILABLE, "TemporarySenderError", id="temporary" + HTTPStatus.SERVICE_UNAVAILABLE, "RetryableDeliveryError", id="temporary" ), - pytest.param(HTTPStatus.NOT_FOUND, "PermanentSenderError", id="permanent"), + pytest.param(HTTPStatus.NOT_FOUND, "PermanentDeliveryError", id="permanent"), ], ) def test_discovery_maps_get_status_to_exception( diff --git a/tests/tasks/test_json_serializer.py b/tests/tasks/test_json_serializer.py index 1b09783..442e6a9 100644 --- a/tests/tasks/test_json_serializer.py +++ b/tests/tasks/test_json_serializer.py @@ -13,14 +13,14 @@ from flask import Flask from huey.utils import SKIPPED, Error from webmentions_ssg.tasks.extension import Huey -from webmentions_ssg.tasks.json_serializer import JSONSerializer +from webmentions_ssg.tasks.json_serializer import TaggedJSONSerializer def test_json_is_default_even_for_production_style_broker(app: Flask) -> None: app.config["HUEY"] = {"immediate": False} huey = Huey(app) - assert isinstance(huey.huey.serializer, JSONSerializer) + assert isinstance(huey.huey.serializer, TaggedJSONSerializer) assert huey.huey.serializer.serialize("hello") == b'"hello"' @@ -52,7 +52,7 @@ def test_uuid_and_retry_message_survive_enqueue_dequeue(app: Flask) -> None: def test_result_and_error_data_roundtrip() -> None: - serializer = JSONSerializer() + serializer = TaggedJSONSerializer() identifier = uuid.uuid4() payload = { "ids": [identifier], @@ -65,13 +65,13 @@ def test_result_and_error_data_roundtrip() -> None: def test_rejects_pickle_payload() -> None: - serializer = JSONSerializer() + serializer = TaggedJSONSerializer() with pytest.raises((ValueError, UnicodeDecodeError)): serializer.deserialize(pickle.dumps({"untrusted": True})) def test_rejects_unknown_and_malformed_types() -> None: - serializer = JSONSerializer() + serializer = TaggedJSONSerializer() with pytest.raises(ValueError, match="Unsupported Huey JSON type"): serializer.deserialize(b'{"type":"arbitrary.class","value":{}}') with pytest.raises(ValueError, match="Invalid Huey JSON data"): diff --git a/tests/tasks/test_mentions.py b/tests/tasks/test_mentions.py deleted file mode 100644 index a5da56c..0000000 --- a/tests/tasks/test_mentions.py +++ /dev/null @@ -1,176 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause - -from types import ModuleType -from unittest.mock import Mock, patch - -import pytest -from flask import Flask - -SOURCE_URL = "https://source.example/article" -TARGET_URL = "https://dennisfink.me/blog/example/" - - -@pytest.fixture -def receiver(app: Flask) -> ModuleType: - _ = app - from webmentions_ssg.tasks import mentions - - return mentions - - -@pytest.mark.parametrize( - ("body", "expected"), - [ - pytest.param(f'Reply', True, id="a-href"), - pytest.param(f'Target', True, id="area-href"), - pytest.param( - f'', True, id="link-href" - ), - pytest.param(f'', True, id="img-src"), - pytest.param(f'', True, id="audio-src"), - pytest.param(f'', True, id="video-src"), - pytest.param( - f'', True, id="audio-source-src" - ), - pytest.param( - f'', True, id="video-source-src" - ), - pytest.param(f'', True, id="iframe-src"), - pytest.param(f'', True, id="embed-src"), - pytest.param(f'', True, id="script-src"), - pytest.param( - f'', True, id="track-src" - ), - pytest.param( - f'', - True, - id="image-input-src", - ), - pytest.param( - f'', - True, - id="image-input-case-insensitive", - ), - pytest.param( - f'
Quotation
', - True, - id="blockquote-cite", - ), - pytest.param(f'Quotation', True, id="q-cite"), - pytest.param(f'Addition', True, id="ins-cite"), - pytest.param(f'Removal', True, id="del-cite"), - pytest.param( - 'Reply', - True, - id="base-url", - ), - pytest.param(f'', False, id="img-cite-invalid"), - pytest.param( - f'
Quote
', - False, - id="blockquote-src-invalid", - ), - pytest.param(f'Reply', False, id="a-src-invalid"), - pytest.param(f'
', False, id="div-href-invalid"), - pytest.param(f'', False, id="link-src-invalid"), - pytest.param( - f'', - False, - id="text-input-src-invalid", - ), - pytest.param( - f'', - False, - id="picture-source-src-invalid", - ), - pytest.param(f'', False, id="base-is-not-mention"), - pytest.param( - f'Different page', False, id="longer-url" - ), - pytest.param( - f'', - False, - id="invalid-cite-does-not-override-valid-src", - ), - pytest.param(f"

{TARGET_URL}

", False, id="text-content"), - pytest.param( - 'Other site', False, id="missing-target" - ), - ], -) -def test_html_mentions_target(receiver: ModuleType, body: str, expected: bool) -> None: - assert ( - receiver.html_mentions_target(body.encode(), SOURCE_URL, TARGET_URL) is expected - ) - - -def test_html_mentions_relative_target(receiver: ModuleType) -> None: - assert receiver.html_mentions_target( - b'Reply', - "https://source.example/posts/article/", - "https://source.example/posts/target/", - ) - - -@pytest.mark.parametrize( - ("body", "target_url", "expected"), - [ - (TARGET_URL, TARGET_URL, True), - (f"This post replies to {TARGET_URL}", TARGET_URL, True), - ( - f"https://example.com/first {TARGET_URL} https://example.com/last", - TARGET_URL, - True, - ), - pytest.param( - f"See {TARGET_URL}, thanks", TARGET_URL, True, id="trailing-comma" - ), - pytest.param(f"({TARGET_URL})", TARGET_URL, True, id="parenthesized-url"), - pytest.param( - f"See ({TARGET_URL}), thanks!", TARGET_URL, True, id="mixed-punctuation" - ), - pytest.param(f"See {TARGET_URL}. Next", TARGET_URL, True, id="trailing-period"), - pytest.param( - "https://example.com/wiki/Foo_(bar)", - "https://example.com/wiki/Foo_(bar)", - True, - id="balanced-parentheses-in-url", - ), - pytest.param( - "https://example.com/wiki/Foo_(bar)", - "https://example.com/wiki/Foo_(bar", - False, - id="balanced-parentheses-not-trimmed", - ), - (f"{TARGET_URL}more", TARGET_URL, False), - pytest.param( - f"{TARGET_URL},another-page", - TARGET_URL, - False, - id="comma-within-longer-url", - ), - ("https://dennisfink.me/blog/other/", TARGET_URL, False), - ("/blog/example/", TARGET_URL, False), - ("There are no links here.", TARGET_URL, False), - ], -) -def test_text_mentions_target( - receiver: ModuleType, body: str, target_url: str, expected: bool -) -> None: - assert receiver.text_mentions_target(body, target_url) is expected - - -@patch("webmentions_ssg.tasks.mentions.BeautifulSoup") -def test_html_mentions_target_ignores_non_string_attribute( - beautiful_soup: Mock, receiver: ModuleType -) -> None: - document = beautiful_soup.return_value - document.select_one.return_value = None - - element = Mock() - element.get.return_value = ["not", "a", "string"] - document.select.return_value = [element] - - assert not receiver.html_mentions_target(b"", SOURCE_URL, TARGET_URL) diff --git a/tests/tasks/test_notifications.py b/tests/tasks/test_notifications.py index 876c2b3..a1a002e 100644 --- a/tests/tasks/test_notifications.py +++ b/tests/tasks/test_notifications.py @@ -41,7 +41,7 @@ def create_mention(app: Flask, status: str = "verified") -> uuid.UUID: def test_digest_sends_one_email_for_multiple_verified_mentions( notifications: ModuleType, app: Flask ) -> None: - app.config["NOTIFICATION_EMAIL"] = "owner@example.com" + app.config["DIGEST_EMAIL"] = "owner@example.com" first = create_mention(app) second = create_mention(app) @@ -75,7 +75,7 @@ def test_digest_sends_one_email_for_multiple_verified_mentions( def test_digest_skips_when_notifications_disabled( notifications: ModuleType, app: Flask ) -> None: - app.config["NOTIFICATION_EMAIL"] = None + app.config["DIGEST_EMAIL"] = None identifier = create_mention(app) with app.app_context(): @@ -92,7 +92,7 @@ def test_digest_skips_when_notifications_disabled( def test_digest_does_not_mark_mentions_when_email_fails( notifications: ModuleType, app: Flask ) -> None: - app.config["NOTIFICATION_EMAIL"] = "owner@example.com" + app.config["DIGEST_EMAIL"] = "owner@example.com" identifier = create_mention(app) with app.app_context(): @@ -123,7 +123,7 @@ def test_digest_does_not_mark_mentions_when_email_fails( def test_digest_skips_when_no_new_mentions( notifications: ModuleType, app: Flask ) -> None: - app.config["NOTIFICATION_EMAIL"] = "owner@example.com" + app.config["DIGEST_EMAIL"] = "owner@example.com" with app.app_context(): with patch("webmentions_ssg.tasks.notifications.mail.send") as send: diff --git a/tests/tasks/test_receiver.py b/tests/tasks/test_receiver.py index feb7d7d..7241247 100644 --- a/tests/tasks/test_receiver.py +++ b/tests/tasks/test_receiver.py @@ -70,7 +70,7 @@ def set_stream_response(httpx_client: Mock, response: httpx.Response) -> Mock: @patch("webmentions_ssg.tasks.receiver.httpx.Client") -def test_fetch_source_returns_response_and_body( +def test_fetch_source_document_returns_response_and_body( httpx_client: Mock, app: Flask, receiver: ModuleType ) -> None: response = httpx.Response( @@ -82,7 +82,7 @@ def test_fetch_source_returns_response_and_body( client = set_stream_response(httpx_client, response) with app.app_context(): - fetched_response, body = receiver.fetch_source(SOURCE_URL) + fetched_response, body = receiver.fetch_source_document(SOURCE_URL) assert fetched_response is response assert body == b"

Document

" @@ -107,14 +107,14 @@ def test_fetch_source_returns_response_and_body( (HTTPStatus.BAD_REQUEST, "VerificationError"), (HTTPStatus.NOT_FOUND, "VerificationError"), (HTTPStatus.GONE, "SourceGoneError"), - (HTTPStatus.REQUEST_TIMEOUT, "TemporaryFetchError"), - (HTTPStatus.TOO_EARLY, "TemporaryFetchError"), - (HTTPStatus.TOO_MANY_REQUESTS, "TemporaryFetchError"), - (HTTPStatus.INTERNAL_SERVER_ERROR, "TemporaryFetchError"), - (HTTPStatus.SERVICE_UNAVAILABLE, "TemporaryFetchError"), + (HTTPStatus.REQUEST_TIMEOUT, "RetryableFetchError"), + (HTTPStatus.TOO_EARLY, "RetryableFetchError"), + (HTTPStatus.TOO_MANY_REQUESTS, "RetryableFetchError"), + (HTTPStatus.INTERNAL_SERVER_ERROR, "RetryableFetchError"), + (HTTPStatus.SERVICE_UNAVAILABLE, "RetryableFetchError"), ], ) -def test_fetch_source_maps_http_status_to_exception( +def test_fetch_source_document_maps_http_status_to_exception( httpx_client: Mock, app: Flask, receiver: ModuleType, @@ -129,11 +129,11 @@ def test_fetch_source_maps_http_status_to_exception( exception_type = getattr(receiver, exception_name) with app.app_context(), pytest.raises(exception_type, match=f"HTTP {status_code}"): - receiver.fetch_source(SOURCE_URL) + receiver.fetch_source_document(SOURCE_URL) @patch("webmentions_ssg.tasks.receiver.httpx.Client") -def test_fetch_source_propagates_network_error( +def test_fetch_source_document_propagates_network_error( httpx_client: Mock, app: Flask, receiver: ModuleType ) -> None: client = httpx_client.return_value.__enter__.return_value @@ -145,14 +145,14 @@ def test_fetch_source_propagates_network_error( app.app_context(), pytest.raises(httpx.ConnectError, match="Connection refused"), ): - receiver.fetch_source(SOURCE_URL) + receiver.fetch_source_document(SOURCE_URL) @patch("webmentions_ssg.tasks.receiver.httpx.Client") -def test_fetch_source_rejects_declared_oversized_body( +def test_fetch_source_document_rejects_declared_oversized_body( httpx_client: Mock, app: Flask, receiver: ModuleType ) -> None: - app.config["MAX_SOURCE_BYTES"] = 10 + app.config["MAX_RECEIVED_SOURCE_BYTES"] = 10 set_stream_response( httpx_client, @@ -168,14 +168,14 @@ def test_fetch_source_rejects_declared_oversized_body( app.app_context(), pytest.raises(receiver.VerificationError, match="Source document is too large"), ): - receiver.fetch_source(SOURCE_URL) + receiver.fetch_source_document(SOURCE_URL) @patch("webmentions_ssg.tasks.receiver.httpx.Client") -def test_fetch_source_rejects_streamed_oversized_body( +def test_fetch_source_document_rejects_streamed_oversized_body( httpx_client: Mock, app: Flask, receiver: ModuleType ) -> None: - app.config["MAX_SOURCE_BYTES"] = 10 + app.config["MAX_RECEIVED_SOURCE_BYTES"] = 10 class BodyStream(httpx.SyncByteStream): def __iter__(self): @@ -196,11 +196,11 @@ def test_fetch_source_rejects_streamed_oversized_body( app.app_context(), pytest.raises(receiver.VerificationError, match="Source document is too large"), ): - receiver.fetch_source(SOURCE_URL) + receiver.fetch_source_document(SOURCE_URL) @patch("webmentions_ssg.tasks.receiver.httpx.Client") -def test_fetch_source_does_not_mask_unexpected_value_error( +def test_fetch_source_document_does_not_mask_unexpected_value_error( httpx_client: Mock, app: Flask, receiver: ModuleType, @@ -215,10 +215,10 @@ def test_fetch_source_does_not_mask_unexpected_value_error( ) with app.app_context(), pytest.raises(ValueError, match="unexpected bug"): - receiver.fetch_source(SOURCE_URL) + receiver.fetch_source_document(SOURCE_URL) -@patch("webmentions_ssg.tasks.receiver.fetch_source") +@patch("webmentions_ssg.tasks.receiver.fetch_source_document") @pytest.mark.parametrize( ("content_type", "body", "expected"), [ @@ -270,7 +270,7 @@ def test_fetch_source_does_not_mask_unexpected_value_error( ], ) def test_source_mentions_target_by_media_type( - fetch_source: Mock, + fetch_source_document: Mock, receiver: ModuleType, content_type: str, body: bytes, @@ -282,13 +282,13 @@ def test_source_mentions_target_by_media_type( content=body, request=httpx.Request("GET", SOURCE_URL), ) - fetch_source.return_value = response, body + fetch_source_document.return_value = response, body assert receiver.source_mentions_target(SOURCE_URL, TARGET_URL) is expected - fetch_source.assert_called_once_with(SOURCE_URL) + fetch_source_document.assert_called_once_with(SOURCE_URL) -@patch("webmentions_ssg.tasks.receiver.fetch_source") +@patch("webmentions_ssg.tasks.receiver.fetch_source_document") @pytest.mark.parametrize( ("content_type", "expected_media_type"), [ @@ -298,7 +298,7 @@ def test_source_mentions_target_by_media_type( ], ) def test_source_mentions_target_rejects_unsupported_media_type( - fetch_source: Mock, + fetch_source_document: Mock, receiver: ModuleType, content_type: str, expected_media_type: str, @@ -314,7 +314,7 @@ def test_source_mentions_target_rejects_unsupported_media_type( content=b"Document", request=httpx.Request("GET", SOURCE_URL), ) - fetch_source.return_value = response, b"Document" + fetch_source_document.return_value = response, b"Document" with pytest.raises( receiver.VerificationError, @@ -327,7 +327,7 @@ def test_source_mentions_target_rejects_unsupported_media_type( ("mentions", "expected_status", "expected_reason"), [(True, "verified", None), (False, "deleted", "Source does not mention target")], ) -def test_check_source_returns_outcome( +def test_determine_verification_outcome_returns_outcome( receiver: ModuleType, monkeypatch: pytest.MonkeyPatch, mentions: bool, @@ -339,7 +339,10 @@ def test_check_source_returns_outcome( uuid.uuid7(), SOURCE_URL, TARGET_URL, datetime.now(UTC) ) - assert receiver.check_source(claim) == (expected_status, expected_reason) + assert receiver.determine_verification_outcome(claim) == ( + expected_status, + expected_reason, + ) @patch("webmentions_ssg.tasks.receiver.source_mentions_target") @@ -415,7 +418,7 @@ def test_verify_webmention_persists_retryable_failure_and_reraises( match failure: case "temporary-http": - exception = receiver.TemporaryFetchError("Source returned HTTP 503") + exception = receiver.RetryableFetchError("Source returned HTTP 503") case "network": exception = httpx.ConnectError( "Connection refused", request=httpx.Request("GET", SOURCE_URL) @@ -460,15 +463,15 @@ def set_webmention_updated_at( def test_reaper_requeues_stale_verifications( app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch, status: str ) -> None: - app.config["VERIFICATION_TIMEOUT_SECONDS"] = 900 + app.config["VERIFICATION_STALE_AFTER_SECONDS"] = 900 identifier = create_webmention(app, status=status, failure_reason="Interrupted") set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1)) enqueue = Mock() monkeypatch.setattr(receiver, "verify_webmention", enqueue) with app.app_context(): - assert receiver.recover_stale_webmentions() == 1 - assert receiver.recover_stale_webmentions() == 0 + assert receiver.recover_stale_verifications() == 1 + assert receiver.recover_stale_verifications() == 0 row = db.session.get(ReceivedWebmention, identifier) assert row is not None assert row.updated_at.replace(tzinfo=UTC) > datetime.now(UTC) - timedelta( @@ -494,7 +497,7 @@ def test_reaper_does_not_touch_recent_or_terminal_webmentions( monkeypatch.setattr(receiver, "verify_webmention", enqueue) with app.app_context(): - assert receiver.recover_stale_webmentions() == 0 + assert receiver.recover_stale_verifications() == 0 assert get_webmention_state(app, recent_verifying) == ("verifying", None) assert get_webmention_state(app, recent_received) == ("received", None) @@ -510,17 +513,17 @@ def test_reaper_recovers_after_enqueue_failure( monkeypatch.setattr(receiver, "verify_webmention", Mock(side_effect=RuntimeError)) with app.app_context(), pytest.raises(RuntimeError): - receiver.recover_stale_webmentions() + receiver.recover_stale_verifications() assert get_webmention_state(app, identifier) == ("received", None) # The reaper can recover the row again if the process died after the commit # but before Huey persisted the task. - app.config["VERIFICATION_TIMEOUT_SECONDS"] = 0 + app.config["VERIFICATION_STALE_AFTER_SECONDS"] = 0 enqueue = Mock() monkeypatch.setattr(receiver, "verify_webmention", enqueue) with app.app_context(): - assert receiver.recover_stale_webmentions() == 1 + assert receiver.recover_stale_verifications() == 1 enqueue.assert_called_once_with(identifier) @@ -534,10 +537,10 @@ def test_late_worker_cannot_overwrite_recovered_verification( def concurrent_recovery(_source: str, _target: str) -> bool: # The reaper supersedes this still-running attempt; a newer attempt # subsequently completes before the original fetch returns. - app.config["VERIFICATION_TIMEOUT_SECONDS"] = 0 + app.config["VERIFICATION_STALE_AFTER_SECONDS"] = 0 with app.app_context(): with patch.object(receiver, "verify_webmention", Mock()) as enqueue: - assert receiver.recover_stale_webmentions() == 1 + assert receiver.recover_stale_verifications() == 1 enqueue.assert_called_once_with(identifier) db.session.execute( sa.update(ReceivedWebmention) @@ -562,7 +565,7 @@ def test_reaper_requeues_and_verifies_integration( monkeypatch.setattr(receiver, "source_mentions_target", check) with app.app_context(): - assert receiver.recover_stale_webmentions() == 1 + assert receiver.recover_stale_verifications() == 1 assert get_webmention_state(app, identifier) == ("verified", None) check.assert_called_once_with(SOURCE_URL, TARGET_URL) @@ -594,7 +597,7 @@ def test_verify_webmention_records_unexpected_error_as_terminal_failure( # Failed rows are not eligible for automatic reaper recovery. set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1)) with app.app_context(): - assert receiver.recover_stale_webmentions() == 0 + assert receiver.recover_stale_verifications() == 0 def test_reaper_caps_stale_verification_recoveries( @@ -611,7 +614,7 @@ def test_reaper_caps_stale_verification_recoveries( app, identifier, datetime.now(UTC) - timedelta(hours=1) ) with app.app_context(): - assert receiver.recover_stale_webmentions() == 1 + assert receiver.recover_stale_verifications() == 1 row = db.session.get(ReceivedWebmention, identifier) assert row is not None assert row.status == "received" @@ -619,13 +622,13 @@ def test_reaper_caps_stale_verification_recoveries( set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1)) with app.app_context(): - assert receiver.recover_stale_webmentions() == 0 + assert receiver.recover_stale_verifications() == 0 row = db.session.get(ReceivedWebmention, identifier) assert row is not None assert row.status == "failed" assert row.failure_reason == receiver.RECOVERY_EXHAUSTED_REASON assert row.recovery_attempts == 3 - assert receiver.recover_stale_webmentions() == 0 + assert receiver.recover_stale_verifications() == 0 assert enqueue.call_count == 3 @@ -639,7 +642,7 @@ def test_reaper_caps_received_rows_lost_before_first_task( monkeypatch.setattr(receiver, "verify_webmention", enqueue) with app.app_context(): - assert receiver.recover_stale_webmentions() == 0 + assert receiver.recover_stale_verifications() == 0 assert get_webmention_state(app, identifier) == ( "failed", receiver.RECOVERY_EXHAUSTED_REASON, @@ -680,7 +683,7 @@ def test_verify_webmention_ignores_unknown_identifier( @patch("webmentions_ssg.tasks.receiver.httpx.Client") -def test_fetch_source_rejects_unsafe_destination( +def test_fetch_source_document_rejects_unsafe_destination( httpx_client: Mock, app: Flask, receiver: ModuleType ) -> None: from webmentions_ssg.net import UnsafeAddressError @@ -691,24 +694,24 @@ def test_fetch_source_rejects_unsafe_destination( app.app_context(), pytest.raises(receiver.VerificationError, match="non-public address"), ): - receiver.fetch_source(SOURCE_URL) + receiver.fetch_source_document(SOURCE_URL) @patch("webmentions_ssg.tasks.receiver.httpx.Client") -def test_fetch_source_treats_dns_failure_as_temporary( +def test_fetch_source_document_treats_dns_failure_as_temporary( httpx_client: Mock, app: Flask, receiver: ModuleType ) -> None: client = httpx_client.return_value.__enter__.return_value client.stream.side_effect = AddressResolutionError("DNS timeout") with ( app.app_context(), - pytest.raises(receiver.TemporaryFetchError, match="could not be resolved"), + pytest.raises(receiver.RetryableFetchError, match="could not be resolved"), ): - receiver.fetch_source(SOURCE_URL) + receiver.fetch_source_document(SOURCE_URL) @patch("webmentions_ssg.tasks.receiver.httpx.Client") -def test_fetch_source_ignores_invalid_content_length( +def test_fetch_source_document_ignores_invalid_content_length( httpx_client: Mock, app: Flask, receiver: ModuleType ) -> None: response = httpx.Response( @@ -720,15 +723,15 @@ def test_fetch_source_ignores_invalid_content_length( set_stream_response(httpx_client, response) with app.app_context(): - fetched_response, body = receiver.fetch_source(SOURCE_URL) + fetched_response, body = receiver.fetch_source_document(SOURCE_URL) assert fetched_response is response assert body == b"

Document

" -@patch("webmentions_ssg.tasks.receiver.fetch_source") +@patch("webmentions_ssg.tasks.receiver.fetch_source_document") def test_source_mentions_target_falls_back_for_unknown_encoding( - fetch_source: Mock, receiver: ModuleType + fetch_source_document: Mock, receiver: ModuleType ) -> None: body = f"Reply to {TARGET_URL}".encode() @@ -739,7 +742,7 @@ def test_source_mentions_target_falls_back_for_unknown_encoding( request=httpx.Request("GET", SOURCE_URL), ) response.encoding = "unknown-encoding" - fetch_source.return_value = response, body + fetch_source_document.return_value = response, body assert receiver.source_mentions_target(SOURCE_URL, TARGET_URL) - fetch_source.assert_called_once_with(SOURCE_URL) + fetch_source_document.assert_called_once_with(SOURCE_URL) diff --git a/tests/tasks/test_scanner.py b/tests/tasks/test_scanner.py index d16729c..00256d0 100644 --- a/tests/tasks/test_scanner.py +++ b/tests/tasks/test_scanner.py @@ -82,7 +82,7 @@ def test_manual_scan_propagates_unrelated_errors( scanner_module.manual_scan_sources.call_local() -def test_scan_tree_returns_count_and_seen_paths( +def test_reconcile_source_tree_returns_count_and_seen_paths( app: Flask, scanner_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch ) -> None: for name in ("good", "bad"): @@ -105,7 +105,7 @@ def test_scan_tree_returns_count_and_seen_paths( urls: dict[str, str] = {} with app.app_context(): - scan = scanner_module.scan_tree( + scan = scanner_module.reconcile_source_tree( tmp_path, BASE_URL, sources, urls, datetime.now(UTC) ) @@ -1248,7 +1248,7 @@ def test_unexpected_persistence_error_is_not_suppressed( monkeypatch.setattr( scanner_module, - "create_source", + "build_source", Mock(side_effect=RuntimeError("database failed")), ) @@ -1376,7 +1376,7 @@ def test_changed_source_url_is_rejected( def test_scan_sources_returns_when_source_directory_is_not_configured( app: Flask, scanner_module: ModuleType ) -> None: - app.config["SOURCE_DIRECTORY"] = None + app.config["SITE_OUTPUT_DIRECTORY"] = None run_scan(app, scanner_module) diff --git a/tests/tasks/test_sender.py b/tests/tasks/test_sender.py index 3a22769..032b638 100644 --- a/tests/tasks/test_sender.py +++ b/tests/tasks/test_sender.py @@ -110,7 +110,7 @@ def sender_module(app: Flask) -> ModuleType: pytest.param(3, 1, 2, False, id="superseded"), ], ) -def test_attempt_is_current( +def test_revision_still_pending( app: Flask, sender_module: ModuleType, desired_revision: int, @@ -125,10 +125,10 @@ def test_attempt_is_current( with app.app_context(): webmention = db.session.get(SentWebmention, identifier) assert webmention is not None - assert sender_module.attempt_is_current(webmention, revision) is expected + assert sender_module.revision_still_pending(webmention, revision) is expected -def test_deliver_webmention_returns_retryable_outcome( +def test_attempt_delivery_returns_retryable_outcome( app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: mock_sender_requests( @@ -136,7 +136,7 @@ def test_deliver_webmention_returns_retryable_outcome( ) with app.app_context(): - result = sender_module.deliver_webmention( + result = sender_module.attempt_delivery( SOURCE_URL, TARGET_URL, webmention_uuid=uuid.uuid7() ) @@ -290,19 +290,13 @@ def test_send_webmention_persists_temporary_failure_with_backoff( ).total_seconds() == 3600 -def test_retry_delay_is_exponential_and_capped( +def test_backoff_delay_is_exponential_and_capped( app: Flask, sender_module: ModuleType ) -> None: with app.app_context(): - assert [sender_module.retry_delay(n).total_seconds() for n in range(1, 8)] == [ - 3600, - 7200, - 14400, - 28800, - 57600, - 86400, - 86400, - ] + assert [ + sender_module.backoff_delay(n).total_seconds() for n in range(1, 8) + ] == [3600, 7200, 14400, 28800, 57600, 86400, 86400] def test_temporary_failure_backoff_suppresses_duplicate_tasks_and_caps_attempts( @@ -644,7 +638,7 @@ def test_send_webmention_ignores_superseded_attempt( def post_temporary_failure(*args, **kwargs) -> None: supersede_revision() - raise sender_module.TemporarySenderError("Temporary failure") + raise sender_module.RetryableDeliveryError("Temporary failure") post.side_effect = post_temporary_failure diff --git a/tests/tasks/test_source_parser.py b/tests/tasks/test_source_parser.py index 3c3a93e..38752b0 100644 --- a/tests/tasks/test_source_parser.py +++ b/tests/tasks/test_source_parser.py @@ -24,7 +24,7 @@ def source_parser_module(app: Flask, monkeypatch: pytest.MonkeyPatch) -> ModuleT return source_parser -def test_parse_entry_returns_microformats_entry( +def test_parse_h_entry_returns_microformats_entry( source_parser_module: ModuleType, ) -> None: document = parse_html( @@ -45,13 +45,13 @@ def test_parse_entry_returns_microformats_entry( assert element is not None - entry = source_parser_module.parse_entry(element, SOURCE_URL) + entry = source_parser_module.parse_h_entry(element, SOURCE_URL) assert "h-entry" in entry["type"] assert entry["properties"]["url"] == [SOURCE_URL] -def test_parse_entry_rejects_non_entry(source_parser_module: ModuleType) -> None: +def test_parse_h_entry_rejects_non_entry(source_parser_module: ModuleType) -> None: document = parse_html( """
@@ -68,10 +68,12 @@ def test_parse_entry_rejects_non_entry(source_parser_module: ModuleType) -> None source_parser_module.SourceScanError, match="Expected exactly one parsed h-entry", ): - source_parser_module.parse_entry(element, SOURCE_URL) + source_parser_module.parse_h_entry(element, SOURCE_URL) -def test_primary_entry_returns_source_entry(source_parser_module: ModuleType) -> None: +def test_find_primary_h_entry_returns_source_entry( + source_parser_module: ModuleType, +) -> None: document = parse_html( f"""
@@ -90,14 +92,14 @@ def test_primary_entry_returns_source_entry(source_parser_module: ModuleType) -> """ ) - element, entry = source_parser_module.primary_entry(document, SOURCE_URL) + element, entry = source_parser_module.find_primary_h_entry(document, SOURCE_URL) assert element["id"] == "source" assert entry["properties"]["name"] == ["Example post"] assert entry["properties"]["url"] == [SOURCE_URL] -def test_primary_entry_rejects_missing_h_entry( +def test_find_primary_h_entry_rejects_missing_h_entry( source_parser_module: ModuleType, ) -> None: document = parse_html( @@ -112,10 +114,10 @@ def test_primary_entry_rejects_missing_h_entry( source_parser_module.SourceScanError, match="Expected exactly one h-entry, found 0", ): - source_parser_module.primary_entry(document, SOURCE_URL) + source_parser_module.find_primary_h_entry(document, SOURCE_URL) -def test_primary_entry_rejects_multiple_h_entries( +def test_find_primary_h_entry_rejects_multiple_h_entries( source_parser_module: ModuleType, ) -> None: document = parse_html( @@ -141,10 +143,12 @@ def test_primary_entry_rejects_multiple_h_entries( source_parser_module.SourceScanError, match="Expected exactly one h-entry, found 2", ): - source_parser_module.primary_entry(document, SOURCE_URL) + source_parser_module.find_primary_h_entry(document, SOURCE_URL) -def test_primary_entry_rejects_nested_h_entry(source_parser_module: ModuleType) -> None: +def test_find_primary_h_entry_rejects_nested_h_entry( + source_parser_module: ModuleType, +) -> None: document = parse_html( f"""
@@ -170,10 +174,12 @@ def test_primary_entry_rejects_nested_h_entry(source_parser_module: ModuleType) source_parser_module.SourceScanError, match="Expected exactly one h-entry, found 2", ): - source_parser_module.primary_entry(document, SOURCE_URL) + source_parser_module.find_primary_h_entry(document, SOURCE_URL) -def test_primary_entry_rejects_missing_u_url(source_parser_module: ModuleType) -> None: +def test_find_primary_h_entry_rejects_missing_u_url( + source_parser_module: ModuleType, +) -> None: document = parse_html( """
@@ -185,10 +191,10 @@ def test_primary_entry_rejects_missing_u_url(source_parser_module: ModuleType) - ) with pytest.raises(source_parser_module.SourceScanError, match="does not match"): - source_parser_module.primary_entry(document, SOURCE_URL) + source_parser_module.find_primary_h_entry(document, SOURCE_URL) -def test_primary_entry_rejects_nonmatching_u_url( +def test_find_primary_h_entry_rejects_nonmatching_u_url( source_parser_module: ModuleType, ) -> None: document = parse_html( @@ -209,10 +215,10 @@ def test_primary_entry_rejects_nonmatching_u_url( ) with pytest.raises(source_parser_module.SourceScanError, match="does not match"): - source_parser_module.primary_entry(document, SOURCE_URL) + source_parser_module.find_primary_h_entry(document, SOURCE_URL) -def test_content_element_returns_e_content(source_parser_module: ModuleType) -> None: +def test_find_e_content_returns_e_content(source_parser_module: ModuleType) -> None: document = parse_html( """
@@ -227,12 +233,12 @@ def test_content_element_returns_e_content(source_parser_module: ModuleType) -> assert entry is not None - content = source_parser_module.content_element(entry) + content = source_parser_module.find_e_content(entry) assert content["id"] == "content" -def test_content_element_rejects_missing_e_content( +def test_find_e_content_rejects_missing_e_content( source_parser_module: ModuleType, ) -> None: document = parse_html( @@ -251,10 +257,10 @@ def test_content_element_rejects_missing_e_content( source_parser_module.SourceScanError, match="Expected exactly one e-content, found 0", ): - source_parser_module.content_element(entry) + source_parser_module.find_e_content(entry) -def test_content_element_rejects_multiple_e_content( +def test_find_e_content_rejects_multiple_e_content( source_parser_module: ModuleType, ) -> None: document = parse_html( @@ -279,10 +285,12 @@ def test_content_element_rejects_multiple_e_content( source_parser_module.SourceScanError, match="Expected exactly one e-content, found 2", ): - source_parser_module.content_element(entry) + source_parser_module.find_e_content(entry) -def test_canonical_url_returns_absolute_url(source_parser_module: ModuleType) -> None: +def test_declared_canonical_url_returns_absolute_url( + source_parser_module: ModuleType, +) -> None: document = parse_html( f""" @@ -296,14 +304,14 @@ def test_canonical_url_returns_absolute_url(source_parser_module: ModuleType) -> """ ) - result = source_parser_module.canonical_url( + result = source_parser_module.declared_canonical_url( document, relative_path=Path("example/index.html"), base_url=None ) assert result == SOURCE_URL -def test_canonical_url_returns_none_when_missing( +def test_declared_canonical_url_returns_none_when_missing( source_parser_module: ModuleType, ) -> None: document = parse_html( @@ -316,14 +324,14 @@ def test_canonical_url_returns_none_when_missing( """ ) - result = source_parser_module.canonical_url( + result = source_parser_module.declared_canonical_url( document, relative_path=Path("example/index.html"), base_url=None ) assert result is None -def test_canonical_url_resolves_relative_url_with_base_url( +def test_declared_canonical_url_resolves_relative_url_with_base_url( source_parser_module: ModuleType, ) -> None: source_parser_module.is_http_url.side_effect = [False, True] @@ -341,7 +349,7 @@ def test_canonical_url_resolves_relative_url_with_base_url( """ ) - result = source_parser_module.canonical_url( + result = source_parser_module.declared_canonical_url( document, relative_path=Path("example/index.html"), base_url=BASE_URL ) @@ -367,7 +375,7 @@ def test_relative_canonical_requires_base_url(source_parser_module: ModuleType) with pytest.raises( source_parser_module.SourceScanError, match="relative canonical" ): - source_parser_module.canonical_url( + source_parser_module.declared_canonical_url( document, relative_path=Path("example/index.html"), base_url=None ) @@ -389,7 +397,7 @@ def test_empty_canonical_is_rejected(source_parser_module: ModuleType) -> None: with pytest.raises( source_parser_module.SourceScanError, match="empty canonical URL" ): - source_parser_module.canonical_url( + source_parser_module.declared_canonical_url( document, relative_path=Path("example/index.html"), base_url=BASE_URL ) @@ -907,7 +915,7 @@ def test_scan_source_ignores_reaction_to_matching_hostname( assert scanned.targets == frozenset() -def test_canonical_url_rejects_non_http_resolved_url( +def test_declared_canonical_url_rejects_non_http_resolved_url( source_parser_module: ModuleType, ) -> None: source_parser_module.is_http_url.return_value = False @@ -929,17 +937,17 @@ def test_canonical_url_rejects_non_http_resolved_url( source_parser_module.SourceScanError, match="Canonical URL is not a valid HTTP or HTTPS URL", ): - source_parser_module.canonical_url( + source_parser_module.declared_canonical_url( document, relative_path=Path("example/index.html"), base_url=BASE_URL ) -def test_property_urls_ignores_non_mapping_properties( +def test_mf2_string_values_ignores_non_mapping_properties( source_parser_module: ModuleType, ) -> None: entry = {"properties": "invalid"} - assert tuple(source_parser_module.property_urls(entry, "url")) == () + assert tuple(source_parser_module.mf2_string_values(entry, "url")) == () @pytest.mark.parametrize( @@ -960,7 +968,7 @@ def test_property_urls_ignores_non_mapping_properties( ), ], ) -def test_parse_entry_rejects_invalid_parser_output( +def test_parse_h_entry_rejects_invalid_parser_output( source_parser_module: ModuleType, monkeypatch: MonkeyPatch, parsed: object, @@ -981,27 +989,27 @@ def test_parse_entry_rejects_invalid_parser_output( monkeypatch.setattr(source_parser.mf2py, "parse", lambda **kwargs: parsed) with pytest.raises(source_parser_module.SourceScanError, match=message): - source_parser_module.parse_entry(element, SOURCE_URL) + source_parser_module.parse_h_entry(element, SOURCE_URL) -def test_normalize_target_ignores_empty_target( +def test_normalize_target_url_ignores_empty_target( source_parser_module: ModuleType, ) -> None: assert ( - source_parser_module.normalize_target( + source_parser_module.normalize_target_url( " ", base_url=SOURCE_URL, source_url=SOURCE_URL ) is None ) -def test_normalize_target_ignores_url_rejected_by_validator( +def test_normalize_target_url_ignores_url_rejected_by_validator( source_parser_module: ModuleType, ) -> None: source_parser_module.is_http_url.return_value = False assert ( - source_parser_module.normalize_target( + source_parser_module.normalize_target_url( "https://example.com/target", base_url=SOURCE_URL, source_url=SOURCE_URL ) is None diff --git a/tests/tasks/test_target_matching.py b/tests/tasks/test_target_matching.py new file mode 100644 index 0000000..31523af --- /dev/null +++ b/tests/tasks/test_target_matching.py @@ -0,0 +1,176 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +from types import ModuleType +from unittest.mock import Mock, patch + +import pytest +from flask import Flask + +SOURCE_URL = "https://source.example/article" +TARGET_URL = "https://dennisfink.me/blog/example/" + + +@pytest.fixture +def receiver(app: Flask) -> ModuleType: + _ = app + from webmentions_ssg.tasks import target_matching + + return target_matching + + +@pytest.mark.parametrize( + ("body", "expected"), + [ + pytest.param(f'Reply', True, id="a-href"), + pytest.param(f'Target', True, id="area-href"), + pytest.param( + f'', True, id="link-href" + ), + pytest.param(f'', True, id="img-src"), + pytest.param(f'', True, id="audio-src"), + pytest.param(f'', True, id="video-src"), + pytest.param( + f'', True, id="audio-source-src" + ), + pytest.param( + f'', True, id="video-source-src" + ), + pytest.param(f'', True, id="iframe-src"), + pytest.param(f'', True, id="embed-src"), + pytest.param(f'', True, id="script-src"), + pytest.param( + f'', True, id="track-src" + ), + pytest.param( + f'', + True, + id="image-input-src", + ), + pytest.param( + f'', + True, + id="image-input-case-insensitive", + ), + pytest.param( + f'
Quotation
', + True, + id="blockquote-cite", + ), + pytest.param(f'Quotation', True, id="q-cite"), + pytest.param(f'Addition', True, id="ins-cite"), + pytest.param(f'Removal', True, id="del-cite"), + pytest.param( + 'Reply', + True, + id="base-url", + ), + pytest.param(f'', False, id="img-cite-invalid"), + pytest.param( + f'
Quote
', + False, + id="blockquote-src-invalid", + ), + pytest.param(f'Reply', False, id="a-src-invalid"), + pytest.param(f'
', False, id="div-href-invalid"), + pytest.param(f'', False, id="link-src-invalid"), + pytest.param( + f'', + False, + id="text-input-src-invalid", + ), + pytest.param( + f'', + False, + id="picture-source-src-invalid", + ), + pytest.param(f'', False, id="base-is-not-mention"), + pytest.param( + f'Different page', False, id="longer-url" + ), + pytest.param( + f'', + False, + id="invalid-cite-does-not-override-valid-src", + ), + pytest.param(f"

{TARGET_URL}

", False, id="text-content"), + pytest.param( + 'Other site', False, id="missing-target" + ), + ], +) +def test_html_mentions_target(receiver: ModuleType, body: str, expected: bool) -> None: + assert ( + receiver.html_mentions_target(body.encode(), SOURCE_URL, TARGET_URL) is expected + ) + + +def test_html_mentions_relative_target(receiver: ModuleType) -> None: + assert receiver.html_mentions_target( + b'Reply', + "https://source.example/posts/article/", + "https://source.example/posts/target/", + ) + + +@pytest.mark.parametrize( + ("body", "target_url", "expected"), + [ + (TARGET_URL, TARGET_URL, True), + (f"This post replies to {TARGET_URL}", TARGET_URL, True), + ( + f"https://example.com/first {TARGET_URL} https://example.com/last", + TARGET_URL, + True, + ), + pytest.param( + f"See {TARGET_URL}, thanks", TARGET_URL, True, id="trailing-comma" + ), + pytest.param(f"({TARGET_URL})", TARGET_URL, True, id="parenthesized-url"), + pytest.param( + f"See ({TARGET_URL}), thanks!", TARGET_URL, True, id="mixed-punctuation" + ), + pytest.param(f"See {TARGET_URL}. Next", TARGET_URL, True, id="trailing-period"), + pytest.param( + "https://example.com/wiki/Foo_(bar)", + "https://example.com/wiki/Foo_(bar)", + True, + id="balanced-parentheses-in-url", + ), + pytest.param( + "https://example.com/wiki/Foo_(bar)", + "https://example.com/wiki/Foo_(bar", + False, + id="balanced-parentheses-not-trimmed", + ), + (f"{TARGET_URL}more", TARGET_URL, False), + pytest.param( + f"{TARGET_URL},another-page", + TARGET_URL, + False, + id="comma-within-longer-url", + ), + ("https://dennisfink.me/blog/other/", TARGET_URL, False), + ("/blog/example/", TARGET_URL, False), + ("There are no links here.", TARGET_URL, False), + ], +) +def test_text_mentions_target( + receiver: ModuleType, body: str, target_url: str, expected: bool +) -> None: + assert receiver.text_mentions_target(body, target_url) is expected + + +@patch("webmentions_ssg.tasks.target_matching.BeautifulSoup") +def test_html_mentions_target_ignores_non_string_attribute( + beautiful_soup: Mock, receiver: ModuleType +) -> None: + document = beautiful_soup.return_value + document.select_one.return_value = None + + element = Mock() + element.get.return_value = ["not", "a", "string"] + document.select.return_value = [element] + + assert not receiver.html_mentions_target(b"", SOURCE_URL, TARGET_URL) diff --git a/tests/test_auth.py b/tests/test_auth.py index 3b05b6f..8219a07 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -10,18 +10,23 @@ import pytest from flask import Flask from webmentions_ssg import auth, db -from webmentions_ssg.auth import AUTH, is_safe_next_url, load_user, validate_password +from webmentions_ssg.auth import ( + is_safe_next_url, + load_user_from_session_id, + login_manager, + validate_new_password, +) from webmentions_ssg.models import User @pytest.mark.parametrize("character", ["\x00", "\x1f", "\x7f", "\x80", "\x9f"]) def test_validate_password_rejects_unicode_controls(character: str) -> None: with pytest.raises(ValueError, match="control characters"): - validate_password(f"valid passphrase{character}") + validate_new_password(f"valid passphrase{character}") def test_user_loader_is_registered(app: Flask) -> None: - assert AUTH._user_callback is load_user + assert login_manager._user_callback is load_user_from_session_id def test_dummy_password_hash_is_lazily_cached(monkeypatch: pytest.MonkeyPatch) -> None: @@ -48,18 +53,18 @@ def test_load_user_returns_matching_user(app: Flask) -> None: user = User(username="account", password="secret") db.session.add(user) db.session.commit() - assert load_user(user.get_id()) is user + assert load_user_from_session_id(user.get_id()) is user def test_load_user_returns_none_for_unknown_id(app: Flask) -> None: with app.app_context(): - assert load_user("99999:" + "a" * 64) is None + assert load_user_from_session_id("99999:" + "a" * 64) is None @pytest.mark.parametrize("identifier", [None, 123, b"1:" + b"a" * 64]) def test_load_user_rejects_non_string_sessions(app: Flask, identifier: object) -> None: with app.app_context(): - assert load_user(identifier) is None + assert load_user_from_session_id(identifier) is None def test_load_user_rejects_legacy_and_malformed_sessions(app: Flask) -> None: @@ -79,7 +84,7 @@ def test_load_user_rejects_legacy_and_malformed_sessions(app: Flask) -> None: f"{user.id}:" + "0" * 64 + ":trailing", "-1:" + "0" * 64, ): - assert load_user(identifier) is None + assert load_user_from_session_id(identifier) is None def test_password_change_invalidates_existing_session(app: Flask) -> None: @@ -111,8 +116,8 @@ def test_password_change_invalidates_existing_session(app: Flask) -> None: user = db.session.get(User, int(old_identifier.split(":", 1)[0])) assert user is not None assert user.get_id() != old_identifier - assert load_user(old_identifier) is None - assert load_user(user.get_id()) is user + assert load_user_from_session_id(old_identifier) is None + assert load_user_from_session_id(user.get_id()) is user # A pre-existing, validly signed cookie no longer authenticates. assert client.get("/received").status_code == HTTPStatus.FOUND diff --git a/tests/test_cli.py b/tests/test_cli.py index 49494d2..cdaf896 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -329,7 +329,7 @@ def test_upgrade_converts_received_status_without_losing_rows( def test_mail_cli_sends_to_configured_notification_email(app: Flask) -> None: app.config.update( - NOTIFICATION_EMAIL="owner@example.com", MAIL_DEFAULT_SENDER="sender@example.com" + DIGEST_EMAIL="owner@example.com", MAIL_DEFAULT_SENDER="sender@example.com" ) with patch("webmentions_ssg.mail.send") as send: @@ -347,7 +347,7 @@ def test_mail_cli_sends_to_configured_notification_email(app: Flask) -> None: def test_mail_cli_overrides_configured_recipient(app: Flask) -> None: app.config.update( - NOTIFICATION_EMAIL="owner@example.com", MAIL_DEFAULT_SENDER="sender@example.com" + DIGEST_EMAIL="owner@example.com", MAIL_DEFAULT_SENDER="sender@example.com" ) with patch("webmentions_ssg.mail.send") as send: @@ -361,18 +361,18 @@ def test_mail_cli_overrides_configured_recipient(app: Flask) -> None: def test_mail_cli_requires_recipient(app: Flask) -> None: - app.config.update(NOTIFICATION_EMAIL=None, MAIL_DEFAULT_SENDER="sender@example.com") + app.config.update(DIGEST_EMAIL=None, MAIL_DEFAULT_SENDER="sender@example.com") with patch("webmentions_ssg.mail.send") as send: result = app.test_cli_runner().invoke(args=["mail", "test"]) assert result.exit_code != 0 - assert "Set NOTIFICATION_EMAIL or pass --to" in result.output + assert "Set DIGEST_EMAIL or pass --to" in result.output send.assert_not_called() def test_mail_cli_requires_default_sender(app: Flask) -> None: - app.config.update(NOTIFICATION_EMAIL="owner@example.com", MAIL_DEFAULT_SENDER=None) + app.config.update(DIGEST_EMAIL="owner@example.com", MAIL_DEFAULT_SENDER=None) with patch("webmentions_ssg.mail.send") as send: result = app.test_cli_runner().invoke(args=["mail", "test"]) @@ -384,7 +384,7 @@ def test_mail_cli_requires_default_sender(app: Flask) -> None: def test_mail_cli_reports_smtp_failure(app: Flask) -> None: app.config.update( - NOTIFICATION_EMAIL="owner@example.com", MAIL_DEFAULT_SENDER="sender@example.com" + DIGEST_EMAIL="owner@example.com", MAIL_DEFAULT_SENDER="sender@example.com" ) with patch( diff --git a/tests/test_config.py b/tests/test_config.py index 4a59236..a4c5e9b 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 AllowedHostname +from webmentions_ssg.forms import AllowedTargetHostname @pytest.mark.parametrize( @@ -66,7 +66,7 @@ def test_missing_development_configuration_is_optional(tmp_path: Path) -> None: LOG_FILE = str(tmp_path / "development.log") app = create_app(LocalDevelopmentConfig) - assert app.config["ALLOWED_HOSTNAMES"] == set() + assert app.config["ALLOWED_TARGET_HOSTNAMES"] == set() def test_development_creates_local_directory_on_first_start( @@ -98,27 +98,27 @@ def test_testing_creates_local_log_directory_on_first_start( def test_endpoint_limits_are_inherited_from_defaults() -> None: for config in (DefaultConfig, DevelopmentConfig, ProductionConfig, TestingConfig): assert config.MAX_REDIRECTS == 20 - assert config.MAX_SOURCE_BYTES == 1_000_000 - assert config.MAX_TARGET_BYTES == 1_000_000 - assert config.REQUEST_TIMEOUT == 5.0 - assert config.REQUEST_TOTAL_TIMEOUT == 30.0 + assert config.MAX_RECEIVED_SOURCE_BYTES == 1_000_000 + assert config.MAX_DISCOVERY_TARGET_BYTES == 1_000_000 + assert config.REQUEST_TIMEOUT_SECONDS == 5.0 + assert config.REQUEST_TOTAL_TIMEOUT_SECONDS == 30.0 - assert set() == DefaultConfig.ALLOWED_HOSTNAMES - assert DefaultConfig.IGNORED_HOSTNAMES == () + assert set() == DefaultConfig.ALLOWED_TARGET_HOSTNAMES + assert DefaultConfig.IGNORED_TARGET_HOSTNAMES == () def test_allowed_hostnames_are_normalized_at_startup(tmp_path: Path) -> None: class MixedCaseConfig(TestingConfig): LOG_FILE = str(tmp_path / "test.log") - ALLOWED_HOSTNAMES = [" DennisFink.Me ", "EXAMPLE.COM", "dennisfink.me"] + ALLOWED_TARGET_HOSTNAMES = [" DennisFink.Me ", "EXAMPLE.COM", "dennisfink.me"] app = create_app(MixedCaseConfig) - assert app.config["ALLOWED_HOSTNAMES"] == {"dennisfink.me", "example.com"} - assert isinstance(app.config["ALLOWED_HOSTNAMES"], set) + assert app.config["ALLOWED_TARGET_HOSTNAMES"] == {"dennisfink.me", "example.com"} + assert isinstance(app.config["ALLOWED_TARGET_HOSTNAMES"], set) # The form validator must compare complete hostnames, never substrings. target = Field() - validator = AllowedHostname(message="Hostname not allowed") + validator = AllowedTargetHostname(message="Hostname not allowed") form = BaseForm({}) with app.app_context(): for url in ("https://dennisfink.me/post", "https://EXAMPLE.com/post"): @@ -148,19 +148,19 @@ def test_invalid_allowed_hostnames_fail_at_startup( ) -> None: class InvalidHostnameConfig(TestingConfig): LOG_FILE = str(tmp_path / "test.log") - ALLOWED_HOSTNAMES = hostnames + ALLOWED_TARGET_HOSTNAMES = hostnames - with pytest.raises(expected_exception, match="ALLOWED_HOSTNAMES"): + with pytest.raises(expected_exception, match="ALLOWED_TARGET_HOSTNAMES"): create_app(InvalidHostnameConfig) def test_ignored_hostnames_are_normalized_at_startup(tmp_path: Path) -> None: class MixedCaseConfig(TestingConfig): LOG_FILE = str(tmp_path / "test.log") - IGNORED_HOSTNAMES = [" Example.COM. ", "*.EXAMPLE.com.", "example.com"] + IGNORED_TARGET_HOSTNAMES = [" Example.COM. ", "*.EXAMPLE.com.", "example.com"] app = create_app(MixedCaseConfig) - assert app.config["IGNORED_HOSTNAMES"] == {"*.example.com", "example.com"} + assert app.config["IGNORED_TARGET_HOSTNAMES"] == {"*.example.com", "example.com"} @pytest.mark.parametrize( @@ -181,9 +181,9 @@ def test_invalid_ignored_hostnames_fail_at_startup( ) -> None: class InvalidHostnameConfig(TestingConfig): LOG_FILE = str(tmp_path / "test.log") - IGNORED_HOSTNAMES = hostnames + IGNORED_TARGET_HOSTNAMES = hostnames - with pytest.raises(expected_exception, match="IGNORED_HOSTNAMES"): + with pytest.raises(expected_exception, match="IGNORED_TARGET_HOSTNAMES"): create_app(InvalidHostnameConfig) @@ -197,7 +197,7 @@ def test_production_requires_nonempty_allowed_hostnames(tmp_path: Path) -> None: class EmptyHostnamesConfig(ProductionConfig): CONFIG_FILE = str(config_path) - with pytest.raises(RuntimeError, match="ALLOWED_HOSTNAMES"): + with pytest.raises(RuntimeError, match="ALLOWED_TARGET_HOSTNAMES"): create_app(EmptyHostnamesConfig) @@ -221,7 +221,7 @@ def test_production_rejects_ephemeral_or_discarding_huey_queue( class ProductionWithQueue(ProductionConfig): CONFIG_FILE = str(config_path) SECRET_KEY = "a-long-secret-string-for-a-production-test" - ALLOWED_HOSTNAMES = {"example.com"} + ALLOWED_TARGET_HOSTNAMES = {"example.com"} RATELIMIT_STORAGE_URI = "redis://localhost:6379/1" LOG_FILE = str(tmp_path / "production.log") @@ -242,7 +242,7 @@ def test_production_prefers_explicit_huey_url_over_legacy_option( class ProductionWithExplicitQueue(ProductionConfig): CONFIG_FILE = str(config_path) SECRET_KEY = "a-long-secret-string-for-a-production-test" - ALLOWED_HOSTNAMES = {"example.com"} + ALLOWED_TARGET_HOSTNAMES = {"example.com"} RATELIMIT_STORAGE_URI = "redis://localhost:6379/1" LOG_FILE = str(tmp_path / "production.log") HUEY = {"url": "memory://"} @@ -256,15 +256,15 @@ def test_environment_allowed_hostnames_override_config_file( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: config_path = tmp_path / "development.cfg" - config_path.write_text('ALLOWED_HOSTNAMES = ["first.example"]\n') - monkeypatch.setenv("WEBMENTIONS_SSG_ALLOWED_HOSTNAMES", '["SECOND.EXAMPLE"]') + config_path.write_text('ALLOWED_TARGET_HOSTNAMES = ["first.example"]\n') + monkeypatch.setenv("WEBMENTIONS_SSG_ALLOWED_TARGET_HOSTNAMES", '["SECOND.EXAMPLE"]') class LocalDevelopmentConfig(DevelopmentConfig): CONFIG_FILE = str(config_path) LOG_FILE = str(tmp_path / "development.log") app = create_app(LocalDevelopmentConfig) - assert app.config["ALLOWED_HOSTNAMES"] == {"second.example"} + assert app.config["ALLOWED_TARGET_HOSTNAMES"] == {"second.example"} @pytest.mark.parametrize( @@ -288,7 +288,7 @@ def test_production_refuses_missing_weak_or_example_secret_key( class SecretKeyConfig(ProductionConfig): CONFIG_FILE = str(config_file) LOG_FILE = str(tmp_path / "production.log") - ALLOWED_HOSTNAMES = {"example.com"} + ALLOWED_TARGET_HOSTNAMES = {"example.com"} RATELIMIT_STORAGE_URI = "redis://localhost:6379" SECRET_KEY = secret @@ -308,7 +308,7 @@ def test_production_uses_persistent_configured_secret( class ConfiguredProduction(ProductionConfig): CONFIG_FILE = str(config_file) LOG_FILE = str(tmp_path / "production.log") - ALLOWED_HOSTNAMES = {"example.com"} + ALLOWED_TARGET_HOSTNAMES = {"example.com"} RATELIMIT_STORAGE_URI = "redis://localhost:6379" HUEY_URL = f"sqlite:///{tmp_path / 'huey.db'}" @@ -330,7 +330,7 @@ def test_production_accepts_environment_secret_override( class EnvConfiguredProduction(ProductionConfig): CONFIG_FILE = str(config_file) LOG_FILE = str(tmp_path / "production.log") - ALLOWED_HOSTNAMES = {"example.com"} + ALLOWED_TARGET_HOSTNAMES = {"example.com"} RATELIMIT_STORAGE_URI = "redis://localhost:6379" HUEY_URL = f"sqlite:///{tmp_path / 'huey.db'}" diff --git a/tests/test_forms.py b/tests/test_forms.py index e636eae..b5b3296 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -8,12 +8,12 @@ from werkzeug.datastructures import MultiDict from wtforms import Form, StringField, ValidationError from webmentions_ssg.forms import ( - AdminActionForm, - AllowedHostname, - EndpointForm, + AllowedTargetHostname, + ConfirmActionForm, LoginForm, + MaxURLBytes, PublicIPLiteral, - URLMaxBytes, + WebmentionSubmissionForm, ) VALID_SOURCE = "https://source.example/post" @@ -81,7 +81,9 @@ 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 = EndpointForm(formdata=MultiDict(form_data), meta={"csrf": False}) + form = WebmentionSubmissionForm( + formdata=MultiDict(form_data), meta={"csrf": False} + ) assert not form.validate() assert expected_error in form.errors[invalid_field] @@ -92,7 +94,7 @@ def test_endpoint_form_accepts_http_schemes_case_insensitively( app: Flask, scheme: str ) -> None: with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict( { "source": f"{scheme}://source.example/post", @@ -106,7 +108,7 @@ def test_endpoint_form_accepts_http_schemes_case_insensitively( def test_endpoint_form_accepts_valid_data(app: Flask) -> None: with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict({"source": VALID_SOURCE, "target": VALID_TARGET}), meta={"csrf": False}, ) @@ -143,7 +145,7 @@ def test_login_form_accepts_credentials(app: Flask) -> None: def test_admin_action_form_accepts_submission(app: Flask) -> None: with app.test_request_context(method="POST"): - form = AdminActionForm(meta={"csrf": False}) + form = ConfirmActionForm(meta={"csrf": False}) assert form.validate() @@ -158,7 +160,7 @@ def test_endpoint_does_not_do_dns_in_web_request( ) with app.test_request_context("/endpoint", method="POST"): for source in (VALID_SOURCE, "ftp://source.example/post", "not a URL"): - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict({"source": source, "target": VALID_TARGET}), meta={"csrf": False}, ) @@ -170,7 +172,7 @@ def test_endpoint_does_not_do_dns_in_web_request( ) def test_endpoint_rejects_private_ip_literals(app: Flask, source: str) -> None: with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict({"source": source, "target": VALID_TARGET}), meta={"csrf": False}, ) @@ -180,7 +182,7 @@ def test_endpoint_rejects_private_ip_literals(app: Flask, source: str) -> None: def test_endpoint_rejects_private_ip_literal_with_trailing_dot(app: Flask) -> None: with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict( {"source": "http://127.0.0.1./", "target": VALID_TARGET} ), @@ -192,7 +194,7 @@ def test_endpoint_rejects_private_ip_literal_with_trailing_dot(app: Flask) -> No def test_endpoint_handles_malformed_ipv6_without_crashing(app: Flask) -> None: with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict({"source": VALID_SOURCE, "target": "http://[::1"}), meta={"csrf": False}, ) @@ -205,7 +207,7 @@ def test_endpoint_form_applies_configurable_url_byte_limit_to_both_fields( ) -> None: with app.test_request_context("/endpoint", method="POST"): app.config["MAX_WEBMENTION_URL_BYTES"] = len(VALID_SOURCE.encode("utf-8")) - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict({"source": VALID_SOURCE + "x", "target": VALID_TARGET}), meta={"csrf": False}, ) @@ -214,7 +216,7 @@ def test_endpoint_form_applies_configurable_url_byte_limit_to_both_fields( assert any("bytes" in message for message in form.errors["source"]) app.config["MAX_WEBMENTION_URL_BYTES"] = len(VALID_TARGET.encode("utf-8")) - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict({"source": VALID_SOURCE, "target": VALID_TARGET + "x"}), meta={"csrf": False}, ) @@ -243,14 +245,14 @@ def test_endpoint_rejects_invalid_httpx_urls_and_whitespace( data = {"source": VALID_SOURCE, "target": VALID_TARGET} data[field_name] = invalid_url with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm(formdata=MultiDict(data), meta={"csrf": False}) + 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 = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict( {"source": "https://source.example/a%20b", "target": VALID_TARGET} ), @@ -261,7 +263,7 @@ def test_endpoint_accepts_encoded_path_space(app: Flask) -> None: def test_endpoint_form_rejects_matching_source_and_target(app: Flask) -> None: with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict({"source": VALID_TARGET, "target": VALID_TARGET}), meta={"csrf": False}, ) @@ -271,27 +273,27 @@ def test_endpoint_form_rejects_matching_source_and_target(app: Flask) -> None: def test_allowed_hostname_accepts_configured_hostname(app: Flask) -> None: - app.config["ALLOWED_HOSTNAMES"] = ["dennisfink.me"] + app.config["ALLOWED_TARGET_HOSTNAMES"] = ["dennisfink.me"] form = URLForm(MultiDict({"url": "https://dennisfink.me/blog/example/"})) with app.app_context(): - AllowedHostname()(form, form.url) + AllowedTargetHostname()(form, form.url) def test_allowed_hostname_rejects_unconfigured_hostname(app: Flask) -> None: - app.config["ALLOWED_HOSTNAMES"] = ["dennisfink.me"] + 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"): - AllowedHostname()(form, form.url) + AllowedTargetHostname()(form, form.url) def test_allowed_hostname_uses_custom_message(app: Flask) -> None: - app.config["ALLOWED_HOSTNAMES"] = ["dennisfink.me"] + 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"), ): - AllowedHostname("Hostname is not allowed")(form, form.url) + AllowedTargetHostname("Hostname is not allowed")(form, form.url) @pytest.mark.parametrize( @@ -312,11 +314,11 @@ def test_public_ip_literal_accepts_public_ipv6_address() -> None: 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"): - AllowedHostname()(form, form.url) + AllowedTargetHostname()(form, form.url) def test_url_max_bytes_respects_utf8_byte_length_and_current_config(app: Flask) -> None: - validator = URLMaxBytes() + validator = MaxURLBytes() form = URLForm(MultiDict({"url": "é" * 3})) with app.app_context(): app.config["MAX_WEBMENTION_URL_BYTES"] = 6 @@ -331,10 +333,10 @@ def test_url_max_bytes_custom_message(app: Flask) -> None: with app.app_context(): app.config["MAX_WEBMENTION_URL_BYTES"] = 2 with pytest.raises(ValidationError, match="URL too long"): - URLMaxBytes(message="URL too long")(form, form.url) + 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"): - URLMaxBytes()(form, form.url) + MaxURLBytes()(form, form.url) diff --git a/tests/test_init.py b/tests/test_init.py index 2b5ea00..44375de 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -14,7 +14,7 @@ from webmentions_ssg.net import is_http_url def test_create_app_registers_blueprint_and_extensions(app: Flask) -> None: - assert "root" in app.blueprints + assert "webmentions" in app.blueprints assert app.extensions["huey"] is huey assert app.jinja_env.filters["is_http_url"] is is_http_url @@ -34,14 +34,14 @@ from webmentions_ssg import huey from webmentions_ssg.tasks.scanner import manual_scan_sources from webmentions_ssg.tasks.receiver import verify_webmention from webmentions_ssg.tasks.sender import send_webmention -from webmentions_ssg.views import root_page +from webmentions_ssg.views import webmentions_bp assert huey.app is None assert huey._huey is None assert manual_scan_sources.__name__ == 'manual_scan_sources' assert verify_webmention.__name__ == 'verify_webmention' assert send_webmention.__name__ == 'send_webmention' -assert root_page.name == 'root' +assert webmentions_bp.name == 'webmentions' """ result = subprocess.run( [sys.executable, "-c", script], capture_output=True, text=True, check=False diff --git a/tests/test_net.py b/tests/test_net.py index 3b754c0..c7c65ba 100644 --- a/tests/test_net.py +++ b/tests/test_net.py @@ -17,17 +17,17 @@ from flask import Flask from webmentions_ssg.net import ( AddressResolutionError, DeadlineStream, - NetworkDeadline, PublicHTTPTransport, PublicNetworkBackend, + RequestDeadline, ResponseTooLargeError, UnsafeAddressError, - content_type_of, - hostname_of, is_http_url, is_public_address, - public_addresses, + media_type_of, + normalized_hostname, read_limited_body, + resolve_public_addresses, ) @@ -38,7 +38,7 @@ def test_backend_rejects_private_ip_without_socket( create_socket = Mock(side_effect=AssertionError("must not open socket")) monkeypatch.setattr(socket, "socket", create_socket) with pytest.raises(UnsafeAddressError): - PublicNetworkBackend(NetworkDeadline(5)).connect_tcp(host, 80) + PublicNetworkBackend(RequestDeadline(5)).connect_tcp(host, 80) create_socket.assert_not_called() @@ -48,7 +48,7 @@ def test_rebinding_cannot_change_socket_peer(monkeypatch: pytest.MonkeyPatch) -> :param monkeypatch: Pytest fixture for replacing the DNS resolver and socket calls. """ dns = Mock(return_value=("93.184.216.34",)) - monkeypatch.setattr("webmentions_ssg.net.public_addresses", dns) + monkeypatch.setattr("webmentions_ssg.net.resolve_public_addresses", dns) socket_instance = Mock() create_socket = Mock(return_value=socket_instance) monkeypatch.setattr(socket, "socket", create_socket) @@ -56,7 +56,7 @@ def test_rebinding_cannot_change_socket_peer(monkeypatch: pytest.MonkeyPatch) -> socket, "getaddrinfo", Mock(side_effect=AssertionError("second resolution")) ) - stream = PublicNetworkBackend(NetworkDeadline(5)).connect_tcp( + stream = PublicNetworkBackend(RequestDeadline(5)).connect_tcp( "rebinding.example", 443, timeout=2.0 ) assert isinstance(stream, DeadlineStream) @@ -72,14 +72,14 @@ def test_backend_closes_failed_socket_and_tries_next_address( monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr( - "webmentions_ssg.net.public_addresses", + "webmentions_ssg.net.resolve_public_addresses", Mock(return_value=("93.184.216.34", "8.8.8.8")), ) first, second = Mock(), Mock() first.connect.side_effect = TimeoutError("connection timed out") monkeypatch.setattr(socket, "socket", Mock(side_effect=[first, second])) - stream = PublicNetworkBackend(NetworkDeadline(5)).connect_tcp("example.com", 443) + stream = PublicNetworkBackend(RequestDeadline(5)).connect_tcp("example.com", 443) assert isinstance(stream, DeadlineStream) first.close.assert_called_once_with() @@ -106,7 +106,7 @@ def test_backend_closes_socket_on_failure( monkeypatch.setattr(socket, "socket", Mock(return_value=sock)) with pytest.raises(expected, match=str(failure)): - PublicNetworkBackend(NetworkDeadline(5)).connect_tcp("8.8.8.8", 443) + PublicNetworkBackend(RequestDeadline(5)).connect_tcp("8.8.8.8", 443) sock.close.assert_called_once_with() @@ -184,7 +184,7 @@ def test_deadline_clips_each_read_and_stops_trickle( monkeypatch.setattr("webmentions_ssg.net.time.monotonic", lambda: clock[0]) fake_stream = Mock() fake_stream.read.return_value = b"x" - stream = DeadlineStream(fake_stream, NetworkDeadline(5.0)) + stream = DeadlineStream(fake_stream, RequestDeadline(5.0)) clock[0] = 103.0 assert stream.read(1, timeout=5.0) == b"x" @@ -249,17 +249,17 @@ def test_is_http_url(url: str, expected: bool) -> None: def test_ip_literal_does_not_use_dns() -> None: with patch("webmentions_ssg.net.dns.resolver.resolve_name") as dns_resolve: with pytest.raises(UnsafeAddressError): - public_addresses("169.254.169.254") + resolve_public_addresses("169.254.169.254") with pytest.raises(UnsafeAddressError): - public_addresses("::1") - assert public_addresses("8.8.8.8") == ("8.8.8.8",) + resolve_public_addresses("::1") + assert resolve_public_addresses("8.8.8.8") == ("8.8.8.8",) dns_resolve.assert_not_called() def test_public_addresses_resolves_hostname_once() -> None: with patch("webmentions_ssg.net.dns.resolver.resolve_name") as resolve: resolve.return_value.addresses.return_value = ["93.184.216.34", "8.8.8.8"] - assert public_addresses("example.com", lifetime=1.5) == ( + assert resolve_public_addresses("example.com", lifetime=1.5) == ( "93.184.216.34", "8.8.8.8", ) @@ -282,7 +282,7 @@ def test_public_addresses_resolves_hostname_once() -> None: def test_public_addresses_rejects_ipv6_transition_literals(address: str) -> None: with patch("webmentions_ssg.net.dns.resolver.resolve_name") as resolve: with pytest.raises(UnsafeAddressError, match="Non-public"): - public_addresses(address) + resolve_public_addresses(address) resolve.assert_not_called() @@ -293,7 +293,7 @@ def test_public_addresses_rejects_transition_addresses_in_dns(address: str) -> N with patch("webmentions_ssg.net.dns.resolver.resolve_name") as resolve: resolve.return_value.addresses.return_value = ["93.184.216.34", address] with pytest.raises(UnsafeAddressError, match="Non-public"): - public_addresses("transition.example") + resolve_public_addresses("transition.example") @pytest.mark.parametrize("address", ["2002:0a00:0001::1", "::ffff:10.0.0.1"]) @@ -310,7 +310,7 @@ def test_public_addresses_rejects_mixed_dns_results() -> None: with patch("webmentions_ssg.net.dns.resolver.resolve_name") as resolve: resolve.return_value.addresses.return_value = ["93.184.216.34", "127.0.0.1"] with pytest.raises(UnsafeAddressError, match="Non-public"): - public_addresses("rebinding.example", lifetime=1.5) + resolve_public_addresses("rebinding.example", lifetime=1.5) resolve.assert_called_once_with("rebinding.example", lifetime=1.5) @@ -318,7 +318,7 @@ def test_public_addresses_rejects_empty_dns_result() -> None: with patch("webmentions_ssg.net.dns.resolver.resolve_name") as resolve: resolve.return_value.addresses.return_value = [] with pytest.raises(AddressResolutionError): - public_addresses("empty.example") + resolve_public_addresses("empty.example") @pytest.mark.parametrize( @@ -336,11 +336,11 @@ def test_public_addresses_rejects_empty_dns_result() -> None: pytest.param(600, False, id="outside-server-error-range"), ], ) -def test_temporary_http_status(app: Flask, status: int, expected: bool) -> None: +def test_is_retryable_status(app: Flask, status: int, expected: bool) -> None: _ = app from webmentions_ssg.tasks import discovery - assert discovery.is_temporary_status(status) is expected + assert discovery.is_retryable_status(status) is expected @pytest.mark.parametrize( @@ -352,8 +352,8 @@ def test_temporary_http_status(app: Flask, status: int, expected: bool) -> None: pytest.param("http://[::1", None, id="malformed-ipv6"), ], ) -def test_hostname_of(url: str, expected: str | None) -> None: - assert hostname_of(url) == expected +def test_normalized_hostname(url: str, expected: str | None) -> None: + assert normalized_hostname(url) == expected @pytest.mark.parametrize( @@ -370,5 +370,5 @@ def test_hostname_of(url: str, expected: str | None) -> None: ), ], ) -def test_content_type_of(headers: dict[str, str], expected: str) -> None: - assert content_type_of(httpx.Headers(headers)) == expected +def test_media_type_of(headers: dict[str, str], expected: str) -> None: + assert media_type_of(httpx.Headers(headers)) == expected diff --git a/tests/views/helpers.py b/tests/views/helpers.py index 1fe1ed2..2da2ace 100644 --- a/tests/views/helpers.py +++ b/tests/views/helpers.py @@ -40,7 +40,9 @@ def mock_endpoint_form( form.target.data = target form.errors = errors or {} - monkeypatch.setattr(views_module.forms, "EndpointForm", Mock(return_value=form)) + monkeypatch.setattr( + views_module.forms, "WebmentionSubmissionForm", Mock(return_value=form) + ) return form @@ -126,7 +128,9 @@ def mock_admin_action_form( form = Mock() form.validate_on_submit.return_value = valid - monkeypatch.setattr(views_module.forms, "AdminActionForm", Mock(return_value=form)) + monkeypatch.setattr( + views_module.forms, "ConfirmActionForm", Mock(return_value=form) + ) return form diff --git a/tests/views/test_admin.py b/tests/views/test_admin.py index a88fbfa..90d406b 100644 --- a/tests/views/test_admin.py +++ b/tests/views/test_admin.py @@ -35,7 +35,7 @@ def views_module(app: Flask) -> ModuleType: return module -@pytest.mark.parametrize("path", ["/sent", f"/sent/{uuid.uuid7()}"]) +@pytest.mark.parametrize("path", ["/sources", f"/sources/{uuid.uuid7()}"]) def test_sent_views_require_login(client: FlaskClient, path: str) -> None: response = client.get(path) @@ -47,7 +47,7 @@ def test_sent_lists_sources(app: Flask, client: FlaskClient) -> None: log_in(app, client) identifier = create_sent_source(app) - response = client.get("/sent") + response = client.get("/sources") assert response.status_code == HTTPStatus.OK @@ -55,14 +55,14 @@ def test_sent_lists_sources(app: Flask, client: FlaskClient) -> None: assert "blog/example/index.html" in html assert "https://dennisfink.me/blog/example/" in html - assert f"/sent/{identifier}" 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"/sent/{identifier}") + response = client.get(f"/sources/{identifier}") assert response.status_code == HTTPStatus.OK @@ -86,7 +86,7 @@ def test_sent_source_returns_404_for_unknown_source( app: Flask, client: FlaskClient ) -> None: log_in(app, client) - response = client.get(f"/sent/{uuid.uuid7()}") + response = client.get(f"/sources/{uuid.uuid7()}") assert response.status_code == HTTPStatus.NOT_FOUND @@ -205,7 +205,7 @@ def test_reverify_received_webmention( def test_rescan_sent_sources_requires_login(client: FlaskClient) -> None: - response = client.post("/sent/rescan") + response = client.post("/sources/rescan") assert response.status_code == HTTPStatus.FOUND assert "/login" in response.headers["Location"] @@ -224,7 +224,7 @@ def test_rescan_sent_sources( manual_scan_sources = Mock() monkeypatch.setattr(views_module, "manual_scan_sources", manual_scan_sources) - response = client.post("/sent/rescan") + response = client.post("/sources/rescan") assert response.status_code == ( HTTPStatus.FOUND if valid else HTTPStatus.BAD_REQUEST @@ -232,14 +232,14 @@ def test_rescan_sent_sources( form.validate_on_submit.assert_called_once_with() if valid: - assert response.headers["Location"].endswith("/sent") + 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"/sent/{uuid.uuid7()}/{uuid.uuid7()}/resend") + response = client.post(f"/sources/{uuid.uuid7()}/{uuid.uuid7()}/resend") assert response.status_code == HTTPStatus.FOUND assert "/login" in response.headers["Location"] @@ -255,7 +255,9 @@ def test_resend_sent_webmention_rejects_invalid_form( source_identifier, webmention_identifier = create_resendable_sent_webmention(app) mock_admin_action_form(views_module, monkeypatch, valid=False) - response = client.post(f"/sent/{source_identifier}/{webmention_identifier}/resend") + response = client.post( + f"/sources/{source_identifier}/{webmention_identifier}/resend" + ) assert response.status_code == HTTPStatus.BAD_REQUEST @@ -270,7 +272,7 @@ def test_resend_sent_webmention_returns_404_for_unknown_webmention( source_identifier = create_sent_source(app) mock_admin_action_form(views_module, monkeypatch, valid=True) - response = client.post(f"/sent/{source_identifier}/{uuid.uuid7()}/resend") + response = client.post(f"/sources/{source_identifier}/{uuid.uuid7()}/resend") assert response.status_code == HTTPStatus.NOT_FOUND @@ -287,7 +289,7 @@ def test_resend_sent_webmention_rejects_wrong_source( mock_admin_action_form(views_module, monkeypatch, valid=True) response = client.post( - f"/sent/{wrong_source_identifier}/{webmention_identifier}/resend" + f"/sources/{wrong_source_identifier}/{webmention_identifier}/resend" ) assert response.status_code == HTTPStatus.NOT_FOUND @@ -312,10 +314,12 @@ def test_resend_sent_webmention_reopens_current_revision( webmention.next_retry_at = datetime.now(UTC) + timedelta(hours=24) db.session.commit() - response = client.post(f"/sent/{source_identifier}/{webmention_identifier}/resend") + response = client.post( + f"/sources/{source_identifier}/{webmention_identifier}/resend" + ) assert response.status_code == HTTPStatus.FOUND - assert response.headers["Location"].endswith(f"/sent/{source_identifier}") + assert response.headers["Location"].endswith(f"/sources/{source_identifier}") with app.app_context(): webmention = db.session.get(SentWebmention, webmention_identifier) @@ -358,7 +362,7 @@ def test_admin_only_links_safe_existing_status_urls( session["_user_id"] = str(user_id) session["_fresh"] = True - response = client.get(f"/sent/{source_identifier}") + 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 diff --git a/tests/views/test_session.py b/tests/views/test_session.py index a91a6b2..a2d3268 100644 --- a/tests/views/test_session.py +++ b/tests/views/test_session.py @@ -434,3 +434,10 @@ def test_logout_ends_session(app: Flask, client: FlaskClient) -> None: protected_response = client.get("/received") assert protected_response.status_code == HTTPStatus.FOUND assert "/login" in protected_response.headers["Location"] + + +def test_index_redirects_to_received(client: FlaskClient) -> None: + response = client.get("/") + + assert response.status_code == HTTPStatus.FOUND + assert response.headers["Location"].endswith("/received") -- cgit v1.3.1