"""Gemeinsame Fixtures: App mit temporaerer SQLite-DB und TestClient.""" from __future__ import annotations from dataclasses import replace from pathlib import Path import pytest from fastapi.testclient import TestClient from redaktionskern.app import create_app from redaktionskern.config import Settings PROJEKT_WURZEL = Path(__file__).resolve().parents[1] ADMIN_PASSWORD = "test-admin-123" @pytest.fixture(autouse=True) def _hintergrundjobs_deaktiviert(monkeypatch): """BGG-Hintergrund-Sync in Tests standardmäßig aus (keine Threads/Netzwerk). Einzelne Tests aktivieren den Scheduler explizit wieder per monkeypatch.setenv("SPIELE_BGG_SYNC_AKTIV", "1"). """ monkeypatch.setenv("SPIELE_BGG_SYNC_AKTIV", "0") # Auch der tägliche Archiv-Job bleibt in Tests aus. monkeypatch.setenv("SPIELE_ARCHIV_JOB_AKTIV", "0") # Auch der BGG-Hilfsclient des dedup-Plugins bleibt in Tests offline. monkeypatch.setenv("SPIELE_DEDUP_BGG_AKTIV", "0") @pytest.fixture def settings(tmp_path) -> Settings: return Settings( database_url=f"sqlite:///{tmp_path / 'test.db'}", session_secret="test-secret", initial_admin_password=ADMIN_PASSWORD, plugins_dir=PROJEKT_WURZEL / "plugins", ) @pytest.fixture def settings_ohne_plugins(settings) -> Settings: """Nur Entry-Point-Plugins, kein plugins/-Verzeichnis.""" return replace(settings, plugins_dir=None) @pytest.fixture def app(settings): return create_app(settings) @pytest.fixture def client(app) -> TestClient: return TestClient(app) def melde_an( client: TestClient, username: str = "admin", password: str = ADMIN_PASSWORD, ): """Meldet einen Benutzer an und prueft die Weiterleitung.""" antwort = client.post( "/login", data={"username": username, "password": password}, follow_redirects=False, ) assert antwort.status_code == 303, f"Login fehlgeschlagen fuer {username}" return antwort def lege_benutzer_an(app, username: str, role: str, password: str = "test-12345678"): """Legt direkt per DB einen Benutzer an (fuer Rollen-Tests).""" from redaktionskern.auth.models import User from redaktionskern.auth.security import hash_password with app.state.session_factory() as db: db.add( User( username=username, display_name=username.capitalize(), password_hash=hash_password(password), role=role, active=True, ) ) db.commit()