From eb11fb64e9cad6fbda2065362189653416d98577 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Thu, 19 Nov 2026 21:18:37 +0100 Subject: 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. --- tests/test_init.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'tests/test_init.py') diff --git a/tests/test_init.py b/tests/test_init.py index 0966b19..373af01 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -8,6 +8,7 @@ import subprocess import sys from pathlib import Path +import pytest from flask import Flask from sqlalchemy import create_engine @@ -207,3 +208,60 @@ def test_sqlite_connect_listener_does_not_touch_other_dbapis() -> None: # Non-SQLite DB-API connections bypass the listener completely. set_sqlite_pragmas(object(), object()) + + +@pytest.mark.parametrize("development", [False, True]) +def test_create_app_selects_default_configuration_from_environment( + monkeypatch: pytest.MonkeyPatch, development: bool +) -> None: + import webmentions_ssg as package + + class DefaultTestConfig(TestingConfig): + LOG_FILE = str(Path.cwd() / "testing" / "default-config-test.log") + + class DevelopmentTestConfig(DefaultTestConfig): + CONFIG_SELECTION = "development" + + class ProductionTestConfig(DefaultTestConfig): + CONFIG_SELECTION = "production" + + monkeypatch.setattr(package, "DevelopmentConfig", DevelopmentTestConfig) + monkeypatch.setattr(package, "ProductionConfig", ProductionTestConfig) + if development: + monkeypatch.setenv("WEBMENTIONS_SSG_DEV", "true") + else: + monkeypatch.delenv("WEBMENTIONS_SSG_DEV", raising=False) + + application = create_app() + assert application.config["CONFIG_SELECTION"] == ( + "development" if development else "production" + ) + + +def test_sqlite_connect_rejects_journal_mode_that_cannot_be_applied( + app: Flask, monkeypatch: pytest.MonkeyPatch +) -> None: + from unittest.mock import Mock + + from webmentions_ssg import extensions + + cursor = Mock() + cursor.fetchall.return_value = [(0, "main", "/tmp/test-file.sqlite")] + cursor.fetchone.side_effect = [("DELETE",), ("DELETE",)] + + class FakeConnection: + def __init__(self) -> None: + self.autocommit = False + + def cursor(self) -> Mock: + return cursor + + connection = FakeConnection() + with app.app_context(), monkeypatch.context() as patcher: + patcher.setattr(extensions.sqlite3, "Connection", FakeConnection) + with pytest.raises(RuntimeError, match="journal_mode WAL was not applied"): + extensions.set_sqlite_pragmas(connection, None) + + assert connection.autocommit is False + cursor.close.assert_called_once_with() + cursor.execute.assert_any_call("PRAGMA journal_mode = WAL") -- cgit v1.3.1