aboutsummaryrefslogtreecommitdiff
path: root/tests/test_cli.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_cli.py')
-rw-r--r--tests/test_cli.py134
1 files changed, 126 insertions, 8 deletions
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: