| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- # 黑盒自测脚本(接口级) — 对应 tests/BLACKBOX-TEST-PLAN.md 4.x 用例
- # 目标: http://cfc.iwintrue.com
- $ErrorActionPreference = "Continue"
- $BASE = "http://cfc.iwintrue.com"
- $results = @()
- function Invoke-Case([string]$id, [string]$role, [string]$desc, [string]$path, $body, [bool]$auth = $true, [int]$timeoutSec = 20) {
- $headers = @{ "Content-Type" = "application/json" }
- if ($auth -and $script:token) { $headers["Authorization"] = "Bearer $script:token" }
- $jsonBody = if ($null -eq $body) { "{}" } else { $body | ConvertTo-Json -Depth 6 -Compress }
- $t0 = Get-Date
- try {
- $resp = Invoke-RestMethod -Uri ($BASE + $path) -Method Post -Headers $headers -Body $jsonBody -TimeoutSec $timeoutSec
- $ms = [int]((Get-Date) - $t0).TotalMilliseconds
- $code = $resp.code
- $note = $resp.message
- $dataInfo = ""
- if ($null -ne $resp.data) {
- $dataInfo = (($resp.data | ConvertTo-Json -Depth 2 -Compress)).Substring(0, [Math]::Min(120, (($resp.data | ConvertTo-Json -Depth 2 -Compress)).Length))
- }
- $script:results += [pscustomobject]@{ id = $id; role = $role; api = $path; desc = $desc; code = $code; ms = $ms; note = $note; data = $dataInfo }
- "PASS/INFO #$id [$role] $desc -> code=$code ($ms ms)${note} | $dataInfo "
- } catch {
- $ms = [int]((Get-Date) - $t0).TotalMilliseconds
- $err = $_.Exception.Message
- $code = "EX"
- if ($_.Exception.Response) { try { $code = [int]$_.Exception.Response.StatusCode } catch {} }
- $script:results += [pscustomobject]@{ id = $id; role = $role; api = $path; desc = $desc; code = $code; ms = $ms; note = $err; data = "" }
- "FAIL #$id [$role] $desc -> $code ($ms ms) $err"
- }
- }
- function Set-Token([string]$id) {
- try {
- $resp = Invoke-RestMethod -Uri ($BASE + "/api/admin-auth/login-by-password") -Method Post -Headers @{ "Content-Type" = "application/json" } -Body (@{ phone = "13800138000"; password = "admin123" } | ConvertTo-Json -Compress) -TimeoutSec 20
- if ($resp.code -eq 200 -and $resp.data) {
- $script:token = $resp.data.token
- $script:results += [pscustomobject]@{ id = $id; role = "运营"; api = "/api/admin-auth/login-by-password"; desc = "管理端密码登录(种子账号)"; code = $resp.code; ms = 0; note = $resp.message; data = ("token=" + $script:token.Substring(0, 12) + "...") }
- "TOKEN_OK phone=13800138000"
- } else {
- $script:results += [pscustomobject]@{ id = $id; role = "运营"; api = "/api/admin-auth/login-by-password"; desc = "管理端密码登录(种子账号)"; code = $resp.code; ms = 0; note = $resp.message; data = "" }
- "TOKEN_FAIL: $($resp.message)"
- }
- } catch {
- "TOKEN_EX: $($_.Exception.Message)"
- }
- }
- # ===== 1. 登录 =====
- Set-Token "E2E-00"
- # ===== 2. 主链路 / 近期修复回归 =====
- Invoke-Case "P0-3" "运营" "方案生成规则列表(种子≥2条, BUG-C)" "/api/admin/plan-rules/list" $null
- Invoke-Case "P0-4" "运营" "报告解析中台类型列表(含gut_flora, BUG-D)" "/api/admin/report-parser/types" @{ page = 1; size = 100 }
- Invoke-Case "A-03" "运营" "方案管理列表 admin全量(不筛家庭, 无白屏根因)" "/api/guide/plans/family-list" @{ status = "" }
- Invoke-Case "TCH-11" "规划师" "成长计划列表(修复后不400, BUG-E)" "/api/growth/plan/list" @{ }
- Invoke-Case "A-02" "运营" "全局任务统计" "/api/admin/tasks/stats" $null
- Invoke-Case "A-05" "运营" "注册重复报告类型应400(幂等)" "/api/admin/report-parser/types/save" @{ typeId = "gut_flora"; displayName = "菌群检测" }
- Invoke-Case "A-16" "运营" "参数校验:空body删除知识库应400非500(BUG-K)" "/api/admin/knowledge-base/delete" @{ }
- # ===== 3. 顺带修复BUG回归 =====
- Invoke-Case "A-11" "运营" "创建勋章(自动badgeId, BUG-F)" "/api/badges/create" @{
- name = "BLACKBOX自测勋章"; description = "黑盒自测"; icon = "🏅"; category = "打卡"; level = "铜牌";
- rarity = "普通"; triggerType = "task_count"; threshold = 5; sortOrder = 1; expireDays = -1; isActive = 1
- }
- Invoke-Case "A-12" "运营" "新增供应商(字段契约映射, BUG-G)" "/api/admin/supplier/save" @{
- nickname = "BLACKBOX供应商"; phone = "13800009999"; realName = "黑盒测试"; vendorType = "product"; vendorStatus = "active"
- }
- Invoke-Case "BUG-H" "运营" "已发布活动提交审核应400提示非500" "/api/admin/activity/submit-review" @{ id = 38 }
- Invoke-Case "BUG-I" "运营" "查询知识库(契约对齐, id=1)" "/api/admin/knowledge-base/get" @{ id = 1 }
- # 知识库删除:先建再删(自建数据)
- $kbSave = Invoke-Case "A-14a" "运营" "新建知识条目(供删除用)" "/api/admin/knowledge-base/save" @{ title = "BLACKBOX自测知识"; content = "自测内容"; status = 1 }
- $kbId = $null
- try {
- $r2 = Invoke-RestMethod -Uri ($BASE + "/api/admin/knowledge-base/save") -Method Post -Headers @{ "Content-Type" = "application/json"; "Authorization" = "Bearer $script:token" } -Body (@{ title = "BLACKBOX自测知识" + [DateTime]::Now.Ticks; content = "自测内容"; status = 1 } | ConvertTo-Json) -TimeoutSec 15
- if ($r2.code -eq 200) { $kbId = $r2.data.id; "KB_CREATED id=$kbId" }
- } catch { "KB_CREATE_EX: $($_.Exception.Message)" }
- if ($kbId) { Invoke-Case "A-14b" "运营" "删除知识条目(自建, BUG-I)" "/api/admin/knowledge-base/delete" @{ id = $kbId } }
- # 文章创建(计时, BUG-J)
- $artTitle = "BLACKBOX自测文章" + ([DateTime]::Now.Ticks % 100000)
- Invoke-Case "A-08" "运营" "创建文章(1s内返回预期, BUG-J)" "/api/admin/articles/create" @{
- title = $artTitle; content = "这是一篇黑盒自测短文,验证创建接口响应耗时不超过10秒。"; summary = "自测";
- author = "admin"; status = "draft"; articleType = "normal"
- }
- # ===== 4. 汇总 =====
- "`n========== 自测汇总 =========="
- $script:results | Format-Table -AutoSize id, role, desc, code, ms, note | Out-String -Width 200
- $fail = $script:results | Where-Object { $_.code -ne 200 }
- if ($fail) { "FAILED ITEMS: $($fail.id -join ', ')" } else { "ALL 200 (无失败项)" }
|