Files
StarlightRadio/test_e2e.py
T
Xiao-no-love f0fe50e484 feat: StarLight 第三方登录 + 安全加固 + 循环池 + 热配置
功能:
- StarLight Yggdrasil 第三方登录(后端中转,accessToken 不落地)
- 管理员可视化权限管理(用户列表 + 一键授予/移出)
- 设置页热配置(11 项可编辑,立即生效)
- 循环池(每流独立手动歌单,队列空时随机不重复抽取)
- 登录页统一主站浅色风格
- 全站改名「Starlight专属电台」
- 删除关于页

安全:
- 全局 CSRF 防护(双提交 Cookie)
- 登录限流防 XFF 伪造、错误信息脱敏
- 权限中间件 requireAdminForWrites(读放行/写 admin)
- 密码哈希存储(scrypt/sha256)
- SSRF 防护(封面下载域名白名单)
- 空 catch 加日志、SSE 连接上限

修复:
- 多角色账号 selectedProfile 缺失
- 普通用户越权删歌(V4)
- streams 写操作越权(V9)
- 设置页布局溢出

测试:38 项全绿(api16 / e2e17 / write5)
2026-10-06 16:37:03 +08:00

68 lines
2.8 KiB
Python

import urllib.request, urllib.error, http.cookiejar, json, sys, re
BASE = "http://127.0.0.1:3000"
import os
TEST_PASSWORD = os.environ.get("TEST_PASSWORD", "123test")
cj = http.cookiejar.CookieJar()
op = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
def call(path, method="GET", data=None, raw=False):
body = json.dumps(data).encode() if data is not None else None
req = urllib.request.Request(BASE + path, data=body, method=method)
if body: req.add_header("Content-Type", "application/json")
if method.upper() not in ("GET","HEAD","OPTIONS"):
for ck in cj:
if ck.name == "mr_csrf":
req.add_header("X-CSRF-Token", ck.value); break
try:
r = op.open(req, timeout=10)
return r.status, r.read()
except urllib.error.HTTPError as e:
return e.code, e.read()
results = []
def check(name, cond, extra=""):
results.append((name, cond)); print(("PASS" if cond else "FAIL"), name, extra)
# 静态资源可达(不需要登录)
for path in ["/vendor/bootstrap.min.css", "/vendor/bootstrap-icons.css",
"/vendor/Sortable.min.js", "/vendor/bootstrap-icons.woff2",
"/css/style.css", "/js/app.js", "/login.html", "/favicon.ico"]:
st, _ = call(path)
check(f"静态 {path}", st == 200, f"got {st}")
# 登录
st, body = call("/api/auth/login", "POST", {"password": TEST_PASSWORD})
check("登录", st == 200 and json.loads(body).get("ok"), f"got {st}")
# 首页返回面板(含 vendor 引用)
st, body = call("/")
html = body.decode("utf-8", "ignore")
check("首页返回面板", st == 200 and "pageSearch" in html and "loginForm" not in html, f"got {st}")
check("面板引用本地vendor", "vendor/bootstrap.min.css" in html and "cdn.jsdelivr" not in html)
# 资源列表
st, body = call("/api/resources")
d = json.loads(body)
check("资源列表非空", st == 200 and len(d["list"]) >= 1, f"{len(d.get('list',[]))}")
# 检查封面字段(可能为空,本地化后是 /covers/ 或空)
covers = [x["albumImage"] for x in d["list"]]
print(" 封面字段样例:", covers[:2])
# 流列表
st, body = call("/api/streams")
d = json.loads(body)
check("流列表", st == 200 and len(d["list"]) >= 1, str([s["id"] for s in d["list"]]))
# 播放列表
st, body = call("/api/streams/main/playlist")
d = json.loads(body)
check("主频道播放列表", st == 200 and "list" in d, f"{len(d.get('list',[]))}项")
# 检查 index.html 是否有 import/stream 元素
check("HTML含频道按钮", 'id="btnStreamMgr"' in html and 'id="currentChannelName"' in html)
check("HTML含导入卡片", 'id="importCard"' in html or 'id="importFile"' in html)
check("HTML含登出按钮", 'id="btnLogout"' in html)
print("\n=== 汇总:", sum(1 for _,c in results if c), "/", len(results), "通过 ===")
sys.exit(0 if all(c for _,c in results) else 1)