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 | ||||
| -rw-r--r-- | tests/test_cli.py | 134 | ||||
| -rw-r--r-- | tests/test_forms.py | 6 | ||||
| -rw-r--r-- | tests/test_init.py | 2 | ||||
| -rw-r--r-- | tests/test_models.py | 121 | ||||
| -rw-r--r-- | tests/test_net.py | 4 | ||||
| -rw-r--r-- | tests/views/helpers.py | 32 | ||||
| -rw-r--r-- | tests/views/test_admin.py | 4 | ||||
| -rw-r--r-- | tests/views/test_endpoint.py | 4 |
18 files changed, 455 insertions, 300 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) diff --git a/tests/test_cli.py b/tests/test_cli.py index cdaf896..9485494 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -12,13 +12,14 @@ import pytest import sqlalchemy as sa from alembic.script import ScriptDirectory from flask import Flask +from flask_migrate import downgrade as migrate_downgrade from flask_migrate import upgrade as migrate_upgrade from sqlalchemy import inspect from sqlalchemy.exc import IntegrityError from webmentions_ssg import create_app, db, migrate from webmentions_ssg.config import TestingConfig -from webmentions_ssg.models import ReceivedStatus, ReceivedWebmention, User +from webmentions_ssg.models import ReceivedWebmention, ReceivedWebmentionStatus, User def test_cli_groups_are_registered(app: Flask) -> None: @@ -101,7 +102,20 @@ def test_create_db_builds_schema_and_records_latest_migration( received_indexes = { index["name"] for index in inspector.get_indexes("received_webmentions") } - assert {"alembic_version", "users", "received_webmentions"} <= table_names + assert { + "alembic_version", + "users", + "received_webmentions", + "source_documents", + } <= table_names + assert "sources" not in table_names + assert { + "source_uuid", + "linked", + "webmention_endpoint", + "endpoint_response_status", + } <= columns + assert "source_target_digest" in received_columns assert {"attempt_count", "next_retry_at"} <= columns assert "recovery_attempts" in received_columns assert "ix_received_webmentions_status_updated_at" in received_indexes @@ -236,7 +250,7 @@ def test_upgrade_backfills_portable_digest_for_existing_received_rows( :param database_app: Flask application connected to the legacy SQLite schema. """ - from webmentions_ssg.models import received_url_digest + from webmentions_ssg.models import source_target_digest with database_app.app_context(): migrate_upgrade(revision="c0272f25ce17") @@ -259,7 +273,7 @@ def test_upgrade_backfills_portable_digest_for_existing_received_rows( with db.engine.connect() as connection: result = connection.execute( sa.text( - "SELECT source, target, status, url_digest " + "SELECT source, target, status, source_target_digest " "FROM received_webmentions" ) ).one() @@ -268,14 +282,14 @@ def test_upgrade_backfills_portable_digest_for_existing_received_rows( sa.text("SELECT recovery_attempts FROM received_webmentions") ).scalar_one() assert recovery_attempts == 0 - assert result.url_digest == received_url_digest( + assert result.source_target_digest == source_target_digest( result.source, result.target ) constraints = inspect(connection).get_unique_constraints( "received_webmentions" ) assert any( - constraint["column_names"] == ["url_digest"] + constraint["column_names"] == ["source_target_digest"] for constraint in constraints ) @@ -314,7 +328,7 @@ def test_upgrade_converts_received_status_without_losing_rows( for column in inspect(connection).get_columns("received_webmentions") } assert isinstance(columns["status"]["type"], sa.String) - assert columns["status"]["type"].length == 9 + assert columns["status"]["type"].length == len("source_removed") assert ( connection.execute( sa.text("SELECT status FROM received_webmentions") @@ -324,7 +338,111 @@ def test_upgrade_converts_received_status_without_losing_rows( mention = db.session.get(ReceivedWebmention, UUID(hex=identifier)) assert mention is not None - assert mention.status is ReceivedStatus.VERIFYING + assert mention.status is ReceivedWebmentionStatus.VERIFYING + + +def test_name_migration_round_trips_existing_webmention_rows( + database_app: Flask, +) -> None: + """Keep persisted statuses, UUID foreign keys and unique constraints intact.""" + source_uuid = "0" * 31 + "1" + sent_uuid = "0" * 31 + "2" + received_uuid = "0" * 31 + "3" + + with database_app.app_context(): + migrate_upgrade(revision="7c38bb1a935e") + with db.engine.begin() as connection: + connection.execute( + sa.text( + "INSERT INTO sources " + "(uuid, path, url, content_hash, revision, " + "last_seen_at, revised_at) " + "VALUES (:uuid, 'post/index.html', 'https://site.example/post', " + ":hash, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)" + ), + {"uuid": source_uuid, "hash": "0" * 32}, + ) + connection.execute( + sa.text( + "INSERT INTO sent_webmentions " + "(uuid, source_id, target, target_digest, active, desired_revision, " + "status, endpoint, response_status, attempt_count) " + "VALUES (:uuid, :source_id, 'https://target.example/', :digest, " + "1, 1, 'unsupported', 'https://target.example/webmention', 202, 0)" + ), + {"uuid": sent_uuid, "source_id": source_uuid, "digest": "1" * 64}, + ) + connection.execute( + sa.text( + "INSERT INTO received_webmentions " + "(uuid, source, target, url_digest, status, updated_at) " + "VALUES (:uuid, 'https://source.example/', " + "'https://site.example/', :digest, 'deleted', CURRENT_TIMESTAMP)" + ), + {"uuid": received_uuid, "digest": "2" * 64}, + ) + + migrate_upgrade() + with db.engine.connect() as connection: + inspector = inspect(connection) + assert "source_documents" in inspector.get_table_names() + assert "sources" not in inspector.get_table_names() + assert ( + connection.execute( + sa.text("SELECT status FROM received_webmentions") + ).scalar_one() + == "source_removed" + ) + sent_row = connection.execute( + sa.text( + "SELECT source_uuid, status, linked, webmention_endpoint, " + "endpoint_response_status FROM sent_webmentions" + ) + ).one() + assert sent_row == ( + source_uuid, + "no_endpoint", + 1, + "https://target.example/webmention", + 202, + ) + assert any( + constraint["column_names"] == ["source_target_digest"] + for constraint in inspector.get_unique_constraints( + "received_webmentions" + ) + ) + assert any( + constraint["column_names"] == ["source_uuid", "target_digest"] + for constraint in inspector.get_unique_constraints("sent_webmentions") + ) + assert any( + foreign_key["constrained_columns"] == ["source_uuid"] + and foreign_key["referred_table"] == "source_documents" + for foreign_key in inspector.get_foreign_keys("sent_webmentions") + ) + + migrate_downgrade(revision="7c38bb1a935e") + with db.engine.connect() as connection: + inspector = inspect(connection) + assert "sources" in inspector.get_table_names() + assert ( + connection.execute( + sa.text("SELECT status FROM received_webmentions") + ).scalar_one() + == "deleted" + ) + assert ( + connection.execute( + sa.text("SELECT status FROM sent_webmentions") + ).scalar_one() + == "unsupported" + ) + assert any( + foreign_key["constrained_columns"] == ["source_id"] + and foreign_key["referred_table"] == "sources" + for foreign_key in inspector.get_foreign_keys("sent_webmentions") + ) def test_mail_cli_sends_to_configured_notification_email(app: Flask) -> None: diff --git a/tests/test_forms.py b/tests/test_forms.py index b5b3296..707ac09 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -12,7 +12,7 @@ from webmentions_ssg.forms import ( ConfirmActionForm, LoginForm, MaxURLBytes, - PublicIPLiteral, + RejectNonPublicIPLiteral, WebmentionSubmissionForm, ) @@ -303,12 +303,12 @@ def test_allowed_hostname_uses_custom_message(app: Flask) -> None: def test_public_ip_literal_rejects_ipv6_transition_addresses(address: str) -> None: form = URLForm(MultiDict({"url": f"https://[{address}]/"})) with pytest.raises(ValidationError, match="URL must use a public address"): - PublicIPLiteral()(form, form.url) + RejectNonPublicIPLiteral()(form, form.url) def test_public_ip_literal_accepts_public_ipv6_address() -> None: form = URLForm(MultiDict({"url": "https://[2606:2800:220:1:248:1893:25c8:1946]/"})) - PublicIPLiteral()(form, form.url) + RejectNonPublicIPLiteral()(form, form.url) def test_allowed_hostname_handles_invalid_ipv6(app: Flask) -> None: diff --git a/tests/test_init.py b/tests/test_init.py index 44375de..f334c88 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -32,7 +32,7 @@ def test_importing_views_and_tasks_before_creating_app() -> None: script = """ 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.verification import verify_webmention from webmentions_ssg.tasks.sender import send_webmention from webmentions_ssg.views import webmentions_bp diff --git a/tests/test_models.py b/tests/test_models.py index d2b667c..4e785c7 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -16,28 +16,31 @@ from sqlalchemy.orm import Mapped from webmentions_ssg import db from webmentions_ssg.models import ( - ReceivedStatus, ReceivedWebmention, + ReceivedWebmentionStatus, SentWebmention, - Source, + SourceDocument, User, - received_url_digest, - sent_target_digest, + source_target_digest, + target_digest, uuid7_to_datetime, ) def test_uuid_annotations_and_relationships_resolve() -> None: """Verify that UUID columns and forward relationships map correctly.""" - for model in (ReceivedWebmention, Source, SentWebmention): + for model in (ReceivedWebmention, SourceDocument, SentWebmention): assert get_type_hints(model)["uuid"] == Mapped[uuid.UUID] assert sa.inspect(model).columns["uuid"].primary_key assert ( - sa.inspect(Source).relationships["sent_webmentions"].mapper.class_ + sa.inspect(SourceDocument).relationships["sent_webmentions"].mapper.class_ is SentWebmention ) - assert sa.inspect(SentWebmention).relationships["source"].mapper.class_ is Source + assert ( + sa.inspect(SentWebmention).relationships["source"].mapper.class_ + is SourceDocument + ) def test_user_repr() -> None: @@ -61,14 +64,16 @@ def test_user_password_is_hashed_and_can_be_checked() -> None: @pytest.mark.parametrize( ("status", "expected"), [ - (ReceivedStatus.RECEIVED, False), - (ReceivedStatus.VERIFYING, False), - (ReceivedStatus.VERIFIED, True), - (ReceivedStatus.FAILED, False), - (ReceivedStatus.DELETED, False), + (ReceivedWebmentionStatus.RECEIVED, False), + (ReceivedWebmentionStatus.VERIFYING, False), + (ReceivedWebmentionStatus.VERIFIED, True), + (ReceivedWebmentionStatus.FAILED, False), + (ReceivedWebmentionStatus.SOURCE_REMOVED, False), ], ) -def test_received_webmention_verified(status: ReceivedStatus, expected: bool) -> None: +def test_received_webmention_verified( + status: ReceivedWebmentionStatus, expected: bool +) -> None: webmention = ReceivedWebmention( uuid=uuid.uuid7(), source="https://source.example/post", @@ -91,7 +96,7 @@ def test_sent_webmention_pending( desired_revision=desired_revision, processed_revision=processed_revision, ) - assert webmention.pending is expected + assert webmention.awaiting_processing is expected def test_sent_webmention_pending_sql_matches_python(app: Flask) -> None: @@ -99,7 +104,7 @@ def test_sent_webmention_pending_sql_matches_python(app: Flask) -> None: with app.app_context(): now = datetime.now(UTC) - source = Source( + source = SourceDocument( path="pending/index.html", url="https://site.example/pending/", content_hash="0" * 32, @@ -119,18 +124,18 @@ def test_sent_webmention_pending_sql_matches_python(app: Flask) -> None: pending_ids = set( db.session.scalars( - sa.select(SentWebmention.uuid).where(SentWebmention.pending) + sa.select(SentWebmention.uuid).where(SentWebmention.awaiting_processing) ) ) for webmention, (_, _, expected) in zip( source.sent_webmentions, cases, strict=True ): - assert webmention.pending is expected + assert webmention.awaiting_processing is expected assert (webmention.uuid in pending_ids) is expected result = db.session.execute( sa.update(SentWebmention) - .where(SentWebmention.pending) + .where(SentWebmention.awaiting_processing) .values(attempt_count=1) ) assert result.rowcount == 3 @@ -156,7 +161,7 @@ def test_uuid7_to_datetime() -> None: id="received-webmention", ), pytest.param( - lambda identifier: Source( + lambda identifier: SourceDocument( uuid=identifier, path="example/index.html", url="https://dennisfink.me/blog/example/", @@ -176,7 +181,9 @@ def test_uuid7_to_datetime() -> None: ], ) def test_created_at_comes_from_uuid7_timestamp( - factory: Callable[[uuid.UUID], ReceivedWebmention | Source | SentWebmention], + factory: Callable[ + [uuid.UUID], ReceivedWebmention | SourceDocument | SentWebmention + ], ) -> None: identifier = uuid.uuid7() model = factory(identifier) @@ -197,7 +204,7 @@ def test_received_webmention_repr() -> None: def test_source_repr() -> None: - source = Source( + source = SourceDocument( path="example/index.html", url="https://dennisfink.me/blog/example/", content_hash="0" * 32, @@ -205,7 +212,7 @@ def test_source_repr() -> None: last_seen_at=datetime.now(UTC), revised_at=datetime.now(UTC), ) - assert repr(source) == "<Source 'https://dennisfink.me/blog/example/'>" + assert repr(source) == "<SourceDocument 'https://dennisfink.me/blog/example/'>" def test_sent_webmention_repr() -> None: @@ -233,9 +240,9 @@ def test_received_status_uses_non_native_enum() -> None: status_type = ReceivedWebmention.__table__.c.status.type assert isinstance(status_type, sa.Enum) - assert status_type.enum_class is ReceivedStatus + assert status_type.enum_class is ReceivedWebmentionStatus assert status_type.native_enum is False - assert status_type.enums == [status.value for status in ReceivedStatus] + assert status_type.enums == [status.value for status in ReceivedWebmentionStatus] def test_received_status_round_trips_as_enum(app: Flask) -> None: @@ -245,7 +252,7 @@ def test_received_status_round_trips_as_enum(app: Flask) -> None: uuid=identifier, source="https://source.example/post", target="https://dennisfink.me/blog/example/", - status=ReceivedStatus.VERIFIED, + status=ReceivedWebmentionStatus.VERIFIED, ) db.session.add(mention) db.session.commit() @@ -261,22 +268,22 @@ def test_received_status_round_trips_as_enum(app: Flask) -> None: db.session.expunge_all() loaded = db.session.get(ReceivedWebmention, identifier) assert loaded is not None - assert loaded.status is ReceivedStatus.VERIFIED + assert loaded.status is ReceivedWebmentionStatus.VERIFIED def test_received_verification_reset_is_reusable_for_atomic_updates() -> None: - from webmentions_ssg.models import ReceivedStatus + from webmentions_ssg.models import ReceivedWebmentionStatus mention = ReceivedWebmention( uuid=uuid.uuid7(), source="https://source.example/post", target="https://dennisfink.me/post", - status=ReceivedStatus.FAILED, + status=ReceivedWebmentionStatus.FAILED, failure_reason="Old error", recovery_attempts=3, ) - assert mention.reset_for_verification() is None - assert mention.status == ReceivedStatus.RECEIVED + assert mention.reopen_verification() is None + assert mention.status == ReceivedWebmentionStatus.RECEIVED assert mention.failure_reason is None assert mention.recovery_attempts == 0 assert mention.updated_at is not None @@ -288,7 +295,7 @@ def test_reopen_preserves_delivery_history() -> None: sent_at = datetime.now(UTC) mention = SentWebmention( target="https://example.com/post", - active=True, + linked=True, desired_revision=2, processed_revision=2, sent_revision=2, @@ -297,9 +304,9 @@ def test_reopen_preserves_delivery_history() -> None: next_retry_at=sent_at, last_sent_at=sent_at, ) - assert mention.reopen(3, False) is None + assert mention.reset_for_revision(3, False) is None assert mention.desired_revision == 3 - assert mention.active is False + assert mention.linked is False assert mention.attempt_count == 0 assert mention.next_retry_at is None assert mention.status == SentWebmentionStatus.SENT @@ -312,31 +319,31 @@ def test_resend_requeues_current_revision_without_losing_send_history() -> None: sent_at = datetime.now(UTC) mention = SentWebmention( target="https://example.com/post", - active=True, + linked=True, desired_revision=3, processed_revision=3, sent_revision=2, status=SentWebmentionStatus.FAILED, failure_reason="Previous failure", - endpoint="https://example.com/webmention", - response_status=HTTPStatus.SERVICE_UNAVAILABLE, + webmention_endpoint="https://example.com/webmention", + endpoint_response_status=HTTPStatus.SERVICE_UNAVAILABLE, status_url="https://example.com/status", attempt_count=3, next_retry_at=sent_at, last_attempted_at=sent_at, last_sent_at=sent_at, ) - assert not mention.pending + assert not mention.awaiting_processing - assert mention.resend() is None + assert mention.clear_result_for_resend() is None assert mention.desired_revision == 3 - assert mention.active is True - assert mention.pending + assert mention.linked is True + assert mention.awaiting_processing assert mention.processed_revision is None assert mention.status is None assert mention.failure_reason is None - assert mention.endpoint is None - assert mention.response_status is None + assert mention.webmention_endpoint is None + assert mention.endpoint_response_status is None assert mention.status_url is None assert mention.attempt_count == 0 assert mention.next_retry_at is None @@ -346,8 +353,8 @@ def test_resend_requeues_current_revision_without_losing_send_history() -> None: def test_received_pair_fingerprint_is_unambiguous() -> None: - assert received_url_digest("a", "bc") != received_url_digest("ab", "c") - assert received_url_digest("a", "b") != received_url_digest("b", "a") + assert source_target_digest("a", "bc") != source_target_digest("ab", "c") + assert source_target_digest("a", "b") != source_target_digest("b", "a") def test_models_hash_url_fields_when_created_or_changed() -> None: @@ -356,16 +363,22 @@ def test_models_hash_url_fields_when_created_or_changed() -> None: source="https://source.example/a", target="https://site.example/b", ) - assert received.url_digest == received_url_digest(received.source, received.target) + assert received.source_target_digest == source_target_digest( + received.source, received.target + ) received.source = "https://source.example/c" - assert received.url_digest == received_url_digest(received.source, received.target) + assert received.source_target_digest == source_target_digest( + received.source, received.target + ) received.target = "https://site.example/d" - assert received.url_digest == received_url_digest(received.source, received.target) + assert received.source_target_digest == source_target_digest( + received.source, received.target + ) sent = SentWebmention(target="https://site.example/a", desired_revision=1) - assert sent.target_digest == sent_target_digest(sent.target) + assert sent.target_digest == target_digest(sent.target) sent.target = "https://site.example/b" - assert sent.target_digest == sent_target_digest(sent.target) + assert sent.target_digest == target_digest(sent.target) def test_long_received_pair_can_be_stored_with_unique_digest(app: Flask) -> None: @@ -380,8 +393,8 @@ def test_long_received_pair_can_be_stored_with_unique_digest(app: Flask) -> None db.session.commit() match = db.session.scalar( sa.select(ReceivedWebmention).where( - ReceivedWebmention.url_digest - == received_url_digest(long_source, long_target) + ReceivedWebmention.source_target_digest + == source_target_digest(long_source, long_target) ) ) assert match is not None @@ -403,7 +416,7 @@ def test_long_sent_targets_enforce_uniqueness_without_indexing_url_text( now = datetime.now(UTC) long_target = "https://destination.example/" + "a" * 4000 with app.app_context(): - source = Source( + source = SourceDocument( path="long/index.html", url="https://example.com/long", content_hash="0" * 32, @@ -416,9 +429,7 @@ def test_long_sent_targets_enforce_uniqueness_without_indexing_url_text( ) db.session.add(source) db.session.commit() - assert source.sent_webmentions[0].target_digest == sent_target_digest( - long_target - ) + assert source.sent_webmentions[0].target_digest == target_digest(long_target) source.sent_webmentions.append( SentWebmention(target=long_target, desired_revision=1) diff --git a/tests/test_net.py b/tests/test_net.py index c7c65ba..24785ee 100644 --- a/tests/test_net.py +++ b/tests/test_net.py @@ -338,9 +338,9 @@ def test_public_addresses_rejects_empty_dns_result() -> None: ) def test_is_retryable_status(app: Flask, status: int, expected: bool) -> None: _ = app - from webmentions_ssg.tasks import discovery + from webmentions_ssg.tasks import endpoint_discovery - assert discovery.is_retryable_status(status) is expected + assert endpoint_discovery.is_retryable_status(status) is expected @pytest.mark.parametrize( diff --git a/tests/views/helpers.py b/tests/views/helpers.py index 2da2ace..3c74a1b 100644 --- a/tests/views/helpers.py +++ b/tests/views/helpers.py @@ -17,7 +17,7 @@ from webmentions_ssg.models import ( ReceivedWebmention, SentWebmention, SentWebmentionStatus, - Source, + SourceDocument, User, ) @@ -64,7 +64,7 @@ def create_sent_source(app: Flask) -> uuid.UUID: now = datetime.now(UTC) with app.app_context(): - source = Source( + source = SourceDocument( path="blog/example/index.html", url="https://dennisfink.me/blog/example/", content_hash="0" * 32, @@ -77,24 +77,24 @@ def create_sent_source(app: Flask) -> uuid.UUID: [ SentWebmention( target="https://example.com/b", - active=True, + linked=True, desired_revision=2, processed_revision=2, sent_revision=2, status=SentWebmentionStatus.SENT, - endpoint="https://example.com/webmention", - response_status=HTTPStatus.ACCEPTED, + webmention_endpoint="https://example.com/webmention", + endpoint_response_status=HTTPStatus.ACCEPTED, ), SentWebmention( target="https://example.com/a", - active=True, + linked=True, desired_revision=2, processed_revision=2, sent_revision=1, status=SentWebmentionStatus.FAILED, failure_reason="Webmention endpoint returned HTTP 400", - endpoint="https://example.com/webmention", - response_status=HTTPStatus.BAD_REQUEST, + webmention_endpoint="https://example.com/webmention", + endpoint_response_status=HTTPStatus.BAD_REQUEST, ), ] ) @@ -161,7 +161,7 @@ def create_resendable_sent_webmention(app: Flask) -> tuple[uuid.UUID, uuid.UUID] now = datetime.now(UTC) with app.app_context(): - source = Source( + source = SourceDocument( path="blog/resend/index.html", url="https://dennisfink.me/blog/resend/", content_hash="1" * 32, @@ -171,14 +171,14 @@ def create_resendable_sent_webmention(app: Flask) -> tuple[uuid.UUID, uuid.UUID] ) webmention = SentWebmention( target="https://example.com/resend", - active=True, + linked=True, desired_revision=3, processed_revision=3, sent_revision=2, status=SentWebmentionStatus.FAILED, failure_reason="Previous failure", - endpoint="https://example.com/webmention", - response_status=HTTPStatus.INTERNAL_SERVER_ERROR, + webmention_endpoint="https://example.com/webmention", + endpoint_response_status=HTTPStatus.INTERNAL_SERVER_ERROR, status_url="https://example.com/webmention/status/123", ) source.sent_webmentions.append(webmention) @@ -194,7 +194,7 @@ def create_outgoing( ) -> tuple[uuid.UUID, uuid.UUID]: now = datetime.now(UTC) with app.app_context(): - source = Source( + source = SourceDocument( path="blog/post/index.html", url="https://source.example/blog/post", content_hash="0" * 32, @@ -204,10 +204,12 @@ def create_outgoing( ) mention = SentWebmention( target="https://example.com/article", - active=True, + linked=True, desired_revision=1, status_url=status_url, - response_status=HTTPStatus.CREATED if status_url is not None else None, + endpoint_response_status=( + HTTPStatus.CREATED if status_url is not None else None + ), ) source.sent_webmentions.append(mention) db.session.add(source) diff --git a/tests/views/test_admin.py b/tests/views/test_admin.py index 90d406b..fa5575b 100644 --- a/tests/views/test_admin.py +++ b/tests/views/test_admin.py @@ -331,8 +331,8 @@ def test_resend_sent_webmention_reopens_current_revision( assert webmention.sent_revision == 2 assert webmention.status is None assert webmention.failure_reason is None - 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 send_webmention.assert_called_once_with(webmention_identifier) diff --git a/tests/views/test_endpoint.py b/tests/views/test_endpoint.py index 52ef77d..88b665f 100644 --- a/tests/views/test_endpoint.py +++ b/tests/views/test_endpoint.py @@ -253,11 +253,11 @@ def test_endpoint_rejects_digest_collision( ) db.session.add(existing) db.session.commit() - digest = existing.url_digest + digest = existing.source_target_digest form.source.data = "https://source.example/different" monkeypatch.setattr( - views_module, "received_url_digest", lambda source, target: digest + views_module, "source_target_digest", lambda source, target: digest ) response = client.post("/endpoint") |
