Medium: - Frontend: Error-Handling in fetchUsers/fetchApiKeys (try/catch) - Frontend: Loading-Race behoben (Promise.all + .finally) - Frontend: API-Keys maskiert (nur letzte 4 Zeichen sichtbar) - Tests: Setup-Code aus test_auth.py in conftest.py konsolidiert - Tests: Fixture-Scope vereinheitlicht (function statt session) Low: - bare except in database.py → except Exception - datetime.utcnow → datetime.now(timezone.utc) durchgängig - DateTime(timezone=True) in allen Modell-Spalten - .gitignore hinzugefügt (.env, *.db, __pycache__, .idea, node_modules) Docs: - README aktualisiert (Sicherheit, Konfiguration, Projektstruktur, Tests) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
81 lines
1.9 KiB
Python
81 lines
1.9 KiB
Python
import pytest
|
|
import os
|
|
|
|
os.environ.setdefault("OLLAMA_URL", "http://127.0.0.1:9999")
|
|
|
|
|
|
def _setup_db():
|
|
from database import Base, engine, SessionLocal
|
|
from models import User, Quota
|
|
from crud import create_api_key, hash_password
|
|
|
|
Base.metadata.drop_all(bind=engine)
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
db = SessionLocal()
|
|
|
|
test_user = User(
|
|
username="testuser",
|
|
email="test@example.com",
|
|
hashed_password=hash_password("test123"),
|
|
is_active=True,
|
|
)
|
|
db.add(test_user)
|
|
db.commit()
|
|
db.refresh(test_user)
|
|
|
|
db.add(Quota(
|
|
user_id=test_user.id,
|
|
daily_tokens=1_000_000,
|
|
monthly_tokens=10_000_000,
|
|
daily_requests=1000,
|
|
monthly_requests=10000,
|
|
))
|
|
db.commit()
|
|
|
|
_, raw_key = create_api_key(db, test_user.id, "test-key")
|
|
os.environ["TEST_API_KEY"] = raw_key
|
|
|
|
admin_user = User(
|
|
username="admin",
|
|
email="admin@example.com",
|
|
hashed_password=hash_password("admin123"),
|
|
is_active=True,
|
|
is_admin=True,
|
|
)
|
|
db.add(admin_user)
|
|
db.commit()
|
|
db.refresh(admin_user)
|
|
|
|
db.add(Quota(
|
|
user_id=admin_user.id,
|
|
daily_tokens=10_000_000,
|
|
monthly_tokens=100_000_000,
|
|
daily_requests=10000,
|
|
monthly_requests=100000,
|
|
))
|
|
db.commit()
|
|
|
|
_, admin_raw_key = create_api_key(db, admin_user.id, "admin-key")
|
|
os.environ["ADMIN_API_KEY"] = admin_raw_key
|
|
|
|
db.close()
|
|
|
|
|
|
def _teardown_db():
|
|
from database import engine
|
|
from models import Base
|
|
Base.metadata.drop_all(bind=engine)
|
|
os.environ.pop("TEST_API_KEY", None)
|
|
os.environ.pop("ADMIN_API_KEY", None)
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def test_client():
|
|
_setup_db()
|
|
from main import app
|
|
from fastapi.testclient import TestClient
|
|
client = TestClient(app, raise_server_exceptions=False)
|
|
yield client
|
|
_teardown_db()
|