- Datenmodell + eigene Migration 0001_neuheiten_tabelle (Tabelle neuheiten: Titel, Verlag, Autor, Erscheinungsjahr, BGG-ID, Status 'neuheit', Quelle, Zeitstempel; bgg_id eindeutig als Merge-Kriterium) - BoardGameGeek XML API2-Client (search + thing, Batches à 20 IDs): Rate-Limit >= 1 s zwischen Requests, Retry mit exponentiellem Backoff bei 5xx/429/Netzwerkfehlern, HTTP 202 gemäß Retry-After, robustes XML-Parsing; Transport/Uhr/Sleep injizierbar (keine echten Calls in Tests) - Filter: Erweiterungen (boardgameexpansion) auf Request- und Elementebene ausgeschlossen; Prototypen per Titel-Heuristik (BGG hat keinen Marker) - Sync-Service mit Update-statt-Duplikat-Logik über die eindeutige BGG-ID (Status bleibt erhalten); Fehler je Suchbegriff brechen den Lauf nicht ab - APScheduler-Hintergrundjob (Standard 24 h) mit Überlappungsschutz, abschaltbar/intervallkonfigurierbar per Env; manueller 'Jetzt synchronisieren'-Endpunkt nur für Admin/Redakteur, optional mit Sofort-Suchbegriff - UI /neuheiten: sortier-/filterbare Tabelle mit Volltextsuche (HTMX-Teilladung, noscript-fähig), deutsche Oberfläche, BGG-Links, Ergebnis-Banner - Plugin-Loader: idempotentes Laden (Modul-Caching), damit mehrere create_app()-Aufrufe dieselben Plugin-Klassen/Tabellen nutzen - Tests: Parsing, Erweiterungs-/Prototyp-Filter, Rate-Limit/Backoff/202, Update-statt-Duplikat, Rollen am Sync-Endpunkt, Scheduler-Lifecycle — ausschließlich mit gemockten BGG-Antworten (uv run pytest: 96 grün) - README/AGENTS: Plugin-Doku, Env-Variablen, Fortschrittstabelle aktualisiert
83 lines
2.3 KiB
Python
83 lines
2.3 KiB
Python
"""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")
|
|
|
|
|
|
@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()
|