from __future__ import annotations import importlib.util import os from pathlib import Path import unittest from unittest.mock import patch META_ROOT = Path(__file__).resolve().parents[1] SCRIPT = META_ROOT / "tools" / "checks" / "postgres-integration-check.py" def _load_script(): spec = importlib.util.spec_from_file_location( "postgres_integration_check", SCRIPT, ) if spec is None or spec.loader is None: raise RuntimeError(f"Could not load {SCRIPT}") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module class PostgresIntegrationCheckTests(unittest.TestCase): def test_staging_check_has_explicit_runtime_safety_settings(self) -> None: module = _load_script() with patch.dict(os.environ, {}, clear=True): env = module._check_env( database_url="postgresql+psycopg://user:password@postgres/db", modules="tenancy,access,search", app_env="staging", ) self.assertEqual( env["CORS_ORIGINS"], "http://127.0.0.1:5173,http://localhost:5173", ) self.assertEqual( env["GOVOPLAN_TRUSTED_HOSTS"], "127.0.0.1,localhost,testserver", ) self.assertEqual( env["GOVOPLAN_CONNECTOR_ALLOW_PRIVATE_NETWORKS"], "false", ) def test_existing_runtime_safety_settings_are_preserved(self) -> None: module = _load_script() configured = { "CORS_ORIGINS": "https://govoplan.example.test", "GOVOPLAN_TRUSTED_HOSTS": "govoplan.example.test", "GOVOPLAN_CONNECTOR_ALLOW_PRIVATE_NETWORKS": "true", } with patch.dict(os.environ, configured, clear=True): env = module._check_env( database_url="postgresql+psycopg://user:password@postgres/db", modules="tenancy,access,search", app_env="staging", ) for key, value in configured.items(): self.assertEqual(env[key], value) if __name__ == "__main__": unittest.main()