- 登录/注册/me 接口返回 has_password 标志 - 新增 POST /api/auth/set-password:登录态直接设密码(无需邮箱验证码) - 前端:has_password=false 的用户登录后弹出不可关闭的设置密码层,设置成功才能进入应用 - 测试 +4(47/47):标志返回、端点鉴权、改密生效、旧密码失效
63 lines
3.2 KiB
Python
63 lines
3.2 KiB
Python
"""set-password 端点与 has_password 标志(强制设置密码流程的后端契约)。
|
|
|
|
无密码老账号的模拟不直接改库(避免测试代码写敏感列),
|
|
端点契约由"修改密码 → 新密码可登录/旧密码失效 + 登录返回标志"覆盖;
|
|
前端强制弹层的显示条件是 has_password === false,属纯 UI 逻辑。
|
|
|
|
Run with: python -m unittest discover -s tests -p "test_force_password.py" -v
|
|
"""
|
|
from test_api import ApiTestCase, TEST_PASSWORD, get_code, register
|
|
|
|
|
|
class ForcePasswordTests(ApiTestCase):
|
|
async def test_registered_user_has_password_flag(self):
|
|
code = await get_code(self.client, "newbie@example.test", "register")
|
|
r = await self.client.post("/api/auth/register", json={
|
|
"display_name": "新丁", "department": "综合部", "email": "newbie@example.test",
|
|
"phone": "13800000098", "email_code": code,
|
|
"password": TEST_PASSWORD, "confirm_password": TEST_PASSWORD,
|
|
})
|
|
self.assertEqual(r.status_code, 200)
|
|
self.assertTrue(r.json()["user"]["has_password"])
|
|
|
|
async def test_set_password_flow_and_flag(self):
|
|
user = await register(self.client, "阿强", email="strong@example.test", phone="13800000097")
|
|
headers = {"Authorization": f"Bearer {user['token']}"}
|
|
|
|
# 两次密码不一致:422
|
|
r = await self.client.post("/api/auth/set-password", headers=headers,
|
|
json={"password": TEST_PASSWORD, "confirm_password": TEST_PASSWORD + "x"})
|
|
self.assertEqual(r.status_code, 422)
|
|
|
|
# 修改密码成功
|
|
new_password = TEST_PASSWORD + "New"
|
|
r = await self.client.post("/api/auth/set-password", headers=headers,
|
|
json={"password": new_password, "confirm_password": new_password})
|
|
self.assertEqual(r.status_code, 200, r.text)
|
|
self.assertTrue(r.json()["has_password"])
|
|
|
|
# 新密码可登录(老密码失效)
|
|
r = await self.client.post("/api/auth/login", json={"email": "strong@example.test", "password": new_password})
|
|
self.assertEqual(r.status_code, 200)
|
|
self.assertTrue(r.json()["user"]["has_password"])
|
|
r = await self.client.post("/api/auth/login", json={"email": "strong@example.test", "password": TEST_PASSWORD})
|
|
self.assertEqual(r.status_code, 401)
|
|
|
|
async def test_set_password_requires_login(self):
|
|
r = await self.client.post("/api/auth/set-password",
|
|
json={"password": TEST_PASSWORD, "confirm_password": TEST_PASSWORD})
|
|
self.assertEqual(r.status_code, 401, "未登录不能调用")
|
|
|
|
async def test_login_flags_password_state(self):
|
|
# 有密码账号(正常注册)→ True;验证码登录返回同样带标志
|
|
user = await register(self.client, "阿明", email="ming@example.test", phone="13800000096")
|
|
self.assertTrue(user["user"]["has_password"])
|
|
code = await get_code(self.client, "ming@example.test", "login")
|
|
r = await self.client.post("/api/auth/login", json={"email": "ming@example.test", "email_code": code})
|
|
self.assertEqual(r.status_code, 200)
|
|
self.assertTrue(r.json()["user"]["has_password"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|