-
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import jwt
|
||||
|
||||
from app.core.config import settings
|
||||
from app.models import AdminRole
|
||||
from tests.conftest import PASSWORD
|
||||
|
||||
|
||||
async def test_health(client):
|
||||
r = await client.get("/api/health")
|
||||
assert r.status_code == 200
|
||||
body = r.json()
|
||||
assert body["status"] == "ok" and body["database"] == "ok"
|
||||
assert body["watermark_logo_present"] is True
|
||||
|
||||
|
||||
async def test_login_success_and_me(client, create_admin):
|
||||
await create_admin("Owner@Example.com".lower(), AdminRole.SUPER_ADMIN)
|
||||
r = await client.post("/api/auth/login", json={"email": "OWNER@example.com", "password": PASSWORD})
|
||||
assert r.status_code == 200
|
||||
data = r.json()
|
||||
assert data["token_type"] == "bearer" and data["expires_in"] > 0
|
||||
assert "hashed_password" not in data["admin"]
|
||||
|
||||
me = await client.get("/api/auth/me", headers={"Authorization": f"Bearer {data['access_token']}"})
|
||||
assert me.status_code == 200
|
||||
assert me.json()["email"] == "owner@example.com"
|
||||
assert me.json()["last_login_at"] is not None
|
||||
|
||||
|
||||
async def test_login_wrong_password_and_unknown_email(client, create_admin):
|
||||
await create_admin("a@example.com", AdminRole.ADMIN)
|
||||
r1 = await client.post("/api/auth/login", json={"email": "a@example.com", "password": "nope"})
|
||||
r2 = await client.post("/api/auth/login", json={"email": "ghost@example.com", "password": "nope"})
|
||||
assert r1.status_code == r2.status_code == 401
|
||||
assert r1.json()["detail"] == r2.json()["detail"] # no user enumeration
|
||||
|
||||
|
||||
async def test_disabled_account_cannot_login(client, create_admin):
|
||||
await create_admin("off@example.com", AdminRole.ADMIN, active=False)
|
||||
r = await client.post("/api/auth/login", json={"email": "off@example.com", "password": PASSWORD})
|
||||
assert r.status_code == 403
|
||||
|
||||
|
||||
async def test_admin_routes_require_token(client):
|
||||
for method, url in [
|
||||
("get", "/api/auth/me"),
|
||||
("get", "/api/admin/enquiries"),
|
||||
("get", "/api/admin/dashboard/stats"),
|
||||
("post", "/api/admin/projects"),
|
||||
("post", "/api/admin/projects/1/images"),
|
||||
("delete", "/api/admin/project-images/1"),
|
||||
("post", "/api/admin/services"),
|
||||
("put", "/api/admin/content/hero"),
|
||||
]:
|
||||
r = await getattr(client, method)(url)
|
||||
assert r.status_code == 401, (method, url, r.status_code)
|
||||
|
||||
|
||||
async def test_invalid_and_expired_tokens(client, create_admin):
|
||||
admin = await create_admin("t@example.com", AdminRole.ADMIN)
|
||||
r = await client.get("/api/auth/me", headers={"Authorization": "Bearer not-a-jwt"})
|
||||
assert r.status_code == 401
|
||||
|
||||
forged = jwt.encode({"sub": str(admin.id), "type": "access", "iat": datetime.now(UTC),
|
||||
"exp": datetime.now(UTC) + timedelta(hours=1)}, "x" * 40, algorithm="HS256")
|
||||
r = await client.get("/api/auth/me", headers={"Authorization": f"Bearer {forged}"})
|
||||
assert r.status_code == 401
|
||||
|
||||
past = datetime.now(UTC) - timedelta(hours=2)
|
||||
expired = jwt.encode({"sub": str(admin.id), "type": "access", "iat": past,
|
||||
"exp": past + timedelta(minutes=1)}, settings.JWT_SECRET_KEY, algorithm="HS256")
|
||||
r = await client.get("/api/auth/me", headers={"Authorization": f"Bearer {expired}"})
|
||||
assert r.status_code == 401
|
||||
assert "expired" in r.json()["detail"].lower()
|
||||
|
||||
|
||||
async def test_login_rate_limited(client):
|
||||
codes = [
|
||||
(await client.post("/api/auth/login", json={"email": "x@example.com", "password": "bad"})).status_code
|
||||
for _ in range(6)
|
||||
]
|
||||
assert codes[:5] == [401] * 5
|
||||
assert codes[5] == 429
|
||||
|
||||
|
||||
async def test_role_based_access(client, editor_headers, admin_headers):
|
||||
# create an enquiry publicly
|
||||
await client.post("/api/enquiries", json={"name": "Ravi", "phone": "9876543210"})
|
||||
# editor can read but not delete enquiries
|
||||
assert (await client.get("/api/admin/enquiries", headers=editor_headers)).status_code == 200
|
||||
assert (await client.delete("/api/admin/enquiries/1", headers=editor_headers)).status_code == 403
|
||||
assert (await client.delete("/api/admin/enquiries/1", headers=admin_headers)).status_code == 204
|
||||
# only super admins manage admin accounts
|
||||
assert (await client.get("/api/admin/users", headers=admin_headers)).status_code == 403
|
||||
|
||||
|
||||
async def test_super_admin_manages_users(client, super_headers):
|
||||
r = await client.post(
|
||||
"/api/admin/users",
|
||||
headers=super_headers,
|
||||
json={"email": "New@Example.com", "full_name": "New Person", "password": "weak", "role": "editor"},
|
||||
)
|
||||
assert r.status_code == 422
|
||||
r = await client.post(
|
||||
"/api/admin/users",
|
||||
headers=super_headers,
|
||||
json={"email": "New@Example.com", "full_name": "New Person", "password": "Str0ngEnough", "role": "editor"},
|
||||
)
|
||||
assert r.status_code == 201
|
||||
assert r.json()["email"] == "new@example.com"
|
||||
dup = await client.post(
|
||||
"/api/admin/users",
|
||||
headers=super_headers,
|
||||
json={"email": "new@example.com", "full_name": "Dup", "password": "Str0ngEnough"},
|
||||
)
|
||||
assert dup.status_code == 409
|
||||
|
||||
uid = r.json()["id"]
|
||||
r = await client.patch(f"/api/admin/users/{uid}", headers=super_headers, json={"is_active": False})
|
||||
assert r.status_code == 200 and r.json()["is_active"] is False
|
||||
me = (await client.get("/api/auth/me", headers=super_headers)).json()
|
||||
r = await client.patch(f"/api/admin/users/{me['id']}", headers=super_headers, json={"is_active": False})
|
||||
assert r.status_code == 400 # cannot lock yourself out
|
||||
|
||||
|
||||
async def test_change_password(client, admin_headers):
|
||||
r = await client.post(
|
||||
"/api/auth/change-password",
|
||||
headers=admin_headers,
|
||||
json={"current_password": "wrong", "new_password": "An0therPassword"},
|
||||
)
|
||||
assert r.status_code == 400
|
||||
r = await client.post(
|
||||
"/api/auth/change-password",
|
||||
headers=admin_headers,
|
||||
json={"current_password": PASSWORD, "new_password": "An0therPassword"},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
r = await client.post("/api/auth/login", json={"email": "admin@example.com", "password": "An0therPassword"})
|
||||
assert r.status_code == 200
|
||||
Reference in New Issue
Block a user