diff options
| author | Dennis Fink | 2026-11-19 21:18:37 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-19 21:18:37 +0100 |
| commit | eb11fb64e9cad6fbda2065362189653416d98577 (patch) | |
| tree | 0bc5737b1c273ea6f04adc015154565c385dc719 /tests/test_cli.py | |
| parent | 88f54d9a7699944b7d56f978e724b1c0b54dde98 (diff) | |
| download | webmentions-ssg-eb11fb64e9cad6fbda2065362189653416d98577.tar.gz webmentions-ssg-eb11fb64e9cad6fbda2065362189653416d98577.zip | |
test: expand failure and migration coverage
Add regression coverage for malformed input, lost task claims,
moderation races, network failures, serializer validation, and
production configuration guards.
Exercise migration downgrade, backfill, and autogeneration paths plus
additional receiver and sender edge cases.
Diffstat (limited to 'tests/test_cli.py')
| -rw-r--r-- | tests/test_cli.py | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/tests/test_cli.py b/tests/test_cli.py index 5699bb3..1507544 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -900,3 +900,95 @@ def test_users_cli_accepts_minimum_whitespace_and_unbounded_passwords( assert result.exit_code == 0, result.output user = _get_user(app, "alice") assert user is not None and user.check_password(passphrase) + + +def test_all_migrations_can_downgrade_to_empty_schema(database_app: Flask) -> None: + """Exercise every downgrade, including the two merged migration branches.""" + with database_app.app_context(): + migrate_upgrade() + assert _database_revision(database_app) == _head_revision(database_app) + assert "received_webmentions" in inspect(db.engine).get_table_names() + + migrate_downgrade(revision="base") + with db.engine.connect() as connection: + tables = set(inspect(connection).get_table_names()) + # Alembic may keep its empty version table after downgrading to base. + assert tables <= {"alembic_version"} + if "alembic_version" in tables: + assert ( + connection.execute(sa.text("SELECT count(*) FROM alembic_version")) + .scalar_one() + == 0 + ) + + +def test_fingerprint_migration_backfills_existing_sent_target( + database_app: Flask, +) -> None: + from webmentions_ssg.models.utils import target_digest + + source_uuid = "0" * 31 + "1" + sent_uuid = "0" * 31 + "2" + target = "https://target.example/legacy" + with database_app.app_context(): + migrate_upgrade(revision="c0272f25ce17") + 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://source.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, active, desired_revision, attempt_count) " + "VALUES (:uuid, :source_id, :target, 1, 1, 0)" + ), + {"uuid": sent_uuid, "source_id": source_uuid, "target": target}, + ) + + migrate_upgrade(revision="d845aa7fbc19") + with db.engine.connect() as connection: + row = connection.execute( + sa.text("SELECT target, target_digest FROM sent_webmentions") + ).one() + assert row.target == target + assert row.target_digest == target_digest(target) + assert any( + constraint["column_names"] == ["source_id", "target_digest"] + for constraint in inspect(connection).get_unique_constraints( + "sent_webmentions" + ) + ) + + +def test_autogenerate_skips_empty_migration_revisions( + database_app: Flask, monkeypatch: pytest.MonkeyPatch +) -> None: + from types import SimpleNamespace + from unittest.mock import Mock + + with database_app.app_context(): + migrate_upgrade(revision="f63641044cc1") + callback = database_app.extensions["migrate"].configure_args[ + "process_revision_directives" + ] + config = callback.__globals__["config"] + monkeypatch.setattr( + config, "cmd_opts", SimpleNamespace(autogenerate=True), raising=False + ) + empty = Mock() + empty.upgrade_ops.is_empty.return_value = True + directives = [empty] + callback(None, None, directives) + assert directives == [] + + nonempty = Mock() + nonempty.upgrade_ops.is_empty.return_value = False + directives = [nonempty] + callback(None, None, directives) + assert directives == [nonempty] |
