You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

143 lines
5.7 KiB

# GoFaster API 修复版测试脚本
# 用于测试修复后的用户管理API接口
param(
[string]$BaseUrl = "http://localhost:8080",
[string]$Username = "admin",
[string]$Password = "password"
)
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " GoFaster API 修复版测试脚本" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# 1. 测试健康检查端点
Write-Host "1. 测试健康检查端点..." -ForegroundColor Yellow
try {
$healthResponse = Invoke-RestMethod -Uri "$BaseUrl/health" -Method GET -TimeoutSec 10
Write-Host "✅ 健康检查通过: $($healthResponse.status)" -ForegroundColor Green
} catch {
Write-Host "❌ 健康检查失败: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
Write-Host ""
# 2. 测试登录接口
Write-Host "2. 测试登录接口..." -ForegroundColor Yellow
try {
$loginData = @{
username = $Username
password = $Password
} | ConvertTo-Json
$loginResponse = Invoke-RestMethod -Uri "$BaseUrl/api/auth/login" -Method POST -Body $loginData -ContentType "application/json" -TimeoutSec 10
if ($loginResponse.data.token) {
Write-Host "✅ 登录成功,获取到token" -ForegroundColor Green
$token = $loginResponse.data.token
} else {
Write-Host "❌ 登录失败,未获取到token" -ForegroundColor Red
Write-Host "响应内容: $($loginResponse | ConvertTo-Json)" -ForegroundColor Red
exit 1
}
} catch {
Write-Host "❌ 登录失败: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
Write-Host ""
# 3. 测试原始管理员路由(需要完整权限)
Write-Host "3. 测试原始管理员路由..." -ForegroundColor Yellow
try {
$headers = @{
"Authorization" = "Bearer $token"
"Content-Type" = "application/json"
}
$usersResponse = Invoke-RestMethod -Uri "$BaseUrl/api/auth/admin/users?page=1&pageSize=10" -Method GET -Headers $headers -TimeoutSec 10
if ($usersResponse.data) {
Write-Host "✅ 原始管理员路由 - 用户列表获取成功" -ForegroundColor Green
Write-Host " 用户数量: $($usersResponse.data.Count)" -ForegroundColor White
Write-Host " 总数量: $($usersResponse.total)" -ForegroundColor White
} else {
Write-Host "❌ 原始管理员路由 - 用户列表获取失败" -ForegroundColor Red
Write-Host "响应内容: $($usersResponse | ConvertTo-Json)" -ForegroundColor Red
}
} catch {
Write-Host "❌ 原始管理员路由 - 用户列表获取失败: $($_.Exception.Message)" -ForegroundColor Red
if ($_.Exception.Response) {
$statusCode = $_.Exception.Response.StatusCode
Write-Host "HTTP状态码: $statusCode" -ForegroundColor Red
}
}
Write-Host ""
# 4. 测试简化权限路由(只检查JWT)
Write-Host "4. 测试简化权限路由..." -ForegroundColor Yellow
try {
$testUsersResponse = Invoke-RestMethod -Uri "$BaseUrl/api/auth/test-admin/users?page=1&pageSize=10" -Method GET -Headers $headers -TimeoutSec 10
if ($testUsersResponse.data) {
Write-Host "✅ 简化权限路由 - 用户列表获取成功" -ForegroundColor Green
Write-Host " 用户数量: $($testUsersResponse.data.Count)" -ForegroundColor White
Write-Host " 总数量: $($testUsersResponse.total)" -ForegroundColor White
} else {
Write-Host "❌ 简化权限路由 - 用户列表获取失败" -ForegroundColor Red
Write-Host "响应内容: $($testUsersResponse | ConvertTo-Json)" -ForegroundColor Red
}
} catch {
Write-Host "❌ 简化权限路由 - 用户列表获取失败: $($_.Exception.Message)" -ForegroundColor Red
if ($_.Exception.Response) {
$statusCode = $_.Exception.Response.StatusCode
Write-Host "HTTP状态码: $statusCode" -ForegroundColor Red
}
}
Write-Host ""
# 5. 测试角色列表接口
Write-Host "5. 测试角色列表接口..." -ForegroundColor Yellow
try {
$rolesResponse = Invoke-RestMethod -Uri "$BaseUrl/api/auth/test-admin/roles" -Method GET -Headers $headers -TimeoutSec 10
if ($rolesResponse.data) {
Write-Host "✅ 角色列表获取成功" -ForegroundColor Green
Write-Host " 角色数量: $($rolesResponse.data.Count)" -ForegroundColor White
foreach ($role in $rolesResponse.data) {
Write-Host " - $($role.name) ($($role.code))" -ForegroundColor White
}
} else {
Write-Host "❌ 角色列表获取失败,响应格式异常" -ForegroundColor Red
Write-Host "响应内容: $($rolesResponse | ConvertTo-Json)" -ForegroundColor Red
}
} catch {
Write-Host "❌ 角色列表获取失败: $($_.Exception.Message)" -ForegroundColor Red
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " API 测试完成" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# 显示使用说明
Write-Host "使用说明:" -ForegroundColor Yellow
Write-Host " - 默认用户名: admin" -ForegroundColor White
Write-Host " - 默认密码: password" -ForegroundColor White
Write-Host " - 原始管理员路由: /api/auth/admin/*" -ForegroundColor White
Write-Host " - 简化权限路由: /api/auth/test-admin/*" -ForegroundColor White
Write-Host ""
Write-Host "修复说明:" -ForegroundColor Yellow
Write-Host " - UserController 现在使用统一的响应格式" -ForegroundColor White
Write-Host " - 添加了简化权限的测试路由" -ForegroundColor White
Write-Host " - 权限中间件暂时放宽了检查" -ForegroundColor White
Write-Host ""
Write-Host "按任意键退出..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")