Files
verify/scripts/mock-vfy-device-limit.py

68 lines
3.4 KiB
Python
Executable File
Raw Permalink 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.
#!/usr/bin/env python3
"""mock 验证服务端(仅开发/验收用)
/login 一律回 HTTP 403 + error_code=DEVICE_LIMIT_EXCEEDED(带 3 台设备明细,上限 2),
/unbind-device-with-auth、/card-devices、/info、/check-update、/announcements 回成功 JSON。
用途:不需要真数据库就能把壳的「设备管理页 / 每行解绑 / 在线状态」全程跑一遍。
用法:python3 scripts/mock-vfy-device-limit.py (监听 127.0.0.1:9998)
配套验收步骤见 docs/shell-device-ui.md
"""
import json
import sys
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
DEVICES = [
{"id": 11, "device_id": "AAAA-1111-2222-3333", "device_name": "工作室-PC01", "device_type": "windows", "online_count": 1, "created_at": "2026-11-01T10:00:00+08:00"},
{"id": 12, "device_id": "BBBB-4444-5555-6666", "device_name": "笔记本-Liu", "device_type": "windows", "online_count": 0, "created_at": "2026-11-10T21:12:00+08:00"},
{"id": 13, "device_id": "CCCC-7777-8888-9999", "device_name": "ANDROID-9f3c", "device_type": "android", "online_count": 1, "created_at": "2026-11-20T08:30:00+08:00"},
]
class H(BaseHTTPRequestHandler):
def _log(self, body=b""):
sys.stderr.write("[mock] %s %s %s\n" % (self.command, self.path, body[:200].decode("utf-8", "replace")))
sys.stderr.flush()
def do_GET(self):
self._log()
if "info" in self.path:
return self._json(200, {"code": 200, "data": {
"allow_register": 1, "enable_register_verify": 1, "enable_password_reset": 1,
"password_reset_method": "email", "billing_type": "card", "max_devices": 2,
"name": "dogtest"}})
if "devices" in self.path or "instances" in self.path:
return self._json(200, {"code": 200, "data": {"devices": DEVICES, "max_devices": 2, "device_count": 3}})
return self._json(200, {"code": 200, "data": {}})
def do_POST(self):
n = int(self.headers.get("Content-Length") or 0)
body = self.rfile.read(n) if n else b""
self._log(body)
if self.path.rstrip("/").endswith("/login"):
# 与真实后端一致:error_code 放在 data 里;顶层也带一份,壳只按 "DEVICE_LIMIT" 字符串判断
return self._json(403, {"code": 403, "error_code": "DEVICE_LIMIT_EXCEEDED",
"message": "设备绑定数量已达上限,请解绑后再试",
"data": {"error_code": "DEVICE_LIMIT_EXCEEDED",
"max_devices": 2, "device_count": 3, "devices": DEVICES}})
if "unbind" in self.path:
return self._json(200, {"code": 200, "message": "解绑成功"})
if "card-devices" in self.path:
return self._json(200, {"code": 200, "data": {"devices": DEVICES, "max_devices": 2, "device_count": 3}})
return self._json(200, {"code": 200, "data": {}})
def _json(self, code, obj):
raw = json.dumps(obj, ensure_ascii=False, separators=(",", ":")).encode()
self.send_response(code)
self.send_header("Content-Type", "application/json; charset=utf-8")
self.send_header("Content-Length", str(len(raw)))
self.end_headers()
self.wfile.write(raw)
def log_message(self, *a):
pass
if __name__ == "__main__":
ThreadingHTTPServer(("127.0.0.1", 9998), H).serve_forever()