Download tests/test_admin_auth.py from binhugg2/api: direct link, hf CLI and curl.
- Browser
- Download file 2.24 kB
-
https://huggingface.co/spaces/binhugg2/api/resolve/main/tests/test_admin_auth.py
- Command line
-
hf download hf://spaces/binhugg2/api/tests/test_admin_auth.py
-
curl -L -o test_admin_auth.py https://huggingface.co/spaces/binhugg2/api/resolve/main/tests/test_admin_auth.py
2.24 kB
| import pytest | |
| 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 | |
| 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"] | |
| 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 | |
| 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" | |
| async def test_admin_unauthorized(client): | |
| resp = await client.get("/admin/auth/user-info") | |
| assert resp.status_code == 403 | |
| 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 | |