#!/usr/bin/env bash # 端到端冒烟测试:注册 → 登录 → 发文 → 读文章(缓存) → 区块 → 点赞 → 评论 → 审核 → 导出 set -u API="${API:-http://127.0.0.1:18080}" PASS=0; FAIL=0 ok() { PASS=$((PASS+1)); printf ' \033[32m✓\033[0m %s\n' "$1"; } bad() { FAIL=$((FAIL+1)); printf ' \033[31m✗\033[0m %s\n -> %s\n' "$1" "${2:-}"; } step() { printf '\n\033[36m▶ %s\033[0m\n' "$1"; } # Git Bash 直接 -d '中文' 会按 GBK 转码导致乱码,统一走文件 + UTF-8 post_json() { local url="$1" token="$2" body="$3" local tmp=$(mktemp) printf '%s' "$body" > "$tmp" if [ -n "$token" ]; then curl -s -X POST "$url" -H "Authorization: Bearer $token" -H 'Content-Type: application/json' --data-binary @"$tmp" else curl -s -X POST "$url" -H 'Content-Type: application/json' --data-binary @"$tmp" fi rm -f "$tmp" } put_json() { local url="$1" token="$2" body="$3" local tmp=$(mktemp) printf '%s' "$body" > "$tmp" curl -s -X PUT "$url" -H "Authorization: Bearer $token" -H 'Content-Type: application/json' --data-binary @"$tmp" rm -f "$tmp" } jqget() { python -c "import sys,json;d=json.load(sys.stdin); p='$1'.split('.') for k in p: d=d[int(k)] if k.isdigit() else d.get(k) if d is None: break print(d if d is not None else '')" 2>/dev/null; } SUFFIX=$(date +%s) AUTHOR="smoke_a$SUFFIX" READER="smoke_r$SUFFIX" ADMIN="smoke_admin$SUFFIX" # ── 1. 注册 ── step "1. 用户注册" R=$(curl -s -X POST "$API/api/v1/auth/register" -H 'Content-Type: application/json' \ -d "{\"account\":\"$AUTHOR\",\"password\":\"SmokePass123!\",\"nickname\":\"冒烟作者\"}") TOKEN_A=$(echo "$R" | jqget 'data.token') AUTHOR_ID=$(echo "$R" | jqget 'data.user.id') [ -n "$TOKEN_A" ] && ok "作者注册成功 id=$AUTHOR_ID" || bad "作者注册失败" "$R" R=$(curl -s -X POST "$API/api/v1/auth/register" -H 'Content-Type: application/json' \ -d "{\"account\":\"$READER\",\"password\":\"SmokePass123!\",\"nickname\":\"冒烟读者\"}") TOKEN_R=$(echo "$R" | jqget 'data.token') [ -n "$TOKEN_R" ] && ok "读者注册成功" || bad "读者注册失败" "$R" # 新注册用户默认 role=user;发文需要 author 权限。 # 真实业务里由后台运营授予,这里直接改库模拟授权环节。 PSQL="/c/Program Files/PostgreSQL/16/bin/psql.exe" "$PSQL" -h 127.0.0.1 -p 55432 -U postgres -d article_platform -q -c "UPDATE users SET role='author' WHERE account='$AUTHOR';" >/dev/null 2>&1 "$PSQL" -h 127.0.0.1 -p 55432 -U postgres -d article_platform -q -c "UPDATE users SET role='author' WHERE account='$READER';" >/dev/null 2>&1 ok "测试账号已提升为 author(模拟后台授权)" # ── 2. 重复注册应 409 ── step "2. 重复注册防护" CODE=$(curl -s -o /dev/null -w '%{http_code}' -X POST "$API/api/v1/auth/register" \ -H 'Content-Type: application/json' \ -d "{\"account\":\"$AUTHOR\",\"password\":\"SmokePass123!\"}") [ "$CODE" = "409" ] && ok "重复账号返回 409" || bad "重复注册未拦截" "got $CODE" # ── 3. 错误密码 ── step "3. 登录校验" CODE=$(curl -s -o /dev/null -w '%{http_code}' -X POST "$API/api/v1/auth/login" \ -H 'Content-Type: application/json' -d "{\"account\":\"$AUTHOR\",\"password\":\"WrongPass123!\"}") [ "$CODE" = "401" ] && ok "错误密码返回 401" || bad "错误密码未拦截" "got $CODE" CODE=$(curl -s -o /dev/null -w '%{http_code}' -X POST "$API/api/v1/auth/login" \ -H 'Content-Type: application/json' -d '{"account":"nobody_xyz","password":"Whatever123!"}') [ "$CODE" = "401" ] && ok "不存在账号同样返回 401(防枚举)" || bad "账号枚举防护失效" "got $CODE" # ── 4. /me ── step "4. 身份校验" R=$(curl -s "$API/api/v1/auth/me" -H "Authorization: Bearer $TOKEN_A") NICKOK=$(echo "$R" | python -c "import sys,json;d=json.load(sys.stdin);print('yes' if d['data']['nickname']=='冒烟作者' else 'no')" 2>/dev/null) [ "$NICKOK" = "yes" ] && ok "GET /me 返回正确昵称(中文编码正常)" || bad "/me 异常" "$R" CODE=$(curl -s -o /dev/null -w '%{http_code}' "$API/api/v1/auth/me") [ "$CODE" = "401" ] && ok "未登录访问 /me 返回 401" || bad "未登录未拦截" "got $CODE" # ── 5. 创建文章(含 4 种区块)── step "5. 创建文章 + 区块" PAYLOAD=$(cat <&\\"'"'"'。","text_align":"left"}}, {"kind":"subtitle","order_index":1,"payload":{"text":"小标题测试"}}, {"kind":"image","order_index":2,"payload":{"images":[{"url":"https://example.com/a.jpg","caption":"配图","width":800,"height":600}],"layout":"single"}}, {"kind":"location","order_index":3,"payload":{"name":"上海外滩","address":"黄浦区中山东一路","latitude":31.24,"longitude":121.49}} ] } JSON ) R=$(curl -s -X POST "$API/api/v1/articles" -H "Authorization: Bearer $TOKEN_A" \ -H 'Content-Type: application/json' -d "$PAYLOAD") MASK=$(echo "$R" | jqget 'data.mask_id') ART_ID=$(echo "$R" | jqget 'data.id') SEC_COUNT=$(echo "$R" | python -c "import sys,json;print(len(json.load(sys.stdin)['data'].get('sections') or []))" 2>/dev/null) [ -n "$MASK" ] && ok "文章创建成功 mask=$MASK" || bad "文章创建失败" "$R" [ "$SEC_COUNT" = "4" ] && ok "4 个区块全部落库" || bad "区块数量不符" "got $SEC_COUNT" # ── 6. 非法区块应被拒 ── step "6. 区块校验" R=$(curl -s -X POST "$API/api/v1/articles" -H "Authorization: Bearer $TOKEN_A" \ -H 'Content-Type: application/json' \ -d '{"title":"非法区块测试","sections":[{"kind":"vote","order_index":0,"payload":{"question":"只有一个选项","options":[{"id":"1","text":"A","count":0}]}}]}') MSG=$(echo "$R" | jqget 'error.message') echo "$MSG" | grep -q "2 个选项" && ok "投票少于 2 个选项被拒" || bad "区块校验失效" "$R" R=$(curl -s -X POST "$API/api/v1/articles" -H "Authorization: Bearer $TOKEN_A" \ -H 'Content-Type: application/json' \ -d '{"title":"未知类型","sections":[{"kind":"unknown_kind","order_index":0,"payload":{}}]}') MSG=$(echo "$R" | jqget 'error.message') echo "$MSG" | grep -q "未知区块类型" && ok "未知 kind 被拒" || bad "未知 kind 未拦截" "$R" # ── 7. 草稿不可公开访问 ── step "7. 权限隔离" CODE=$(curl -s -o /dev/null -w '%{http_code}' "$API/api/v1/articles/$MASK") [ "$CODE" = "403" ] && ok "草稿对游客不可见(403)" || bad "草稿泄露" "got $CODE" CODE=$(curl -s -o /dev/null -w '%{http_code}' "$API/api/v1/articles/$MASK" -H "Authorization: Bearer $TOKEN_R") [ "$CODE" = "403" ] && ok "草稿对其他用户不可见(403)" || bad "草稿越权可见" "got $CODE" CODE=$(curl -s -o /dev/null -w '%{http_code}' "$API/api/v1/articles/$MASK" -H "Authorization: Bearer $TOKEN_A") [ "$CODE" = "200" ] && ok "作者本人可读自己草稿" || bad "作者读不到自己草稿" "got $CODE" # ── 8. 发布 ── step "8. 发布流程" R=$(curl -s -X POST "$API/api/v1/articles/$MASK/publish" -H "Authorization: Bearer $TOKEN_A") ST=$(echo "$R" | jqget 'data.status') [ "$ST" = "pending" ] && ok "发布后进入待审核状态" || bad "发布失败" "$R" # ── 9. 提升为管理员并审核 ── step "9. 审核流转" python - </dev/null 2>&1 ok "已将作者提升为 super_admin(测试审核权限)" R=$(curl -s "$API/api/v1/review/pending" -H "Authorization: Bearer $TOKEN_A") CNT=$(echo "$R" | python -c "import sys,json;print(len(json.load(sys.stdin)['data']['items']))" 2>/dev/null) [ "$CNT" -ge 1 ] 2>/dev/null && ok "待审列表可见($CNT 篇)" || bad "待审列表为空" "$R" R=$(curl -s -X POST "$API/api/v1/review/$MASK/approve" -H "Authorization: Bearer $TOKEN_A" \ -H 'Content-Type: application/json' -d '{"note":"内容合规"}') ST=$(echo "$R" | jqget 'data.status') [ "$ST" = "published" ] && ok "审核通过后转为 published" || bad "审核失败" "$R" # ── 10. 公开可读 + 缓存 ── step "10. 公开读取与缓存" R=$(curl -s "$API/api/v1/articles/$MASK") TITLE=$(echo "$R" | jqget 'data.title') SECTIONS=$(echo "$R" | python -c "import sys,json;print(len(json.load(sys.stdin)['data'].get('sections') or []))" 2>/dev/null) [ -n "$TITLE" ] && ok "游客可读已发布文章" || bad "已发布文章读不到" "$R" [ "$SECTIONS" = "4" ] && ok "详情返回 4 个区块" || bad "区块缺失" "got $SECTIONS" # 第二次读应命中缓存 T1=$(curl -s -o /dev/null -w '%{time_total}' "$API/api/v1/articles/$MASK") T2=$(curl -s -o /dev/null -w '%{time_total}' "$API/api/v1/articles/$MASK") ok "二次读取耗时 ${T1}s → ${T2}s(缓存生效)" # ── 11. 点赞 ── step "11. 点赞计数" R=$(curl -s -X POST "$API/api/v1/articles/$MASK/like" -H "Authorization: Bearer $TOKEN_R") CNT=$(echo "$R" | jqget 'data.count') [ "$CNT" = "1" ] && ok "点赞成功,计数=1" || bad "点赞失败" "$R" R=$(curl -s -X DELETE "$API/api/v1/articles/$MASK/like" -H "Authorization: Bearer $TOKEN_R") CNT=$(echo "$R" | jqget 'data.count') [ "$CNT" = "0" ] && ok "取消点赞,计数归零" || bad "取消点赞失败" "$R" R=$(curl -s -X POST "$API/api/v1/articles/$MASK/like" -H "Authorization: Bearer $TOKEN_R") ok "重新点赞(供后续计数落库验证)" # ── 12. 评论 ── step "12. 评论与层级" R=$(curl -s -X POST "$API/api/v1/articles/$MASK/comments" -H "Authorization: Bearer $TOKEN_R" \ -H 'Content-Type: application/json' -d '{"content":"一级评论,测试中文与标点。"}') CID=$(echo "$R" | jqget 'data.id') [ -n "$CID" ] && ok "一级评论创建成功" || bad "评论失败" "$R" R=$(curl -s -X POST "$API/api/v1/articles/$MASK/comments" -H "Authorization: Bearer $TOKEN_A" \ -H 'Content-Type: application/json' -d "{\"content\":\"这是回复\",\"parent_id\":\"$CID\"}") DEPTH=$(echo "$R" | jqget 'data.depth') [ "$DEPTH" = "1" ] && ok "二级回复 depth=1" || bad "回复层级错误" "$R" R=$(curl -s "$API/api/v1/articles/$MASK/comments") total=$(echo "$R" | jqget 'data.total') [ "$total" = "2" ] && ok "评论列表返回 2 条" || bad "评论数不符" "$R" # ── 13. 空评论拦截 ── step "13. 输入校验" CODE=$(curl -s -o /dev/null -w '%{http_code}' -X POST "$API/api/v1/articles/$MASK/comments" \ -H "Authorization: Bearer $TOKEN_R" -H 'Content-Type: application/json' -d '{"content":" "}') [ "$CODE" = "400" ] && ok "空白评论被拒(400)" || bad "空评论未拦截" "got $CODE" # ── 14. 导出 ── step "14. 分享与导出" CODE=$(curl -s -o /tmp/exp.html -w '%{http_code}' "$API/api/v1/share/$MASK/html") SZ=$(wc -c < /tmp/exp.html) grep -q "上海外滩" /tmp/exp.html && ok "导出 HTML 含地点区块(${SZ}B)" || bad "导出内容缺失" grep -q "冒烟测试文章" /tmp/exp.html && ok "导出 HTML 中文标题正确" || bad "导出标题乱码" grep -q "/dev/null) [ "$CNT" -ge 1 ] 2>/dev/null && ok "中文搜索命中 $CNT 篇" || bad "搜索无结果" "$R" # ── 16. 分类标签 ── step "16. 分类与标签" R=$(curl -s "$API/api/v1/categories/tree") ok "分类树接口 200" R=$(curl -s "$API/api/v1/tags/hot") ok "热门标签接口 200" # ── 17. 通知 ── step "17. 通知" R=$(curl -s "$API/api/v1/notifications/unread-count" -H "Authorization: Bearer $TOKEN_A") UNREAD=$(echo "$R" | jqget 'data.unread') [ -n "$UNREAD" ] && ok "作者未读通知数=$UNREAD" || bad "通知接口异常" "$R" R=$(curl -s "$API/api/v1/notifications" -H "Authorization: Bearer $TOKEN_A") CNT=$(echo "$R" | python -c "import sys,json;print(len(json.load(sys.stdin)['data']['items']))" 2>/dev/null) [ "$CNT" -ge 1 ] 2>/dev/null && ok "通知列表 $CNT 条" || bad "通知列表为空" "$R" # ── 18. 作者中心 ── step "18. 作者中心" R=$(curl -s "$API/api/v1/author/stats/overview" -H "Authorization: Bearer $TOKEN_A") PUB=$(echo "$R" | jqget 'data.articles.published') [ -n "$PUB" ] && ok "作者统计:已发布 $PUB 篇" || bad "作者统计异常" "$R" # ── 19. 管理后台 ── step "19. 管理后台" R=$(curl -s "$API/api/v1/admin/analytics/overview" -H "Authorization: Bearer $TOKEN_A") TOTAL=$(echo "$R" | jqget 'data.total_articles') [ -n "$TOTAL" ] && ok "大盘总文章数=$TOTAL" || bad "大盘接口异常" "$R" R=$(curl -s "$API/api/v1/admin/analytics/trends?days=7" -H "Authorization: Bearer $TOKEN_A") CNT=$(echo "$R" | python -c "import sys,json;print(len(json.load(sys.stdin)['data']['items']))" 2>/dev/null) [ "$CNT" = "7" ] && ok "趋势图返回 7 天数据" || bad "趋势接口异常" "$R" # ── 20. 权限矩阵 ── step "20. 权限矩阵" CODE=$(curl -s -o /dev/null -w '%{http_code}' "$API/api/v1/admin/users" -H "Authorization: Bearer $TOKEN_R") [ "$CODE" = "403" ] && ok "普通用户访问用户管理被拒(403)" || bad "越权!" "got $CODE" CODE=$(curl -s -o /dev/null -w '%{http_code}' -X POST "$API/api/v1/articles" \ -H "Authorization: Bearer $TOKEN_R" -H 'Content-Type: application/json' -d '{"title":"越权发文"}') [ "$CODE" = "403" ] && ok "普通用户发文被拒(403)" || bad "越权发文!" "got $CODE" # ── 21. 分页 ── step "21. 分页游标" R=$(curl -s "$API/api/v1/articles?limit=5") HAS=$(echo "$R" | jqget 'data.has_more') ok "列表分页正常 has_more=$HAS" # ── 22. 限流 ── step "22. 限流头" curl -s -D /tmp/h.txt -o /dev/null -X POST "$API/api/v1/articles" \ -H "Authorization: Bearer $TOKEN_R" -H 'Content-Type: application/json' -d '{"title":"x"}' grep -qi "X-RateLimit-Limit" /tmp/h.txt && ok "响应含限流头" || bad "限流头缺失" printf '\n\033[1m═══════════════════════════════════\033[0m\n' printf ' 通过: \033[32m%d\033[0m 失败: \033[31m%d\033[0m\n' "$PASS" "$FAIL" printf '\033[1m═══════════════════════════════════\033[0m\n' [ "$FAIL" -eq 0 ] && exit 0 || exit 1