File size: 2,002 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
import pytest


@pytest.mark.asyncio
async def test_user_register(client):
    resp = await client.post("/user/auth/register", json={
        "username": "newuser01",
        "password": "Pass@1234",
    })
    assert resp.status_code == 200
    body = resp.json()
    assert body["code"] == 0
    assert "accessToken" in body["data"]


@pytest.mark.asyncio
async def test_user_register_duplicate(client):
    await client.post("/user/auth/register", json={
        "username": "dupuser",
        "password": "Pass@1234",
    })
    resp = await client.post("/user/auth/register", json={
        "username": "dupuser",
        "password": "Pass@1234",
    })
    assert resp.json()["code"] == 40001


@pytest.mark.asyncio
async def test_user_login_success(client):
    await client.post("/user/auth/register", json={
        "username": "loginuser",
        "password": "Pass@1234",
    })
    resp = await client.post("/user/auth/login", json={
        "username": "loginuser",
        "password": "Pass@1234",
    })
    assert resp.status_code == 200
    assert resp.json()["code"] == 0
    assert "accessToken" in resp.json()["data"]


@pytest.mark.asyncio
async def test_user_login_wrong_password(client):
    await client.post("/user/auth/register", json={
        "username": "wrongpwduser",
        "password": "Pass@1234",
    })
    resp = await client.post("/user/auth/login", json={
        "username": "wrongpwduser",
        "password": "WrongPass",
    })
    assert resp.json()["code"] == 40001


@pytest.mark.asyncio
async def test_user_profile(client, user_token):
    resp = await client.get(
        "/user/auth/profile",
        headers={"Authorization": f"Bearer {user_token}"},
    )
    assert resp.status_code == 200
    body = resp.json()
    assert body["code"] == 0
    assert body["data"]["username"] == "testuser"


@pytest.mark.asyncio
async def test_user_profile_unauthorized(client):
    resp = await client.get("/user/auth/profile")
    assert resp.status_code == 403