diff options
Diffstat (limited to 'tests/test_models.py')
| -rw-r--r-- | tests/test_models.py | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/tests/test_models.py b/tests/test_models.py index 392b246..26439b9 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -7,12 +7,18 @@ from collections.abc import Callable from datetime import UTC, datetime import pytest +import sqlalchemy as sa +from flask import Flask +from sqlalchemy.exc import IntegrityError +from webmentions_ssg import DATABASE as db from webmentions_ssg.models import ( ReceivedWebmention, SentWebmention, Source, User, + received_url_digest, + sent_target_digest, uuid7_to_datetime, ) @@ -162,3 +168,144 @@ def test_user_session_id_changes_when_password_hash_changes() -> None: user.password = "a strong passphrase" assert user.password_hash != original_hash assert user.get_id() != original_id + + +def test_received_status_remains_text_with_enum_constants() -> None: + from sqlalchemy import Text + + from webmentions_ssg.models import ReceivedStatus + + assert isinstance(ReceivedWebmention.__table__.c.status.type, Text) + assert ReceivedStatus.VERIFIED == "verified" + assert ReceivedStatus.DELETED == "deleted" + + +def test_received_verification_reset_is_reusable_for_atomic_updates() -> None: + from webmentions_ssg.models import ReceivedStatus + + mention = ReceivedWebmention( + uuid=uuid.uuid7(), + source="https://source.example/post", + target="https://dennisfink.me/post", + status=ReceivedStatus.FAILED, + failure_reason="Old error", + recovery_attempts=3, + ) + assert mention.reset_for_verification() is mention + assert mention.status == ReceivedStatus.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_unless_explicit_resend() -> None: + from webmentions_ssg.models import SentWebmentionStatus + + sent_at = datetime.now(UTC) + mention = SentWebmention( + target="https://example.com/post", + active=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.reopen(3, False) is mention + assert mention.desired_revision == 3 + assert mention.active is False + assert mention.attempt_count == 0 + assert mention.next_retry_at is None + assert mention.status == SentWebmentionStatus.SENT + assert mention.sent_revision == 2 + + assert mention.reopen(3, True, resend=True) is mention + assert mention.processed_revision is None + assert mention.status is None + assert mention.sent_revision == 2 + assert mention.last_sent_at == sent_at + + +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") + + +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.url_digest == received_url_digest(received.source, received.target) + received.source = "https://source.example/c" + assert received.url_digest == received_url_digest(received.source, received.target) + received.target = "https://site.example/d" + assert received.url_digest == received_url_digest(received.source, received.target) + + sent = SentWebmention(target="https://site.example/a", desired_revision=1) + assert sent.target_digest == sent_target_digest(sent.target) + sent.target = "https://site.example/b" + assert sent.target_digest == sent_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.url_digest + == received_url_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 = Source( + 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 == sent_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() |
