api / tests /test_admin_auth.py
hanbinsite's picture
feat: complete core feature improvements
bf34e9d
Raw History Blame Contribute Delete
2.24 kB
import pytest
@pytest.mark.asyncio
async def test_health(client):
resp = await client.get("/health")
assert resp.status_code == 200
data = resp.json()
assert data["status"] in ("ok", "degraded")
assert "db" in data
@pytest.mark.asyncio
async def test_admin_login_success(client, db_session):
from app.models.admin import Admin
from app.utils.auth import hash_password
admin = Admin(
username="login_test_admin",
password_hash=hash_password("Admin@123"),
is_active=True,
)
db_session.add(admin)
await db_session.commit()
resp = await client.post("/admin/auth/login", json={
"username": "login_test_admin",
"password": "Admin@123",
})
assert resp.status_code == 200
body = resp.json()
assert body["code"] == 0
assert "accessToken" in body["data"]
@pytest.mark.asyncio
async def test_admin_login_wrong_password(client, db_session):
from app.models.admin import Admin
from app.utils.auth import hash_password
admin = Admin(
username="wrong_pwd_admin",
password_hash=hash_password("correct_password"),
is_active=True,
)
db_session.add(admin)
await db_session.commit()
resp = await client.post("/admin/auth/login", json={
"username": "wrong_pwd_admin",
"password": "wrong_password",
})
assert resp.status_code == 200
assert resp.json()["code"] == 40001
@pytest.mark.asyncio
async def test_admin_get_user_info(client, superadmin_token):
resp = await client.get(
"/admin/auth/user-info",
headers={"Authorization": f"Bearer {superadmin_token}"},
)
assert resp.status_code == 200
body = resp.json()
assert body["code"] == 0
assert body["data"]["username"] == "test_superadmin"
@pytest.mark.asyncio
async def test_admin_unauthorized(client):
resp = await client.get("/admin/auth/user-info")
assert resp.status_code == 403
@pytest.mark.asyncio
async def test_admin_get_menus(client, superadmin_token):
resp = await client.get(
"/admin/auth/menus",
headers={"Authorization": f"Bearer {superadmin_token}"},
)
assert resp.status_code == 200
assert resp.json()["code"] == 0