api / tests /test_user_auth.py
hanbinsite's picture
feat: complete core feature improvements
bf34e9d
Raw History Blame Contribute Delete
2 kB
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