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/test_models.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/test_models.py | 439 |
1 files changed, 0 insertions, 439 deletions
diff --git a/tests/test_models.py b/tests/test_models.py deleted file mode 100644 index 4e785c7..0000000 --- a/tests/test_models.py +++ /dev/null @@ -1,439 +0,0 @@ -# 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 -from http import HTTPStatus -from typing import get_type_hints - -import pytest -import sqlalchemy as sa -from flask import Flask -from sqlalchemy.exc import IntegrityError -from sqlalchemy.orm import Mapped - -from webmentions_ssg import db -from webmentions_ssg.models import ( - ReceivedWebmention, - ReceivedWebmentionStatus, - SentWebmention, - SourceDocument, - User, - 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, SourceDocument, SentWebmention): - assert get_type_hints(model)["uuid"] == Mapped[uuid.UUID] - assert sa.inspect(model).columns["uuid"].primary_key - - assert ( - sa.inspect(SourceDocument).relationships["sent_webmentions"].mapper.class_ - is SentWebmention - ) - assert ( - sa.inspect(SentWebmention).relationships["source"].mapper.class_ - is SourceDocument - ) - - -def test_user_repr() -> None: - user = User(username="admin", password="secret") - assert repr(user) == "<User admin>" - - -def test_user_password_is_write_only() -> None: - user = User(username="admin", password="secret") - with pytest.raises(AttributeError, match="Password is write-only"): - _ = user.password - - -def test_user_password_is_hashed_and_can_be_checked() -> None: - user = User(username="admin", password="secret") - assert user.password_hash != "secret" - assert user.check_password("secret") - assert not user.check_password("wrong") - - -@pytest.mark.parametrize( - ("status", "expected"), - [ - (ReceivedWebmentionStatus.RECEIVED, False), - (ReceivedWebmentionStatus.VERIFYING, False), - (ReceivedWebmentionStatus.VERIFIED, True), - (ReceivedWebmentionStatus.FAILED, False), - (ReceivedWebmentionStatus.SOURCE_REMOVED, False), - ], -) -def test_received_webmention_verified( - status: ReceivedWebmentionStatus, expected: bool -) -> None: - webmention = ReceivedWebmention( - uuid=uuid.uuid7(), - source="https://source.example/post", - target="https://dennisfink.me/blog/example/", - status=status, - ) - - assert webmention.verified is expected - - -@pytest.mark.parametrize( - ("desired_revision", "processed_revision", "expected"), - [(1, None, True), (1, 0, True), (2, 1, True), (1, 1, False), (1, 2, False)], -) -def test_sent_webmention_pending( - desired_revision: int, processed_revision: int | None, expected: bool -) -> None: - webmention = SentWebmention( - target="https://example.com/post", - desired_revision=desired_revision, - processed_revision=processed_revision, - ) - assert webmention.awaiting_processing is expected - - -def test_sent_webmention_pending_sql_matches_python(app: Flask) -> None: - cases = [(1, None, True), (1, 0, True), (2, 1, True), (1, 1, False), (1, 2, False)] - - with app.app_context(): - now = datetime.now(UTC) - source = SourceDocument( - path="pending/index.html", - url="https://site.example/pending/", - content_hash="0" * 32, - last_seen_at=now, - revised_at=now, - ) - for i, (desired, processed, _) in enumerate(cases): - source.sent_webmentions.append( - SentWebmention( - target=f"https://example.com/post/{i}", - desired_revision=desired, - processed_revision=processed, - ) - ) - db.session.add(source) - db.session.commit() - - pending_ids = set( - db.session.scalars( - sa.select(SentWebmention.uuid).where(SentWebmention.awaiting_processing) - ) - ) - for webmention, (_, _, expected) in zip( - source.sent_webmentions, cases, strict=True - ): - assert webmention.awaiting_processing is expected - assert (webmention.uuid in pending_ids) is expected - - result = db.session.execute( - sa.update(SentWebmention) - .where(SentWebmention.awaiting_processing) - .values(attempt_count=1) - ) - assert result.rowcount == 3 - - -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_webmention_repr() -> None: - webmention = ReceivedWebmention( - uuid=uuid.uuid7(), - source="https://source.example/post", - target="https://dennisfink.me/blog/example/", - ) - - assert repr(webmention) == ( - "<ReceivedWebmention 'https://source.example/post' -> " - "'https://dennisfink.me/blog/example/'>" - ) - - -def test_source_repr() -> None: - source = SourceDocument( - 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), - ) - assert repr(source) == "<SourceDocument 'https://dennisfink.me/blog/example/'>" - - -def test_sent_webmention_repr() -> None: - webmention = SentWebmention(target="https://example.com/post", desired_revision=1) - assert repr(webmention) == "<SentWebmention 'https://example.com/post'>" - - -def test_user_session_id_changes_when_password_hash_changes() -> None: - user = User(username="admin", password="a strong passphrase", id=42) - original_hash = user.password_hash - original_id = user.get_id() - - assert original_id.startswith("42:") - assert original_hash not in original_id - assert len(original_id.split(":", 1)[1]) == 64 - - # Even a reset to the same password rehashes with a new salt, revoking - # any old session token. - user.password = "a strong passphrase" - assert user.password_hash != original_hash - assert user.get_id() != original_id - - -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 ReceivedWebmentionStatus - assert status_type.native_enum is False - assert status_type.enums == [status.value for status in ReceivedWebmentionStatus] - - -def test_received_status_round_trips_as_enum(app: Flask) -> None: - with app.app_context(): - identifier = uuid.uuid7() - mention = ReceivedWebmention( - uuid=identifier, - source="https://source.example/post", - target="https://dennisfink.me/blog/example/", - status=ReceivedWebmentionStatus.VERIFIED, - ) - db.session.add(mention) - db.session.commit() - - assert ( - db.session.scalar( - sa.text("SELECT status FROM received_webmentions WHERE uuid = :uuid"), - {"uuid": identifier.hex}, - ) - == "verified" - ) - - db.session.expunge_all() - loaded = db.session.get(ReceivedWebmention, identifier) - assert loaded is not None - assert loaded.status is ReceivedWebmentionStatus.VERIFIED - - -def test_received_verification_reset_is_reusable_for_atomic_updates() -> None: - from webmentions_ssg.models import ReceivedWebmentionStatus - - mention = ReceivedWebmention( - uuid=uuid.uuid7(), - source="https://source.example/post", - target="https://dennisfink.me/post", - status=ReceivedWebmentionStatus.FAILED, - failure_reason="Old error", - recovery_attempts=3, - ) - 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 - - -def test_reopen_preserves_delivery_history() -> None: - from webmentions_ssg.models import SentWebmentionStatus - - sent_at = datetime.now(UTC) - mention = SentWebmention( - target="https://example.com/post", - linked=True, - desired_revision=2, - processed_revision=2, - sent_revision=2, - status=SentWebmentionStatus.SENT, - attempt_count=3, - next_retry_at=sent_at, - last_sent_at=sent_at, - ) - assert mention.reset_for_revision(3, False) is None - assert mention.desired_revision == 3 - assert mention.linked is False - assert mention.attempt_count == 0 - assert mention.next_retry_at is None - assert mention.status == SentWebmentionStatus.SENT - assert mention.sent_revision == 2 - - -def test_resend_requeues_current_revision_without_losing_send_history() -> None: - from webmentions_ssg.models import SentWebmentionStatus - - sent_at = datetime.now(UTC) - mention = SentWebmention( - target="https://example.com/post", - linked=True, - desired_revision=3, - processed_revision=3, - sent_revision=2, - status=SentWebmentionStatus.FAILED, - failure_reason="Previous failure", - 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.awaiting_processing - - assert mention.clear_result_for_resend() is None - assert mention.desired_revision == 3 - 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.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 - assert mention.sent_revision == 2 - assert mention.last_attempted_at == sent_at - assert mention.last_sent_at == sent_at - - -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) - - -def test_long_received_pair_can_be_stored_with_unique_digest(app: Flask) -> None: - long_source = "https://source.example/" + "a" * 4000 - long_target = "https://dennisfink.me/" + "b" * 4000 - with app.app_context(): - db.session.add( - ReceivedWebmention( - uuid=uuid.uuid7(), source=long_source, target=long_target - ) - ) - db.session.commit() - match = db.session.scalar( - sa.select(ReceivedWebmention).where( - ReceivedWebmention.source_target_digest - == source_target_digest(long_source, long_target) - ) - ) - assert match is not None - assert match.source == long_source - assert match.target == long_target - db.session.add( - ReceivedWebmention( - uuid=uuid.uuid7(), source=long_source, target=long_target - ) - ) - with pytest.raises(IntegrityError): - db.session.commit() - db.session.rollback() - - -def test_long_sent_targets_enforce_uniqueness_without_indexing_url_text( - app: Flask, -) -> None: - now = datetime.now(UTC) - long_target = "https://destination.example/" + "a" * 4000 - with app.app_context(): - source = SourceDocument( - path="long/index.html", - url="https://example.com/long", - content_hash="0" * 32, - revision=1, - last_seen_at=now, - revised_at=now, - ) - source.sent_webmentions.append( - SentWebmention(target=long_target, desired_revision=1) - ) - db.session.add(source) - db.session.commit() - assert source.sent_webmentions[0].target_digest == target_digest(long_target) - - source.sent_webmentions.append( - SentWebmention(target=long_target, desired_revision=1) - ) - with pytest.raises(IntegrityError): - db.session.commit() - db.session.rollback() |
