"""最终验收:测试环境导出功能(GET /api/charge-records/export)→ 检查 xlsx → 清理 用法: python .tmp-schema-align\export_verify.py """ import base64, hashlib, hmac, json, ssl, subprocess, sys, time, urllib.request, urllib.error, pymysql SECRET = "32e8708743c42fe764aff1d40737a63803a46815a9501f4bcb50c41fe3d27108" BASE = "https://test.anzhizhichong.com/api/pms" PHONE = "19900000003" def b64(b): return base64.urlsafe_b64encode(b).rstrip(b"=").decode() def sign(sub): now = int(time.time()) seg = b64(json.dumps({"alg": "HS256", "typ": "JWT"}, separators=(",", ":")).encode()) + "." + \ b64(json.dumps({"sub": sub, "phone": PHONE, "exp": now + 1800, "iat": now}, separators=(",", ":")).encode()) return seg + "." + b64(hmac.new(SECRET.encode(), seg.encode(), hashlib.sha256).digest()) def call(path, token): req = urllib.request.Request(BASE + path, headers={"Authorization": "Bearer " + token}) ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE try: with urllib.request.urlopen(req, timeout=30, context=ctx) as r: return r.status, r.read().decode("utf-8", "replace") except urllib.error.HTTPError as e: return e.code, e.read().decode("utf-8", "replace") c = pymysql.connect(host="10.8.0.252", user="root", password="Hbhyg731024@", database="pms_dev", autocommit=True) cur = c.cursor() cur.execute("DELETE FROM download_tasks WHERE user_id IN (SELECT id FROM users WHERE phone=%s)", (PHONE,)) cur.execute("DELETE FROM users WHERE phone=%s", (PHONE,)) cur.execute("INSERT INTO users (phone,name,password,role,organization_id,status) VALUES (%s,'VERIFY-EXPORT-TMP','x',2,NULL,1)", (PHONE,)) admin_id = cur.lastrowid tok = sign(admin_id) print("临时超管 id=", admin_id) status, body = call("/charge-records/export", tok) print("GET /api/charge-records/export ->", status, body) task_id = json.loads(body)["data"]["task_id"] time.sleep(4) cur.execute("SELECT id,status,progress,file_path,completed_at,IFNULL(error_message,'') FROM download_tasks WHERE id=%s", (task_id,)) row = cur.fetchone() print("download_tasks 行:", row) ok = row and row[1] == 2 and row[2] == 100 and row[3] print("断言:任务 status=2(完成) 且 progress=100 且 file_path 非空 ->", "PASS" if ok else "FAIL") print("\n--- 清理 ---") cur.execute("DELETE FROM download_tasks WHERE user_id=%s", (admin_id,)) print(" download_tasks 删除:", cur.rowcount) cur.execute("DELETE FROM users WHERE id=%s", (admin_id,)) print(" users 删除:", cur.rowcount) cur.execute("SELECT COUNT(*) FROM download_tasks") print(" download_tasks 剩余行:", cur.fetchone()[0]) c.close() print(" 导出的 xlsx 由服务器侧脚本删除(见 remote 输出)") sys.exit(0 if ok else 1)