123 lines
5.8 KiB
Python
Executable File
123 lines
5.8 KiB
Python
Executable File
"""登录 / 会话 / 个人设置。"""
|
|
from __future__ import annotations
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.main import app
|
|
|
|
|
|
def test_未登录时业务接口一律401(client):
|
|
for path in ("/api/projects", "/api/custom/projects", "/api/staff", "/api/focus",
|
|
"/api/rbac/users", "/api/settings", "/api/export/custom"):
|
|
r = client.get(path)
|
|
assert r.status_code == 401, (path, r.status_code, r.text)
|
|
assert client.post("/api/projects", json={"name": "越权新建"}).status_code == 401
|
|
|
|
|
|
def test_登录只认cookie不认body(client):
|
|
r = client.post("/api/auth/login", json={"username": "admin", "password": "admin123"})
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["username"] == "admin" and "password_hash" not in body
|
|
assert body["menus"]["rbac"] == "rw" # 内置管理员:全菜单可编辑
|
|
assert body["role_names"] == ["管理员"]
|
|
assert body["last_login_at"]
|
|
assert "unis_session" in r.cookies # 会话是 HttpOnly Cookie
|
|
# 会话只在这台客户端上有效:另开一个不带 cookie 的客户端照样 401
|
|
assert TestClient(app).get("/api/auth/me").status_code == 401
|
|
|
|
|
|
def test_登录失败只给笼统提示(h):
|
|
r = h.anon.post("/api/auth/login", json={"username": "admin", "password": "错的"})
|
|
assert r.status_code == 401
|
|
assert r.json()["detail"] == "用户名或密码不正确" # 不暴露账号是否存在
|
|
r2 = h.anon.post("/api/auth/login", json={"username": "查无此人", "password": "x123456"})
|
|
assert r2.status_code == 401 and r2.json()["detail"] == "用户名或密码不正确"
|
|
|
|
|
|
def test_连续输错五次后锁定该账号(h):
|
|
for i in range(5):
|
|
r = h.anon.post("/api/auth/login", json={"username": "admin", "password": f"错{i}"})
|
|
assert r.status_code == 401, i
|
|
r = h.anon.post("/api/auth/login", json={"username": "admin", "password": "admin123"})
|
|
assert r.status_code == 429 # 锁定期内连正确密码也不给过
|
|
assert "秒后再试" in r.json()["detail"]
|
|
# 限流按「IP + 账号」计,别人的账号不受影响
|
|
h.user("other_ok", menus={"board": "ro"})
|
|
assert h.login("other_ok").get("/api/auth/me").status_code == 200
|
|
|
|
|
|
def test_成功登录会清零失败计数(h):
|
|
for _ in range(4):
|
|
assert h.anon.post("/api/auth/login", json={"username": "admin", "password": "错"}).status_code == 401
|
|
assert h.anon.post("/api/auth/login", json={"username": "admin", "password": "admin123"}).status_code == 200
|
|
for _ in range(4):
|
|
assert h.anon.post("/api/auth/login", json={"username": "admin", "password": "错"}).status_code == 401
|
|
assert h.anon.post("/api/auth/login", json={"username": "admin", "password": "admin123"}).status_code == 200
|
|
|
|
|
|
def test_停用账号既拦登录也断已有会话(h):
|
|
u = h.user("sleepy", menus={"board": "ro"})
|
|
c = h.login("sleepy")
|
|
assert c.get("/api/auth/me").status_code == 200
|
|
assert h.admin.patch(f"/api/rbac/users/{u['id']}", json={"active": False}).status_code == 200
|
|
assert c.get("/api/auth/me").status_code == 401 # 现有 token 立刻失效
|
|
r = h.anon.post("/api/auth/login", json={"username": "sleepy", "password": "pw123456"})
|
|
assert r.status_code == 403 and "停用" in r.json()["detail"]
|
|
# 重新启用后能正常登录
|
|
h.admin.patch(f"/api/rbac/users/{u['id']}", json={"active": True})
|
|
assert h.login("sleepy").get("/api/auth/me").status_code == 200
|
|
|
|
|
|
def test_改密后其他设备下线(h):
|
|
h.user("multi_dev", menus={"board": "ro"})
|
|
c1 = h.login("multi_dev")
|
|
c2 = h.login("multi_dev")
|
|
assert c1.get("/api/auth/me").status_code == c2.get("/api/auth/me").status_code == 200
|
|
r = c1.post("/api/auth/password", json={"old_password": "pw123456", "new_password": "newpass8"})
|
|
assert r.status_code == 200, r.text
|
|
assert c1.get("/api/auth/me").status_code == 200 # 当前这次登录保留
|
|
assert c2.get("/api/auth/me").status_code == 401 # 其余设备全部下线
|
|
assert h.anon.post("/api/auth/login", json={"username": "multi_dev",
|
|
"password": "pw123456"}).status_code == 401
|
|
assert h.login("multi_dev", "newpass8").get("/api/auth/me").status_code == 200
|
|
|
|
|
|
def test_改密的三条前置校验(h):
|
|
h.user("pw_rule", menus={"board": "ro"})
|
|
c = h.login("pw_rule")
|
|
cases = [
|
|
({"old_password": "不对的", "new_password": "whatever1"}, "原密码不正确"),
|
|
({"old_password": "pw123456", "new_password": "123"}, "至少 6 位"),
|
|
({"old_password": "pw123456", "new_password": "pw123456"}, "不能与原密码相同"),
|
|
]
|
|
for body, expect in cases:
|
|
r = c.post("/api/auth/password", json=body)
|
|
assert r.status_code == 400, body
|
|
assert expect in r.json()["detail"], body
|
|
|
|
|
|
def test_退出登录后token失效(h):
|
|
h.user("bye", menus={"board": "ro"})
|
|
c = h.login("bye")
|
|
assert c.post("/api/auth/logout").json() == {"ok": True}
|
|
assert c.get("/api/auth/me").status_code == 401
|
|
|
|
|
|
def test_改昵称只能改自己(h):
|
|
h.user("nick", menus={"board": "ro"})
|
|
c = h.login("nick")
|
|
assert c.put("/api/auth/me", json={"name": "新昵称"}).json()["name"] == "新昵称"
|
|
assert c.put("/api/auth/me", json={"name": " "}).status_code == 400
|
|
# 昵称之外的字段改不动
|
|
r = c.put("/api/auth/me", json={"name": "再改一次", "username": "hacked", "active": False})
|
|
assert r.json()["username"] == "nick" and r.json()["active"] is True
|
|
|
|
|
|
def test_强制下线会踢掉全部会话(h):
|
|
u = h.user("kickme", menus={"board": "ro"})
|
|
c = h.login("kickme")
|
|
assert c.get("/api/auth/me").status_code == 200
|
|
assert h.admin.post(f"/api/rbac/users/{u['id']}/revoke").json()["revoked"] is True
|
|
assert c.get("/api/auth/me").status_code == 401
|