Add GET /api/logs/{name} endpoint to admin API
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import os
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
os.environ.setdefault("ADMIN_PASSWORD", "test-admin-pw")
|
||||
os.environ.setdefault("OLLAMA_URL", "http://127.0.0.1:9999")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(tmp_path):
|
||||
log_file = tmp_path / "usage.log"
|
||||
log_file.write_text("\n".join(f"Zeile {i}" for i in range(1, 16)) + "\n")
|
||||
(tmp_path / "error.log").write_text("Fehler A\nFehler B\n")
|
||||
os.environ["LOG_FILE"] = str(log_file)
|
||||
|
||||
from database import Base, engine
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
from admin import app
|
||||
yield TestClient(app, raise_server_exceptions=False)
|
||||
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
os.environ.pop("LOG_FILE", None)
|
||||
|
||||
|
||||
AUTH = {"Authorization": "Bearer test-admin-pw"}
|
||||
|
||||
|
||||
def test_logs_usage_returns_last_10_lines(client):
|
||||
resp = client.get("/api/logs/usage", headers=AUTH)
|
||||
assert resp.status_code == 200
|
||||
lines = resp.json()["lines"]
|
||||
assert len(lines) == 10
|
||||
assert lines[-1] == "Zeile 15"
|
||||
assert lines[0] == "Zeile 6"
|
||||
|
||||
|
||||
def test_logs_error_returns_content(client):
|
||||
resp = client.get("/api/logs/error", headers=AUTH)
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["lines"] == ["Fehler A", "Fehler B"]
|
||||
|
||||
|
||||
def test_logs_missing_file_returns_empty(client, tmp_path):
|
||||
os.environ["LOG_FILE"] = str(tmp_path / "nonexistent.log")
|
||||
resp = client.get("/api/logs/usage", headers=AUTH)
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["lines"] == []
|
||||
|
||||
|
||||
def test_logs_invalid_name_returns_400(client):
|
||||
resp = client.get("/api/logs/secret", headers=AUTH)
|
||||
assert resp.status_code == 400
|
||||
|
||||
|
||||
def test_logs_requires_auth(client):
|
||||
resp = client.get("/api/logs/usage")
|
||||
assert resp.status_code == 401
|
||||
Reference in New Issue
Block a user