55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
import base64
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
|
|
import pytest
|
|
|
|
_TMP = tempfile.mkdtemp(prefix="stream-recorder-tests-")
|
|
os.environ["DATA_DIR"] = os.path.join(_TMP, "data")
|
|
os.environ["STATIC_DIR"] = os.path.join(_TMP, "no-static")
|
|
os.environ["RECORDING_ROOTS"] = os.path.join(_TMP, "recordings")
|
|
os.environ.pop("ENABLE_API_DOCS", None)
|
|
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
|
|
import app as app_module # noqa: E402
|
|
from config import hash_password # noqa: E402
|
|
from fastapi.testclient import TestClient # noqa: E402
|
|
from models import get_session, Job, Station, Recording, LogEntry # noqa: E402
|
|
|
|
TEST_PASSWORD = "geheim-12345"
|
|
# Schneller Hash für Tests
|
|
TEST_HASH = hash_password(TEST_PASSWORD, iterations=1000)
|
|
|
|
|
|
def basic(user, password):
|
|
return "Basic " + base64.b64encode(f"{user}:{password}".encode()).decode()
|
|
|
|
|
|
@pytest.fixture
|
|
def app_mod():
|
|
return app_module
|
|
|
|
|
|
@pytest.fixture
|
|
def client(app_mod):
|
|
cfg = app_mod.config
|
|
with cfg.lock:
|
|
cfg.data["auth"] = {"enabled": False, "username": "admin", "password_hash": TEST_HASH}
|
|
cfg.data["ntfy"] = {"url": "https://ntfy.example/topic", "token": "tk_secret", "events": "error"}
|
|
cfg.data["plik"] = {"url": "https://plik.example", "api_key": "plik_secret", "ttl": "30d"}
|
|
cfg.data["recording"]["download_path"] = os.environ["RECORDING_ROOTS"]
|
|
app_mod.clear_auth_cache()
|
|
app_mod.reset_auth_failures()
|
|
|
|
session = get_session()
|
|
try:
|
|
for model in (Job, Station, Recording, LogEntry):
|
|
session.query(model).delete()
|
|
session.commit()
|
|
finally:
|
|
session.close()
|
|
|
|
return TestClient(app_mod.app)
|