47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
"""
|
|
认证 API 测试(依据 API 文档 2.1 登录)
|
|
|
|
测试与开发环境严格隔离:通过 HTTP 请求已启动的后端服务,真实调用登录接口。
|
|
运行前请先启动后端:http://127.0.0.1:8000
|
|
"""
|
|
|
|
|
|
def test_login_success_returns_200_with_token_and_user(client):
|
|
"""正确账号密码返回 200 且包含 token 和 user"""
|
|
r = client.post(
|
|
"/api/auth/login",
|
|
json={"username": "admin", "password": "123456"},
|
|
)
|
|
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
|