"""Gedeelde fixtures. Elke test krijgt een eigen /data-map, zodat de suite nooit de echte config.json, audit-db of git-cache van een draaiende instantie raakt.""" import importlib import sys from pathlib import Path import pytest APP_DIR = Path(__file__).resolve().parent.parent / "server-up" if str(APP_DIR) not in sys.path: sys.path.insert(0, str(APP_DIR)) @pytest.fixture def env(tmp_path, monkeypatch): """Verse omgeving: config, audit-db, git-cache en secret in tmp_path.""" data = tmp_path / "data" data.mkdir() lib = tmp_path / "stacks" lib.mkdir() monkeypatch.setenv("SU_CONFIG", str(data / "config.json")) monkeypatch.setenv("SU_AUDIT", str(data / "audit.db")) monkeypatch.setenv("SU_GIT_CACHE", str(data / "git")) monkeypatch.setenv("SU_SECRET", str(data / "secret.key")) monkeypatch.setenv("SU_JOBS", str(data / "jobs")) monkeypatch.setenv("LIBRARY_DIR", str(lib)) monkeypatch.setenv("DATA_DIR", str(tmp_path / "appdata")) monkeypatch.setenv("BACKUP_DIR", str(tmp_path / "backups")) # Modules opnieuw importeren zodat ze de nieuwe paden oppikken. for name in list(sys.modules): if name == "core" or name.startswith("core.") or name == "app": del sys.modules[name] core = importlib.import_module("core") core._path = Path(data / "config.json") return {"data": data, "lib": lib, "tmp": tmp_path, "core": core} @pytest.fixture def client(env, monkeypatch): """Flask-testclient met een aangemaakt account en geldige sessie.""" import app as app_module app_module.app.config["TESTING"] = True c = app_module.app.test_client() return c @pytest.fixture def anon_client(env): """Testclient zonder sessie.""" import app as app_module app_module.app.config["TESTING"] = True return app_module.app.test_client() def login(client, username="tester", password="hunter2hunter2"): """Maak het eerste account aan en retourneer het CSRF-token.""" r = client.post("/api/auth/setup", json={"username": username, "password": password}) assert r.status_code == 200, r.get_json() return r.get_json()["csrf_token"]