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 ++++++++++++++++++++++++++++++++++++ 11 files changed, 333 insertions(+), 328 deletions(-) delete mode 100644 tests/tasks/test_mentions.py create mode 100644 tests/tasks/test_target_matching.py (limited to 'tests/tasks') 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) -- cgit v1.3.1