62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
import sys
|
|
import os
|
|
import json
|
|
sys.path.insert(0, 'app')
|
|
|
|
from fastapi.testclient import TestClient
|
|
from main import app
|
|
|
|
client = TestClient(app)
|
|
|
|
def test_admin_logs_access():
|
|
print("Testing /admin/logs access...")
|
|
|
|
# 1. Unauthenticated -> Redirect to login
|
|
response = client.get("/admin/logs", allow_redirects=False)
|
|
assert response.status_code == 302, f"Expected 302, got {response.status_code}"
|
|
print("✅ Unauthenticated access blocked")
|
|
|
|
# Mock Admin User Session
|
|
# Note: TestClient cookies handling is specific, we rely on session middleware
|
|
# For a real test we'd need to mock the dependency override, but let's try a simpler approach
|
|
# by mocking the dependency directly in the app for this test run.
|
|
|
|
# 2. Mock Admin Dependency
|
|
from deps import current_user
|
|
app.dependency_overrides[current_user] = lambda: {"role": "admin", "email": "admin@test.com"}
|
|
|
|
response = client.get("/admin/logs")
|
|
assert response.status_code == 200, f"Expected 200, got {response.status_code}"
|
|
assert "System Logs & Stats" in response.text
|
|
print("✅ Admin access allowed")
|
|
|
|
# 3. Test Data Endpoint
|
|
response = client.get("/admin/logs/data")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "logs" in data
|
|
print("✅ Data endpoint works")
|
|
|
|
# 4. Test Stats Endpoint
|
|
response = client.get("/admin/logs/stats")
|
|
assert response.status_code == 200
|
|
stats = response.json()
|
|
assert "total_interactions" in stats
|
|
assert "total_tokens_estimated" in stats
|
|
assert "tokens_by_role" in stats
|
|
print("✅ Stats endpoint works")
|
|
|
|
# Clean up
|
|
app.dependency_overrides = {}
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
test_admin_logs_access()
|
|
print("\n🎉 All Admin Logs tests passed!")
|
|
except AssertionError as e:
|
|
print(f"\n❌ Test failed: {e}")
|
|
sys.exit(1)
|
|
except Exception as e:
|
|
print(f"\n❌ Error: {e}")
|
|
sys.exit(1)
|