48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
def test_login_success(client):
|
|
"""测试登录成功"""
|
|
response = client.post(
|
|
"/api/auth/login",
|
|
data={"username": "test_admin", "password": "test_password"}
|
|
)
|
|
assert response.status_code == 200
|
|
assert "access_token" in response.json()
|
|
assert response.json()["token_type"] == "bearer"
|
|
|
|
|
|
def test_login_invalid_username(client):
|
|
"""测试登录失败 - 用户名无效"""
|
|
response = client.post(
|
|
"/api/auth/login",
|
|
data={"username": "invalid_user", "password": "test_password"}
|
|
)
|
|
assert response.status_code == 401
|
|
assert "Incorrect username or password" in response.json()["detail"]
|
|
|
|
|
|
def test_login_invalid_password(client):
|
|
"""测试登录失败 - 密码无效"""
|
|
response = client.post(
|
|
"/api/auth/login",
|
|
data={"username": "test_admin", "password": "invalid_password"}
|
|
)
|
|
assert response.status_code == 401
|
|
assert "Incorrect username or password" in response.json()["detail"]
|
|
|
|
|
|
def test_login_empty_username(client):
|
|
"""测试登录失败 - 用户名为空"""
|
|
response = client.post(
|
|
"/api/auth/login",
|
|
data={"username": "", "password": "test_password"}
|
|
)
|
|
assert response.status_code == 422
|
|
|
|
|
|
def test_login_empty_password(client):
|
|
"""测试登录失败 - 密码为空"""
|
|
response = client.post(
|
|
"/api/auth/login",
|
|
data={"username": "test_admin", "password": ""}
|
|
)
|
|
assert response.status_code == 422
|