diff options
| author | Dennis Fink | 2026-11-17 17:09:59 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-17 17:09:59 +0100 |
| commit | 6d6d079a3a99d3d3a922a640b6925eb2adf044de (patch) | |
| tree | 33cc19241f4ce86ea43b2f194087c58e3fdc8a4d /tests/models/test_utils.py | |
| parent | 227092b57acc886469e16bcc4c0d3256e86d3d1e (diff) | |
| download | webmentions-ssg-6d6d079a3a99d3d3a922a640b6925eb2adf044de.tar.gz webmentions-ssg-6d6d079a3a99d3d3a922a640b6925eb2adf044de.zip | |
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.
Diffstat (limited to '')
| -rw-r--r-- | tests/models/test_utils.py | 95 |
1 files changed, 95 insertions, 0 deletions
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 <me+coding@dennisfink.me> +# +# 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) |
