27 lines
831 B
Python
27 lines
831 B
Python
"""登录接口测试。"""
|
||
import pytest
|
||
from fastapi.testclient import TestClient
|
||
|
||
from app.main import app
|
||
|
||
client = TestClient(app)
|
||
|
||
|
||
def test_login_fail_wrong_password():
|
||
r = client.post("/api/auth/login", json={"username": "admin", "password": "wrong"})
|
||
assert r.status_code == 401
|
||
|
||
|
||
def test_login_fail_wrong_user():
|
||
r = client.post("/api/auth/login", json={"username": "nobody", "password": "admin"})
|
||
assert r.status_code == 401
|
||
|
||
|
||
def test_login_returns_json():
|
||
"""无 DB 时可能 401;有 DB 且 admin 存在时 200。仅断言响应为 JSON 且含 code。"""
|
||
r = client.post("/api/auth/login", json={"username": "admin", "password": "admin"})
|
||
assert r.headers.get("content-type", "").startswith("application/json")
|
||
data = r.json()
|
||
assert "code" in data
|
||
assert "trace_id" in data
|