Files
govoplan-core/tests/test_devserver.py

71 lines
2.7 KiB
Python

from __future__ import annotations
import os
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
from govoplan_core.devserver import redacted_database_url
class DevserverSmokeTests(unittest.TestCase):
def test_database_url_redaction_hides_passwords(self) -> None:
rendered = redacted_database_url(
"postgresql+psycopg://govoplan:database-secret@db.example.test/govoplan"
)
self.assertEqual(
rendered,
"postgresql+psycopg://govoplan:***@db.example.test/govoplan",
)
self.assertNotIn("database-secret", rendered)
self.assertEqual(redacted_database_url("://database-secret"), "<redacted>")
def test_smoke_mode_bootstraps_missing_local_sqlite_database(self) -> None:
repo_root = Path(__file__).resolve().parents[1]
src_root = repo_root / "src"
with tempfile.TemporaryDirectory(prefix="govoplan-devserver-smoke-") as directory:
runtime_root = Path(directory)
env = os.environ.copy()
for key in (
"DATABASE_URL",
"DEV_BOOTSTRAP_ENABLED",
"GOVOPLAN_DEV_DATABASE_BACKEND",
"GOVOPLAN_DEV_DATABASE_URL",
"GOVOPLAN_DEV_DATABASE_URL_PGTOOLS",
"GOVOPLAN_DATABASE_URL_PGTOOLS",
"GOVOPLAN_SERVER_CONFIG",
"FILE_STORAGE_LOCAL_ROOT",
"MOCK_MAILBOX_DIR",
):
env.pop(key, None)
env["APP_ENV"] = "dev"
env["CELERY_ENABLED"] = "false"
env["ENABLED_MODULES"] = "access"
env["GOVOPLAN_DEV_DATABASE_BACKEND"] = "sqlite"
env["PYTHONPATH"] = str(src_root) + (os.pathsep + env["PYTHONPATH"] if env.get("PYTHONPATH") else "")
result = subprocess.run(
[sys.executable, "-m", "govoplan_core.devserver", "--smoke", "--no-reload"],
cwd=runtime_root,
env=env,
text=True,
capture_output=True,
check=False,
)
self.assertEqual(result.returncode, 0, result.stderr or result.stdout)
db_path = runtime_root / "runtime" / "govoplan-dev.db"
self.assertTrue(db_path.exists(), result.stdout)
self.assertGreater(db_path.stat().st_size, 0, result.stdout)
self.assertIn(f"Runtime root: {runtime_root}", result.stdout)
self.assertIn(f"Database: sqlite:///{db_path}", result.stdout)
self.assertIn("Dev bootstrap for missing SQLite DB: enabled", result.stdout)
self.assertIn("Smoke check: OK", result.stdout)
if __name__ == "__main__":
unittest.main()