feat(admin): finalizing modular refactoring with testing suite and UI polish
This commit is contained in:
65
backend/tests/conftest.py
Normal file
65
backend/tests/conftest.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from backend.database import Base, get_db
|
||||
from backend.main import app
|
||||
from backend.auth import get_current_admin, TokenData
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
# Use in-memory SQLite for tests
|
||||
SQLALCHEMY_DATABASE_URL = "sqlite://"
|
||||
|
||||
engine = create_engine(
|
||||
SQLALCHEMY_DATABASE_URL,
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=StaticPool,
|
||||
)
|
||||
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
@pytest.fixture(scope="function", autouse=True)
|
||||
def mock_scheduler():
|
||||
from backend.scheduler import scheduler
|
||||
if scheduler.running:
|
||||
scheduler.shutdown()
|
||||
yield
|
||||
if scheduler.running:
|
||||
scheduler.shutdown()
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def db():
|
||||
Base.metadata.create_all(bind=engine)
|
||||
session = TestingSessionLocal()
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
session.close()
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def client(db):
|
||||
def override_get_db():
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
pass
|
||||
|
||||
# Mock admin user with valid TokenData
|
||||
def override_get_current_admin():
|
||||
return TokenData(
|
||||
sub=1,
|
||||
username="admin",
|
||||
role="admin",
|
||||
exp=datetime.now(timezone.utc)
|
||||
)
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
app.dependency_overrides[get_current_admin] = override_get_current_admin
|
||||
|
||||
with TestClient(app) as c:
|
||||
yield c
|
||||
|
||||
app.dependency_overrides.clear()
|
||||
35
backend/tests/test_admin.py
Normal file
35
backend/tests/test_admin.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import pytest
|
||||
|
||||
def test_get_backups(client):
|
||||
response = client.get("/admin/db/backups")
|
||||
assert response.status_code == 200
|
||||
assert isinstance(response.json(), list)
|
||||
|
||||
def test_db_settings_workflow(client):
|
||||
# GET settings
|
||||
response = client.get("/admin/db/settings")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "retention_count" in data
|
||||
|
||||
# PATCH settings
|
||||
new_settings = {
|
||||
"retention_count": 25,
|
||||
"schedule_hour": 5,
|
||||
"schedule_freq_days": 2
|
||||
}
|
||||
response = client.patch("/admin/db/settings", json=new_settings)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["retention_count"] == 25
|
||||
|
||||
# Verify persistence
|
||||
response = client.get("/admin/db/settings")
|
||||
assert response.json()["retention_count"] == 25
|
||||
|
||||
def test_ai_config(client):
|
||||
response = client.get("/admin/db/settings/ai")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "active_provider" in data
|
||||
assert "providers" in data
|
||||
assert len(data["providers"]) == 2
|
||||
Reference in New Issue
Block a user