Files
cuotibiye/tests/smoke_login.py
T
dsh c1cce198fd ocr: 用腾讯云 QuestionSplitOCR 替换 TextIn 擦除
- 新增 backend/tencent_ocr.py(TC3 签名 + 结果解析,零依赖)
- create_paper_from_image 改为 腾讯 -> 本机 OCR+正则 -> 演示题库 三级兜底
- 删掉 TextIn 客户端、擦除页路由 /api/papers/{pid}/clean 与相关测试
- 新增 tests/test_tencent_ocr.py(13 例,全打桩);单测 114 例、冒烟 39 项全过
2026-11-18 20:57:45 +08:00

134 lines
5.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""端到端核对:网页登录(手机号 + 验证码)+ 通知列表(临时库、临时端口)。"""
import json
import os
import subprocess
import sys
import time
import urllib.error
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
DB = ROOT / "data" / "http_login.db"
for s in ("", "-wal", "-shm"):
p = Path(str(DB) + s)
if p.exists():
p.unlink()
ENV = dict(os.environ, QINGLING_DB=str(DB), PYTHONUTF8="1")
PORT = 8033
LOG = ROOT / "tests" / "_login_server.log"
fh = open(LOG, "w", encoding="utf-8")
proc = subprocess.Popen([sys.executable, "-m", "backend.app", "--port", str(PORT)],
cwd=str(ROOT), env=ENV, stdout=fh, stderr=subprocess.STDOUT)
BASE = f"http://127.0.0.1:{PORT}"
def call(method, path, body=None, token=None):
data = json.dumps(body).encode() if body is not None else None
req = urllib.request.Request(BASE + path, data=data, method=method)
req.add_header("Content-Type", "application/json")
if token:
req.add_header("Authorization", "Bearer " + token)
try:
with urllib.request.urlopen(req, timeout=15) as r:
return r.status, json.loads(r.read().decode())
except urllib.error.HTTPError as e:
return e.code, json.loads(e.read().decode())
ok = []
def check(name, cond, extra=""):
ok.append(bool(cond))
print((" ok " if cond else " FAIL ") + name + ("" if cond else " " + str(extra)))
try:
up = False
for _ in range(60):
if proc.poll() is not None:
break
try:
if call("GET", "/api/health")[0] == 200:
up = True
break
except Exception:
time.sleep(0.25)
if not up:
fh.flush()
print("服务没起来,日志:")
print(LOG.read_text(encoding="utf-8", errors="replace")[-2000:])
sys.exit(1)
st, r = call("GET", "/api/health")
check("health 200", st == 200, r)
check("routes=57", r["data"]["routes"] == 56, r["data"].get("routes"))
st, r = call("POST", "/api/auth/sms-code", {"phone": "13800138000"})
check("发码 200", st == 200 and r["data"]["sent"] is True, r)
code = (r["data"] or {}).get("dev_code") or ""
check("发码返回 dev_code(6 位)", len(code) == 6, code)
st, r = call("POST", "/api/auth/sms-code", {"phone": "13800138000"})
check("60 秒内重复发 → 429", st == 429 and r["error"]["code"] == "SMS_TOO_OFTEN", r)
st, r = call("POST", "/api/auth/sms-code", {"phone": "123"})
check("手机号非法 → 400", st == 400 and r["error"]["code"] == "BAD_PHONE", r)
st, r = call("POST", "/api/auth/sms-login", {"phone": "13800138000", "code": "000000"})
check("验证码错 → 400", st == 400 and r["error"]["code"] in ("CODE_WRONG", "CODE_EXPIRED"), r)
st, r = call("POST", "/api/auth/sms-login", {"phone": "13800138000", "code": code})
check("登录 200", st == 200 and bool(r["data"]["token"]), r)
tok = r["data"]["token"]
check("新用户 needs_consent", r["data"]["needs_consent"] is True, r["data"])
check("试用 3 天", (r["data"]["entitlement"] or {}).get("days_left") in (2, 3), r["data"]["entitlement"])
st, r = call("POST", "/api/auth/sms-login", {"phone": "13800138000", "code": code})
check("同一个码第二次 → 400", st == 400, r)
st, r = call("GET", "/api/me", token=tok)
check("带令牌看 /api/me 200", st == 200 and r["data"]["guardian"]["phone"] == "13800138000", r)
st, r = call("GET", "/api/notifications", token=tok)
check("通知列表 200 且为空", st == 200 and r["data"]["items"] == [], r)
check("通知列表带 quota", "quota" in (r["data"] or {}), r)
st, r = call("GET", "/api/notifications")
check("未登录看通知 → 401", st == 401, r)
# 一次性门票(小程序 → 工作台免登录)
st, r = call("POST", "/api/auth/web-ticket", {"base_url": "http://127.0.0.1:8000"}, tok)
check("换门票 200", st == 200 and bool(r["data"]["ticket"]), r)
tic = r["data"]["ticket"]
check("门票拼了 url", "ticket=" in (r["data"].get("url") or ""), r["data"])
st, r = call("POST", "/api/auth/ticket-login", {"ticket": tic})
check("门票换登录 200", st == 200 and bool(r["data"]["token"]), r)
st, r = call("POST", "/api/auth/ticket-login", {"ticket": tic})
check("门票只能用一次 → 401", st == 401, r)
st, r = call("POST", "/api/auth/ticket-login", {"ticket": ""})
check("空门票 → 400", st == 400, r)
st, r = call("POST", "/api/auth/sms-login", {"phone": "13900139000", "code": code})
check("码不能跨手机号用", st == 400, r)
finally:
proc.terminate()
try:
proc.wait(timeout=8)
except subprocess.TimeoutExpired:
proc.kill()
fh.close()
for s in ("", "-wal", "-shm"):
p = Path(str(DB) + s)
if p.exists():
p.unlink()
if LOG.exists():
LOG.unlink()
print()
print("通过 %d 项,失败 %d 项" % (sum(ok), len(ok) - sum(ok)))
sys.exit(0 if all(ok) else 1)