Bläddra i källkod

test: 邮箱登录/密码/强制绑定全流程 e2e 断言

wenhongquan 1 månad sedan
förälder
incheckning
a855733efb
1 ändrade filer med 97 tillägg och 2 borttagningar
  1. 97 2
      server/e2e_test.py

+ 97 - 2
server/e2e_test.py

@@ -38,6 +38,18 @@ def check(name, cond, extra=""):
         sys.exit(1)
 
 
+def bypass_email_rate_limit(email):
+    # 把该邮箱最近一条验证码记录的发送时间拨回,绕过 60s 限流(仅测试环境)
+    import os
+    import pymysql
+    conn = pymysql.connect(host='127.0.0.1', port=int(os.environ.get('PL_DB_PORT', 3307)),
+                           user='root', password='pouchlab', database='pouchlab')
+    cur = conn.cursor()
+    cur.execute("UPDATE email_codes SET sent_at = sent_at - 120 WHERE email = %s ORDER BY id DESC LIMIT 1",
+                (email,))
+    conn.commit(); conn.close()
+
+
 # --- 管理员登录 ---
 st, r = call("POST", "/api/auth/admin/login", body={"username": "admin", "password": "admin123"})
 check("管理员登录", st == 200 and r.get("token"))
@@ -64,6 +76,89 @@ token = r["token"]
 st, r = call("GET", "/api/auth/me", token=token)
 check("获取当前用户", st == 200 and r["phone"] == "" + PHONE + "")
 
+# --- 邮箱流程 ---
+EMAIL = "e2e-%08d@pouchlab.test" % random.randint(0, 99999999)
+
+st, r = call("POST", "/api/auth/email/send", body={"email": " not-an-email "})
+check("非法邮箱拒绝", st == 400)
+
+st, r = call("POST", "/api/auth/email/send", body={"email": EMAIL})
+check("发送邮箱验证码(demo 返回 code)", st == 200 and r.get("demo") and r.get("code"))
+ecode = r["code"]
+
+st, r = call("POST", "/api/auth/email/send", body={"email": EMAIL})
+check("邮箱验证码 60 秒限流", st == 429)
+
+st, r = call("POST", "/api/auth/email/login", body={"email": EMAIL, "code": "000000"})
+check("邮箱错误验证码拒绝", st == 400)
+
+st, r = call("POST", "/api/auth/email/login", body={"email": EMAIL, "code": ecode})
+check("邮箱验证码登录(自动注册)", st == 200 and r.get("token")
+      and r["user"]["email"] == EMAIL and r["user"]["needsBindEmail"] is False)
+etok = r["token"]
+
+st, r = call("GET", "/api/orders/mine", token=etok)
+check("邮箱用户可直接下单查询", st == 200)
+
+st, r = call("POST", "/api/auth/email/password-login", body={"email": EMAIL, "password": "password123"})
+check("未设密码账号密码登录被拒", st == 400)
+
+st, r = call("POST", "/api/auth/set-password", token=etok, body={"newPassword": "short1"})
+check("弱密码拒绝", st == 400)
+
+st, r = call("POST", "/api/auth/set-password", token=etok, body={"newPassword": "password123"})
+check("首次设置密码(免旧密码)", st == 200)
+
+st, r = call("POST", "/api/auth/email/password-login", body={"email": EMAIL, "password": "password123"})
+check("密码登录成功", st == 200 and r.get("token"))
+
+st, r = call("POST", "/api/auth/set-password", token=etok, body={"newPassword": "password456"})
+check("再次设置需旧密码(缺旧密码被拒)", st == 400)
+
+st, r = call("POST", "/api/auth/set-password", token=etok,
+             body={"oldPassword": "password123", "newPassword": "password456"})
+check("旧密码正确可改密", st == 200)
+
+# --- legacy 手机号登录 → 强制绑定邮箱 ---
+PHONE2 = "139%08d" % random.randint(0, 99999999)
+st, r = call("POST", "/api/auth/sms/send", body={"phone": PHONE2})
+check("legacy 短信验证码(demo)", st == 200 and r.get("code"))
+pcode = r["code"]
+
+st, r = call("POST", "/api/auth/sms/login", body={"phone": PHONE2, "code": pcode})
+check("legacy 手机号登录返回 needsBindEmail", st == 200 and r["user"]["needsBindEmail"] is True)
+ptok = r["token"]
+
+st, r = call("GET", "/api/orders/mine", token=ptok)
+check("未绑邮箱业务接口 403 bind-email",
+      st == 403 and isinstance(r.get("detail"), dict) and r["detail"].get("code") == "bind-email")
+
+bypass_email_rate_limit(EMAIL)  # 同一邮箱第三次发送会命中 60s 限流,测试环境拨回 sent_at
+st, r = call("POST", "/api/auth/email/send", body={"email": EMAIL})
+check("绑定用邮箱验证码(换邮箱地址)", st == 200 and r.get("code"))
+bcode = r["code"]
+
+st, r = call("POST", "/api/auth/bind-email", token=ptok, body={"email": EMAIL, "code": bcode})
+check("绑定已被占用邮箱被拒", st == 400)
+
+EMAIL2 = "e2e-%08d@pouchlab.test" % random.randint(0, 99999999)
+st, r = call("POST", "/api/auth/email/send", body={"email": EMAIL2})
+bcode2 = r["code"]
+st, r = call("POST", "/api/auth/bind-email", token=ptok, body={"email": EMAIL2, "code": bcode2})
+check("绑定新邮箱成功", st == 200 and r["user"]["email"] == EMAIL2
+      and r["user"]["needsBindEmail"] is False)
+
+st, r = call("GET", "/api/orders/mine", token=ptok)
+check("绑定后业务接口放行", st == 200)
+
+# --- 老手机号用户补绑邮箱(解除 bind-email 拦截,恢复后续下单流程) ---
+EMAIL3 = "e2e-%08d@pouchlab.test" % random.randint(0, 99999999)
+st, r = call("POST", "/api/auth/email/send", body={"email": EMAIL3})
+check("老手机号用户获取绑定验证码", st == 200 and r.get("code"))
+st, r = call("POST", "/api/auth/bind-email", token=token, body={"email": EMAIL3, "code": r["code"]})
+check("老手机号用户绑定邮箱", st == 200 and r["user"]["email"] == EMAIL3
+      and r["user"]["needsBindEmail"] is False)
+
 # --- 纸盒订单(带刀版几何)---
 box_body = {
     "type": "box",
@@ -342,9 +437,9 @@ else:
     check("AI 生成(已配置环境)", st in (200, 502), str(st))
 st, r = call("POST", "/api/ai/layout", token=token, body={"type": "bag"})
 check("空需求被拒", st == 400)
-# 额度=0 → 403
+# 额度=0 → 403(按手机号精确命中 token 用户,避免新增邮箱注册用户占据 items[0])
 uid_for_ai = None
-st, r = call("GET", "/api/admin/users", token=admin)
+st, r = call("GET", "/api/admin/users?query=%s" % PHONE, token=admin)
 uid_for_ai = r["items"][0]["id"] if r["items"] else None
 if uid_for_ai:
     call("PATCH", "/api/admin/users/%d" % uid_for_ai, token=admin, body={"ai_quota": 0})