$ErrorActionPreference = 'Stop' function Invoke-Api { param( [string]$Method, [string]$Url, [hashtable]$Headers = @{}, $Body = $null, [int]$TimeoutSec = 40 ) try { $params = @{ Method = $Method Uri = $Url Headers = $Headers UseBasicParsing = $true SkipHttpErrorCheck = $true TimeoutSec = $TimeoutSec } if ($null -ne $Body) { if ($Body -is [string]) { $params.ContentType = 'application/json' $params.Body = $Body } else { $params.ContentType = 'application/json' $params.Body = ($Body | ConvertTo-Json -Depth 20) } } $resp = Invoke-WebRequest @params $json = $null if ($resp.Content) { try { $json = $resp.Content | ConvertFrom-Json -Depth 50 } catch {} } [pscustomobject]@{ http = [int]$resp.StatusCode; json = $json; raw = $resp.Content } } catch { [pscustomobject]@{ http = -1; json = $null; raw = $_.Exception.Message } } } function Add-Result { param( [System.Collections.Generic.List[object]]$Results, [string]$Method, [string]$Path, [string]$Status, [string]$Note, [object]$Resp ) $apiCode = $null if ($Resp -and $Resp.json -and $null -ne $Resp.json.code) { $apiCode = $Resp.json.code } $Results.Add([pscustomobject]@{ method = $Method path = $Path status = $Status http = if ($Resp) { $Resp.http } else { $null } apiCode = $apiCode note = $Note }) } function Get-AdminCaptcha { $cap = Invoke-Api -Method GET -Url 'http://localhost:9000/admin/auth/captcha' if ($cap.http -ne 200 -or -not $cap.json) { throw "获取后台验证码失败: $($cap.raw)" } $key = $cap.json.data.captchaKey $code = docker exec redis redis-cli -a redis_EsJ8FX -n 5 --raw GET ('admin:captcha:' + $key) [pscustomobject]@{ key = $key; code = $code.Trim('"').Trim() } } $authData = Get-Content 'C:\Users\toyas\Desktop\本科毕业设计\参考\workspace\HA-OVP\backend\API_TEST_RESULTS_auth_user.json' -Raw | ConvertFrom-Json -Depth 20 $userAId = [long]$authData.ids.userA $userBId = [long]$authData.ids.userB $userBName = [string]$authData.users[1] $results = [System.Collections.Generic.List[object]]::new() $cap = Get-AdminCaptcha $noCaptcha = Invoke-Api -Method POST -Url 'http://localhost:9000/admin/auth/login' -Body @{ username='admin'; password='admin123' } Add-Result $results 'POST' '/admin/auth/login' ($(if($noCaptcha.http -eq 403){'PASSED'}else{'FAILED'})) '后台无验证码登录应被拦截' $noCaptcha $login = Invoke-Api -Method POST -Url 'http://localhost:9000/admin/auth/login' -Body @{ username='admin'; password='admin123'; captcha=$cap.code; captchaKey=$cap.key } $headers = @{ Authorization = 'Bearer ' + $login.json.data.token } Add-Result $results 'POST' '/admin/auth/login' ($(if($login.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台登录' $login $info = Invoke-Api -Method GET -Url 'http://localhost:9000/admin/auth/info' -Headers $headers Add-Result $results 'GET' '/admin/auth/info' ($(if($info.json.code -eq 200){'PASSED'}else{'FAILED'})) '管理员信息' $info $userList = Invoke-Api -Method GET -Url 'http://localhost:9000/admin/platform/user/list?page=1&size=5' -Headers $headers Add-Result $results 'GET' '/admin/platform/user/list' ($(if($userList.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台用户列表' $userList $userDetail = Invoke-Api -Method GET -Url "http://localhost:9000/admin/platform/user/$userAId" -Headers $headers Add-Result $results 'GET' '/admin/platform/user/{userId}' ($(if($userDetail.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台用户详情' $userDetail $disable = Invoke-Api -Method PUT -Url "http://localhost:9000/admin/platform/user/$userBId/status/0" -Headers $headers Add-Result $results 'PUT' '/admin/platform/user/{userId}/status/{status}' ($(if($disable.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台禁用用户' $disable $capUser = Invoke-WebRequest -UseBasicParsing -SkipHttpErrorCheck -Uri 'http://localhost:9000/auth/captcha' $capUserJson = $capUser.Content | ConvertFrom-Json $userKey = $capUserJson.data.captchaKey $userCode = docker exec redis redis-cli -a redis_EsJ8FX --raw GET ('captcha:' + $userKey) $disabledLogin = Invoke-Api -Method POST -Url 'http://localhost:9000/auth/login' -Body @{ username=$userBName; password='Pass@123456'; captcha=$userCode.Trim('"').Trim(); captchaKey=$userKey } Add-Result $results 'POST' '/auth/login' ($(if($disabledLogin.json.code -ne 200){'PASSED'}else{'FAILED'})) '禁用用户登录被拦截' $disabledLogin $enable = Invoke-Api -Method PUT -Url "http://localhost:9000/admin/platform/user/$userBId/status/1" -Headers $headers Add-Result $results 'PUT' '/admin/platform/user/{userId}/status/{status}' ($(if($enable.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台启用用户' $enable $reset = Invoke-Api -Method PUT -Url "http://localhost:9000/admin/platform/user/$userBId/reset-password?newPassword=NewPass@123" -Headers $headers Add-Result $results 'PUT' '/admin/platform/user/{userId}/reset-password' ($(if($reset.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台重置密码' $reset $capUser2 = Invoke-WebRequest -UseBasicParsing -SkipHttpErrorCheck -Uri 'http://localhost:9000/auth/captcha' $capUserJson2 = $capUser2.Content | ConvertFrom-Json $userKey2 = $capUserJson2.data.captchaKey $userCode2 = docker exec redis redis-cli -a redis_EsJ8FX --raw GET ('captcha:' + $userKey2) $newLogin = Invoke-Api -Method POST -Url 'http://localhost:9000/auth/login' -Body @{ username=$userBName; password='NewPass@123'; captcha=$userCode2.Trim('"').Trim(); captchaKey=$userKey2 } Add-Result $results 'POST' '/auth/login' ($(if($newLogin.json.code -eq 200){'PASSED'}else{'FAILED'})) '重置密码后登录' $newLogin $categoryList = Invoke-Api -Method GET -Url 'http://localhost:9000/admin/platform/content/category/list' -Headers $headers Add-Result $results 'GET' '/admin/platform/content/category/list' ($(if($categoryList.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台分类列表' $categoryList $catCode = 'admcat' + [guid]::NewGuid().ToString('N').Substring(0,6) $catAdd = Invoke-Api -Method POST -Url 'http://localhost:9000/admin/platform/content/category' -Headers $headers -Body @{ name='后台分类'; code=$catCode; parentId=0; level=1; sort=88; status=1 } Add-Result $results 'POST' '/admin/platform/content/category' ($(if($catAdd.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台添加分类' $catAdd $categoryList2 = Invoke-Api -Method GET -Url 'http://localhost:9000/admin/platform/content/category/list' -Headers $headers $cat = @($categoryList2.json.data | Where-Object { $_.code -eq $catCode }) | Select-Object -First 1 if ($cat) { $catUpd = Invoke-Api -Method PUT -Url 'http://localhost:9000/admin/platform/content/category' -Headers $headers -Body @{ id=$cat.id; name='后台分类更新'; code=$catCode; parentId=0; level=1; sort=89; status=1 } $catDel = Invoke-Api -Method DELETE -Url "http://localhost:9000/admin/platform/content/category/$($cat.id)" -Headers $headers Add-Result $results 'PUT' '/admin/platform/content/category' ($(if($catUpd.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台更新分类' $catUpd Add-Result $results 'DELETE' '/admin/platform/content/category/{categoryId}' ($(if($catDel.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台删除分类' $catDel } $commentList = Invoke-Api -Method GET -Url 'http://localhost:9000/admin/platform/comment/list?page=1&size=5' -Headers $headers Add-Result $results 'GET' '/admin/platform/comment/list' ($(if($commentList.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台评论列表' $commentList $commentStats = Invoke-Api -Method GET -Url 'http://localhost:9000/admin/platform/comment/stats' -Headers $headers Add-Result $results 'GET' '/admin/platform/comment/stats' ($(if($commentStats.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台评论统计' $commentStats $firstComment = $null if ($commentList.json -and $commentList.json.data -and $commentList.json.data.records) { $firstComment = @($commentList.json.data.records) | Select-Object -First 1 } if ($firstComment) { $commentDetail = Invoke-Api -Method GET -Url "http://localhost:9000/admin/platform/comment/$($firstComment.id)" -Headers $headers Add-Result $results 'GET' '/admin/platform/comment/{commentId}' ($(if($commentDetail.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台评论详情' $commentDetail } else { Add-Result $results 'GET' '/admin/platform/comment/{commentId}' 'UNTESTED' '当前库里没有可测评论记录' $null } $videoList = Invoke-Api -Method GET -Url 'http://localhost:9000/admin/platform/content/video/list?page=1&size=5' -Headers $headers Add-Result $results 'GET' '/admin/platform/content/video/list' ($(if($videoList.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台视频列表' $videoList $firstVideo = $null if ($videoList.json -and $videoList.json.data -and $videoList.json.data.records) { $firstVideo = @($videoList.json.data.records) | Select-Object -First 1 } if ($firstVideo) { $videoDetail = Invoke-Api -Method GET -Url "http://localhost:9000/admin/platform/content/video/$($firstVideo.id)" -Headers $headers Add-Result $results 'GET' '/admin/platform/content/video/{videoId}' ($(if($videoDetail.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台视频详情' $videoDetail $videoDisable = Invoke-Api -Method PUT -Url "http://localhost:9000/admin/platform/content/video/disable/$($firstVideo.id)" -Headers $headers Add-Result $results 'PUT' '/admin/platform/content/video/disable/{videoId}' ($(if($videoDisable.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台下架视频' $videoDisable } else { Add-Result $results 'GET' '/admin/platform/content/video/{videoId}' 'UNTESTED' '当前库里没有可测视频记录' $null Add-Result $results 'PUT' '/admin/platform/content/video/disable/{videoId}' 'UNTESTED' '当前库里没有可测视频记录' $null } $dashboard = Invoke-Api -Method GET -Url 'http://localhost:9000/admin/dashboard/stats' -Headers $headers Add-Result $results 'GET' '/admin/dashboard/stats' ($(if($dashboard.json.code -eq 200){'PASSED'}else{'FAILED'})) '后台仪表盘' $dashboard $out = [pscustomobject]@{ generatedAt = (Get-Date).ToString('s') ids = [pscustomobject]@{ userA=$userAId; userB=$userBId } results = $results } $outPath = 'C:\Users\toyas\Desktop\本科毕业设计\参考\workspace\HA-OVP\backend\API_TEST_RESULTS_admin.json' $out | ConvertTo-Json -Depth 8 | Set-Content -Path $outPath -Encoding utf8BOM $passed = ($results | Where-Object { $_.status -eq 'PASSED' }).Count $failed = ($results | Where-Object { $_.status -eq 'FAILED' }).Count $untested = ($results | Where-Object { $_.status -eq 'UNTESTED' }).Count [pscustomobject]@{ passed=$passed; failed=$failed; untested=$untested; file=$outPath } | ConvertTo-Json -Compress