"""Production boundary, fixture cleanup, health and the runtime role.""" from __future__ import annotations from pathlib import Path import psycopg import pytest from pydantic import ValidationError from app.auth.accounts import create_account from app.core.settings import Settings from app.entrypoint import prepare from app.fixtures import purge_fixture_data from app.ops import create_case from app.seed import ADA_EMAIL, BEN_EMAIL, is_test_database, seed_demo from tests.conftest import FORBIDDEN from tests.support import PASSWORD def test_database_is_ephemeral(database): assert is_test_database(database.db_name) assert database.db_name.lower() not in FORBIDDEN assert database.tsumugu_env == "development" def test_unknown_environment_refuses_to_start(): with pytest.raises(ValidationError): Settings(tsumugu_env="staging", db_name="x", db_user="x", db_password="x") def test_seed_never_touches_dev_or_prod(): for env, name in (("production", "tsumugu"), ("development", "tsumugu_dev"), ("development", "tsumugu")): settings = Settings( tsumugu_env=env, db_host="192.0.2.1", db_name=name, db_user="x", db_password="x", seed_demo=True, ) assert seed_demo(settings) is False def test_production_refuses_seed_flag(): settings = Settings( tsumugu_env="production", db_host="192.0.2.1", db_name="tsumugu", db_user="x", db_password="x", seed_demo=True, ) with pytest.raises(SystemExit, match="SEED_DEMO"): prepare(settings) def test_import_does_not_open_the_database_by_itself(): import app.main assert app.main.app.title == "Tsumugu" def test_health_stays_small(client): response = client.get("/api/health") assert response.status_code == 200 assert response.json() == {"status": "ok", "app": "tsumugu"} def test_runtime_role_cannot_create_tables(database): with psycopg.connect(database.dsn(user=database.db_runtime_user)) as conn: with pytest.raises(psycopg.errors.InsufficientPrivilege): conn.execute("CREATE TABLE pytest_forbidden (id integer)") def test_purge_removes_fixtures_and_keeps_operator_work(database): with psycopg.connect(database.dsn(), row_factory=psycopg.rows.dict_row) as conn: operator = create_account(conn, "betreiber-echt@example.test", "Betreiber Beispiel", PASSWORD) kept = create_case(conn, operator["id"], operator["id"], "Echte Akte", None, "explicit") conn.commit() kept_id = kept["id"] seeded = database.model_copy(update={"seed_demo": True}) assert seed_demo(seeded) is True assert any(Path(database.media_root).iterdir()) assert purge_fixture_data(database) == 2 with psycopg.connect(database.dsn(), row_factory=psycopg.rows.dict_row) as conn: assert conn.execute("SELECT 1 FROM auth_accounts WHERE email = %s", (ADA_EMAIL,)).fetchone() is None assert conn.execute("SELECT 1 FROM auth_accounts WHERE email = %s", (BEN_EMAIL,)).fetchone() is None assert conn.execute("SELECT 1 FROM data_rooms WHERE name = %s", ("Haushalt Musterstraße",)).fetchone() is None row = conn.execute("SELECT title FROM cases WHERE id = %s", (kept_id,)).fetchone() assert row["title"] == "Echte Akte" assert list(Path(database.media_root).iterdir()) == []