diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/tasks/test_consumer.py | 6 | ||||
| -rw-r--r-- | tests/tasks/test_endpoint_discovery.py (renamed from tests/tasks/test_discovery.py) | 8 | ||||
| -rw-r--r-- | tests/tasks/test_extension.py | 28 | ||||
| -rw-r--r-- | tests/tasks/test_init.py | 6 | ||||
| -rw-r--r-- | tests/tasks/test_json_serializer.py | 10 | ||||
| -rw-r--r-- | tests/tasks/test_notifications.py | 14 | ||||
| -rw-r--r-- | tests/tasks/test_retention.py | 6 | ||||
| -rw-r--r-- | tests/tasks/test_scanner.py | 84 | ||||
| -rw-r--r-- | tests/tasks/test_sender.py | 51 | ||||
| -rw-r--r-- | tests/tasks/test_verification.py (renamed from tests/tasks/test_receiver.py) | 235 |
10 files changed, 236 insertions, 212 deletions
diff --git a/tests/tasks/test_consumer.py b/tests/tasks/test_consumer.py index 78f1b6b..bc18583 100644 --- a/tests/tasks/test_consumer.py +++ b/tests/tasks/test_consumer.py @@ -37,13 +37,13 @@ def test_consumer_registers_scanner_digest_reaper_and_retention( monkeypatch.setattr(huey, "periodic_task", periodic_task) consumer = importlib.import_module("webmentions_ssg.tasks.consumer") - from webmentions_ssg.tasks import notifications, receiver, retention, scanner + from webmentions_ssg.tasks import notifications, retention, scanner, verification assert consumer.huey is huey.huey assert periodic_task.call_count == 4 assert registered_tasks == [ scanner.scan_sources, - receiver.recover_stale_verifications, - notifications.send_webmention_digest, + verification.recover_stale_verifications, + notifications.email_verified_digest, retention.purge_old_received_webmentions, ] diff --git a/tests/tasks/test_discovery.py b/tests/tasks/test_endpoint_discovery.py index 4616a53..98c167b 100644 --- a/tests/tasks/test_discovery.py +++ b/tests/tasks/test_endpoint_discovery.py @@ -10,7 +10,7 @@ import httpx import pytest from flask import Flask -from webmentions_ssg.tasks import discovery +from webmentions_ssg.tasks import endpoint_discovery SOURCE_URL = "https://dennisfink.me/blog/example/" TARGET_URL = "https://example.com/post" @@ -21,9 +21,9 @@ STATUS_URL = "https://example.com/webmention/status/123" @pytest.fixture def sender_module(app: Flask) -> ModuleType: _ = app - from webmentions_ssg.tasks import discovery + from webmentions_ssg.tasks import endpoint_discovery - return discovery + return endpoint_discovery @pytest.mark.parametrize( @@ -707,7 +707,7 @@ 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) + monkeypatch.setattr(endpoint_discovery, "is_http_url", is_http_url) response = httpx.Response(HTTPStatus.OK, request=httpx.Request("GET", TARGET_URL)) with pytest.raises( sender_module.PermanentDeliveryError, match="Invalid Webmention endpoint" diff --git a/tests/tasks/test_extension.py b/tests/tasks/test_extension.py index 323353e..adbad09 100644 --- a/tests/tasks/test_extension.py +++ b/tests/tasks/test_extension.py @@ -11,18 +11,18 @@ import pytest from flask import Flask, current_app, has_app_context from huey import crontab -from webmentions_ssg.tasks.extension import Huey +from webmentions_ssg.tasks.extension import FlaskHuey def test_init_without_app() -> None: - huey = Huey() + huey = FlaskHuey() assert huey.app is None with pytest.raises(RuntimeError, match="Huey has not been initialized"): _ = huey.huey def test_init_with_app(app: Flask) -> None: - huey = Huey(app) + huey = FlaskHuey(app) assert huey.app is app assert huey.huey.name == app.import_name assert huey.huey.results is True @@ -42,7 +42,7 @@ def test_init_app_uses_huey_configuration(app: Flask) -> None: "immediate": False, } - huey = Huey() + huey = FlaskHuey() huey.init_app(app) assert huey.huey.name == "custom-name" @@ -56,12 +56,14 @@ def test_huey_url_takes_precedence_over_huey_configuration(app: Flask) -> None: app.config["HUEY"] = {"url": "blackhole://"} app.config["HUEY_URL"] = "memory://" - huey = Huey(app) + huey = FlaskHuey(app) assert type(huey.huey).__name__ == "MemoryHuey" -def task_decorator(huey: Huey, periodic: bool) -> Callable[[Callable[..., Any]], Any]: +def task_decorator( + huey: FlaskHuey, periodic: bool +) -> Callable[[Callable[..., Any]], Any]: if periodic: return huey.periodic_task(crontab(minute="*")) @@ -70,7 +72,7 @@ def task_decorator(huey: Huey, periodic: bool) -> Callable[[Callable[..., Any]], @pytest.mark.parametrize("periodic", [False, True], ids=["task", "periodic-task"]) def test_task_runs_in_app_context(app: Flask, periodic: bool) -> None: - huey = Huey(app) + huey = FlaskHuey(app) @task_decorator(huey, periodic) def add(a: int, b: int) -> int: @@ -82,7 +84,7 @@ def test_task_runs_in_app_context(app: Flask, periodic: bool) -> None: @pytest.mark.parametrize("periodic", [False, True], ids=["task", "periodic-task"]) def test_task_raises_without_app(app: Flask, periodic: bool) -> None: - huey = Huey(app) + huey = FlaskHuey(app) @task_decorator(huey, periodic) def task() -> None: @@ -100,7 +102,7 @@ def test_task_declaration_before_init_and_rebind_after_init(periodic: bool) -> N :param periodic: Pytest fixture providing the periodic-task test function. """ - queue = Huey() + queue = FlaskHuey() observed_apps: list[str] = [] @task_decorator(queue, periodic) @@ -138,7 +140,7 @@ def test_task_declaration_before_init_and_rebind_after_init(periodic: bool) -> N def test_multiple_deferred_tasks_registered_on_initialization() -> None: - queue = Huey() + queue = FlaskHuey() @queue.task(retries=2, retry_delay=50) def first(value: int) -> int: @@ -157,7 +159,7 @@ def test_multiple_deferred_tasks_registered_on_initialization() -> None: def test_getattr_delegates_to_huey(app: Flask) -> None: - huey = Huey(app) + huey = FlaskHuey(app) assert huey.immediate is True @@ -221,7 +223,7 @@ def test_backend_from_url( monkeypatch.setattr(huey_package, backend_name, backend_class) - actual_class, actual_storage_kwargs = Huey.backend_from_url(url) + actual_class, actual_storage_kwargs = FlaskHuey.backend_from_url(url) assert actual_class is backend_class assert actual_storage_kwargs == storage_kwargs @@ -254,4 +256,4 @@ def test_backend_from_url( ) def test_backend_from_url_rejects_invalid_url(url: str, message: str) -> None: with pytest.raises(RuntimeError, match=re.escape(message)): - Huey.backend_from_url(url) + FlaskHuey.backend_from_url(url) diff --git a/tests/tasks/test_init.py b/tests/tasks/test_init.py index 1a51fb8..380f885 100644 --- a/tests/tasks/test_init.py +++ b/tests/tasks/test_init.py @@ -4,9 +4,9 @@ """Test the public background-task package API.""" -from webmentions_ssg.tasks import Huey -from webmentions_ssg.tasks.extension import Huey as ExtensionHuey +from webmentions_ssg.tasks import FlaskHuey +from webmentions_ssg.tasks.extension import FlaskHuey as ExtensionHuey def test_tasks_package_exports_huey_extension() -> None: - assert Huey is ExtensionHuey + assert FlaskHuey is ExtensionHuey diff --git a/tests/tasks/test_json_serializer.py b/tests/tasks/test_json_serializer.py index 442e6a9..6077ebd 100644 --- a/tests/tasks/test_json_serializer.py +++ b/tests/tasks/test_json_serializer.py @@ -12,13 +12,13 @@ import pytest from flask import Flask from huey.utils import SKIPPED, Error -from webmentions_ssg.tasks.extension import Huey +from webmentions_ssg.tasks.extension import FlaskHuey 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) + huey = FlaskHuey(app) assert isinstance(huey.huey.serializer, TaggedJSONSerializer) assert huey.huey.serializer.serialize("hello") == b'"hello"' @@ -26,7 +26,7 @@ def test_json_is_default_even_for_production_style_broker(app: Flask) -> None: def test_uuid_and_retry_message_survive_enqueue_dequeue(app: Flask) -> None: app.config["HUEY"] = {"immediate": False} - huey = Huey(app) + huey = FlaskHuey(app) identifier = uuid.uuid4() @huey.task(retries=2, retry_delay=50) @@ -87,12 +87,12 @@ def test_rejects_unknown_and_malformed_types() -> None: def test_refuses_unsafe_serializer_configuration(app: Flask) -> None: app.config["HUEY"] = {"serializer": None} with pytest.raises(RuntimeError, match="JSON is required"): - Huey(app) + FlaskHuey(app) def test_compression_with_json(app: Flask) -> None: app.config["HUEY"] = {"compression": True, "immediate": False} - huey = Huey(app) + huey = FlaskHuey(app) payload = {"id": uuid.uuid4()} raw = huey.huey.serializer.serialize(payload) diff --git a/tests/tasks/test_notifications.py b/tests/tasks/test_notifications.py index a1a002e..ca89a7a 100644 --- a/tests/tasks/test_notifications.py +++ b/tests/tasks/test_notifications.py @@ -49,8 +49,8 @@ def test_digest_sends_one_email_for_multiple_verified_mentions( with app.app_context(): with patch("webmentions_ssg.tasks.notifications.mail.send") as send: - notifications.send_webmention_digest() - notifications.send_webmention_digest() + notifications.email_verified_digest() + notifications.email_verified_digest() send.assert_called_once() message = send.call_args.args[0] @@ -80,7 +80,7 @@ def test_digest_skips_when_notifications_disabled( with app.app_context(): with patch("webmentions_ssg.tasks.notifications.mail.send") as send: - notifications.send_webmention_digest() + notifications.email_verified_digest() send.assert_not_called() mention = db.session.get(ReceivedWebmention, identifier) @@ -103,7 +103,7 @@ def test_digest_does_not_mark_mentions_when_email_fails( ), pytest.raises(RuntimeError, match="SMTP unavailable"), ): - notifications.send_webmention_digest() + notifications.email_verified_digest() mention = db.session.get(ReceivedWebmention, identifier) @@ -111,7 +111,7 @@ def test_digest_does_not_mark_mentions_when_email_fails( assert mention.notification_sent_at is None with patch("webmentions_ssg.tasks.notifications.mail.send") as send: - notifications.send_webmention_digest() + notifications.email_verified_digest() send.assert_called_once() mention = db.session.get(ReceivedWebmention, identifier) @@ -127,7 +127,7 @@ def test_digest_skips_when_no_new_mentions( with app.app_context(): with patch("webmentions_ssg.tasks.notifications.mail.send") as send: - notifications.send_webmention_digest() + notifications.email_verified_digest() send.assert_not_called() @@ -149,4 +149,4 @@ def test_digest_lock_uses_current_backend( huey.lock_task("send-webmention-digest"), pytest.raises(TaskLockedException), ): - notifications.send_webmention_digest() + notifications.email_verified_digest() diff --git a/tests/tasks/test_retention.py b/tests/tasks/test_retention.py index 9e08837..70ab0e0 100644 --- a/tests/tasks/test_retention.py +++ b/tests/tasks/test_retention.py @@ -22,9 +22,9 @@ def test_retention_only_removes_old_failed_or_deleted_rows(app: Flask) -> None: with app.app_context(): for suffix, status, age_days in ( ("old-failed", "failed", 31), - ("old-deleted", "deleted", 31), + ("old-source-removed", "source_removed", 31), ("recent-failed", "failed", 2), - ("recent-deleted", "deleted", 2), + ("recent-source-removed", "source_removed", 2), ("old-verified", "verified", 50), ("old-received", "received", 50), ("old-verifying", "verifying", 50), @@ -49,7 +49,7 @@ def test_retention_only_removes_old_failed_or_deleted_rows(app: Flask) -> None: assert remaining == { identifier for key, identifier in identifiers.items() - if key not in {"old-failed", "old-deleted"} + if key not in {"old-failed", "old-source-removed"} } diff --git a/tests/tasks/test_scanner.py b/tests/tasks/test_scanner.py index 00256d0..4cf2a67 100644 --- a/tests/tasks/test_scanner.py +++ b/tests/tasks/test_scanner.py @@ -17,7 +17,7 @@ from huey.exceptions import RetryTask from pytest import MonkeyPatch from webmentions_ssg import db -from webmentions_ssg.models import SentWebmention, SentWebmentionStatus, Source +from webmentions_ssg.models import SentWebmention, SentWebmentionStatus, SourceDocument from webmentions_ssg.tasks import source_parser from .scanner_helpers import BASE_URL, SOURCE_URL, configure_scanner, write_post @@ -65,7 +65,7 @@ def test_manual_scan_runs_without_lock_contention( with app.app_context(): scanner_module.manual_scan_sources.call_local() source = db.session.scalar( - sa.select(Source).where(Source.path == "manual/index.html") + sa.select(SourceDocument).where(SourceDocument.path == "manual/index.html") ) assert source is not None assert source.url == f"{BASE_URL}manual/" @@ -101,7 +101,7 @@ def test_reconcile_source_tree_returns_count_and_seen_paths( ) monkeypatch.setattr(scanner_module, "scan_source_file", parse) - sources: dict[str, Source] = {} + sources: dict[str, SourceDocument] = {} urls: dict[str, str] = {} with app.app_context(): @@ -141,7 +141,7 @@ def test_scan_creates_source_and_webmentions( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source)) + source = db.session.scalar(sa.select(SourceDocument)) assert source is not None assert source.path == "example/index.html" @@ -161,7 +161,7 @@ def test_scan_creates_source_and_webmentions( "https://example.com/b", ] - assert all(webmention.active for webmention in webmentions) + assert all(webmention.linked for webmention in webmentions) assert all(webmention.desired_revision == 1 for webmention in webmentions) assert all(webmention.processed_revision is None for webmention in webmentions) @@ -198,7 +198,7 @@ def test_duplicate_canonical_url_skips_alias_and_commits_other_sources( run_scan(app, scanner_module) with app.app_context(): - sources = list(db.session.scalars(sa.select(Source))) + sources = list(db.session.scalars(sa.select(SourceDocument))) webmentions = list(db.session.scalars(sa.select(SentWebmention))) assert len(sources) == 2 @@ -262,7 +262,7 @@ def test_existing_source_owns_canonical_url_when_alias_is_added( run_scan(app, scanner_module) with app.app_context(): - sources = list(db.session.scalars(sa.select(Source))) + sources = list(db.session.scalars(sa.select(SourceDocument))) targets = list(db.session.scalars(sa.select(SentWebmention.target))) assert len(sources) == 1 @@ -311,7 +311,8 @@ def test_deleted_source_still_reserves_its_canonical_url( with app.app_context(): sources = { - source.path: source for source in db.session.scalars(sa.select(Source)) + source.path: source + for source in db.session.scalars(sa.select(SourceDocument)) } assert set(sources) == {"owner/index.html", "unrelated/index.html"} assert sources["owner/index.html"].deleted_at is not None @@ -358,7 +359,7 @@ def test_unchanged_source_does_not_increment_revision( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source)) + source = db.session.scalar(sa.select(SourceDocument)) assert source is not None assert source.revision == 1 @@ -388,7 +389,7 @@ def test_changing_relative_timestamp_does_not_resend( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source)) + source = db.session.scalar(sa.select(SourceDocument)) webmention = db.session.scalar(sa.select(SentWebmention)) assert source is not None and webmention is not None original_hash = source.content_hash @@ -405,7 +406,7 @@ def test_changing_relative_timestamp_does_not_resend( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source)) + source = db.session.scalar(sa.select(SourceDocument)) webmention = db.session.scalar(sa.select(SentWebmention)) assert source is not None and webmention is not None assert source.content_hash == original_hash @@ -485,7 +486,7 @@ def test_updated_source_increments_revision( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source)) + source = db.session.scalar(sa.select(SourceDocument)) webmention = db.session.scalar(sa.select(SentWebmention)) assert source is not None @@ -493,7 +494,7 @@ def test_updated_source_increments_revision( assert source.revision == 2 - assert webmention.active + assert webmention.linked assert webmention.desired_revision == 2 assert webmention.processed_revision == 1 assert webmention.sent_revision == 1 @@ -555,7 +556,7 @@ def test_new_target_is_added_on_update( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source)) + source = db.session.scalar(sa.select(SourceDocument)) webmention = db.session.scalar(sa.select(SentWebmention)) assert source is not None @@ -564,7 +565,7 @@ def test_new_target_is_added_on_update( assert source.revision == 2 assert webmention.target == ("https://example.com/new") - assert webmention.active + assert webmention.linked assert webmention.desired_revision == 2 assert webmention.processed_revision is None assert webmention.sent_revision is None @@ -638,7 +639,7 @@ def test_removed_sent_target_is_queued_again( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source)) + source = db.session.scalar(sa.select(SourceDocument)) webmention = db.session.scalar(sa.select(SentWebmention)) assert source is not None @@ -646,7 +647,7 @@ def test_removed_sent_target_is_queued_again( assert source.revision == 2 - assert not webmention.active + assert not webmention.linked assert webmention.desired_revision == 2 assert webmention.processed_revision == 1 assert webmention.sent_revision == 1 @@ -706,7 +707,7 @@ def test_removed_unsent_target_is_not_queued( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source)) + source = db.session.scalar(sa.select(SourceDocument)) webmention = db.session.scalar(sa.select(SentWebmention)) assert source is not None @@ -714,7 +715,7 @@ def test_removed_unsent_target_is_not_queued( assert source.revision == 2 - assert not webmention.active + assert not webmention.linked assert webmention.desired_revision == 2 assert webmention.processed_revision == 2 assert webmention.sent_revision is None @@ -756,14 +757,14 @@ def test_empty_output_directory_does_not_delete_sources_or_queue_removals( run_scan(app, scanner_module) with app.app_context(): - sources = list(db.session.scalars(sa.select(Source))) + sources = list(db.session.scalars(sa.select(SourceDocument))) webmentions = list(db.session.scalars(sa.select(SentWebmention))) assert len(sources) == 2 assert all(source.revision == 1 for source in sources) assert all(source.deleted_at is None for source in sources) assert len(webmentions) == 2 - assert all(webmention.active for webmention in webmentions) + assert all(webmention.linked for webmention in webmentions) assert all(webmention.desired_revision == 1 for webmention in webmentions) assert queued == [] @@ -775,7 +776,7 @@ def test_empty_output_directory_does_not_delete_sources_or_queue_removals( run_scan(app, scanner_module) with app.app_context(): - sources = list(db.session.scalars(sa.select(Source))) + sources = list(db.session.scalars(sa.select(SourceDocument))) assert all(source.revision == 1 for source in sources) assert all(source.deleted_at is None for source in sources) @@ -802,7 +803,7 @@ def test_no_successful_pages_defers_pending_webmentions( assert queued == [] with app.app_context(): - source = db.session.scalar(sa.select(Source)) + source = db.session.scalar(sa.select(SourceDocument)) webmention = db.session.scalar(sa.select(SentWebmention)) assert source is not None and source.deleted_at is None assert webmention is not None and webmention.desired_revision == 1 @@ -826,7 +827,7 @@ def test_failed_scan_does_not_delete_missing_source( run_scan(app, scanner_module) with app.app_context(): - sources = list(db.session.scalars(sa.select(Source))) + sources = list(db.session.scalars(sa.select(SourceDocument))) assert len(sources) == 2 assert all(source.revision == 1 for source in sources) assert all(source.deleted_at is None for source in sources) @@ -873,7 +874,9 @@ def test_deleted_source_queues_previously_sent_webmention( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source).where(Source.url == SOURCE_URL)) + source = db.session.scalar( + sa.select(SourceDocument).where(SourceDocument.url == SOURCE_URL) + ) webmention = db.session.scalar(sa.select(SentWebmention)) assert source is not None @@ -882,7 +885,7 @@ def test_deleted_source_queues_previously_sent_webmention( assert source.revision == 2 assert source.deleted_at is not None - assert not webmention.active + assert not webmention.linked assert webmention.desired_revision == 2 assert webmention.processed_revision == 1 assert webmention.sent_revision == 1 @@ -919,7 +922,9 @@ def test_deleted_source_does_not_queue_unsent_webmention( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source).where(Source.url == SOURCE_URL)) + source = db.session.scalar( + sa.select(SourceDocument).where(SourceDocument.url == SOURCE_URL) + ) webmention = db.session.scalar(sa.select(SentWebmention)) assert source is not None @@ -928,7 +933,7 @@ def test_deleted_source_does_not_queue_unsent_webmention( assert source.revision == 2 assert source.deleted_at is not None - assert not webmention.active + assert not webmention.linked assert webmention.desired_revision == 2 assert webmention.processed_revision == 2 assert webmention.sent_revision is None @@ -988,7 +993,9 @@ def test_restored_source_creates_new_revision( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source).where(Source.url == SOURCE_URL)) + source = db.session.scalar( + sa.select(SourceDocument).where(SourceDocument.url == SOURCE_URL) + ) webmention = db.session.scalar(sa.select(SentWebmention)) assert source is not None @@ -997,7 +1004,7 @@ def test_restored_source_creates_new_revision( assert source.revision == 3 assert source.deleted_at is None - assert webmention.active + assert webmention.linked assert webmention.desired_revision == 3 assert webmention.sent_revision == 1 @@ -1129,7 +1136,7 @@ def test_invalid_known_source_is_not_deleted( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source)) + source = db.session.scalar(sa.select(SourceDocument)) assert source is not None assert source.revision == 1 @@ -1186,7 +1193,7 @@ def test_unexpected_parser_error_does_not_abort_other_sources( run_scan(app, scanner_module) with app.app_context(): - sources = list(db.session.scalars(sa.select(Source))) + sources = list(db.session.scalars(sa.select(SourceDocument))) webmention = db.session.scalar(sa.select(SentWebmention)) assert [source.path for source in sources] == ["good/index.html"] @@ -1225,7 +1232,8 @@ def test_unexpected_parser_error_does_not_delete_known_source( with app.app_context(): sources = { - source.path: source for source in db.session.scalars(sa.select(Source)) + source.path: source + for source in db.session.scalars(sa.select(SourceDocument)) } bad = sources["bad/index.html"] good = sources["good/index.html"] @@ -1274,7 +1282,7 @@ def test_scan_sources_skips_symlink_outside_root( run_scan(app, scanner_module) with app.app_context(): - sources = list(db.session.scalars(sa.select(Source))) + sources = list(db.session.scalars(sa.select(SourceDocument))) assert len(sources) == 1 assert sources[0].path == "example/index.html" assert "escapes source directory" in caplog.text @@ -1314,7 +1322,7 @@ def test_scan_sources_accepts_valid_base_url( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source)) + source = db.session.scalar(sa.select(SourceDocument)) assert source is not None assert source.url == SOURCE_URL @@ -1364,7 +1372,7 @@ def test_changed_source_url_is_rejected( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source)) + source = db.session.scalar(sa.select(SourceDocument)) assert source is not None assert source.url == SOURCE_URL @@ -1381,7 +1389,7 @@ def test_scan_sources_returns_when_source_directory_is_not_configured( run_scan(app, scanner_module) with app.app_context(): - assert db.session.scalar(sa.select(Source)) is None + assert db.session.scalar(sa.select(SourceDocument)) is None def test_scan_sources_rejects_missing_source_directory( @@ -1448,7 +1456,7 @@ def test_scan_sources_ignores_root_index( run_scan(app, scanner_module) with app.app_context(): - sources = list(db.session.scalars(sa.select(Source))) + sources = list(db.session.scalars(sa.select(SourceDocument))) assert len(sources) == 1 assert sources[0].path == "example/index.html" diff --git a/tests/tasks/test_sender.py b/tests/tasks/test_sender.py index 032b638..9a0430c 100644 --- a/tests/tasks/test_sender.py +++ b/tests/tasks/test_sender.py @@ -14,7 +14,7 @@ import pytest from flask import Flask from webmentions_ssg import db -from webmentions_ssg.models import SentWebmention, SentWebmentionStatus, Source +from webmentions_ssg.models import SentWebmention, SentWebmentionStatus, SourceDocument SOURCE_URL = "https://dennisfink.me/blog/example/" TARGET_URL = "https://example.com/post" @@ -25,7 +25,7 @@ STATUS_URL = "https://example.com/webmention/status/123" def create_sent_webmention( app: Flask, *, - active: bool = True, + linked: bool = True, desired_revision: int = 1, processed_revision: int | None = None, sent_revision: int | None = None, @@ -40,7 +40,7 @@ def create_sent_webmention( now = datetime.now(UTC) with app.app_context(): - source = Source( + source = SourceDocument( path="example/index.html", url=SOURCE_URL, content_hash="0" * 32, @@ -51,14 +51,14 @@ def create_sent_webmention( webmention = SentWebmention( target=TARGET_URL, - active=active, + linked=linked, desired_revision=desired_revision, processed_revision=processed_revision, sent_revision=sent_revision, status=status, failure_reason=failure_reason, - endpoint=endpoint, - response_status=response_status, + webmention_endpoint=endpoint, + endpoint_response_status=response_status, status_url=status_url, attempt_count=attempt_count, next_retry_at=next_retry_at, @@ -145,8 +145,7 @@ def test_attempt_delivery_returns_retryable_outcome( reason="Webmention endpoint returned HTTP 503", endpoint=ENDPOINT_URL, response_status=HTTPStatus.SERVICE_UNAVAILABLE, - terminal=False, - temporary_failure=True, + retryable=True, ) @@ -174,8 +173,8 @@ def test_send_webmention_persists_success( assert webmention.sent_revision == 1 assert webmention.status == SentWebmentionStatus.SENT assert webmention.failure_reason is None - assert webmention.endpoint == ENDPOINT_URL - assert webmention.response_status == HTTPStatus.CREATED + assert webmention.webmention_endpoint == ENDPOINT_URL + assert webmention.endpoint_response_status == HTTPStatus.CREATED assert webmention.status_url == STATUS_URL assert webmention.last_attempted_at is not None assert webmention.last_sent_at is not None @@ -212,10 +211,10 @@ def test_send_webmention_marks_unsupported_target_processed( assert webmention is not None assert webmention.processed_revision == 2 assert webmention.sent_revision == 1 - assert webmention.status == SentWebmentionStatus.UNSUPPORTED + assert webmention.status == SentWebmentionStatus.NO_ENDPOINT assert webmention.failure_reason == "No Webmention endpoint discovered" - assert webmention.endpoint is None - assert webmention.response_status is None + assert webmention.webmention_endpoint is None + assert webmention.endpoint_response_status is None assert webmention.status_url is None assert webmention.last_attempted_at is not None @@ -246,8 +245,8 @@ def test_send_webmention_persists_permanent_failure( assert webmention.sent_revision == 1 assert webmention.status == SentWebmentionStatus.FAILED assert webmention.failure_reason == "Webmention endpoint returned HTTP 400" - assert webmention.endpoint == ENDPOINT_URL - assert webmention.response_status == HTTPStatus.BAD_REQUEST + assert webmention.webmention_endpoint == ENDPOINT_URL + assert webmention.endpoint_response_status == HTTPStatus.BAD_REQUEST assert webmention.status_url is None @@ -277,8 +276,8 @@ def test_send_webmention_persists_temporary_failure_with_backoff( assert webmention.sent_revision == 1 assert webmention.status == SentWebmentionStatus.FAILED assert webmention.failure_reason == "Webmention endpoint returned HTTP 503" - assert webmention.endpoint == ENDPOINT_URL - assert webmention.response_status == HTTPStatus.SERVICE_UNAVAILABLE + assert webmention.webmention_endpoint == ENDPOINT_URL + assert webmention.endpoint_response_status == HTTPStatus.SERVICE_UNAVAILABLE assert webmention.status_url is None assert webmention.attempt_count == 1 assert webmention.next_retry_at is not None @@ -443,8 +442,10 @@ def test_sender_malformed_request_is_terminal( assert webmention.processed_revision == 1 assert webmention.attempt_count == 1 assert webmention.next_retry_at is None - assert webmention.endpoint == (ENDPOINT_URL if stage == "post" else None) - assert webmention.response_status is None + assert webmention.webmention_endpoint == ( + ENDPOINT_URL if stage == "post" else None + ) + assert webmention.endpoint_response_status is None assert webmention.last_attempted_at is not None assert httpx_client.call_count == 1 @@ -554,7 +555,7 @@ def test_send_webmention_lock_is_scoped_to_uuid( first = db.session.get(SentWebmention, first_id) assert first is not None second = SentWebmention( - source_id=first.source_id, target=other_target, desired_revision=1 + source_uuid=first.source_uuid, target=other_target, desired_revision=1 ) db.session.add(second) db.session.commit() @@ -601,7 +602,7 @@ def test_send_webmention_ignores_processed_revision( @pytest.mark.parametrize( - "outcome", ["success", "unsupported", "temporary-failure", "permanent-failure"] + "outcome", ["success", "no_endpoint", "temporary-failure", "permanent-failure"] ) def test_send_webmention_ignores_superseded_attempt( app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch, outcome: str @@ -627,12 +628,12 @@ def test_send_webmention_ignores_superseded_attempt( post.side_effect = post_success - case "unsupported": + case "no_endpoint": - def discover_unsupported(*args, **kwargs) -> None: + def discover_no_endpoint(*args, **kwargs) -> None: supersede_revision() - discover.side_effect = discover_unsupported + discover.side_effect = discover_no_endpoint case "temporary-failure": @@ -708,5 +709,5 @@ def test_send_does_not_persist_unsafe_status_url( mention = db.session.get(SentWebmention, identifier) assert mention is not None assert mention.status == SentWebmentionStatus.SENT - assert mention.response_status == HTTPStatus.CREATED + assert mention.endpoint_response_status == HTTPStatus.CREATED assert mention.status_url == expected diff --git a/tests/tasks/test_receiver.py b/tests/tasks/test_verification.py index 7241247..c336b19 100644 --- a/tests/tasks/test_receiver.py +++ b/tests/tasks/test_verification.py @@ -23,10 +23,10 @@ TARGET_URL = "https://dennisfink.me/blog/example/" @pytest.fixture -def receiver(app: Flask) -> ModuleType: - from webmentions_ssg.tasks import receiver +def verification(app: Flask) -> ModuleType: + from webmentions_ssg.tasks import verification - return receiver + return verification def create_webmention( @@ -69,9 +69,9 @@ def set_stream_response(httpx_client: Mock, response: httpx.Response) -> Mock: return client -@patch("webmentions_ssg.tasks.receiver.httpx.Client") +@patch("webmentions_ssg.tasks.verification.httpx.Client") def test_fetch_source_document_returns_response_and_body( - httpx_client: Mock, app: Flask, receiver: ModuleType + httpx_client: Mock, app: Flask, verification: ModuleType ) -> None: response = httpx.Response( HTTPStatus.OK, @@ -82,7 +82,7 @@ def test_fetch_source_document_returns_response_and_body( client = set_stream_response(httpx_client, response) with app.app_context(): - fetched_response, body = receiver.fetch_source_document(SOURCE_URL) + fetched_response, body = verification.fetch_source_document(SOURCE_URL) assert fetched_response is response assert body == b"<p>Document</p>" @@ -92,7 +92,7 @@ def test_fetch_source_document_returns_response_and_body( assert options["headers"] == { "Accept": "text/html, application/xhtml+xml;q=0.9, text/plain;q=0.8", - "User-Agent": f"{receiver.APP_NAME}/{receiver.VERSION} ReceivedWebmention", + "User-Agent": f"{verification.APP_NAME}/{verification.VERSION} ReceivedWebmention", } assert options["follow_redirects"] is True assert options["max_redirects"] == app.config.get("MAX_REDIRECTS", 20) @@ -100,7 +100,7 @@ def test_fetch_source_document_returns_response_and_body( assert options["transport"].__class__.__name__ == "PublicHTTPTransport" -@patch("webmentions_ssg.tasks.receiver.httpx.Client") +@patch("webmentions_ssg.tasks.verification.httpx.Client") @pytest.mark.parametrize( ("status_code", "exception_name"), [ @@ -117,7 +117,7 @@ def test_fetch_source_document_returns_response_and_body( def test_fetch_source_document_maps_http_status_to_exception( httpx_client: Mock, app: Flask, - receiver: ModuleType, + verification: ModuleType, status_code: int, exception_name: str, ) -> None: @@ -126,15 +126,15 @@ def test_fetch_source_document_maps_http_status_to_exception( httpx.Response(status_code, request=httpx.Request("GET", SOURCE_URL)), ) - exception_type = getattr(receiver, exception_name) + exception_type = getattr(verification, exception_name) with app.app_context(), pytest.raises(exception_type, match=f"HTTP {status_code}"): - receiver.fetch_source_document(SOURCE_URL) + verification.fetch_source_document(SOURCE_URL) -@patch("webmentions_ssg.tasks.receiver.httpx.Client") +@patch("webmentions_ssg.tasks.verification.httpx.Client") def test_fetch_source_document_propagates_network_error( - httpx_client: Mock, app: Flask, receiver: ModuleType + httpx_client: Mock, app: Flask, verification: ModuleType ) -> None: client = httpx_client.return_value.__enter__.return_value client.stream.side_effect = httpx.ConnectError( @@ -145,12 +145,12 @@ def test_fetch_source_document_propagates_network_error( app.app_context(), pytest.raises(httpx.ConnectError, match="Connection refused"), ): - receiver.fetch_source_document(SOURCE_URL) + verification.fetch_source_document(SOURCE_URL) -@patch("webmentions_ssg.tasks.receiver.httpx.Client") +@patch("webmentions_ssg.tasks.verification.httpx.Client") def test_fetch_source_document_rejects_declared_oversized_body( - httpx_client: Mock, app: Flask, receiver: ModuleType + httpx_client: Mock, app: Flask, verification: ModuleType ) -> None: app.config["MAX_RECEIVED_SOURCE_BYTES"] = 10 @@ -166,14 +166,16 @@ def test_fetch_source_document_rejects_declared_oversized_body( with ( app.app_context(), - pytest.raises(receiver.VerificationError, match="Source document is too large"), + pytest.raises( + verification.VerificationError, match="Source document is too large" + ), ): - receiver.fetch_source_document(SOURCE_URL) + verification.fetch_source_document(SOURCE_URL) -@patch("webmentions_ssg.tasks.receiver.httpx.Client") +@patch("webmentions_ssg.tasks.verification.httpx.Client") def test_fetch_source_document_rejects_streamed_oversized_body( - httpx_client: Mock, app: Flask, receiver: ModuleType + httpx_client: Mock, app: Flask, verification: ModuleType ) -> None: app.config["MAX_RECEIVED_SOURCE_BYTES"] = 10 @@ -194,16 +196,18 @@ def test_fetch_source_document_rejects_streamed_oversized_body( with ( app.app_context(), - pytest.raises(receiver.VerificationError, match="Source document is too large"), + pytest.raises( + verification.VerificationError, match="Source document is too large" + ), ): - receiver.fetch_source_document(SOURCE_URL) + verification.fetch_source_document(SOURCE_URL) -@patch("webmentions_ssg.tasks.receiver.httpx.Client") +@patch("webmentions_ssg.tasks.verification.httpx.Client") def test_fetch_source_document_does_not_mask_unexpected_value_error( httpx_client: Mock, app: Flask, - receiver: ModuleType, + verification: ModuleType, monkeypatch: pytest.MonkeyPatch, ) -> None: set_stream_response( @@ -211,14 +215,16 @@ def test_fetch_source_document_does_not_mask_unexpected_value_error( httpx.Response(HTTPStatus.OK, request=httpx.Request("GET", SOURCE_URL)), ) monkeypatch.setattr( - receiver, "read_limited_body", Mock(side_effect=ValueError("unexpected bug")) + verification, + "read_limited_body", + Mock(side_effect=ValueError("unexpected bug")), ) with app.app_context(), pytest.raises(ValueError, match="unexpected bug"): - receiver.fetch_source_document(SOURCE_URL) + verification.fetch_source_document(SOURCE_URL) -@patch("webmentions_ssg.tasks.receiver.fetch_source_document") +@patch("webmentions_ssg.tasks.verification.fetch_source_document") @pytest.mark.parametrize( ("content_type", "body", "expected"), [ @@ -271,7 +277,7 @@ def test_fetch_source_document_does_not_mask_unexpected_value_error( ) def test_source_mentions_target_by_media_type( fetch_source_document: Mock, - receiver: ModuleType, + verification: ModuleType, content_type: str, body: bytes, expected: bool, @@ -284,11 +290,11 @@ def test_source_mentions_target_by_media_type( ) fetch_source_document.return_value = response, body - assert receiver.source_mentions_target(SOURCE_URL, TARGET_URL) is expected + assert verification.source_mentions_target(SOURCE_URL, TARGET_URL) is expected fetch_source_document.assert_called_once_with(SOURCE_URL) -@patch("webmentions_ssg.tasks.receiver.fetch_source_document") +@patch("webmentions_ssg.tasks.verification.fetch_source_document") @pytest.mark.parametrize( ("content_type", "expected_media_type"), [ @@ -299,7 +305,7 @@ def test_source_mentions_target_by_media_type( ) def test_source_mentions_target_rejects_unsupported_media_type( fetch_source_document: Mock, - receiver: ModuleType, + verification: ModuleType, content_type: str, expected_media_type: str, ) -> None: @@ -317,37 +323,42 @@ def test_source_mentions_target_rejects_unsupported_media_type( fetch_source_document.return_value = response, b"Document" with pytest.raises( - receiver.VerificationError, + verification.VerificationError, match=f"Unsupported source content type: {expected_media_type}", ): - receiver.source_mentions_target(SOURCE_URL, TARGET_URL) + verification.source_mentions_target(SOURCE_URL, TARGET_URL) @pytest.mark.parametrize( ("mentions", "expected_status", "expected_reason"), - [(True, "verified", None), (False, "deleted", "Source does not mention target")], + [ + (True, "verified", None), + (False, "source_removed", "Source does not mention target"), + ], ) def test_determine_verification_outcome_returns_outcome( - receiver: ModuleType, + verification: ModuleType, monkeypatch: pytest.MonkeyPatch, mentions: bool, expected_status: str, expected_reason: str | None, ) -> None: - monkeypatch.setattr(receiver, "source_mentions_target", Mock(return_value=mentions)) - claim = receiver.VerificationClaim( + monkeypatch.setattr( + verification, "source_mentions_target", Mock(return_value=mentions) + ) + claim = verification.VerificationClaim( uuid.uuid7(), SOURCE_URL, TARGET_URL, datetime.now(UTC) ) - assert receiver.determine_verification_outcome(claim) == ( + assert verification.determine_verification_outcome(claim) == ( expected_status, expected_reason, ) -@patch("webmentions_ssg.tasks.receiver.source_mentions_target") +@patch("webmentions_ssg.tasks.verification.source_mentions_target") def test_verify_webmention_marks_row_verifying_before_check( - source_mentions_target: Mock, app: Flask, receiver: ModuleType + source_mentions_target: Mock, app: Flask, verification: ModuleType ) -> None: identifier = create_webmention( app, status="failed", failure_reason="Earlier failure" @@ -365,24 +376,24 @@ def test_verify_webmention_marks_row_verifying_before_check( return True source_mentions_target.side_effect = verify_source - receiver.verify_webmention.call_local(identifier) + verification.verify_webmention.call_local(identifier) assert get_webmention_state(app, identifier) == ("verified", None) -@patch("webmentions_ssg.tasks.receiver.source_mentions_target") +@patch("webmentions_ssg.tasks.verification.source_mentions_target") @pytest.mark.parametrize( ("outcome", "expected_status", "expected_reason"), [ ("verified", "verified", None), - ("missing", "deleted", "Source does not mention target"), - ("gone", "deleted", "Source returned HTTP 410"), + ("missing", "source_removed", "Source does not mention target"), + ("gone", "source_removed", "Source returned HTTP 410"), ("permanent-failure", "failed", "Source returned HTTP 404"), ], ) def test_verify_webmention_persists_final_state( source_mentions_target: Mock, app: Flask, - receiver: ModuleType, + verification: ModuleType, outcome: str, expected_status: str, expected_reason: str | None, @@ -395,30 +406,30 @@ def test_verify_webmention_persists_final_state( case "missing": source_mentions_target.return_value = False case "gone": - source_mentions_target.side_effect = receiver.SourceGoneError( + source_mentions_target.side_effect = verification.SourceGoneError( "Source returned HTTP 410" ) case "permanent-failure": - source_mentions_target.side_effect = receiver.VerificationError( + source_mentions_target.side_effect = verification.VerificationError( "Source returned HTTP 404" ) case _: raise AssertionError(f"Unexpected outcome: {outcome}") - receiver.verify_webmention.call_local(identifier) + verification.verify_webmention.call_local(identifier) assert get_webmention_state(app, identifier) == (expected_status, expected_reason) -@patch("webmentions_ssg.tasks.receiver.source_mentions_target") +@patch("webmentions_ssg.tasks.verification.source_mentions_target") @pytest.mark.parametrize("failure", ["temporary-http", "network"]) def test_verify_webmention_persists_retryable_failure_and_reraises( - source_mentions_target: Mock, app: Flask, receiver: ModuleType, failure: str + source_mentions_target: Mock, app: Flask, verification: ModuleType, failure: str ) -> None: identifier = create_webmention(app) match failure: case "temporary-http": - exception = receiver.RetryableFetchError("Source returned HTTP 503") + exception = verification.RetryableFetchError("Source returned HTTP 503") case "network": exception = httpx.ConnectError( "Connection refused", request=httpx.Request("GET", SOURCE_URL) @@ -429,19 +440,19 @@ def test_verify_webmention_persists_retryable_failure_and_reraises( source_mentions_target.side_effect = exception with pytest.raises(type(exception), match=str(exception)): - receiver.verify_webmention.call_local(identifier) + verification.verify_webmention.call_local(identifier) assert get_webmention_state(app, identifier) == ("failed", str(exception)) -@patch("webmentions_ssg.tasks.receiver.source_mentions_target") -@pytest.mark.parametrize("status", ["verifying", "verified", "deleted"]) +@patch("webmentions_ssg.tasks.verification.source_mentions_target") +@pytest.mark.parametrize("status", ["verifying", "verified", "source_removed"]) def test_verify_webmention_skips_active_and_terminal_states( - source_mentions_target: Mock, app: Flask, receiver: ModuleType, status: str + source_mentions_target: Mock, app: Flask, verification: ModuleType, status: str ) -> None: identifier = create_webmention(app, status=status) - receiver.verify_webmention.call_local(identifier) + verification.verify_webmention.call_local(identifier) assert get_webmention_state(app, identifier) == (status, None) source_mentions_target.assert_not_called() @@ -461,17 +472,17 @@ def set_webmention_updated_at( @pytest.mark.parametrize("status", ["verifying", "received"]) def test_reaper_requeues_stale_verifications( - app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch, status: str + app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch, status: str ) -> None: 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) + monkeypatch.setattr(verification, "verify_webmention", enqueue) with app.app_context(): - assert receiver.recover_stale_verifications() == 1 - assert receiver.recover_stale_verifications() == 0 + assert verification.recover_stale_verifications() == 1 + assert verification.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( @@ -483,7 +494,7 @@ def test_reaper_requeues_stale_verifications( def test_reaper_does_not_touch_recent_or_terminal_webmentions( - app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch + app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: recent_verifying = create_webmention(app, status="verifying") recent_received = create_webmention( @@ -494,10 +505,10 @@ def test_reaper_does_not_touch_recent_or_terminal_webmentions( ) set_webmention_updated_at(app, old_verified, datetime.now(UTC) - timedelta(hours=1)) enqueue = Mock() - monkeypatch.setattr(receiver, "verify_webmention", enqueue) + monkeypatch.setattr(verification, "verify_webmention", enqueue) with app.app_context(): - assert receiver.recover_stale_verifications() == 0 + assert verification.recover_stale_verifications() == 0 assert get_webmention_state(app, recent_verifying) == ("verifying", None) assert get_webmention_state(app, recent_received) == ("received", None) @@ -506,14 +517,16 @@ def test_reaper_does_not_touch_recent_or_terminal_webmentions( def test_reaper_recovers_after_enqueue_failure( - app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch + app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: identifier = create_webmention(app, status="verifying") set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1)) - monkeypatch.setattr(receiver, "verify_webmention", Mock(side_effect=RuntimeError)) + monkeypatch.setattr( + verification, "verify_webmention", Mock(side_effect=RuntimeError) + ) with app.app_context(), pytest.raises(RuntimeError): - receiver.recover_stale_verifications() + verification.recover_stale_verifications() assert get_webmention_state(app, identifier) == ("received", None) @@ -521,26 +534,26 @@ def test_reaper_recovers_after_enqueue_failure( # but before Huey persisted the task. app.config["VERIFICATION_STALE_AFTER_SECONDS"] = 0 enqueue = Mock() - monkeypatch.setattr(receiver, "verify_webmention", enqueue) + monkeypatch.setattr(verification, "verify_webmention", enqueue) with app.app_context(): - assert receiver.recover_stale_verifications() == 1 + assert verification.recover_stale_verifications() == 1 enqueue.assert_called_once_with(identifier) def test_late_worker_cannot_overwrite_recovered_verification( - app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch + app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: identifier = create_webmention(app) - original_verify = receiver.verify_webmention + original_verify = verification.verify_webmention 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_STALE_AFTER_SECONDS"] = 0 with app.app_context(): - with patch.object(receiver, "verify_webmention", Mock()) as enqueue: - assert receiver.recover_stale_verifications() == 1 + with patch.object(verification, "verify_webmention", Mock()) as enqueue: + assert verification.recover_stale_verifications() == 1 enqueue.assert_called_once_with(identifier) db.session.execute( sa.update(ReceivedWebmention) @@ -550,28 +563,28 @@ def test_late_worker_cannot_overwrite_recovered_verification( db.session.commit() return False - monkeypatch.setattr(receiver, "source_mentions_target", concurrent_recovery) + monkeypatch.setattr(verification, "source_mentions_target", concurrent_recovery) original_verify.call_local(identifier) assert get_webmention_state(app, identifier) == ("verified", None) def test_reaper_requeues_and_verifies_integration( - app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch + app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: identifier = create_webmention(app, status="verifying") set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1)) check = Mock(return_value=True) - monkeypatch.setattr(receiver, "source_mentions_target", check) + monkeypatch.setattr(verification, "source_mentions_target", check) with app.app_context(): - assert receiver.recover_stale_verifications() == 1 + assert verification.recover_stale_verifications() == 1 assert get_webmention_state(app, identifier) == ("verified", None) check.assert_called_once_with(SOURCE_URL, TARGET_URL) -@patch("webmentions_ssg.tasks.receiver.source_mentions_target") +@patch("webmentions_ssg.tasks.verification.source_mentions_target") @pytest.mark.parametrize( "unexpected_error", [ @@ -584,29 +597,29 @@ def test_reaper_requeues_and_verifies_integration( def test_verify_webmention_records_unexpected_error_as_terminal_failure( source_mentions_target: Mock, app: Flask, - receiver: ModuleType, + verification: ModuleType, unexpected_error: Exception, ) -> None: identifier = create_webmention(app, source="http://ex☃mple.test/") source_mentions_target.side_effect = unexpected_error - receiver.verify_webmention.call_local(identifier) + verification.verify_webmention.call_local(identifier) assert get_webmention_state(app, identifier) == ("failed", str(unexpected_error)) source_mentions_target.assert_called_once() # 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_verifications() == 0 + assert verification.recover_stale_verifications() == 0 def test_reaper_caps_stale_verification_recoveries( - app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch + app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: app.config["VERIFICATION_MAX_RECOVERIES"] = 3 identifier = create_webmention(app, status="verifying") enqueue = Mock() - monkeypatch.setattr(receiver, "verify_webmention", enqueue) + monkeypatch.setattr(verification, "verify_webmention", enqueue) # Simulate workers that repeatedly crash before completing the task. for attempt in range(3): @@ -614,7 +627,7 @@ def test_reaper_caps_stale_verification_recoveries( app, identifier, datetime.now(UTC) - timedelta(hours=1) ) with app.app_context(): - assert receiver.recover_stale_verifications() == 1 + assert verification.recover_stale_verifications() == 1 row = db.session.get(ReceivedWebmention, identifier) assert row is not None assert row.status == "received" @@ -622,69 +635,69 @@ 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_verifications() == 0 + assert verification.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.failure_reason == verification.RECOVERY_EXHAUSTED_REASON assert row.recovery_attempts == 3 - assert receiver.recover_stale_verifications() == 0 + assert verification.recover_stale_verifications() == 0 assert enqueue.call_count == 3 def test_reaper_caps_received_rows_lost_before_first_task( - app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch + app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: app.config["VERIFICATION_MAX_RECOVERIES"] = 1 identifier = create_webmention(app, status="received", recovery_attempts=1) set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1)) enqueue = Mock() - monkeypatch.setattr(receiver, "verify_webmention", enqueue) + monkeypatch.setattr(verification, "verify_webmention", enqueue) with app.app_context(): - assert receiver.recover_stale_verifications() == 0 + assert verification.recover_stale_verifications() == 0 assert get_webmention_state(app, identifier) == ( "failed", - receiver.RECOVERY_EXHAUSTED_REASON, + verification.RECOVERY_EXHAUSTED_REASON, ) enqueue.assert_not_called() def test_exhausted_row_cannot_be_revived_by_late_huey_retry( - app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch + app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: identifier = create_webmention( app, status="failed", - failure_reason=receiver.RECOVERY_EXHAUSTED_REASON, + failure_reason=verification.RECOVERY_EXHAUSTED_REASON, recovery_attempts=3, ) source_mentions_target = Mock(return_value=True) - monkeypatch.setattr(receiver, "source_mentions_target", source_mentions_target) + monkeypatch.setattr(verification, "source_mentions_target", source_mentions_target) - receiver.verify_webmention.call_local(identifier) + verification.verify_webmention.call_local(identifier) assert get_webmention_state(app, identifier) == ( "failed", - receiver.RECOVERY_EXHAUSTED_REASON, + verification.RECOVERY_EXHAUSTED_REASON, ) source_mentions_target.assert_not_called() def test_verify_webmention_ignores_unknown_identifier( - receiver: ModuleType, caplog: pytest.LogCaptureFixture + verification: ModuleType, caplog: pytest.LogCaptureFixture ) -> None: identifier = uuid.uuid7() with caplog.at_level(logging.WARNING): - receiver.verify_webmention.call_local(identifier) + verification.verify_webmention.call_local(identifier) assert f"Cannot verify unknown ReceivedWebmention {identifier}" in caplog.text -@patch("webmentions_ssg.tasks.receiver.httpx.Client") +@patch("webmentions_ssg.tasks.verification.httpx.Client") def test_fetch_source_document_rejects_unsafe_destination( - httpx_client: Mock, app: Flask, receiver: ModuleType + httpx_client: Mock, app: Flask, verification: ModuleType ) -> None: from webmentions_ssg.net import UnsafeAddressError @@ -692,27 +705,27 @@ def test_fetch_source_document_rejects_unsafe_destination( client.stream.side_effect = UnsafeAddressError("Local address") with ( app.app_context(), - pytest.raises(receiver.VerificationError, match="non-public address"), + pytest.raises(verification.VerificationError, match="non-public address"), ): - receiver.fetch_source_document(SOURCE_URL) + verification.fetch_source_document(SOURCE_URL) -@patch("webmentions_ssg.tasks.receiver.httpx.Client") +@patch("webmentions_ssg.tasks.verification.httpx.Client") def test_fetch_source_document_treats_dns_failure_as_temporary( - httpx_client: Mock, app: Flask, receiver: ModuleType + httpx_client: Mock, app: Flask, verification: ModuleType ) -> None: client = httpx_client.return_value.__enter__.return_value client.stream.side_effect = AddressResolutionError("DNS timeout") with ( app.app_context(), - pytest.raises(receiver.RetryableFetchError, match="could not be resolved"), + pytest.raises(verification.RetryableFetchError, match="could not be resolved"), ): - receiver.fetch_source_document(SOURCE_URL) + verification.fetch_source_document(SOURCE_URL) -@patch("webmentions_ssg.tasks.receiver.httpx.Client") +@patch("webmentions_ssg.tasks.verification.httpx.Client") def test_fetch_source_document_ignores_invalid_content_length( - httpx_client: Mock, app: Flask, receiver: ModuleType + httpx_client: Mock, app: Flask, verification: ModuleType ) -> None: response = httpx.Response( HTTPStatus.OK, @@ -723,15 +736,15 @@ def test_fetch_source_document_ignores_invalid_content_length( set_stream_response(httpx_client, response) with app.app_context(): - fetched_response, body = receiver.fetch_source_document(SOURCE_URL) + fetched_response, body = verification.fetch_source_document(SOURCE_URL) assert fetched_response is response assert body == b"<p>Document</p>" -@patch("webmentions_ssg.tasks.receiver.fetch_source_document") +@patch("webmentions_ssg.tasks.verification.fetch_source_document") def test_source_mentions_target_falls_back_for_unknown_encoding( - fetch_source_document: Mock, receiver: ModuleType + fetch_source_document: Mock, verification: ModuleType ) -> None: body = f"Reply to {TARGET_URL}".encode() @@ -744,5 +757,5 @@ def test_source_mentions_target_falls_back_for_unknown_encoding( response.encoding = "unknown-encoding" fetch_source_document.return_value = response, body - assert receiver.source_mentions_target(SOURCE_URL, TARGET_URL) + assert verification.source_mentions_target(SOURCE_URL, TARGET_URL) fetch_source_document.assert_called_once_with(SOURCE_URL) |
