Files
site/apps/api/tests/modules/admin/test_admin_router.py
T

126 lines
4.0 KiB
Python

from app.core.security import hash_password
from app.modules.users import repository
def _admin_headers(client):
login = client.post(
"/api/v1/auth/login",
json={"email": "admin@compton.example", "password": "Admin1234"},
)
token = login.json()["access_token"]
return {"Authorization": f"Bearer {token}"}
def _plain_admin_headers(client):
email = "ops-admin@compton.example"
if not repository.get_user_by_email(email):
repository.create_user(
email=email,
password_hash=hash_password("Admin1234"),
role="admin",
is_superuser=False,
status="active",
)
login = client.post("/api/v1/auth/login", json={"email": email, "password": "Admin1234"})
token = login.json()["access_token"]
return {"Authorization": f"Bearer {token}"}
def test_admin_users_list(client):
response = client.get("/api/v1/admin/users", headers=_admin_headers(client))
assert response.status_code == 200
assert "data" in response.json()
def test_non_superuser_cannot_patch_settings(client):
response = client.patch(
"/api/v1/admin/settings",
json={"values": {"enable_docs": False}},
headers=_plain_admin_headers(client),
)
assert response.status_code == 403
assert response.json()["detail"] == "SUPERUSER_ONLY"
def test_superuser_can_patch_settings(client):
response = client.patch(
"/api/v1/admin/settings",
json={"values": {"enable_docs": False}},
headers=_admin_headers(client),
)
assert response.status_code == 200
payload = response.json()
assert "values" in payload
def test_superuser_can_create_and_delete_user(client):
created = client.post(
"/api/v1/admin/users",
json={
"email": "created-by-admin@compton.example",
"password": "StrongPass123A",
"role": "user",
"is_superuser": False,
"status": "active",
},
headers=_admin_headers(client),
)
assert created.status_code == 200
user_id = created.json()["id"]
deleted = client.delete(f"/api/v1/admin/users/{user_id}", headers=_admin_headers(client))
assert deleted.status_code == 200
assert deleted.json()["status"] == "deleted"
def test_admin_summary_and_stats(client):
headers = _admin_headers(client)
summary = client.get("/api/v1/admin/summary", headers=headers)
assert summary.status_code == 200
assert "users_count" in summary.json()
stats = client.get("/api/v1/admin/stats", headers=headers)
assert stats.status_code == 200
def test_superuser_diagnostics_and_server_log(client):
headers = _admin_headers(client)
diagnostics = client.get("/api/v1/admin/diagnostics/report", headers=headers)
assert diagnostics.status_code == 200
assert "checks" in diagnostics.json()
activity = client.get("/api/v1/admin/activity-feed", headers=headers)
assert activity.status_code == 200
assert "events" in activity.json()
server_log = client.get("/api/v1/admin/server-log", headers=headers)
assert server_log.status_code == 200
assert "lines" in server_log.json()
def test_admin_ui_activity(client):
response = client.post(
"/api/v1/admin/ui-activity",
json={"event": "tab_open", "meta": {"tab": "users"}},
headers=_admin_headers(client),
)
assert response.status_code == 200
assert response.json()["status"] == "ok"
def test_admin_sync_metrics_with_enterprise(client):
from app.modules.sync import repository as sync_repo
ent = sync_repo.create_enterprise("Metrics Test Farm", "metrics-test-farm")
headers = _admin_headers(client)
response = client.get("/api/v1/admin/sync-metrics", headers=headers)
assert response.status_code == 200
payload = response.json()
assert isinstance(payload, list)
match = next((row for row in payload if row["enterprise_id"] == ent.id), None)
assert match is not None
assert match["name"] == "Metrics Test Farm"
assert "hubs" in match
assert "outbox_pending" in match