From 7035e9f9467684cccae2b7ff3faaa62e6a9f91aa Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sat, 31 Oct 2026 20:43:33 +0100 Subject: fix(core): harden local setup and Huey configuration Create local development and test directories automatically so fresh checkouts can start without pre-existing logging or instance paths. Reject in-memory and blackhole Huey backends in production because they cannot reliably deliver queued work across separate web and worker processes. --- tests/test_config.py | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) (limited to 'tests/test_config.py') diff --git a/tests/test_config.py b/tests/test_config.py index c483ee7..cc58f73 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -70,6 +70,32 @@ def test_missing_development_configuration_is_optional(tmp_path: Path) -> None: assert app.config["ALLOWED_HOSTNAMES"] == set() +def test_development_creates_local_directory_on_first_start( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.chdir(tmp_path) + assert not (tmp_path / "testing").exists() + + class FreshDevelopmentConfig(DevelopmentConfig): + pass + + app = create_app(FreshDevelopmentConfig) + + assert Path(app.instance_path) == tmp_path / "testing" + assert (tmp_path / "testing" / "webmentions-ssg.log").is_file() + + +def test_testing_creates_local_log_directory_on_first_start( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.chdir(tmp_path) + assert not (tmp_path / "testing").exists() + + create_app(TestingConfig) + + assert (tmp_path / "testing" / "webmentions-ssg-tests.log").is_file() + + def test_endpoint_limits_are_inherited_from_defaults() -> None: for config in (DefaultConfig, DevelopmentConfig, ProductionConfig, TestingConfig): assert config.MAX_REDIRECTS == 20 @@ -175,6 +201,57 @@ def test_production_requires_nonempty_allowed_hostnames(tmp_path: Path) -> None: create_app(EmptyHostnamesConfig) +@pytest.mark.parametrize( + "queue_settings", + [ + {}, # missing URL silently selected Huey's memory:// fallback + {"HUEY_URL": "memory://"}, + {"HUEY_URL": "MEMORY:///"}, + {"HUEY_URL": "blackhole://"}, + {"HUEY": {"url": "memory://"}}, + {"HUEY": {"url": "blackhole://"}}, + ], +) +def test_production_rejects_ephemeral_or_discarding_huey_queue( + tmp_path: Path, queue_settings: dict[str, object] +) -> None: + config_path = tmp_path / "production.cfg" + config_path.write_text("# Valid options are provided by the config subclass.\n") + + class ProductionWithQueue(ProductionConfig): + CONFIG_FILE = str(config_path) + SECRET_KEY = "a-long-secret-string-for-a-production-test" + ALLOWED_HOSTNAMES = {"example.com"} + RATELIMIT_STORAGE_URI = "redis://localhost:6379/1" + LOG_FILE = str(tmp_path / "production.log") + + for key, value in queue_settings.items(): + setattr(ProductionWithQueue, key, value) + + with pytest.raises(RuntimeError, match="HUEY_URL"): + create_app(ProductionWithQueue) + + +def test_production_prefers_explicit_huey_url_over_legacy_option( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setattr(LIMITER, "init_app", lambda app: None) + config_path = tmp_path / "production.cfg" + config_path.write_text("# Configuration inherited from subclass.\n") + + class ProductionWithExplicitQueue(ProductionConfig): + CONFIG_FILE = str(config_path) + SECRET_KEY = "a-long-secret-string-for-a-production-test" + ALLOWED_HOSTNAMES = {"example.com"} + RATELIMIT_STORAGE_URI = "redis://localhost:6379/1" + LOG_FILE = str(tmp_path / "production.log") + HUEY = {"url": "memory://"} + HUEY_URL = f"sqlite:///{tmp_path / 'huey.db'}" + + app = create_app(ProductionWithExplicitQueue) + assert app.extensions["huey"].huey.__class__.__name__ == "SqliteHuey" + + def test_environment_allowed_hostnames_override_config_file( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: @@ -233,6 +310,7 @@ def test_production_uses_persistent_configured_secret( LOG_FILE = str(tmp_path / "production.log") ALLOWED_HOSTNAMES = {"example.com"} RATELIMIT_STORAGE_URI = "redis://localhost:6379" + HUEY_URL = f"sqlite:///{tmp_path / 'huey.db'}" first = create_app(ConfiguredProduction) second = create_app(ConfiguredProduction) @@ -254,6 +332,7 @@ def test_production_accepts_environment_secret_override( LOG_FILE = str(tmp_path / "production.log") ALLOWED_HOSTNAMES = {"example.com"} RATELIMIT_STORAGE_URI = "redis://localhost:6379" + HUEY_URL = f"sqlite:///{tmp_path / 'huey.db'}" app = create_app(EnvConfiguredProduction) assert app.secret_key == key -- cgit v1.3.1