Initial commit: site monorepo with API, web, and infra.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def _super_headers(client) -> dict[str, str]:
|
||||
login = client.post(
|
||||
"/api/v1/auth/login",
|
||||
json={"email": "admin@compton.example", "password": "Admin1234"},
|
||||
)
|
||||
assert login.status_code == 200
|
||||
return {"Authorization": f"Bearer {login.json()['access_token']}"}
|
||||
|
||||
|
||||
def test_admin_create_user_rejects_weak_password(client):
|
||||
headers = _super_headers(client)
|
||||
response = client.post(
|
||||
"/api/v1/admin/users",
|
||||
headers=headers,
|
||||
json={
|
||||
"email": "weak-pass@example.com",
|
||||
"password": "password",
|
||||
"role": "user",
|
||||
"is_superuser": False,
|
||||
"status": "active",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 422
|
||||
@@ -0,0 +1,125 @@
|
||||
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
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def _login(client, email: str, password: str) -> dict[str, str]:
|
||||
response = client.post("/api/v1/auth/login", json={"email": email, "password": password})
|
||||
assert response.status_code == 200
|
||||
return {"Authorization": f"Bearer {response.json()['access_token']}"}
|
||||
|
||||
|
||||
def test_superuser_can_read_install_secrets(client):
|
||||
headers = _login(client, "admin@compton.example", "Admin1234")
|
||||
response = client.get("/api/v1/admin/secrets", headers=headers)
|
||||
assert response.status_code == 200
|
||||
payload = response.json()
|
||||
assert "secrets_status" in payload
|
||||
assert payload["secrets_status"]["postgres_password"] in {"configured", "missing"}
|
||||
assert "POSTGRES_PASSWORD" not in str(payload)
|
||||
|
||||
|
||||
def test_non_superuser_cannot_read_install_secrets(client):
|
||||
headers = _login(client, "ops@compton.example", "OpsAdmin1234")
|
||||
response = client.get("/api/v1/admin/secrets", headers=headers)
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_superuser_can_reveal_db_password(client):
|
||||
headers = _login(client, "admin@compton.example", "Admin1234")
|
||||
response = client.post(
|
||||
"/api/v1/admin/secrets/reveal",
|
||||
headers=headers,
|
||||
json={"key": "database_password"},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["key"] == "database_password"
|
||||
assert "value" in response.json()
|
||||
@@ -0,0 +1,10 @@
|
||||
from app.modules.admin.security_diagnostics import build_security_diagnostics_report
|
||||
|
||||
|
||||
def test_build_security_diagnostics_report_returns_checks():
|
||||
report = build_security_diagnostics_report()
|
||||
assert "checks" in report
|
||||
assert len(report["checks"]) >= 10
|
||||
ids = {check["id"] for check in report["checks"]}
|
||||
assert "jwt_access_secret" in ids
|
||||
assert "install_secrets_locked" in ids
|
||||
@@ -0,0 +1,88 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from app.modules.admin.service import (
|
||||
create_admin_user,
|
||||
delete_admin_user,
|
||||
get_server_log_tail,
|
||||
patch_user,
|
||||
reset_user_password,
|
||||
)
|
||||
from app.modules.users import repository
|
||||
from app.modules.users.repository import create_user, get_user_by_email
|
||||
from app.core.security import hash_password
|
||||
|
||||
|
||||
def test_admin_cannot_self_demote():
|
||||
admin = get_user_by_email("admin@compton.example")
|
||||
try:
|
||||
patch_user(admin, admin.id, "user", None)
|
||||
assert False, "Expected self-demotion error"
|
||||
except ValueError as exc:
|
||||
assert str(exc) == "SELF_DEMOTION_FORBIDDEN"
|
||||
|
||||
|
||||
def test_admin_can_promote_user():
|
||||
admin = get_user_by_email("admin@compton.example")
|
||||
regular = create_user("sample@example.com", hash_password("Valid123"), role="user", status="active")
|
||||
result = patch_user(admin, regular.id, "admin", None)
|
||||
assert result["role"] == "admin"
|
||||
|
||||
|
||||
def test_last_admin_protected():
|
||||
admin = get_user_by_email("admin@compton.example")
|
||||
target = create_user("target@example.com", hash_password("Valid123"), role="admin", status="active")
|
||||
with patch.object(repository, "count_admins", return_value=1):
|
||||
try:
|
||||
patch_user(admin, target.id, "user", None)
|
||||
assert False, "Expected last-admin protection"
|
||||
except ValueError as exc:
|
||||
assert str(exc) == "LAST_ADMIN_PROTECTED"
|
||||
|
||||
|
||||
def test_admin_cannot_self_block():
|
||||
admin = get_user_by_email("admin@compton.example")
|
||||
try:
|
||||
patch_user(admin, admin.id, None, "blocked")
|
||||
assert False, "Expected self-block error"
|
||||
except ValueError as exc:
|
||||
assert str(exc) == "SELF_BLOCK_FORBIDDEN"
|
||||
|
||||
|
||||
def test_reset_user_password_revokes_sessions():
|
||||
admin = get_user_by_email("admin@compton.example")
|
||||
target = create_user("reset-pw@example.com", hash_password("Valid123"), status="active")
|
||||
result = reset_user_password(admin, target.id, "NewValid123")
|
||||
assert result["status"] == "ok"
|
||||
|
||||
|
||||
def test_delete_admin_user_forbidden_for_self():
|
||||
admin = get_user_by_email("admin@compton.example")
|
||||
try:
|
||||
delete_admin_user(admin, admin.id)
|
||||
assert False, "Expected self-delete error"
|
||||
except ValueError as exc:
|
||||
assert str(exc) == "SELF_DELETE_FORBIDDEN"
|
||||
|
||||
|
||||
def test_create_admin_user_rejects_duplicate_email():
|
||||
admin = get_user_by_email("admin@compton.example")
|
||||
try:
|
||||
create_admin_user(
|
||||
admin,
|
||||
email="user@compton.example",
|
||||
password="Valid123",
|
||||
role="user",
|
||||
is_superuser=False,
|
||||
status="active",
|
||||
)
|
||||
assert False, "Expected duplicate user error"
|
||||
except ValueError as exc:
|
||||
assert str(exc) == "USER_EXISTS"
|
||||
|
||||
|
||||
def test_get_server_log_tail_empty_when_missing():
|
||||
with patch("app.modules.admin.service.settings") as mock_settings:
|
||||
mock_settings.server_log_path = "/tmp/compton-missing-log.txt"
|
||||
payload = get_server_log_tail()
|
||||
assert payload["lines"] == []
|
||||
|
||||
Reference in New Issue
Block a user