File size: 2,242 Bytes
bf34e9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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