完成错误测试用例

This commit is contained in:
Your Name
2026-01-31 11:02:59 +08:00
parent 588e3bb478
commit 11cef531bd
15 changed files with 909 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
"""
认证 API 测试(依据 API 文档 2.1 登录)
TDD:先写失败用例,再实现路由
"""
def test_login_success_returns_200_with_token_and_user(client):
"""正确账号密码返回 200 且包含 token 和 user"""
r = client.post(
"/api/auth/login",
json={"username": "market", "password": "valid-password"},
)
assert r.status_code == 200
body = r.json()
assert "token" in body
assert "user" in body
assert "id" in body["user"]
assert "username" in body["user"]
assert "role" in body["user"]
assert "displayName" in body["user"]
def test_login_wrong_credentials_returns_401_with_code_and_message(client):
"""错误账号或密码返回 401 且包含 code 和 message"""
r = client.post(
"/api/auth/login",
json={"username": "nobody", "password": "wrong"},
)
assert r.status_code == 401
body = r.json()
assert body.get("code") == 401
assert "message" in body
def test_login_missing_username_returns_400(client):
"""缺少 username 时返回 400"""
r = client.post("/api/auth/login", json={"password": "any"})
assert r.status_code == 400
def test_login_missing_password_returns_400(client):
"""缺少 password 时返回 400"""
r = client.post("/api/auth/login", json={"username": "any"})
assert r.status_code == 400