From 6d6d079a3a99d3d3a922a640b6925eb2adf044de Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Tue, 17 Nov 2026 17:09:59 +0100 Subject: refactor(core)!: split forms and models by feature Replace monolithic form and model modules with focused packages for receiver, moderation, source, user, and shared validation concerns. Rename source policy concepts to domain moderation, including routes, models, tables, configuration, templates, and tests. BREAKING CHANGE: form and model import paths, moderation routes, and `SOURCE_ALLOWLIST_ENABLED` have changed; use `ALLOWLIST_ENABLED` and the new feature-specific modules. --- tests/models/test_utils.py | 95 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 tests/models/test_utils.py (limited to 'tests/models/test_utils.py') diff --git a/tests/models/test_utils.py b/tests/models/test_utils.py new file mode 100644 index 0000000..8722a02 --- /dev/null +++ b/tests/models/test_utils.py @@ -0,0 +1,95 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import uuid +from collections.abc import Callable +from datetime import UTC, datetime + +import pytest + +from webmentions_ssg.models.received import ReceivedWebmention +from webmentions_ssg.models.sources import SentWebmention, SourceDocument +from webmentions_ssg.models.utils import ( + source_target_digest, + target_digest, + uuid7_to_datetime, +) + + +def test_uuid7_to_datetime() -> None: + identifier = uuid.uuid7() + timestamp = uuid7_to_datetime(identifier) + + assert timestamp.tzinfo is UTC + assert timestamp.timestamp() == pytest.approx(identifier.time / 1000) + + +@pytest.mark.parametrize( + "factory", + [ + pytest.param( + lambda identifier: ReceivedWebmention( + uuid=identifier, + source="https://source.example/post", + target="https://dennisfink.me/blog/example/", + ), + id="received-webmention", + ), + pytest.param( + lambda identifier: SourceDocument( + uuid=identifier, + path="example/index.html", + url="https://dennisfink.me/blog/example/", + content_hash="0" * 32, + revision=1, + last_seen_at=datetime.now(UTC), + revised_at=datetime.now(UTC), + ), + id="source", + ), + pytest.param( + lambda identifier: SentWebmention( + uuid=identifier, target="https://example.com/post", desired_revision=1 + ), + id="sent-webmention", + ), + ], +) +def test_created_at_comes_from_uuid7_timestamp( + factory: Callable[ + [uuid.UUID], ReceivedWebmention | SourceDocument | SentWebmention + ], +) -> None: + identifier = uuid.uuid7() + model = factory(identifier) + assert model.created_at == uuid7_to_datetime(identifier) + + +def test_received_pair_fingerprint_is_unambiguous() -> None: + 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: + received = ReceivedWebmention( + uuid=uuid.uuid7(), + source="https://source.example/a", + target="https://site.example/b", + ) + assert received.source_target_digest == source_target_digest( + received.source, received.target + ) + received.source = "https://source.example/c" + assert received.source_target_digest == source_target_digest( + received.source, received.target + ) + received.target = "https://site.example/d" + 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 == target_digest(sent.target) + sent.target = "https://site.example/b" + assert sent.target_digest == target_digest(sent.target) -- cgit v1.3.1