diff options
45 files changed, 713 insertions, 635 deletions
diff --git a/contrib/webmentions-ssg.example.cfg b/contrib/webmentions-ssg.example.cfg index dead041..33f43af 100644 --- a/contrib/webmentions-ssg.example.cfg +++ b/contrib/webmentions-ssg.example.cfg @@ -46,15 +46,15 @@ SQLALCHEMY_TRACK_MODIFICATIONS = False LOG_FILE = "/var/log/webmentions-ssg.log" LOG_LEVEL = logging.INFO LOG_FORMAT = "[%(asctime)s]:%(levelname)s:%(name)s %(message)s" -LOGFILE_MAX_BYTES = 20_000_000 -LOGFILE_BACKUP_COUNT = 10 +LOG_FILE_MAX_BYTES = 20_000_000 +LOG_FILE_BACKUP_COUNT = 10 # --------------------------------------------------------------------------- # Receiving Webmentions # --------------------------------------------------------------------------- # Required: exact hostnames permitted in the 'target' field of an incoming # Webmention. Include each hostname you serve; wildcards are not supported. -ALLOWED_HOSTNAMES = {"example.com", "www.example.com"} +ALLOWED_TARGET_HOSTNAMES = {"example.com", "www.example.com"} # Maximum size of an incoming HTTP request body, including form encoding. # This global Flask limit also applies to login and administration routes. @@ -75,17 +75,17 @@ RECEIVED_RETENTION_SCHEDULE = "0 3 * * *" # validated DNS IPs, but deployment-level filtering is still necessary. # Limits for fetching/verifying incoming sources. -REQUEST_TIMEOUT = 5.0 # seconds per socket operation (both directions) -REQUEST_TOTAL_TIMEOUT = 30.0 # seconds overall, including DNS, redirects and body +REQUEST_TIMEOUT_SECONDS = 5.0 # seconds per socket operation (both directions) +REQUEST_TOTAL_TIMEOUT_SECONDS = 30.0 # seconds overall, including DNS, redirects and body MAX_REDIRECTS = 20 # also used for outgoing requests -MAX_SOURCE_BYTES = 1_000_000 +MAX_RECEIVED_SOURCE_BYTES = 1_000_000 # Requeue stuck verifications and lost queued tasks after 15 minutes. -VERIFICATION_TIMEOUT_SECONDS = 900 +VERIFICATION_STALE_AFTER_SECONDS = 900 # Max stale-row requeues before marking the Webmention failed. Manual # reverification or a new submission after the cooldown resets this counter. VERIFICATION_MAX_RECOVERIES = 3 # Huey cron expression: every five minutes (in UTC). -VERIFICATION_REAPER_SCHEDULE = "*/5 * * * *" +VERIFICATION_RECOVERY_SCHEDULE = "*/5 * * * *" # --------------------------------------------------------------------------- # Rate limiting @@ -121,13 +121,13 @@ LOGIN_USERNAME_RATE_LIMIT = "20/minute;100/hour" # --------------------------------------------------------------------------- # Folder containing generated pages named index.html. Set to None to disable # scanning. Its root-level index.html is not scanned. -SOURCE_DIRECTORY = "/var/www/example.com/public" +SITE_OUTPUT_DIRECTORY = "/var/www/example.com/public" # Needed for pages without absolute canonical URLs. -SOURCE_BASE_URL = "https://example.com/" +SITE_BASE_URL = "https://example.com/" # Huey cron expression: minute hour day-of-month month day-of-week. -SCANNER_SCHEDULE = "0 * * * *" # every hour +SOURCE_SCAN_SCHEDULE = "0 * * * *" # every hour # Temporary delivery failures use persisted exponential backoff: 1, 2, 4, # 8, then 16 hours between attempts with these defaults. After six total @@ -138,8 +138,8 @@ SEND_RETRY_BASE_SECONDS = 3600 SEND_RETRY_MAX_SECONDS = 86400 # Do not send Webmentions to your own site. Glob patterns are supported here. -IGNORED_HOSTNAMES = ("example.com", "*.example.com") -MAX_TARGET_BYTES = 1_000_000 +IGNORED_TARGET_HOSTNAMES = ("example.com", "*.example.com") +MAX_DISCOVERY_TARGET_BYTES = 1_000_000 # --------------------------------------------------------------------------- # Background tasks (Huey) @@ -174,10 +174,10 @@ HUEY_URL = "redis://127.0.0.1:6379/0" # --------------------------------------------------------------------------- # Email notifications for newly verified received Webmentions # --------------------------------------------------------------------------- -# Set NOTIFICATION_EMAIL = None to disable notification emails. -NOTIFICATION_EMAIL = "admin@example.com" +# Set DIGEST_EMAIL = None to disable notification emails. +DIGEST_EMAIL = "admin@example.com" # Daily notification digest at 08:00 UTC (Huey defaults to UTC). -NOTIFICATION_SCHEDULE = "0 8 * * *" +DIGEST_SCHEDULE = "0 8 * * *" # Example SMTP-over-STARTTLS setup; adapt to your mail provider. MAIL_SERVER = "smtp.example.com" 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", "<https://example.com/webmention") ) -def test_parse_link_value_rejects_malformed_value( +def test_parse_link_header_value_rejects_malformed_value( sender_module: ModuleType, value: str ) -> 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", "</webmention>; 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'<link rel="webmention alternate" href="/webmention">' - 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'<LINK REL="ALTERNATE WEBMENTION" HREF="/webmention">' - 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'<link href="/ignored"><a rel="webmention">No endpoint</a>' - 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_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"<p>Document</p>" @@ -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"<p>Document</p>" -@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( """ <article> @@ -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""" <article class="h-entry" id="source"> @@ -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""" <article class="h-entry"> @@ -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( """ <article class="h-entry"> @@ -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( """ <article class="h-entry"> @@ -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""" <html> @@ -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_mentions.py b/tests/tasks/test_target_matching.py index a5da56c..31523af 100644 --- a/tests/tasks/test_mentions.py +++ b/tests/tasks/test_target_matching.py @@ -15,9 +15,9 @@ TARGET_URL = "https://dennisfink.me/blog/example/" @pytest.fixture def receiver(app: Flask) -> ModuleType: _ = app - from webmentions_ssg.tasks import mentions + from webmentions_ssg.tasks import target_matching - return mentions + return target_matching @pytest.mark.parametrize( @@ -162,7 +162,7 @@ def test_text_mentions_target( assert receiver.text_mentions_target(body, target_url) is expected -@patch("webmentions_ssg.tasks.mentions.BeautifulSoup") +@patch("webmentions_ssg.tasks.target_matching.BeautifulSoup") def test_html_mentions_target_ignores_non_string_attribute( beautiful_soup: Mock, receiver: ModuleType ) -> None: 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") diff --git a/webmentions_ssg/__init__.py b/webmentions_ssg/__init__.py index 223f35d..8e87485 100644 --- a/webmentions_ssg/__init__.py +++ b/webmentions_ssg/__init__.py @@ -13,14 +13,19 @@ import coloredlogs from flask import Flask from werkzeug.middleware.proxy_fix import ProxyFix -from .auth import AUTH +from .auth import login_manager from .cli import DB_CLI, MAIL_CLI, USERS_CLI -from .config import DefaultConfig, DevelopmentConfig, ProductionConfig, validate_config +from .config import ( + DefaultConfig, + DevelopmentConfig, + ProductionConfig, + validated_config_overrides, +) from .extensions import bootstrap, csrf, db, huey, limiter, mail, migrate from .net import is_http_url from .version import APP_NAME as APP_NAME from .version import VERSION -from .views import root_page +from .views import webmentions_bp def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: @@ -61,7 +66,7 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: app.config.from_prefixed_env(prefix="FLASK") app.config.from_prefixed_env(prefix="WEBMENTIONS_SSG") - app.config.update(validate_config(app.config, config_class=config_class)) + app.config.update(validated_config_overrides(app.config, config_class=config_class)) if proxy_hops := app.config["PROXY_FIX_TRUSTED_HOPS"]: # Do not trust forwarded host, port, or URL prefixes. @@ -75,8 +80,8 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: log_file_handler = logging.handlers.RotatingFileHandler( app.config["LOG_FILE"], - maxBytes=app.config["LOGFILE_MAX_BYTES"], - backupCount=app.config["LOGFILE_BACKUP_COUNT"], + maxBytes=app.config["LOG_FILE_MAX_BYTES"], + backupCount=app.config["LOG_FILE_BACKUP_COUNT"], ) log_file_handler.setLevel(app.config["LOG_LEVEL"]) log_file_handler.setFormatter(default_logging_formatter) @@ -111,7 +116,7 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: limiter.init_app(app) mail.init_app(app) - AUTH.init_app(app) + login_manager.init_app(app) bootstrap.init_app(app) huey.init_app(app) @@ -125,7 +130,7 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: app.add_template_filter(is_http_url) - app.register_blueprint(root_page) + app.register_blueprint(webmentions_bp) app.cli.add_command(USERS_CLI) diff --git a/webmentions_ssg/auth.py b/webmentions_ssg/auth.py index 08abaa1..ce4960d 100644 --- a/webmentions_ssg/auth.py +++ b/webmentions_ssg/auth.py @@ -19,9 +19,9 @@ from .extensions import db from .models import User from .validation import is_control_character -AUTH = LoginManager() +login_manager = LoginManager() -AUTH.login_view = "root.login" # pyright: ignore[reportAttributeAccessIssue] +login_manager.login_view = "webmentions.login" # pyright: ignore[reportAttributeAccessIssue] MIN_PASSWORD_LENGTH = 8 @@ -38,7 +38,7 @@ def dummy_password_hash() -> str: return generate_password_hash(secrets.token_urlsafe()) -def validate_password(password: str) -> None: +def validate_new_password(password: str) -> None: """Validate newly chosen passwords without arbitrary composition rules. Allow Unicode, whitespace-only passwords, and arbitrarily long passphrases, @@ -58,8 +58,8 @@ def validate_password(password: str) -> None: raise ValueError("Password must not contain control characters.") -@AUTH.user_loader -def load_user(session_id: object) -> User | None: +@login_manager.user_loader +def load_user_from_session_id(session_id: object) -> User | None: """Load a user from the database by identifier. :param session_id: Database ID and password-hash fingerprint stored in the session. @@ -78,7 +78,7 @@ def load_user(session_id: object) -> User | None: return user -def login_username_limit_key() -> str: +def login_username_rate_limit_key() -> str: """Group usernames across IPs without exposing plaintext to rate-limit storage. :returns: Hashed username rate-limit key, or an IP-specific key if no username was diff --git a/webmentions_ssg/cli.py b/webmentions_ssg/cli.py index 5c2d534..225a39f 100644 --- a/webmentions_ssg/cli.py +++ b/webmentions_ssg/cli.py @@ -13,14 +13,14 @@ from flask_migrate import upgrade as migrate_upgrade from sqlalchemy import inspect from sqlalchemy.exc import IntegrityError -from .auth import validate_password +from .auth import validate_new_password from .extensions import db, mail from .models import User USERS_CLI = AppGroup("users", help="Manage users.") -def _require_user(username: str): +def _get_user_or_fail(username: str): """Return a user by username or report a CLI-friendly error. :param username: Username to look up in the database. @@ -48,7 +48,7 @@ def create_user(username: str, password: str) -> None: raise click.ClickException("Username must not be empty.") try: - validate_password(password) + validate_new_password(password) except ValueError as exc: raise click.ClickException(str(exc)) from exc @@ -89,11 +89,11 @@ def set_user_password(username: str, password: str) -> None: :raises click.ClickException: If the account is missing or the password is invalid. """ try: - validate_password(password) + validate_new_password(password) except ValueError as exc: raise click.ClickException(str(exc)) from exc - user = _require_user(username) + user = _get_user_or_fail(username) user.password = password db.session.commit() click.echo(f"Updated password for user {username!r}.") @@ -109,7 +109,7 @@ def delete_user(username: str, yes: bool) -> None: :param yes: Whether to skip the confirmation prompt. :raises click.ClickException: If the account does not exist. """ - user = _require_user(username) + user = _get_user_or_fail(username) if not yes: click.confirm(f"Delete user {username!r}?", abort=True) @@ -122,19 +122,17 @@ MAIL_CLI = AppGroup("mail", help="Handle mail") @MAIL_CLI.command("test", help="Send an email to test the SMTP configuration") -@click.option( - "--to", "recipient", help="Recipient address (defaults to NOTIFICATION_EMAIL)." -) +@click.option("--to", "recipient", help="Recipient address (defaults to DIGEST_EMAIL).") def send_test_mail(recipient: str | None) -> None: """Send a test email using the configured SMTP server. - :param recipient: Optional recipient overriding NOTIFICATION_EMAIL. + :param recipient: Optional recipient overriding DIGEST_EMAIL. :raises click.ClickException: If configuration is missing or sending fails. """ - recipient = recipient or current_app.config.get("NOTIFICATION_EMAIL") + recipient = recipient or current_app.config.get("DIGEST_EMAIL") if not recipient: raise click.ClickException( - "No recipient specified. Set NOTIFICATION_EMAIL or pass --to." + "No recipient specified. Set DIGEST_EMAIL or pass --to." ) sender = current_app.config.get("MAIL_DEFAULT_SENDER") @@ -162,7 +160,7 @@ def send_test_mail(recipient: str | None) -> None: DB_CLI = AppGroup("db", help="Handle database") -def _table_names() -> set[str]: +def _existing_table_names() -> set[str]: """Return existing table names without modifying the database schema. :returns: Names of tables in the configured database. @@ -179,7 +177,7 @@ def create_db() -> None: :raises click.ClickException: If the database already contains tables. """ - if _table_names(): + if _existing_table_names(): raise click.ClickException( "The database already contains tables. " "Use 'flask db upgrade' for an existing migrated database." @@ -198,7 +196,7 @@ def upgrade_db() -> None: :raises click.ClickException: If the database has no Alembic revision table. """ - if "alembic_version" not in _table_names(): + if "alembic_version" not in _existing_table_names(): raise click.ClickException( "No Flask-Migrate revision table found. " "For a new, empty database run 'flask db create-db'. " diff --git a/webmentions_ssg/config.py b/webmentions_ssg/config.py index 6e6b4bf..7d09e7e 100644 --- a/webmentions_ssg/config.py +++ b/webmentions_ssg/config.py @@ -26,12 +26,12 @@ class DefaultConfig: LOG_FILE = "/var/log/webmentions-ssg.log" LOG_FORMAT = "[%(asctime)s]:%(levelname)s:%(name)s %(message)s" LOG_LEVEL = logging.INFO - LOGFILE_MAX_BYTES = 20_000_000 - LOGFILE_BACKUP_COUNT = 10 + LOG_FILE_MAX_BYTES = 20_000_000 + LOG_FILE_BACKUP_COUNT = 10 SQLALCHEMY_DATABASE_URI = "sqlite:///webmentions-ssg.db" # Empty by default: no target hostnames are accepted until configured. - ALLOWED_HOSTNAMES = frozenset() + ALLOWED_TARGET_HOSTNAMES = frozenset() # Cap incoming HTTP request bodies (also applies to other Flask routes). MAX_CONTENT_LENGTH = 64 * 1024 # Independent UTF-8 byte limit for each submitted source/target URL. @@ -40,28 +40,28 @@ class DefaultConfig: RECEIVED_RETENTION_DAYS = 30 RECEIVED_RETENTION_SCHEDULE = "0 3 * * *" MAX_REDIRECTS = 20 - MAX_SOURCE_BYTES = 1_000_000 - MAX_TARGET_BYTES = 1_000_000 - REQUEST_TIMEOUT = 5.0 + MAX_RECEIVED_SOURCE_BYTES = 1_000_000 + MAX_DISCOVERY_TARGET_BYTES = 1_000_000 + REQUEST_TIMEOUT_SECONDS = 5.0 # Absolute wall-clock budget for each verification or delivery attempt. - REQUEST_TOTAL_TIMEOUT = 30.0 + REQUEST_TOTAL_TIMEOUT_SECONDS = 30.0 # Requeue abandoned verification attempts after 15 minutes. - VERIFICATION_TIMEOUT_SECONDS = 900 + VERIFICATION_STALE_AFTER_SECONDS = 900 # Maximum automatic recoveries of a stale received/verifying row. VERIFICATION_MAX_RECOVERIES = 3 - VERIFICATION_REAPER_SCHEDULE = "*/5 * * * *" + VERIFICATION_RECOVERY_SCHEDULE = "*/5 * * * *" - SOURCE_DIRECTORY = None - SOURCE_BASE_URL = None - IGNORED_HOSTNAMES = () - SCANNER_SCHEDULE = "0 * * * *" + SITE_OUTPUT_DIRECTORY = None + SITE_BASE_URL = None + IGNORED_TARGET_HOSTNAMES = () + SOURCE_SCAN_SCHEDULE = "0 * * * *" # Total send attempts per source revision (not additional retries). SEND_MAX_ATTEMPTS = 6 SEND_RETRY_BASE_SECONDS = 3600 SEND_RETRY_MAX_SECONDS = 86400 - NOTIFICATION_EMAIL = None - NOTIFICATION_SCHEDULE = "0 8 * * *" + DIGEST_EMAIL = None + DIGEST_SCHEDULE = "0 8 * * *" MAIL_SERVER = "localhost" MAIL_PORT = 25 @@ -116,7 +116,7 @@ class TestingConfig(DefaultConfig): HUEY_URL = "memory://" - ALLOWED_HOSTNAMES = frozenset({"dennisfink.me"}) + ALLOWED_TARGET_HOSTNAMES = frozenset({"dennisfink.me"}) def normalize_hostnames(value: object) -> set[str]: @@ -177,7 +177,7 @@ def validate_secret_key(value: object) -> str | bytes: return value -def validate_config( +def validated_config_overrides( config: Config, *, config_class: type[DefaultConfig] ) -> dict[str, object]: """Validate runtime configuration and return normalized values. @@ -194,7 +194,7 @@ def validate_config( unsafe. """ normalized: dict[str, object] = {} - for key in ("ALLOWED_HOSTNAMES", "IGNORED_HOSTNAMES"): + for key in ("ALLOWED_TARGET_HOSTNAMES", "IGNORED_TARGET_HOSTNAMES"): try: normalized[key] = normalize_hostnames(config[key]) except (TypeError, ValueError) as exc: @@ -221,9 +221,9 @@ def validate_config( normalized["SECRET_KEY"] = validate_secret_key(config.get("SECRET_KEY")) if not config["DEBUG"] and not config["TESTING"]: - if not normalized["ALLOWED_HOSTNAMES"]: + if not normalized["ALLOWED_TARGET_HOSTNAMES"]: raise RuntimeError( - "Configure ALLOWED_HOSTNAMES before running in production." + "Configure ALLOWED_TARGET_HOSTNAMES before running in production." ) if config["RATELIMIT_STORAGE_URI"] == "memory://": raise RuntimeError( diff --git a/webmentions_ssg/forms.py b/webmentions_ssg/forms.py index e3dcdc0..c8ad0ba 100644 --- a/webmentions_ssg/forms.py +++ b/webmentions_ssg/forms.py @@ -13,11 +13,11 @@ from wtforms import Field, PasswordField, StringField, SubmitField, ValidationEr from wtforms.form import BaseForm from wtforms.validators import InputRequired, StopValidation -from .net import hostname_of, is_public_address +from .net import is_public_address, normalized_hostname from .validation import is_control_character -class URLMaxBytes: +class MaxURLBytes: """Check the UTF-8 byte length of a submitted URL using the current config.""" def __init__( @@ -43,7 +43,7 @@ class URLMaxBytes: ) -class HTTPURL: +class StrictHTTPURL: """Require an HTTP(S) URL that httpx can parse without rewriting whitespace. httpx automatically percent-encodes some whitespace. Reject it instead of @@ -75,7 +75,7 @@ class HTTPURL: ) -class AllowedHostname: +class AllowedTargetHostname: """Validate that a URL uses an allowed hostname. :param message: Optional validation error message. @@ -95,8 +95,11 @@ class AllowedHostname: :param field: Field containing the URL to validate. :raises ValidationError: If the URL hostname is not configured as allowed. """ - hostname = hostname_of(field.data) - if hostname is not None and hostname in current_app.config["ALLOWED_HOSTNAMES"]: + hostname = normalized_hostname(field.data) + if ( + hostname is not None + and hostname in current_app.config["ALLOWED_TARGET_HOSTNAMES"] + ): return raise ValidationError(self.message or field.gettext("Invalid input.")) @@ -105,7 +108,7 @@ class PublicIPLiteral: """Reject non-public IP literals without performing DNS in the web worker.""" def __call__(self, form: BaseForm, field: Field) -> None: - hostname = hostname_of(field.data) + hostname = normalized_hostname(field.data) if hostname is None: raise ValidationError(field.gettext("Invalid input.")) @@ -126,7 +129,7 @@ class LoginForm(FlaskForm): submit = SubmitField("Sign In") -class EndpointForm(FlaskForm): +class WebmentionSubmissionForm(FlaskForm): """Form for receiving a Webmention. The source must be a public HTTP or HTTPS URL distinct from the target. @@ -135,11 +138,16 @@ class EndpointForm(FlaskForm): source = StringField( "source", - validators=[InputRequired(), URLMaxBytes(), HTTPURL(), PublicIPLiteral()], + validators=[InputRequired(), MaxURLBytes(), StrictHTTPURL(), PublicIPLiteral()], ) target = StringField( "target", - validators=[InputRequired(), URLMaxBytes(), HTTPURL(), AllowedHostname()], + validators=[ + InputRequired(), + MaxURLBytes(), + StrictHTTPURL(), + AllowedTargetHostname(), + ], ) def validate_source(self, field: Field) -> None: @@ -152,7 +160,7 @@ class EndpointForm(FlaskForm): raise ValidationError(field.gettext("Field must not be equal to target.")) -class AdminActionForm(FlaskForm): +class ConfirmActionForm(FlaskForm): """Form for confirming an administrative action.""" submit = SubmitField("Confirm") diff --git a/webmentions_ssg/net.py b/webmentions_ssg/net.py index 262434a..b827180 100644 --- a/webmentions_ssg/net.py +++ b/webmentions_ssg/net.py @@ -21,7 +21,7 @@ from httpcore._backends.sync import SyncStream NAT64_PREFIX = ipaddress.IPv6Network("64:ff9b::/96") IPV4_COMPATIBLE_PREFIX = ipaddress.IPv6Network("::/96") -NETWORK_CONNECT_TIMOUT_ERROR = 599 +NETWORK_CONNECT_TIMEOUT_ERROR = 599 class AddressResolutionError(Exception): @@ -36,16 +36,18 @@ class ResponseTooLargeError(ValueError): """A response body exceeds its configured size limit.""" -class NetworkDeadline: +class RequestDeadline: """One monotonic deadline shared by DNS, TCP, TLS, and response streaming.""" def __init__(self, seconds: float) -> None: if seconds <= 0: - raise ValueError("REQUEST_TOTAL_TIMEOUT must be greater than zero") - self.expires = time.monotonic() + seconds + raise ValueError("REQUEST_TOTAL_TIMEOUT_SECONDS must be greater than zero") + self.expires_at = time.monotonic() + seconds - def timeout(self, original: float | None, error: type[Exception]) -> float: - remaining = self.expires - time.monotonic() + def remaining_timeout( + self, original: float | None, error: type[Exception] + ) -> float: + remaining = self.expires_at - time.monotonic() if remaining <= 0: raise error("Overall HTTP request deadline exceeded") return min(remaining, original) if original is not None else remaining @@ -54,18 +56,20 @@ class NetworkDeadline: class DeadlineStream(httpcore.NetworkStream): """Clip every read/write/TLS operation to the remaining overall deadline.""" - def __init__(self, stream: httpcore.NetworkStream, deadline: NetworkDeadline): + def __init__(self, stream: httpcore.NetworkStream, deadline: RequestDeadline): self._stream = stream self._deadline = deadline def read(self, max_bytes: int, timeout: float | None = None) -> bytes: return self._stream.read( - max_bytes, timeout=self._deadline.timeout(timeout, httpcore.ReadTimeout) + max_bytes, + timeout=self._deadline.remaining_timeout(timeout, httpcore.ReadTimeout), ) def write(self, buffer: bytes, timeout: float | None = None) -> None: self._stream.write( - buffer, timeout=self._deadline.timeout(timeout, httpcore.WriteTimeout) + buffer, + timeout=self._deadline.remaining_timeout(timeout, httpcore.WriteTimeout), ) def start_tls( @@ -78,7 +82,9 @@ class DeadlineStream(httpcore.NetworkStream): self._stream.start_tls( ssl_context, server_hostname=server_hostname, - timeout=self._deadline.timeout(timeout, httpcore.ConnectTimeout), + timeout=self._deadline.remaining_timeout( + timeout, httpcore.ConnectTimeout + ), ), self._deadline, ) @@ -93,10 +99,10 @@ class DeadlineStream(httpcore.NetworkStream): class PublicNetworkBackend(httpcore.SyncBackend): """Resolve and validate in the TCP connection operation, never in a hook.""" - def __init__(self, deadline: NetworkDeadline): + def __init__(self, deadline: RequestDeadline): self._deadline = deadline - def _connect_one( + def _connect_to_address( self, address: str, port: int, @@ -115,7 +121,9 @@ class PublicNetworkBackend(httpcore.SyncBackend): with ExitStack() as cleanup: sock = socket.socket(family, socket.SOCK_STREAM) cleanup.callback(sock.close) - sock.settimeout(self._deadline.timeout(timeout, httpcore.ConnectTimeout)) + sock.settimeout( + self._deadline.remaining_timeout(timeout, httpcore.ConnectTimeout) + ) if local_address is not None: sock.bind((local_address, 0)) @@ -143,13 +151,13 @@ class PublicNetworkBackend(httpcore.SyncBackend): ) -> httpcore.NetworkStream: # No second lookup of `host` is permitted. Even a DNS response that # changes immediately after this check cannot change the socket peer. - dns_timeout = self._deadline.timeout(timeout, httpcore.ConnectTimeout) - addresses = public_addresses(host, lifetime=dns_timeout) + dns_timeout = self._deadline.remaining_timeout(timeout, httpcore.ConnectTimeout) + addresses = resolve_public_addresses(host, lifetime=dns_timeout) last_error: httpcore.ConnectError | httpcore.ConnectTimeout | None = None for address in addresses: try: - return self._connect_one( + return self._connect_to_address( address, port, timeout, local_address, socket_options ) except OSError as exc: @@ -174,11 +182,13 @@ class PublicHTTPTransport(httpx.HTTPTransport): def __init__(self, total_timeout: float): self._pool = httpcore.ConnectionPool( ssl_context=httpx.create_ssl_context(verify=True, trust_env=False), - network_backend=PublicNetworkBackend(NetworkDeadline(total_timeout)), + network_backend=PublicNetworkBackend(RequestDeadline(total_timeout)), ) -def build_client(user_agent: str, *, accept: str | None = None) -> httpx.Client: +def build_public_http_client( + user_agent: str, *, accept: str | None = None +) -> httpx.Client: """Return the hardened HTTP client shared by receiver and sender. :param user_agent: User-Agent header sent with each request. @@ -192,11 +202,13 @@ def build_client(user_agent: str, *, accept: str | None = None) -> httpx.Client: return httpx.Client( headers=headers, - timeout=httpx.Timeout(current_app.config["REQUEST_TIMEOUT"]), + timeout=httpx.Timeout(current_app.config["REQUEST_TIMEOUT_SECONDS"]), follow_redirects=True, max_redirects=current_app.config["MAX_REDIRECTS"], trust_env=False, - transport=PublicHTTPTransport(current_app.config["REQUEST_TOTAL_TIMEOUT"]), + transport=PublicHTTPTransport( + current_app.config["REQUEST_TOTAL_TIMEOUT_SECONDS"] + ), ) @@ -237,7 +249,7 @@ def resolve_hostname( ) from exc -def public_addresses( +def resolve_public_addresses( hostname: str, *, lifetime: float | None = None ) -> tuple[str, ...]: """Resolve *once* and return only checked, globally routable IP addresses. @@ -267,7 +279,7 @@ def public_addresses( return tuple(str(address) for address in addresses) -def hostname_of(url: str) -> str | None: +def normalized_hostname(url: str) -> str | None: """Return a normalized URL hostname, or None for malformed URLs. :param url: URL from which to extract a hostname. @@ -280,7 +292,7 @@ def hostname_of(url: str) -> str | None: return hostname.rstrip(".") if hostname is not None else None -def content_type_of(headers: Mapping[str, str]) -> str: +def media_type_of(headers: Mapping[str, str]) -> str: """Return a normalized media type without Content-Type parameters. :param headers: HTTP response headers to inspect. @@ -330,7 +342,7 @@ def read_limited_body(response: httpx.Response, max_bytes: int, *, label: str) - return bytes(body) -def is_temporary_status(status: int) -> bool: +def is_retryable_status(status: int) -> bool: """Return whether an HTTP status is normally retryable. :param status: HTTP response status code to classify. @@ -343,5 +355,5 @@ def is_temporary_status(status: int) -> bool: HTTPStatus.TOO_EARLY, HTTPStatus.TOO_MANY_REQUESTS, } - or HTTPStatus.INTERNAL_SERVER_ERROR <= status <= NETWORK_CONNECT_TIMOUT_ERROR + or HTTPStatus.INTERNAL_SERVER_ERROR <= status <= NETWORK_CONNECT_TIMEOUT_ERROR ) diff --git a/webmentions_ssg/tasks/consumer.py b/webmentions_ssg/tasks/consumer.py index 13334bc..c24a883 100644 --- a/webmentions_ssg/tasks/consumer.py +++ b/webmentions_ssg/tasks/consumer.py @@ -22,9 +22,9 @@ def create_consumer() -> Huey: app = create_app() for config_key, task in ( - ("SCANNER_SCHEDULE", scanner.scan_sources), - ("VERIFICATION_REAPER_SCHEDULE", receiver.recover_stale_webmentions), - ("NOTIFICATION_SCHEDULE", notifications.send_webmention_digest), + ("SOURCE_SCAN_SCHEDULE", scanner.scan_sources), + ("VERIFICATION_RECOVERY_SCHEDULE", receiver.recover_stale_verifications), + ("DIGEST_SCHEDULE", notifications.send_webmention_digest), ("RECEIVED_RETENTION_SCHEDULE", retention.purge_old_received_webmentions), ): schedule = crontab(*app.config[config_key].split(), strict=True) diff --git a/webmentions_ssg/tasks/discovery.py b/webmentions_ssg/tasks/discovery.py index 6d01e53..c683cb0 100644 --- a/webmentions_ssg/tasks/discovery.py +++ b/webmentions_ssg/tasks/discovery.py @@ -14,48 +14,48 @@ from flask import current_app from ..net import ( ResponseTooLargeError, - content_type_of, is_http_url, - is_temporary_status, + is_retryable_status, + media_type_of, read_limited_body, ) -LINK_SPLIT = re.compile(r",\s*(?=<)") +LINK_HEADER_SPLIT = re.compile(r",\s*(?=<)") DISCOVERY_HEADERS = {"Accept": "text/html, application/xhtml+xml;q=0.9"} -class SenderError(Exception): +class DeliveryError(Exception): """A Webmention cannot be sent.""" -class TemporarySenderError(SenderError): +class RetryableDeliveryError(DeliveryError): """A Webmention send failed for a potentially temporary reason.""" -class PermanentSenderError(SenderError): +class PermanentDeliveryError(DeliveryError): """A Webmention send failed permanently for this source revision.""" -def resolve_endpoint(response: httpx.Response, href: str) -> str: +def resolve_endpoint_url(response: httpx.Response, href: str) -> str: """Resolve and validate a discovered Webmention endpoint. :param response: Response from which the endpoint was discovered. :param href: Endpoint reference to resolve. :returns: Absolute Webmention endpoint URL. - :raises PermanentSenderError: If the resolved endpoint is not an HTTP or HTTPS URL. + :raises PermanentDeliveryError: If the resolved endpoint is not an HTTP or HTTPS URL. """ try: endpoint = urljoin(str(response.url), href.strip()) except ValueError as exc: - raise PermanentSenderError(f"Invalid Webmention endpoint: {href!r}") from exc + raise PermanentDeliveryError(f"Invalid Webmention endpoint: {href!r}") from exc if not is_http_url(endpoint): - raise PermanentSenderError(f"Invalid Webmention endpoint: {endpoint!r}") + raise PermanentDeliveryError(f"Invalid Webmention endpoint: {endpoint!r}") return endpoint -def parse_link_value(value: str) -> tuple[str, set[str]] | None: +def parse_link_header_value(value: str) -> tuple[str, set[str]] | None: """Parse a Link header value into its target and relations. :param value: Link header value to parse. @@ -84,7 +84,7 @@ def parse_link_value(value: str) -> tuple[str, set[str]] | None: return href, relations -def endpoint_from_headers(response: httpx.Response) -> str | None: +def find_endpoint_in_headers(response: httpx.Response) -> str | None: """Find a Webmention endpoint in the response Link headers. :param response: HTTP response whose headers should be inspected. @@ -92,22 +92,22 @@ def endpoint_from_headers(response: httpx.Response) -> str | None: Invalid candidates are ignored. """ for header in response.headers.get_list("Link"): - for value in LINK_SPLIT.split(header): - if (link := parse_link_value(value)) is None: + for value in LINK_HEADER_SPLIT.split(header): + if (link := parse_link_header_value(value)) is None: continue href, relations = link if "webmention" in relations: try: - return resolve_endpoint(response, href) - except PermanentSenderError: + return resolve_endpoint_url(response, href) + except PermanentDeliveryError: continue return None -def endpoint_from_html(response: httpx.Response, body: bytes) -> str | None: +def find_endpoint_in_html(response: httpx.Response, body: bytes) -> str | None: """Find a Webmention endpoint in an HTML document. ``link`` and ``a`` elements are inspected in document order. @@ -123,8 +123,8 @@ def endpoint_from_html(response: httpx.Response, body: bytes) -> str | None: 'link[rel~="webmention" i][href], a[rel~="webmention" i][href]' ): try: - return resolve_endpoint(response, str(element["href"])) - except PermanentSenderError: + return resolve_endpoint_url(response, str(element["href"])) + except PermanentDeliveryError: continue return None @@ -135,14 +135,14 @@ def read_target_body(response: httpx.Response) -> bytes: :param response: HTTP response to read with the configured target-size limit. :returns: Response body, provided it is within the target-size limit. - :raises PermanentSenderError: If the target document is too large. + :raises PermanentDeliveryError: If the target document is too large. """ try: return read_limited_body( - response, current_app.config["MAX_TARGET_BYTES"], label="Target" + response, current_app.config["MAX_DISCOVERY_TARGET_BYTES"], label="Target" ) except ResponseTooLargeError as exc: - raise PermanentSenderError(str(exc)) from exc + raise PermanentDeliveryError(str(exc)) from exc def discover_webmention_endpoint(client: httpx.Client, target: str) -> str | None: @@ -155,8 +155,8 @@ def discover_webmention_endpoint(client: httpx.Client, target: str) -> str | Non :param client: HTTP client to use for discovery requests. :param target: Target URL whose Webmention endpoint should be discovered. :returns: Discovered Webmention endpoint, or ``None`` if none is advertised. - :raises TemporarySenderError: If the target returns a temporary HTTP failure. - :raises PermanentSenderError: If GET returns a permanent HTTP failure. + :raises RetryableDeliveryError: If the target returns a temporary HTTP failure. + :raises PermanentDeliveryError: If GET returns a permanent HTTP failure. """ try: head_response = client.head(target, headers=DISCOVERY_HEADERS) @@ -165,25 +165,25 @@ def discover_webmention_endpoint(client: httpx.Client, target: str) -> str | Non # or drop it even when fetching the document with GET would succeed. pass else: - if endpoint := endpoint_from_headers(head_response): + if endpoint := find_endpoint_in_headers(head_response): return endpoint with client.stream("GET", target, headers=DISCOVERY_HEADERS) as response: - if endpoint := endpoint_from_headers(response): + if endpoint := find_endpoint_in_headers(response): return endpoint - if is_temporary_status(response.status_code): - raise TemporarySenderError(f"Target returned HTTP {response.status_code}") + if is_retryable_status(response.status_code): + raise RetryableDeliveryError(f"Target returned HTTP {response.status_code}") if not response.is_success: - raise PermanentSenderError(f"Target returned HTTP {response.status_code}") + raise PermanentDeliveryError(f"Target returned HTTP {response.status_code}") - media_type = content_type_of(response.headers) + media_type = media_type_of(response.headers) if media_type not in {"text/html", "application/xhtml+xml"}: return None - return endpoint_from_html(response, read_target_body(response)) + return find_endpoint_in_html(response, read_target_body(response)) def post_webmention( diff --git a/webmentions_ssg/tasks/extension.py b/webmentions_ssg/tasks/extension.py index 58e0036..faed364 100644 --- a/webmentions_ssg/tasks/extension.py +++ b/webmentions_ssg/tasks/extension.py @@ -15,10 +15,12 @@ from flask import Flask from huey import Huey as BaseHuey from huey.api import TaskWrapper -from .json_serializer import JSONSerializer +from .json_serializer import TaggedJSONSerializer -def _redis_backend(parsed: SplitResult) -> tuple[type[BaseHuey], dict[str, Any]]: +def _redis_class_and_options( + parsed: SplitResult, +) -> tuple[type[BaseHuey], dict[str, Any]]: """Select a Redis Huey flavour while retaining the underlying URL scheme. :param parsed: Parsed Redis backend URL, including any flavour suffix. @@ -47,7 +49,9 @@ def _redis_backend(parsed: SplitResult) -> tuple[type[BaseHuey], dict[str, Any]] return backends[flavour], {"url": fixed_url} -def _path_backend(url: str, scheme: str) -> tuple[type[BaseHuey], dict[str, Any]]: +def _file_class_and_options( + url: str, scheme: str +) -> tuple[type[BaseHuey], dict[str, Any]]: """Extract a relative or absolute SQLite/file storage path. :param url: Backend URL containing the storage filename or path. @@ -86,7 +90,7 @@ def _path_backend(url: str, scheme: str) -> tuple[type[BaseHuey], dict[str, Any] return backend, {option: path} -class _LazyTask: +class _DeferredTask: """Stable public task handle which follows the extension's current backend. Huey creates task classes and registers them when its decorator is applied. @@ -111,7 +115,7 @@ class _LazyTask: self._registered: TaskWrapper | None = None update_wrapper(self, func) - def register(self) -> None: + def bind(self) -> None: """Bind the original function to the latest initialized backend. Register the task decorator again so its stable handle uses the currently @@ -120,10 +124,10 @@ class _LazyTask: backend = self._extension.huey decorator = backend.periodic_task if self._periodic else backend.task self._registered = decorator(*self._args, **self._kwargs)( - self._extension._in_app_context(self._func) + self._extension._with_app_context(self._func) ) - def _current(self) -> TaskWrapper: + def _bound_task(self) -> TaskWrapper: if self._registered is None: raise RuntimeError( "Huey has not been initialized. Call huey.init_app(app) first." @@ -137,7 +141,7 @@ class _LazyTask: :param kwargs: Keyword arguments passed to the queued task. :returns: Result of enqueueing the task on the current backend. """ - return self._current()(*args, **kwargs) + return self._bound_task()(*args, **kwargs) def __getattr__(self, name: str) -> Any: """Delegate Huey task operations (call_local, schedule, s, etc.). @@ -145,7 +149,7 @@ class _LazyTask: :param name: Huey task attribute to delegate. :returns: Requested attribute of the currently bound Huey task. """ - return getattr(self._current(), name) + return getattr(self._bound_task(), name) class Huey: @@ -162,7 +166,7 @@ class Huey: """ self.app: Flask | None = None self._huey: BaseHuey | None = None - self._tasks: list[_LazyTask] = [] + self._task_handles: list[_DeferredTask] = [] if app is not None: self.init_app(app) @@ -192,7 +196,7 @@ class Huey: raise RuntimeError( "HUEY.serializer cannot be overridden; JSON is required." ) - config["serializer"] = JSONSerializer( + config["serializer"] = TaggedJSONSerializer( compression=config.pop("compression", False), use_zlib=config.pop("use_zlib", False), ) @@ -205,8 +209,8 @@ class Huey: # Import order is immaterial: tasks declared before initialization are # registered now, and previously imported handles follow this backend. - for task in self._tasks: - task.register() + for task in self._task_handles: + task.bind() app.extensions["huey"] = self @@ -224,7 +228,7 @@ class Huey: ) return self._huey - def _in_app_context[**P, R](self, func: Callable[P, R]) -> Callable[P, R]: + def _with_app_context[**P, R](self, func: Callable[P, R]) -> Callable[P, R]: """Return a task wrapper that executes with the configured Flask app. :param func: Task function to wrap in a Flask application context. @@ -241,23 +245,23 @@ class Huey: return wrapped - def _decorate_task[**P, R]( + def _register_task[**P, R]( self, func: Callable[P, R], task_args: tuple[Any, ...], task_kwargs: dict[str, Any], *, periodic: bool, - ) -> _LazyTask: - handle = _LazyTask(self, func, task_args, task_kwargs, periodic=periodic) - self._tasks.append(handle) + ) -> _DeferredTask: + handle = _DeferredTask(self, func, task_args, task_kwargs, periodic=periodic) + self._task_handles.append(handle) if self._huey is not None: - handle.register() + handle.bind() return handle def task[**P, R]( self, *task_args: Any, **task_kwargs: Any - ) -> Callable[[Callable[P, R]], _LazyTask]: + ) -> Callable[[Callable[P, R]], _DeferredTask]: """Declare a task even before :meth:`init_app` has run. :param task_args: Positional arguments for the Huey task decorator. @@ -265,14 +269,14 @@ class Huey: :returns: Decorator that registers and returns a lazily bound task. """ - def decorator(func: Callable[P, R]) -> _LazyTask: - return self._decorate_task(func, task_args, task_kwargs, periodic=False) + def decorator(func: Callable[P, R]) -> _DeferredTask: + return self._register_task(func, task_args, task_kwargs, periodic=False) return decorator def periodic_task[**P, R]( self, *task_args: Any, **task_kwargs: Any - ) -> Callable[[Callable[P, R]], _LazyTask]: + ) -> Callable[[Callable[P, R]], _DeferredTask]: """Declare a periodic task against the current or next backend. :param task_args: Positional arguments for the Huey periodic-task decorator. @@ -280,8 +284,8 @@ class Huey: :returns: Decorator that registers and returns a lazily bound periodic task. """ - def decorator(func: Callable[P, R]) -> _LazyTask: - return self._decorate_task(func, task_args, task_kwargs, periodic=True) + def decorator(func: Callable[P, R]) -> _DeferredTask: + return self._register_task(func, task_args, task_kwargs, periodic=True) return decorator @@ -309,9 +313,9 @@ class Huey: scheme = parsed.scheme.lower() if scheme.partition("+")[0] in {"redis", "rediss"}: - return _redis_backend(parsed) + return _redis_class_and_options(parsed) if scheme in {"sqlite", "file"}: - return _path_backend(url, scheme) + return _file_class_and_options(url, scheme) if scheme in {"postgres", "postgresql"}: from huey import PostgresHuey diff --git a/webmentions_ssg/tasks/json_serializer.py b/webmentions_ssg/tasks/json_serializer.py index 7e51358..804cfee 100644 --- a/webmentions_ssg/tasks/json_serializer.py +++ b/webmentions_ssg/tasks/json_serializer.py @@ -14,7 +14,7 @@ from huey.serializer import Serializer from huey.utils import SKIPPED, Error -class JSONSerializer(Serializer): +class TaggedJSONSerializer(Serializer): """A deliberately restricted, non-pickle serializer for our Huey tasks. JSON has no native UUID, datetime, tuple or Huey message representation. diff --git a/webmentions_ssg/tasks/notifications.py b/webmentions_ssg/tasks/notifications.py index b1762d5..eb540de 100644 --- a/webmentions_ssg/tasks/notifications.py +++ b/webmentions_ssg/tasks/notifications.py @@ -22,16 +22,16 @@ def send_webmention_digest() -> None: """ # Resolve the lock against the current Huey backend, not the import-time one. with huey.lock_task("send-webmention-digest"): - _send_webmention_digest() + _send_digest_locked() -def _send_webmention_digest() -> None: +def _send_digest_locked() -> None: """Do the work while the current backend's digest lock is held. Collect verified mentions, send the configured message, and persist notification timestamps. """ - recipient = current_app.config.get("NOTIFICATION_EMAIL") + recipient = current_app.config.get("DIGEST_EMAIL") if not recipient: return diff --git a/webmentions_ssg/tasks/receiver.py b/webmentions_ssg/tasks/receiver.py index eb228b3..313a8e4 100644 --- a/webmentions_ssg/tasks/receiver.py +++ b/webmentions_ssg/tasks/receiver.py @@ -20,13 +20,13 @@ from ..net import ( AddressResolutionError, ResponseTooLargeError, UnsafeAddressError, - build_client, - content_type_of, - is_temporary_status, + build_public_http_client, + is_retryable_status, + media_type_of, read_limited_body, ) from ..version import APP_NAME, VERSION -from .mentions import html_mentions_target, text_mentions_target +from .target_matching import html_mentions_target, text_mentions_target class VerificationError(Exception): @@ -37,14 +37,14 @@ class SourceGoneError(VerificationError): """The source explicitly reports that it has been removed.""" -class TemporaryFetchError(Exception): +class RetryableFetchError(Exception): """Fetching the source may succeed when retried later.""" RECOVERY_EXHAUSTED_REASON = "Verification recovery limit reached" -def fetch_source(source_url: str) -> tuple[httpx.Response, bytes]: +def fetch_source_document(source_url: str) -> tuple[httpx.Response, bytes]: """Fetch a webmention source document. The request follows redirects while enforcing the configured timeout, @@ -53,13 +53,13 @@ def fetch_source(source_url: str) -> tuple[httpx.Response, bytes]: :param source_url: URL of the source document. :returns: HTTP response and response body. :raises SourceGoneError: If the source returns HTTP 410. - :raises TemporaryFetchError: If the source returns a temporary HTTP error. + :raises RetryableFetchError: If the source returns a temporary HTTP error. :raises VerificationError: If the source returns another unsuccessful HTTP status or exceeds the configured maximum response size. """ try: with ( - build_client( + build_public_http_client( f"{APP_NAME}/{VERSION} ReceivedWebmention", accept="text/html, application/xhtml+xml;q=0.9, text/plain;q=0.8", ) as client, @@ -69,8 +69,8 @@ def fetch_source(source_url: str) -> tuple[httpx.Response, bytes]: pass elif response.status_code == HTTPStatus.GONE: raise SourceGoneError("Source returned HTTP 410") - elif is_temporary_status(response.status_code): - raise TemporaryFetchError( + elif is_retryable_status(response.status_code): + raise RetryableFetchError( f"Source returned HTTP {response.status_code}" ) else: @@ -78,7 +78,9 @@ def fetch_source(source_url: str) -> tuple[httpx.Response, bytes]: try: return response, read_limited_body( - response, current_app.config["MAX_SOURCE_BYTES"], label="Source" + response, + current_app.config["MAX_RECEIVED_SOURCE_BYTES"], + label="Source", ) except ResponseTooLargeError as exc: raise VerificationError(str(exc)) from exc @@ -86,7 +88,7 @@ def fetch_source(source_url: str) -> tuple[httpx.Response, bytes]: except UnsafeAddressError as exc: raise VerificationError("Source resolves to a non-public address") from exc except AddressResolutionError as exc: - raise TemporaryFetchError("Source hostname could not be resolved") from exc + raise RetryableFetchError("Source hostname could not be resolved") from exc def source_mentions_target(source_url: str, target_url: str) -> bool: @@ -100,11 +102,11 @@ def source_mentions_target(source_url: str, target_url: str) -> bool: :returns: Whether the source mentions the target URL. :raises VerificationError: If the source has an unsupported or missing content type. :raises SourceGoneError: If the source explicitly reports that it is gone. - :raises TemporaryFetchError: If fetching the source fails temporarily. + :raises RetryableFetchError: If fetching the source fails temporarily. """ - response, body = fetch_source(source_url) + response, body = fetch_source_document(source_url) - media_type = content_type_of(response.headers) + media_type = media_type_of(response.headers) match media_type: case "text/html" | "application/xhtml+xml": @@ -127,13 +129,13 @@ def source_mentions_target(source_url: str, target_url: str) -> bool: class VerificationClaim: """Source and lease version for one atomically claimed verification.""" - identifier: uuid.UUID + webmention_uuid: uuid.UUID source: str target: str - started_at: datetime + lease_started_at: datetime -def claim_eligible_row(webmention_uuid: uuid.UUID) -> VerificationClaim | None: +def claim_for_verification(webmention_uuid: uuid.UUID) -> VerificationClaim | None: """Claim an eligible row, returning its source, target, and lease version. :param webmention_uuid: Identifier of the received Webmention to claim. @@ -157,10 +159,10 @@ def claim_eligible_row(webmention_uuid: uuid.UUID) -> VerificationClaim | None: return None claim = VerificationClaim( - identifier=webmention_uuid, + webmention_uuid=webmention_uuid, source=webmention.source, target=webmention.target, - started_at=datetime.now(UTC), + lease_started_at=datetime.now(UTC), ) changed = db.session.execute( sa.update(ReceivedWebmention) @@ -172,7 +174,7 @@ def claim_eligible_row(webmention_uuid: uuid.UUID) -> VerificationClaim | None: .values( status=ReceivedStatus.VERIFYING, failure_reason=None, - updated_at=claim.started_at, + updated_at=claim.lease_started_at, ) ) @@ -184,12 +186,14 @@ def claim_eligible_row(webmention_uuid: uuid.UUID) -> VerificationClaim | None: return claim -def check_source(claim: VerificationClaim) -> tuple[ReceivedStatus, str | None]: +def determine_verification_outcome( + claim: VerificationClaim, +) -> tuple[ReceivedStatus, str | None]: """Map verification results and permanent errors to a final status/reason. :param claim: Claim containing the source and target URLs to verify. :returns: Final verification status and optional reason. - :raises TemporaryFetchError: If the source fetch fails transiently and should be + :raises RetryableFetchError: If the source fetch fails transiently and should be retried. :raises httpx.RequestError: If a transient HTTP request error should be retried. """ @@ -199,12 +203,12 @@ def check_source(claim: VerificationClaim) -> tuple[ReceivedStatus, str | None]: return ReceivedStatus.DELETED, str(exc) except VerificationError as exc: return ReceivedStatus.FAILED, str(exc) - except TemporaryFetchError, httpx.RequestError: + except RetryableFetchError, httpx.RequestError: raise # These errors must be persisted and then retried by Huey. except Exception as exc: # InvalidURL is not a RequestError; unexpected source errors are terminal. current_app.logger.exception( - "Unexpected error verifying ReceivedWebmention %s", claim.identifier + "Unexpected error verifying ReceivedWebmention %s", claim.webmention_uuid ) return ReceivedStatus.FAILED, str(exc) or type(exc).__name__ @@ -226,9 +230,9 @@ def release_claim( changed = db.session.execute( sa.update(ReceivedWebmention) .where( - ReceivedWebmention.uuid == claim.identifier, + ReceivedWebmention.uuid == claim.webmention_uuid, ReceivedWebmention.status == ReceivedStatus.VERIFYING, - ReceivedWebmention.updated_at == claim.started_at, + ReceivedWebmention.updated_at == claim.lease_started_at, ) .values(status=status, failure_reason=reason, updated_at=datetime.now(UTC)) ) @@ -246,15 +250,15 @@ def verify_webmention(webmention_uuid: uuid.UUID) -> None: lease. :param webmention_uuid: Identifier of the received webmention to verify. - :raises TemporaryFetchError: If fetching the source fails temporarily. + :raises RetryableFetchError: If fetching the source fails temporarily. :raises httpx.RequestError: If an HTTP request fails while fetching. """ - if (claim := claim_eligible_row(webmention_uuid)) is None: + if (claim := claim_for_verification(webmention_uuid)) is None: return try: - status, reason = check_source(claim) - except (TemporaryFetchError, httpx.RequestError) as exc: + status, reason = determine_verification_outcome(claim) + except (RetryableFetchError, httpx.RequestError) as exc: release_claim( claim, ReceivedStatus.FAILED, str(exc) or "Source could not be fetched" ) @@ -263,7 +267,7 @@ def verify_webmention(webmention_uuid: uuid.UUID) -> None: release_claim(claim, status, reason) -def recover_stale_webmentions() -> int: +def recover_stale_verifications() -> int: """Requeue verification attempts that were abandoned or lost from the queue. Recover both stale ``verifying`` rows (workers that died) and stale @@ -280,7 +284,9 @@ def recover_stale_webmentions() -> int: :returns: Number of webmentions requeued. """ now = datetime.now(UTC) - cutoff = now - timedelta(seconds=current_app.config["VERIFICATION_TIMEOUT_SECONDS"]) + cutoff = now - timedelta( + seconds=current_app.config["VERIFICATION_STALE_AFTER_SECONDS"] + ) stale = db.session.execute( sa.select( diff --git a/webmentions_ssg/tasks/scanner.py b/webmentions_ssg/tasks/scanner.py index 13d2ceb..034b2cf 100644 --- a/webmentions_ssg/tasks/scanner.py +++ b/webmentions_ssg/tasks/scanner.py @@ -20,15 +20,15 @@ from .sender import send_webmention from .source_parser import ScannedSource, SourceScanError, scan_source_file -def create_source(scanned: ScannedSource, scan_time: datetime) -> Source: - """Create a source model from a newly discovered document. +def build_source(scanned: ScannedSource, scan_time: datetime) -> Source: + """Build a source model from a newly discovered document. - A sent Webmention is created for each discovered target at the initial source + A sent Webmention is built for each discovered target at the initial source revision. :param scanned: Scanned source data. :param scan_time: Time at which the source was discovered. - :returns: Newly created source model. + :returns: Newly built source model. """ source = Source( path=scanned.path, @@ -48,7 +48,7 @@ def create_source(scanned: ScannedSource, scan_time: datetime) -> Source: return source -def update_source( +def apply_scan_to_source( source: Source, scanned: ScannedSource, scan_time: datetime ) -> Source: """Update an existing source from newly scanned data. @@ -113,24 +113,24 @@ def scan_sources() -> None: """ # Manual and periodic scans must contend for the current backend's lock. with huey.lock_task("scan-webmention-sources"): - _scan_locked() + _scan_and_enqueue() @dataclass(frozen=True) -class ScanResult: +class TreeScanResult: """Count and discovered paths from a traversal of the source tree.""" scanned_count: int seen_paths: frozenset[str] -def scan_tree( +def reconcile_source_tree( root: Path, base_url: str | None, sources_by_path: dict[str, Source], paths_by_url: dict[str, str], scan_time: datetime, -) -> ScanResult: +) -> TreeScanResult: """Reconcile found pages and return the paths seen, even on parse failures. :param root: Root of the generated static-site output tree. @@ -157,7 +157,7 @@ def scan_tree( index_path, root=root, base_url=base_url, - ignored_hostnames=current_app.config["IGNORED_HOSTNAMES"], + ignored_hostnames=current_app.config["IGNORED_TARGET_HOSTNAMES"], ) except (OSError, SourceScanError) as exc: current_app.logger.warning("Could not scan source %s: %s", index_path, exc) @@ -182,10 +182,10 @@ def scan_tree( try: if (source := sources_by_path.get(scanned.path)) is None: - source = create_source(scanned, scan_time) + source = build_source(scanned, scan_time) db.session.add(source) else: - source = update_source(source, scanned, scan_time) + source = apply_scan_to_source(source, scanned, scan_time) sources_by_path[source.path] = source paths_by_url[source.url] = source.path @@ -193,10 +193,10 @@ def scan_tree( except SourceScanError as exc: current_app.logger.warning("Could not scan source %s: %s", index_path, exc) - return ScanResult(scanned_count, frozenset(seen_paths)) + return TreeScanResult(scanned_count, frozenset(seen_paths)) -def mark_missing( +def mark_missing_sources_deleted( sources_by_path: dict[str, Source], seen_paths: frozenset[str], scan_time: datetime ) -> int: """Mark absent, previously active sources deleted and return their count. @@ -225,7 +225,7 @@ def mark_missing( return marked -def queue_pending(scan_time: datetime) -> int: +def enqueue_due_sends(scan_time: datetime) -> int: """Enqueue due Webmentions and return the number of tasks submitted. :param scan_time: Current scan timestamp used to evaluate retry deadlines. @@ -249,7 +249,7 @@ def queue_pending(scan_time: datetime) -> int: return queued -def _scan_locked() -> None: +def _scan_and_enqueue() -> None: """Scan sources while the caller holds the shared scan lock. Reconcile the generated source tree, persist changes, and enqueue eligible outgoing @@ -257,7 +257,7 @@ def _scan_locked() -> None: :raises RuntimeError: If the source directory or configured base URL is invalid. """ - directory = current_app.config.get("SOURCE_DIRECTORY") + directory = current_app.config.get("SITE_OUTPUT_DIRECTORY") if directory is None: return @@ -265,9 +265,9 @@ def _scan_locked() -> None: if not root.is_dir(): raise RuntimeError(f"Source directory is not a directory: {root}") - if (base_url := current_app.config.get("SOURCE_BASE_URL")) is not None: + if (base_url := current_app.config.get("SITE_BASE_URL")) is not None: if not is_http_url(base_url): - raise RuntimeError("SOURCE_BASE_URL must be an absolute HTTP or HTTPS URL") + raise RuntimeError("SITE_BASE_URL must be an absolute HTTP or HTTPS URL") base_url = f"{base_url.rstrip('/')}/" sources_by_path = { @@ -281,7 +281,9 @@ def _scan_locked() -> None: # take ownership merely by appearing earlier in the traversal. paths_by_url = {source.url: source.path for source in sources_by_path.values()} scan_time = datetime.now(UTC) - scan = scan_tree(root, base_url, sources_by_path, paths_by_url, scan_time) + scan = reconcile_source_tree( + root, base_url, sources_by_path, paths_by_url, scan_time + ) if scan.scanned_count == 0: current_app.logger.warning( @@ -290,9 +292,11 @@ def _scan_locked() -> None: ) return - missing_count = mark_missing(sources_by_path, scan.seen_paths, scan_time) + missing_count = mark_missing_sources_deleted( + sources_by_path, scan.seen_paths, scan_time + ) db.session.commit() - pending_count = queue_pending(scan_time) + pending_count = enqueue_due_sends(scan_time) current_app.logger.info( "Webmention source scan complete: %d sources scanned, " diff --git a/webmentions_ssg/tasks/sender.py b/webmentions_ssg/tasks/sender.py index cf8ab9d..c33cf06 100644 --- a/webmentions_ssg/tasks/sender.py +++ b/webmentions_ssg/tasks/sender.py @@ -19,20 +19,20 @@ from ..models import SentWebmention, SentWebmentionStatus from ..net import ( AddressResolutionError, UnsafeAddressError, - build_client, + build_public_http_client, is_http_url, - is_temporary_status, + is_retryable_status, ) from ..version import APP_NAME, VERSION from .discovery import ( - PermanentSenderError, - TemporarySenderError, + PermanentDeliveryError, + RetryableDeliveryError, discover_webmention_endpoint, post_webmention, ) -def attempt_is_current(webmention: SentWebmention, revision: int) -> bool: +def revision_still_pending(webmention: SentWebmention, revision: int) -> bool: """Check whether a send attempt still represents the desired revision. The Webmention is refreshed from the database before comparing its desired @@ -47,7 +47,7 @@ def attempt_is_current(webmention: SentWebmention, revision: int) -> bool: return webmention.desired_revision == revision and webmention.pending -def retry_delay(attempt: int) -> timedelta: +def backoff_delay(attempt: int) -> timedelta: """Calculate the capped exponential delay after a numbered send attempt. :param attempt: One-based send attempt number. @@ -76,7 +76,7 @@ def send_webmention(webmention_uuid: uuid.UUID) -> None: # The pending/retry checks must happen *under* this lock. try: with huey.lock_task(f"send-webmention-{webmention_uuid}"): - _send_locked(webmention_uuid) + _process_send_locked(webmention_uuid) except TaskLockedException: # Another worker is already sending this Webmention. The active worker # owns its result; a later scan will requeue it if it remains pending. @@ -84,12 +84,12 @@ def send_webmention(webmention_uuid: uuid.UUID) -> None: @dataclass(frozen=True) -class SendAttempt: +class ClaimedSendAttempt: """Claimed source revision and retry budget for one send.""" - identifier: uuid.UUID + webmention_uuid: uuid.UUID revision: int - number: int + attempt_number: int max_attempts: int source: str target: str @@ -112,7 +112,7 @@ class DeliveryResult: temporary_failure: bool = False -def is_due(webmention: SentWebmention, now: datetime) -> bool: +def is_ready_to_send(webmention: SentWebmention, now: datetime) -> bool: """Return whether an unprocessed Webmention has reached its retry deadline. :param webmention: Outgoing Webmention whose retry eligibility is checked. @@ -130,7 +130,9 @@ def is_due(webmention: SentWebmention, now: datetime) -> bool: return aware_deadline <= now -def claim_attempt(webmention: SentWebmention, now: datetime) -> SendAttempt | None: +def reserve_send_attempt( + webmention: SentWebmention, now: datetime +) -> ClaimedSendAttempt | None: """Atomically reserve one send attempt, or finish an exhausted revision. An attempt's next retry deadline is persisted *before* the HTTP request so @@ -172,10 +174,10 @@ def claim_attempt(webmention: SentWebmention, now: datetime) -> SendAttempt | No db.session.commit() return None - attempt = SendAttempt( - identifier=webmention.uuid, + attempt = ClaimedSendAttempt( + webmention_uuid=webmention.uuid, revision=revision, - number=webmention.attempt_count + 1, + attempt_number=webmention.attempt_count + 1, max_attempts=max_attempts, source=webmention.source.url, target=webmention.target, @@ -183,9 +185,9 @@ def claim_attempt(webmention: SentWebmention, now: datetime) -> SendAttempt | No changed = db.session.execute( sa.update(SentWebmention) .where( - SentWebmention.uuid == attempt.identifier, + SentWebmention.uuid == attempt.webmention_uuid, SentWebmention.desired_revision == revision, - SentWebmention.attempt_count == attempt.number - 1, + SentWebmention.attempt_count == attempt.attempt_number - 1, SentWebmention.pending, sa.or_( SentWebmention.next_retry_at.is_(None), @@ -193,9 +195,9 @@ def claim_attempt(webmention: SentWebmention, now: datetime) -> SendAttempt | No ), ) .values( - attempt_count=attempt.number, + attempt_count=attempt.attempt_number, last_attempted_at=now, - next_retry_at=now + retry_delay(attempt.number), + next_retry_at=now + backoff_delay(attempt.attempt_number), endpoint=None, response_status=None, status_url=None, @@ -211,7 +213,7 @@ def claim_attempt(webmention: SentWebmention, now: datetime) -> SendAttempt | No return attempt -def deliver_webmention( +def attempt_delivery( source: str, target: str, *, webmention_uuid: uuid.UUID ) -> DeliveryResult: """Discover and POST a Webmention, returning an outcome without DB writes. @@ -227,9 +229,9 @@ def deliver_webmention( try: if not is_http_url(target): - raise PermanentSenderError(f"Invalid target URL: {target!r}") + raise PermanentDeliveryError(f"Invalid target URL: {target!r}") - with build_client(f"{APP_NAME}/{VERSION} SentWebmention") as client: + with build_public_http_client(f"{APP_NAME}/{VERSION} SentWebmention") as client: endpoint = discover_webmention_endpoint(client, target) if endpoint is None: @@ -243,9 +245,9 @@ def deliver_webmention( ) if not 200 <= response_status <= 299: message = f"Webmention endpoint returned HTTP {response_status}" - if is_temporary_status(response_status): - raise TemporarySenderError(message) - raise PermanentSenderError(message) + if is_retryable_status(response_status): + raise RetryableDeliveryError(message) + raise PermanentDeliveryError(message) return DeliveryResult( status=SentWebmentionStatus.SENT, @@ -254,7 +256,7 @@ def deliver_webmention( status_url=status_url, ) except ( - PermanentSenderError, + PermanentDeliveryError, UnsafeAddressError, httpx.InvalidURL, httpx.UnsupportedProtocol, @@ -267,7 +269,7 @@ def deliver_webmention( endpoint=endpoint, response_status=response_status, ) - except (TemporarySenderError, AddressResolutionError, httpx.RequestError) as exc: + except (RetryableDeliveryError, AddressResolutionError, httpx.RequestError) as exc: return DeliveryResult( status=SentWebmentionStatus.FAILED, reason=str(exc) or "Webmention request failed", @@ -289,9 +291,9 @@ def deliver_webmention( ) -def record_outcome( +def record_delivery_outcome( webmention: SentWebmention, - attempt: SendAttempt, + attempt: ClaimedSendAttempt, result: DeliveryResult, *, terminal: bool, @@ -305,7 +307,7 @@ def record_outcome( :returns: ``True`` if the outcome was recorded for the current revision. """ # A scanner may have reopened or superseded the revision during I/O. - if not attempt_is_current(webmention, attempt.revision): + if not revision_still_pending(webmention, attempt.revision): return False webmention.status = result.status @@ -330,7 +332,7 @@ def record_outcome( return True -def _send_locked(webmention_uuid: uuid.UUID) -> bool: +def _process_send_locked(webmention_uuid: uuid.UUID) -> bool: """Process a due Webmention while holding its per-UUID task lock. :param webmention_uuid: Identifier of the outgoing Webmention being processed. @@ -344,18 +346,18 @@ def _send_locked(webmention_uuid: uuid.UUID) -> bool: return False now = datetime.now(UTC) - if not is_due(webmention, now): + if not is_ready_to_send(webmention, now): return False - if (attempt := claim_attempt(webmention, now)) is None: + if (attempt := reserve_send_attempt(webmention, now)) is None: return False - result = deliver_webmention( + result = attempt_delivery( attempt.source, attempt.target, webmention_uuid=webmention_uuid ) - terminal = result.terminal or attempt.number >= attempt.max_attempts + terminal = result.terminal or attempt.attempt_number >= attempt.max_attempts - if not record_outcome(webmention, attempt, result, terminal=terminal): + if not record_delivery_outcome(webmention, attempt, result, terminal=terminal): return False if result.temporary_failure and not terminal: @@ -363,13 +365,13 @@ def _send_locked(webmention_uuid: uuid.UUID) -> bool: current_app.logger.info( "Webmention %s send attempt %d failed; will retry", webmention_uuid, - attempt.number, + attempt.attempt_number, ) elif result.temporary_failure: current_app.logger.warning( "Webmention %s exhausted %d send attempts: %s", webmention_uuid, - attempt.number, + attempt.attempt_number, result.reason, ) diff --git a/webmentions_ssg/tasks/source_parser.py b/webmentions_ssg/tasks/source_parser.py index 31d0c98..e063525 100644 --- a/webmentions_ssg/tasks/source_parser.py +++ b/webmentions_ssg/tasks/source_parser.py @@ -18,11 +18,11 @@ import xxhash from bs4 import BeautifulSoup, Tag from bs4.element import NavigableString, PageElement -from ..net import hostname_of, is_http_url +from ..net import is_http_url, normalized_hostname REACTION_PROPERTIES = ("in-reply-to", "like-of", "repost-of", "bookmark-of") -CONTENT_BLOCK_TAGS = frozenset( +BLOCK_LEVEL_TAGS = frozenset( { "address", "article", @@ -83,7 +83,7 @@ def source_url_for_path(relative_path: Path, base_url: str) -> str: return urljoin(base_url, f"{quote(directory, safe='/')}/") -def canonical_url( +def declared_canonical_url( document: BeautifulSoup, *, relative_path: Path, base_url: str | None ) -> str | None: """Return the canonical URL declared by a source document. @@ -111,7 +111,7 @@ def canonical_url( if base_url is None: raise SourceScanError( "Document contains a relative canonical URL, but " - "SOURCE_BASE_URL is not configured" + "SITE_BASE_URL is not configured" ) resolved = urljoin(source_url_for_path(relative_path, base_url), href) @@ -124,7 +124,7 @@ def canonical_url( return resolved -def property_urls(entry: dict[str, Any], property_name: str) -> Iterator[str]: +def mf2_string_values(entry: dict[str, Any], property_name: str) -> Iterator[str]: """Yield URL values from a microformats property. :param entry: Parsed microformats entry. @@ -144,7 +144,7 @@ def property_urls(entry: dict[str, Any], property_name: str) -> Iterator[str]: yield from (value for value in values if isinstance(value, str)) -def parse_entry(element: Tag, base_url: str) -> dict[str, Any]: +def parse_h_entry(element: Tag, base_url: str) -> dict[str, Any]: """Parse and validate an h-entry with mf2py. :param element: HTML element containing the h-entry. @@ -175,7 +175,7 @@ def parse_entry(element: Tag, base_url: str) -> dict[str, Any]: return entry -def primary_entry( +def find_primary_h_entry( document: BeautifulSoup, source_url: str ) -> tuple[Tag, dict[str, Any]]: """Return and validate the primary h-entry of a source document. @@ -195,9 +195,9 @@ def primary_entry( raise SourceScanError(f"Expected exactly one h-entry, found {len(entries)}") entry_element = entries[0] - mf2_entry = parse_entry(entry_element, source_url) + mf2_entry = parse_h_entry(entry_element, source_url) - urls = tuple(property_urls(mf2_entry, "url")) + urls = tuple(mf2_string_values(mf2_entry, "url")) if source_url not in urls: raise SourceScanError( @@ -207,15 +207,15 @@ def primary_entry( return entry_element, mf2_entry -def content_element(entry: Tag) -> Tag: +def find_e_content(entry_element: Tag) -> Tag: """Return the e-content element of an h-entry. - :param entry: HTML element containing the h-entry. + :param entry_element: HTML element containing the h-entry. :returns: The h-entry's e-content element. :raises SourceScanError: If the h-entry does not contain exactly one e-content element. """ - contents = entry.find_all(class_="e-content") + contents = entry_element.find_all(class_="e-content") if len(contents) != 1: raise SourceScanError(f"Expected exactly one e-content, found {len(contents)}") @@ -223,7 +223,7 @@ def content_element(entry: Tag) -> Tag: return contents[0] -def normalize_target(value: str, *, base_url: str, source_url: str) -> str | None: +def normalize_target_url(value: str, *, base_url: str, source_url: str) -> str | None: """Resolve and validate a possible Webmention target URL. Empty values, non-HTTP URLs, and URLs referring to the source itself are @@ -249,7 +249,7 @@ def normalize_target(value: str, *, base_url: str, source_url: str) -> str | Non return target -def iter_targets( +def iter_outgoing_targets( content: Tag, mf2_entry: dict[str, Any], *, @@ -277,18 +277,19 @@ def iter_targets( ) reactions = chain.from_iterable( - property_urls(mf2_entry, property_name) for property_name in REACTION_PROPERTIES + mf2_string_values(mf2_entry, property_name) + for property_name in REACTION_PROPERTIES ) for value in chain(hrefs, reactions): - target = normalize_target(value, base_url=base_url, source_url=source_url) + target = normalize_target_url(value, base_url=base_url, source_url=source_url) if target is not None: - hostname = hostname_of(target) or "" + hostname = normalized_hostname(target) or "" if not any(fnmatchcase(hostname, pattern) for pattern in ignored_hostnames): yield target -def normalized_content(content: Tag) -> str: +def stable_content_text(content: Tag) -> str: """Extract stable reader-facing content without generated HTML attributes. :param content: Parsed e-content element to normalize. @@ -312,7 +313,7 @@ def normalized_content(content: Tag) -> str: return f" [image:{node.get('src', '')!s}:{node.get('alt', '')!s}] " text = "".join(extract(child) for child in node.children) - return f" {text} " if node.name in CONTENT_BLOCK_TAGS else text + return f" {text} " if node.name in BLOCK_LEVEL_TAGS else text return " ".join(extract(content).split()) @@ -346,22 +347,24 @@ def scan_source_file( relative_path = path.relative_to(root) document = BeautifulSoup(resolved_path.read_bytes(), "html5lib") - source_url = canonical_url(document, relative_path=relative_path, base_url=base_url) + source_url = declared_canonical_url( + document, relative_path=relative_path, base_url=base_url + ) if source_url is None: if base_url is None: raise SourceScanError( - "Document has no canonical URL and SOURCE_BASE_URL is not configured" + "Document has no canonical URL and SITE_BASE_URL is not configured" ) source_url = source_url_for_path(relative_path, base_url) - entry_element, mf2_entry = primary_entry(document, source_url) + entry_element, mf2_entry = find_primary_h_entry(document, source_url) - content = content_element(entry_element) + content = find_e_content(entry_element) targets = frozenset( - iter_targets( + iter_outgoing_targets( content, mf2_entry, base_url=source_url, @@ -370,7 +373,7 @@ def scan_source_file( ) ) - stable_content = normalized_content(content) + stable_content = stable_content_text(content) fingerprint = json.dumps( {"content": stable_content, "targets": sorted(targets)}, ensure_ascii=False, diff --git a/webmentions_ssg/tasks/mentions.py b/webmentions_ssg/tasks/target_matching.py index 4c3918b..4c3918b 100644 --- a/webmentions_ssg/tasks/mentions.py +++ b/webmentions_ssg/tasks/target_matching.py diff --git a/webmentions_ssg/templates/base.html b/webmentions_ssg/templates/base.html index 19412ed..69e5215 100644 --- a/webmentions_ssg/templates/base.html +++ b/webmentions_ssg/templates/base.html @@ -32,7 +32,8 @@ SPDX-License-Identifier: BSD-3-Clause <header> <nav class="navbar bg-primary py-1"> <div class="container-fluid px-3"> - <a class="navbar-brand fs-6 mb-0" href="{{ url_for('root.index') }}"> + <a class="navbar-brand fs-6 mb-0" + href="{{ url_for('webmentions.index') }}"> <img src="{{ url_for('static', filename='images/logo.svg') }}" width="30" height="30" @@ -42,11 +43,13 @@ SPDX-License-Identifier: BSD-3-Clause </a> {% if current_user.is_authenticated %} <div class="navbar-nav flex-row gap-3"> - <a class="nav-link{% if request.endpoint == 'root.received' %} active{% endif %}" - href='{{ url_for("root.received") }}'>Received</a> - <a class="nav-link{% if request.endpoint == 'root.sent' %} active{% endif %}" - href='{{ url_for("root.sent") }}'>Sent</a> - <form action="{{ url_for('root.logout') }}" method="post" class="m-0"> + <a class="nav-link{% if request.endpoint == 'webmentions.received_list' %} active{% endif %}" + href='{{ url_for("webmentions.received_list") }}'>Received</a> + <a class="nav-link{% if request.endpoint == 'webmentions.source_list' %} active{% endif %}" + href='{{ url_for("webmentions.source_list") }}'>Sent</a> + <form action="{{ url_for('webmentions.logout') }}" + method="post" + class="m-0"> <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"> <button type="submit" class="nav-link border-0 bg-transparent">Logout</button> </form> diff --git a/webmentions_ssg/templates/received.html b/webmentions_ssg/templates/received.html index 3dbb896..f00a5f9 100644 --- a/webmentions_ssg/templates/received.html +++ b/webmentions_ssg/templates/received.html @@ -79,7 +79,7 @@ SPDX-License-Identifier: BSD-3-Clause class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#confirmation-modal" - data-action="{{ url_for('root.reverify_received_webmention', identifier=webmention.uuid, page=webmentions.page) }}" + data-action="{{ url_for('webmentions.reverify_received_webmention', webmention_uuid=webmention.uuid, page=webmentions.page) }}" data-title="Reverify webmention?" data-message="The webmention will be fetched and verified again." data-confirm-label="Reverify" @@ -88,7 +88,7 @@ SPDX-License-Identifier: BSD-3-Clause class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#confirmation-modal" - data-action="{{ url_for('root.delete_received_webmention', identifier=webmention.uuid, page=webmentions.page) }}" + data-action="{{ url_for('webmentions.delete_received_webmention', webmention_uuid=webmention.uuid, page=webmentions.page) }}" data-title="Delete webmention?" data-message="This permanently deletes the webmention and cannot be undone." data-confirm-label="Delete" diff --git a/webmentions_ssg/templates/sent.html b/webmentions_ssg/templates/sent.html index c9662b9..2b0fbe4 100644 --- a/webmentions_ssg/templates/sent.html +++ b/webmentions_ssg/templates/sent.html @@ -16,7 +16,7 @@ SPDX-License-Identifier: BSD-3-Clause class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#confirmation-modal" - data-action="{{ url_for('root.rescan_sent_sources') }}" + data-action="{{ url_for('webmentions.rescan_sources') }}" data-title="Rescan sources?" data-message="All sources will be scanned again for changes and webmentions." data-confirm-label="Rescan" @@ -49,7 +49,7 @@ SPDX-License-Identifier: BSD-3-Clause {% endif %} <tr class="{{ row_class }}"> <td class="text-nowrap"> - <a href="{{ url_for('root.sent_source', source_identifier=source.uuid) }}">{{ source.path }}</a> + <a href="{{ url_for('webmentions.source_detail', source_uuid=source.uuid) }}">{{ source.path }}</a> </td> <td class="text-break"> <a href="{{ source.url }}">{{ source.url }}</a> diff --git a/webmentions_ssg/templates/sent_source.html b/webmentions_ssg/templates/sent_source.html index 974c7b3..31d5c81 100644 --- a/webmentions_ssg/templates/sent_source.html +++ b/webmentions_ssg/templates/sent_source.html @@ -26,7 +26,7 @@ SPDX-License-Identifier: BSD-3-Clause {% block content %} <div class="mb-3"> - <a href="{{ url_for('root.sent') }}">← Back to sources</a> + <a href="{{ url_for('webmentions.source_list') }}">← Back to sources</a> </div> <div class="d-flex justify-content-between align-items-start mb-3"> <h1 class="mb-0">Source Details</h1> @@ -191,7 +191,7 @@ SPDX-License-Identifier: BSD-3-Clause class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#confirmation-modal" - data-action="{{ url_for('root.resend_sent_webmention', source_identifier=source.uuid, webmention_identifier=webmention.uuid) }}" + data-action="{{ url_for('webmentions.resend_webmention', source_uuid=source.uuid, webmention_uuid=webmention.uuid) }}" data-title="Resend Webmention?" data-message="This Webmention will be queued again for delivery to {{ webmention.target }}." data-confirm-label="Resend" diff --git a/webmentions_ssg/views/__init__.py b/webmentions_ssg/views/__init__.py index ffe9a27..7f091f0 100644 --- a/webmentions_ssg/views/__init__.py +++ b/webmentions_ssg/views/__init__.py @@ -2,9 +2,9 @@ # # SPDX-License-Identifier: BSD-3-Clause -"""Collect and expose the routes for the historic root Blueprint.""" +"""Collect and expose the routes for the blueprint.""" from . import admin, endpoint, session -from .blueprint import root_page +from .blueprint import webmentions_bp -__all__ = ["admin", "endpoint", "root_page", "session"] +__all__ = ["admin", "endpoint", "session", "webmentions_bp"] diff --git a/webmentions_ssg/views/admin.py b/webmentions_ssg/views/admin.py index 2ccd92a..9b7c418 100644 --- a/webmentions_ssg/views/admin.py +++ b/webmentions_ssg/views/admin.py @@ -19,12 +19,12 @@ from ..models import ReceivedWebmention, SentWebmention, Source from ..tasks.receiver import verify_webmention from ..tasks.scanner import manual_scan_sources from ..tasks.sender import send_webmention -from .blueprint import root_page +from .blueprint import webmentions_bp -@root_page.route("/received") +@webmentions_bp.route("/received") @login_required -def received() -> ResponseReturnValue: +def received_list() -> ResponseReturnValue: """Display received Webmentions. :returns: Rendered page containing the paginated received Webmentions. @@ -38,22 +38,22 @@ def received() -> ResponseReturnValue: "received.html", title="Received Webmentions", webmentions=webmentions, - action_form=forms.AdminActionForm(), + action_form=forms.ConfirmActionForm(), ) -@root_page.post("/received/<uuid:identifier>/delete") +@webmentions_bp.post("/received/<uuid:webmention_uuid>/delete") @login_required -def delete_received_webmention(identifier: uuid.UUID) -> ResponseReturnValue: +def delete_received_webmention(webmention_uuid: uuid.UUID) -> ResponseReturnValue: """Delete a received Webmention. - :param identifier: Identifier of the Webmention to delete. + :param webmention_uuid: UUID of the Webmention to delete. :returns: Redirect response to the received Webmentions view. """ - if not forms.AdminActionForm().validate_on_submit(): + if not forms.ConfirmActionForm().validate_on_submit(): abort(HTTPStatus.BAD_REQUEST) - webmention = db.get_or_404(ReceivedWebmention, identifier) + webmention = db.get_or_404(ReceivedWebmention, webmention_uuid) db.session.delete(webmention) db.session.commit() @@ -61,41 +61,41 @@ def delete_received_webmention(identifier: uuid.UUID) -> ResponseReturnValue: flash(f"Webmention {webmention.uuid} deleted.", "success") return redirect( - url_for("root.received", page=request.args.get("page", 1, type=int)) + url_for("webmentions.received_list", page=request.args.get("page", 1, type=int)) ) -@root_page.post("/received/<uuid:identifier>/reverify") +@webmentions_bp.post("/received/<uuid:webmention_uuid>/reverify") @login_required -def reverify_received_webmention(identifier: uuid.UUID) -> ResponseReturnValue: +def reverify_received_webmention(webmention_uuid: uuid.UUID) -> ResponseReturnValue: """Queue a received Webmention for reverification. The Webmention status is reset before a new verification task is queued. - :param identifier: Identifier of the Webmention to reverify. + :param webmention_uuid: UUID of the Webmention to reverify. :returns: Redirect response to the received Webmentions view. """ - if not forms.AdminActionForm().validate_on_submit(): + if not forms.ConfirmActionForm().validate_on_submit(): abort(HTTPStatus.BAD_REQUEST) - webmention = db.get_or_404(ReceivedWebmention, identifier) + webmention = db.get_or_404(ReceivedWebmention, webmention_uuid) webmention.reset_for_verification() db.session.commit() - verify_webmention(identifier) + verify_webmention(webmention_uuid) flash(f"Webmention {webmention.uuid} queued for reverification.", "success") return redirect( - url_for("root.received", page=request.args.get("page", 1, type=int)) + url_for("webmentions.received_list", page=request.args.get("page", 1, type=int)) ) -@root_page.route("/sent") +@webmentions_bp.route("/sources") @login_required -def sent() -> ResponseReturnValue: +def source_list() -> ResponseReturnValue: """Display sources with sent Webmentions. :returns: Rendered page containing the paginated source list. @@ -110,35 +110,35 @@ def sent() -> ResponseReturnValue: "sent.html", title="Sent Webmentions", sources=sources, - action_form=forms.AdminActionForm(), + action_form=forms.ConfirmActionForm(), ) -@root_page.post("/sent/rescan") +@webmentions_bp.post("/sources/rescan") @login_required -def rescan_sent_sources() -> ResponseReturnValue: +def rescan_sources() -> ResponseReturnValue: """Queue a manual scan of sent Webmention sources. :returns: Redirect response to the sent Webmentions view. """ - if not forms.AdminActionForm().validate_on_submit(): + if not forms.ConfirmActionForm().validate_on_submit(): abort(HTTPStatus.BAD_REQUEST) manual_scan_sources() flash("Source rescan queued.", "success") - return redirect(url_for("root.sent")) + return redirect(url_for("webmentions.source_list")) -@root_page.route("/sent/<uuid:source_identifier>") +@webmentions_bp.route("/sources/<uuid:source_uuid>") @login_required -def sent_source(source_identifier: uuid.UUID) -> ResponseReturnValue: +def source_detail(source_uuid: uuid.UUID) -> ResponseReturnValue: """Display the sent Webmentions associated with a source. - :param source_identifier: Identifier of the source to display. + :param source_uuid: UUID of the source to display. :returns: Rendered source details page. """ - source = db.get_or_404(Source, source_identifier) + source = db.get_or_404(Source, source_uuid) webmentions = db.paginate( sa.select(SentWebmention) @@ -152,14 +152,14 @@ def sent_source(source_identifier: uuid.UUID) -> ResponseReturnValue: title="Sent Webmentions", source=source, webmentions=webmentions, - action_form=forms.AdminActionForm(), + action_form=forms.ConfirmActionForm(), ) -@root_page.post("/sent/<uuid:source_identifier>/<uuid:webmention_identifier>/resend") +@webmentions_bp.post("/sources/<uuid:source_uuid>/<uuid:webmention_uuid>/resend") @login_required -def resend_sent_webmention( - source_identifier: uuid.UUID, webmention_identifier: uuid.UUID +def resend_webmention( + source_uuid: uuid.UUID, webmention_uuid: uuid.UUID ) -> ResponseReturnValue: """Queue an outgoing Webmention to be sent again. @@ -168,16 +168,16 @@ def resend_sent_webmention( successful-send fields are preserved, while result fields from the previous attempt are cleared. - :param source_identifier: Identifier of the source owning the Webmention. - :param webmention_identifier: Identifier of the sent Webmention to resend. + :param source_uuid: UUID of the source owning the Webmention. + :param webmention_uuid: UUID of the sent Webmention to resend. :returns: Redirect response to the source details view. """ - if not forms.AdminActionForm().validate_on_submit(): + if not forms.ConfirmActionForm().validate_on_submit(): abort(HTTPStatus.BAD_REQUEST) - webmention = db.get_or_404(SentWebmention, webmention_identifier) + webmention = db.get_or_404(SentWebmention, webmention_uuid) - if webmention.source_id != source_identifier: + if webmention.source_id != source_uuid: abort(HTTPStatus.NOT_FOUND) # Requeue the existing desired revision instead of inventing a new source @@ -187,8 +187,8 @@ def resend_sent_webmention( db.session.commit() - send_webmention(webmention_identifier) + send_webmention(webmention_uuid) flash(f"Webmention {webmention.uuid} queued for resend.", "success") - return redirect(url_for("root.sent_source", source_identifier=source_identifier)) + return redirect(url_for("webmentions.source_detail", source_uuid=source_uuid)) diff --git a/webmentions_ssg/views/blueprint.py b/webmentions_ssg/views/blueprint.py index 136d319..6d1e404 100644 --- a/webmentions_ssg/views/blueprint.py +++ b/webmentions_ssg/views/blueprint.py @@ -6,4 +6,4 @@ from flask import Blueprint -root_page = Blueprint("root", __name__) +webmentions_bp = Blueprint("webmentions", __name__) diff --git a/webmentions_ssg/views/endpoint.py b/webmentions_ssg/views/endpoint.py index 06740e5..be2a9ab 100644 --- a/webmentions_ssg/views/endpoint.py +++ b/webmentions_ssg/views/endpoint.py @@ -19,14 +19,14 @@ from ..db_helpers import rows_affected from ..extensions import csrf, db, limiter from ..models import ReceivedWebmention, received_url_digest from ..tasks.receiver import verify_webmention -from .blueprint import root_page +from .blueprint import webmentions_bp class UrlDigestCollisionError(Exception): """The URL digest already belongs to a different source/target pair.""" -def find_webmention_by_digest(source: str, target: str) -> ReceivedWebmention | None: +def find_received_webmention(source: str, target: str) -> ReceivedWebmention | None: """Return the matching Webmention, or raise on a SHA-256 collision. :param source: Exact source URL to look up. @@ -50,7 +50,9 @@ def find_webmention_by_digest(source: str, target: str) -> ReceivedWebmention | return webmention -def insert_webmention(source: str, target: str) -> ReceivedWebmention | None: +def try_insert_received_webmention( + source: str, target: str +) -> ReceivedWebmention | None: """Insert and return a Webmention, or None if another request won the race. Use an atomic insert and treat uniqueness conflicts as concurrent submissions. @@ -71,7 +73,7 @@ def insert_webmention(source: str, target: str) -> ReceivedWebmention | None: return webmention -def reopen_if_cooled_down(webmention: ReceivedWebmention) -> bool: +def reopen_if_cooldown_elapsed(webmention: ReceivedWebmention) -> bool: """Atomically reopen an old Webmention and return whether it was reopened. Only a terminal Webmention beyond the cooldown interval can be reset. @@ -97,7 +99,7 @@ def reopen_if_cooled_down(webmention: ReceivedWebmention) -> bool: return rows_affected(result) == 1 -def submit_received(source: str, target: str) -> tuple[uuid.UUID, bool] | None: +def record_submission(source: str, target: str) -> tuple[uuid.UUID, bool] | None: """Return (identifier, enqueue flag), or None on a digest conflict. A new submission is committed before it can be queued for verification. @@ -110,26 +112,28 @@ def submit_received(source: str, target: str) -> tuple[uuid.UUID, bool] | None: :returns: Webmention identifier and enqueue flag, or ``None`` on a digest collision. """ try: - if (webmention := find_webmention_by_digest(source, target)) is None: - if (webmention := insert_webmention(source, target)) is not None: + if (webmention := find_received_webmention(source, target)) is None: + if ( + webmention := try_insert_received_webmention(source, target) + ) is not None: return webmention.uuid, True # A concurrent insert won: look up its row after rolling back. - if (webmention := find_webmention_by_digest(source, target)) is None: + if (webmention := find_received_webmention(source, target)) is None: return None - return webmention.uuid, reopen_if_cooled_down(webmention) + return webmention.uuid, reopen_if_cooldown_elapsed(webmention) except UrlDigestCollisionError: return None -@root_page.post("/endpoint") +@webmentions_bp.post("/endpoint") @limiter.limit( lambda: current_app.config["ENDPOINT_GLOBAL_RATE_LIMIT"], key_func=lambda: "all-clients", ) @limiter.limit(lambda: current_app.config["ENDPOINT_RATE_LIMIT"]) @csrf.exempt -def endpoint() -> ResponseReturnValue: +def receive_webmention() -> ResponseReturnValue: """Receive and queue a Webmention for verification. Existing Webmentions with the same source and target are reset for @@ -139,7 +143,7 @@ def endpoint() -> ResponseReturnValue: :returns: HTTP 201 response with the Webmention status URL in the ``Location`` header, or validation errors with HTTP 400. """ - form = forms.EndpointForm(meta={"csrf": False}) + form = forms.WebmentionSubmissionForm(meta={"csrf": False}) if not form.validate_on_submit(): return form.errors, HTTPStatus.BAD_REQUEST @@ -147,7 +151,7 @@ def endpoint() -> ResponseReturnValue: source = cast(str, form.source.data) target = cast(str, form.target.data) - result = submit_received(source, target) + result = record_submission(source, target) if result is None: return {"error": "Webmention URL fingerprint conflict"}, HTTPStatus.CONFLICT @@ -157,13 +161,15 @@ def endpoint() -> ResponseReturnValue: if should_verify: verify_webmention(identifier) - status_url = url_for("root.status", identifier=str(identifier), _external=True) + status_url = url_for( + "webmentions.webmention_status", identifier=str(identifier), _external=True + ) return Response(status=HTTPStatus.CREATED, headers={"Location": status_url}) -@root_page.route("/status/<uuid:identifier>") -def status(identifier: uuid.UUID) -> ResponseReturnValue: +@webmentions_bp.route("/status/<uuid:identifier>") +def webmention_status(identifier: uuid.UUID) -> ResponseReturnValue: """Display the verification status of a received Webmention. :param identifier: Identifier of the Webmention to display. diff --git a/webmentions_ssg/views/session.py b/webmentions_ssg/views/session.py index 371997d..d9cec7f 100644 --- a/webmentions_ssg/views/session.py +++ b/webmentions_ssg/views/session.py @@ -11,25 +11,25 @@ from flask_login import current_user, login_required, login_user, logout_user from werkzeug.security import check_password_hash from .. import forms -from ..auth import dummy_password_hash, is_safe_next_url, login_username_limit_key +from ..auth import dummy_password_hash, is_safe_next_url, login_username_rate_limit_key from ..extensions import db, limiter from ..models import User -from .blueprint import root_page +from .blueprint import webmentions_bp -@root_page.route("/") +@webmentions_bp.route("/") def index() -> ResponseReturnValue: """Redirect to the received Webmentions view. :returns: Redirect response to the received Webmentions view. """ - return redirect(url_for("root.received")) + return redirect(url_for("webmentions.received_list")) -@root_page.route("/login", methods=["GET", "POST"]) +@webmentions_bp.route("/login", methods=["GET", "POST"]) @limiter.limit( lambda: current_app.config["LOGIN_USERNAME_RATE_LIMIT"], - key_func=login_username_limit_key, + key_func=login_username_rate_limit_key, methods=["POST"], error_message="Too many login attempts. Please try again later.", ) @@ -48,7 +48,7 @@ def login() -> ResponseReturnValue: :returns: Rendered login page or redirect response. """ if current_user.is_authenticated: - return redirect(url_for("root.index")) + return redirect(url_for("webmentions.index")) form = forms.LoginForm() @@ -61,18 +61,18 @@ def login() -> ResponseReturnValue: password_ok = check_password_hash(password_hash, form.password.data or "") if user is None or not password_ok: flash("Invalid username or password", "danger") - return redirect(url_for("root.login")) + return redirect(url_for("webmentions.login")) login_user(user) next_url = request.args.get("next") if not next_url or not is_safe_next_url(next_url): - next_url = url_for("root.index") + next_url = url_for("webmentions.index") return redirect(next_url) return render_template("login.html", form=form) -@root_page.route("/logout", methods=("POST",)) +@webmentions_bp.route("/logout", methods=("POST",)) @login_required def logout() -> ResponseReturnValue: """End the current user's login session. @@ -80,4 +80,4 @@ def logout() -> ResponseReturnValue: :returns: Redirect response to the application index. """ logout_user() - return redirect(url_for("root.index")) + return redirect(url_for("webmentions.index")) |
