$ErrorActionPreference = 'Stop' function Invoke-Api { param( [string]$Method, [string]$Url, [hashtable]$Headers = @{}, $Body = $null, [int]$TimeoutSec = 60 ) $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) } } try { $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-UserCaptcha { $cap = Invoke-Api -Method GET -Url 'http://localhost:9000/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 --raw GET ('captcha:' + $key) [pscustomobject]@{ key = $key code = $code.Trim('"').Trim() } } function Register-LoginUser { param([string]$Prefix) $password = 'Pass@123456' $username = $Prefix + [guid]::NewGuid().ToString('N').Substring(0, 8) $digits = -join ((48..57) | Get-Random -Count 9 | ForEach-Object { [char]$_ }) $cap = Get-UserCaptcha $register = Invoke-Api -Method POST -Url 'http://localhost:9000/auth/register' -Body @{ username = $username password = $password confirmPassword = $password email = "$username@example.com" phone = '13' + $digits captcha = $cap.code captchaKey = $cap.key } $cap2 = Get-UserCaptcha $login = Invoke-Api -Method POST -Url 'http://localhost:9000/auth/login' -Body @{ username = $username password = $password captcha = $cap2.code captchaKey = $cap2.key } $headers = @{ Authorization = 'Bearer ' + $login.json.data.accessToken } $current = Invoke-Api -Method GET -Url 'http://localhost:9000/auth/current' -Headers $headers [pscustomobject]@{ username = $username password = $password register = $register login = $login headers = $headers userId = [long]$current.json.data.id refreshToken = $login.json.data.refreshToken } } $results = [System.Collections.Generic.List[object]]::new() $uNoCaptcha = Invoke-Api -Method POST -Url 'http://localhost:9000/auth/login' -Body @{ username = 'nouser'; password = 'wrong' } Add-Result -Results $results -Method 'POST' -Path '/auth/login' -Status ($(if ($uNoCaptcha.http -eq 400) { 'PASSED' } else { 'FAILED' })) -Note '无验证码登录应被拦截' -Resp $uNoCaptcha $userA = Register-LoginUser -Prefix 'ua' $userB = Register-LoginUser -Prefix 'ub' Add-Result -Results $results -Method 'POST' -Path '/auth/register' -Status ($(if ($userA.register.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '用户A注册' -Resp $userA.register Add-Result -Results $results -Method 'POST' -Path '/auth/login' -Status ($(if ($userA.login.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '用户A登录' -Resp $userA.login $current = Invoke-Api -Method GET -Url 'http://localhost:9000/auth/current' -Headers $userA.headers Add-Result -Results $results -Method 'GET' -Path '/auth/current' -Status ($(if ($current.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '当前用户' -Resp $current $refresh = Invoke-Api -Method POST -Url "http://localhost:9000/auth/refresh?refreshToken=$($userA.refreshToken)" Add-Result -Results $results -Method 'POST' -Path '/auth/refresh' -Status ($(if ($refresh.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '刷新令牌' -Resp $refresh $usernameById = Invoke-Api -Method GET -Url "http://localhost:9000/auth/username/$($userA.userId)" Add-Result -Results $results -Method 'GET' -Path '/auth/username/{userId}' -Status ($(if ($usernameById.http -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '根据ID获取用户名' -Resp $usernameById $profileCurrent = Invoke-Api -Method GET -Url 'http://localhost:9000/user/profile/current' -Headers $userA.headers Add-Result -Results $results -Method 'GET' -Path '/user/profile/current' -Status ($(if ($profileCurrent.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '当前资料' -Resp $profileCurrent $profilePublic = Invoke-Api -Method GET -Url "http://localhost:9000/user/profile/public/$($userA.userId)" Add-Result -Results $results -Method 'GET' -Path '/user/profile/public/{userId}' -Status ($(if ($profilePublic.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '公开资料' -Resp $profilePublic $updateProfile = Invoke-Api -Method PUT -Url 'http://localhost:9000/user/profile' -Headers $userA.headers -Body @{ nickname = '测试用户A' signature = '签名A' region = '南京' gender = 1 } Add-Result -Results $results -Method 'PUT' -Path '/user/profile' -Status ($(if ($updateProfile.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '更新资料' -Resp $updateProfile $searchUser = Invoke-Api -Method GET -Url "http://localhost:9000/user/profile/search?keyword=$($userA.username)&page=1&size=5" Add-Result -Results $results -Method 'GET' -Path '/user/profile/search' -Status ($(if ($searchUser.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '搜索用户' -Resp $searchUser $getSetting = Invoke-Api -Method GET -Url 'http://localhost:9000/user/setting' -Headers $userA.headers Add-Result -Results $results -Method 'GET' -Path '/user/setting' -Status ($(if ($getSetting.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '获取设置' -Resp $getSetting $updateSetting = Invoke-Api -Method PUT -Url 'http://localhost:9000/user/setting' -Headers $userA.headers -Body @{ privacyLevel = 1 commentNotify = 1 likeNotify = 1 followNotify = 1 systemNotify = 1 } Add-Result -Results $results -Method 'PUT' -Path '/user/setting' -Status ($(if ($updateSetting.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '更新设置' -Resp $updateSetting $follow = Invoke-Api -Method POST -Url "http://localhost:9000/user/follow/$($userA.userId)" -Headers $userB.headers Add-Result -Results $results -Method 'POST' -Path '/user/follow/{followId}' -Status ($(if ($follow.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note 'B关注A' -Resp $follow $checkFollow = Invoke-Api -Method GET -Url "http://localhost:9000/user/follow/check/$($userA.userId)" -Headers $userB.headers Add-Result -Results $results -Method 'GET' -Path '/user/follow/check/{followId}' -Status ($(if ($checkFollow.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '检查关注' -Resp $checkFollow $following = Invoke-Api -Method GET -Url "http://localhost:9000/user/follow/following/$($userB.userId)?page=1&size=5" Add-Result -Results $results -Method 'GET' -Path '/user/follow/following/{userId}' -Status ($(if ($following.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '关注列表' -Resp $following $followers = Invoke-Api -Method GET -Url "http://localhost:9000/user/follow/followers/$($userA.userId)?page=1&size=5" Add-Result -Results $results -Method 'GET' -Path '/user/follow/followers/{userId}' -Status ($(if ($followers.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '粉丝列表' -Resp $followers $noticeList = Invoke-Api -Method GET -Url 'http://localhost:9000/user/notification/list?page=1&size=10' -Headers $userA.headers Add-Result -Results $results -Method 'GET' -Path '/user/notification/list' -Status ($(if ($noticeList.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '通知列表' -Resp $noticeList $noticeUnread = Invoke-Api -Method GET -Url 'http://localhost:9000/user/notification/unread/count' -Headers $userA.headers Add-Result -Results $results -Method 'GET' -Path '/user/notification/unread/count' -Status ($(if ($noticeUnread.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '未读通知' -Resp $noticeUnread $noticeReadAll = Invoke-Api -Method PUT -Url 'http://localhost:9000/user/notification/read' -Headers $userA.headers Add-Result -Results $results -Method 'PUT' -Path '/user/notification/read' -Status ($(if ($noticeReadAll.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '全部通知已读' -Resp $noticeReadAll $msgSend = Invoke-Api -Method POST -Url 'http://localhost:9000/user/message' -Headers $userB.headers -Body @{ receiverId = $userA.userId content = 'hello-from-b' } Add-Result -Results $results -Method 'POST' -Path '/user/message' -Status ($(if ($msgSend.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '发送私信' -Resp $msgSend $msgList = Invoke-Api -Method GET -Url 'http://localhost:9000/user/message/list?page=1&size=10' -Headers $userA.headers Add-Result -Results $results -Method 'GET' -Path '/user/message/list' -Status ($(if ($msgList.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '会话列表' -Resp $msgList $msgDetail = Invoke-Api -Method GET -Url "http://localhost:9000/user/message/detail/$($userB.userId)?page=1&size=10" -Headers $userA.headers Add-Result -Results $results -Method 'GET' -Path '/user/message/detail/{targetId}' -Status ($(if ($msgDetail.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '会话详情' -Resp $msgDetail $msgUnread = Invoke-Api -Method GET -Url 'http://localhost:9000/user/message/unread/count' -Headers $userA.headers Add-Result -Results $results -Method 'GET' -Path '/user/message/unread/count' -Status ($(if ($msgUnread.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '未读私信数' -Resp $msgUnread $msgRead = Invoke-Api -Method PUT -Url "http://localhost:9000/user/message/read/$($userB.userId)" -Headers $userA.headers Add-Result -Results $results -Method 'PUT' -Path '/user/message/read/{senderId}' -Status ($(if ($msgRead.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '私信已读' -Resp $msgRead $logout = Invoke-Api -Method POST -Url 'http://localhost:9000/auth/logout' -Headers $userA.headers Add-Result -Results $results -Method 'POST' -Path '/auth/logout' -Status ($(if ($logout.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '登出' -Resp $logout $unfollow = Invoke-Api -Method DELETE -Url "http://localhost:9000/user/follow/$($userA.userId)" -Headers $userB.headers Add-Result -Results $results -Method 'DELETE' -Path '/user/follow/{followId}' -Status ($(if ($unfollow.json.code -eq 200) { 'PASSED' } else { 'FAILED' })) -Note '取消关注' -Resp $unfollow $out = [pscustomobject]@{ generatedAt = (Get-Date).ToString('s') users = @($userA.username, $userB.username) ids = [pscustomobject]@{ userA = $userA.userId userB = $userB.userId } results = $results } $outPath = 'C:\Users\toyas\Desktop\本科毕业设计\参考\workspace\HA-OVP\backend\API_TEST_RESULTS_auth_user.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 [pscustomobject]@{ passed = $passed failed = $failed file = $outPath } | ConvertTo-Json -Compress