Align the project baseline with the latest admin interface styling and layout structure while documenting setup and usage updates in README.
74 lines
2.3 KiB
Python
74 lines
2.3 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"
|