Безопасность довёл до ума — Cursor-генерацию переписал руками. IDOR закрыл, CSRF задушил, refresh rotation теперь как надо. HSTS на staging, ENABLE_DOCS=false, install.env recovery протестил. Backend: - jwt_denylist + auth_epoch: мгновенный revoke access JWT (logout/block/reset) - auth/admin/users: bump epoch, logout с Bearer, forgot_password skip для blocked - install_secrets: путь всегда apps/api/data/secrets/ (bootstrap из корня не ломает Docker) - seed: SEED_DEMO_USERS=false на prod/staging - тесты: jwt revoke, integration, coverage gate 90% Frontend: - logout шлёт Bearer, обработка TOKEN_REVOKED - guards TypeScript fix - E2E: blocked user → 401 сразу после block Infra: - staging/prod compose, TLS nginx, deploy-скрипты - k6 §17.2, backup/health/smoke scripts Docs: - docs/ на русском: project, security, deploy, release (старые md слили) - README короткий + план ТЗ + стандартные логины dev Код готов к плаванию. Капитан может идти писать фронт.
110 lines
3.4 KiB
Python
110 lines
3.4 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"
|
|
|