refactor: 整理项目根目录结构

整理内容:
- 删除 60+ 临时测试输出文件 (*.txt)
- 移动二进制文件到 bin/ 目录
- 移动 Shell 脚本到 scripts/ 目录
  - scripts/dev/: check_gitea.sh, check_sub2api.sh, run_tests.sh
  - scripts/deploy/: deploy_*.sh, simple_deploy.sh
  - scripts/ops/: fix_nginx.sh, fix_ssl.sh, install_docker.sh
  - scripts/test/: test_*.sh, test_*.bat
- 移动批处理文件到 scripts/
- 移动 Python 脚本到 tools/
- 清理临时日志文件

保留根目录必要文件:
- go.mod, go.sum, go.work
- Makefile, docker-compose.yml
- .env.example, .gitignore
- README.md, AGENTS.md, DEPLOY_GUIDE.md

验证: go build ./... && go test ./... 通过
This commit is contained in:
2026-04-07 18:10:36 +08:00
parent 5dbb530b76
commit 5b6bd93179
152 changed files with 8775 additions and 4084 deletions

137
scripts/test/test_all.bat Normal file
View File

@@ -0,0 +1,137 @@
@echo off
REM 用户管理系统 - Windows测试执行脚本
echo ==========================================
echo 用户管理系统 - 测试执行脚本
echo ==========================================
REM 颜色定义
chcp 65001 >nul
:menu
echo.
echo 请选择测试类型:
echo 1. 运行所有测试
echo 2. 运行单元测试
echo 3. 运行集成测试
echo 4. 运行E2E测试
echo 5. 运行鲁棒性测试
echo 6. 生成覆盖率报告
echo 7. 运行性能基准测试
echo 8. 运行竞态检测
echo 0. 退出
echo.
set /p choice=请输入选项(0-8):
if "%choice%"=="1" goto all_tests
if "%choice%"=="2" goto unit_tests
if "%choice%"=="3" goto integration_tests
if "%choice%"=="4" goto e2e_tests
if "%choice%"=="5" goto robust_tests
if "%choice%"=="6" goto coverage
if "%choice%"=="7" goto benchmark
if "%choice%"=="8" goto race
if "%choice%"=="0" goto end
goto menu
:check_go
echo [INFO] 检查Go环境...
where go >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Go未安装
pause
exit /b 1
)
goto :eof
:all_tests
call :check_go
echo ==========================================
echo 运行所有测试
echo ==========================================
call :unit_tests
echo [INFO] ✅ 所有测试准备完成
echo ==========================================
pause
goto menu
:unit_tests
call :check_go
echo ==========================================
echo 运行单元测试
echo ==========================================
echo [INFO] 测试Domain层...
go test -v ./internal/domain/... -run "Test.*"
echo [INFO] ✅ 单元测试完成
pause
goto menu
:integration_tests
call :check_go
echo ==========================================
echo 运行集成测试
echo ==========================================
echo [INFO] 测试集成层...
go test -v ./internal/integration/... -run "Test.*"
echo [INFO] ✅ 集成测试完成
pause
goto menu
:e2e_tests
call :check_go
echo ==========================================
echo 运行端到端测试
echo ==========================================
echo [INFO] 测试E2E流程...
go test -v ./internal/e2e/... -run "Test.*"
echo [INFO] ✅ 端到端测试完成
pause
goto menu
:robust_tests
call :check_go
echo ==========================================
echo 运行鲁棒性测试
echo ==========================================
echo [INFO] 测试鲁棒性...
go test -v ./internal/robustness/... -run "Test.*"
echo [INFO] ✅ 鲁棒性完成
pause
goto menu
:coverage
call :check_go
echo ==========================================
echo 运行测试并生成覆盖率报告
echo ==========================================
go test -v -coverprofile=coverage.out ./...
go tool cover -html=coverage.out -o coverage.html
echo [INFO] 覆盖率报告已生成: coverage.html
go tool cover -func=coverage.out
echo [INFO] ✅ 覆盖率测试完成
pause
goto menu
:benchmark
call :check_go
echo ==========================================
echo 运行性能基准测试
echo ==========================================
go test -bench=. -benchmem ./internal/domain/...
echo [INFO] ✅ 性能基准测试完成
pause
goto menu
:race
call :check_go
echo ==========================================
echo 运行竞态检测
echo ==========================================
go test -race ./...
echo [INFO] ✅ 竞态检测完成
pause
goto menu
:end
echo 退出测试脚本
exit /b 0

39
scripts/test/test_api.bat Normal file
View File

@@ -0,0 +1,39 @@
# 用户管理系统 API 测试脚本
@if "%TEST_ADMIN_ACCOUNT%"=="" set TEST_ADMIN_ACCOUNT=admin
@if "%TEST_ADMIN_PASSWORD%"=="" (
@echo 请先设置 TEST_ADMIN_PASSWORD
@exit /b 1
)
## 1. 健康检查
@echo "=== 1. 健康检查 ==="
curl http://localhost:8080/health
echo.
## 2. 用户注册
@echo.
@echo "=== 2. 用户注册 ==="
curl -X POST http://localhost:8080/api/v1/auth/register ^
-H "Content-Type: application/json" ^
-d "{\"username\":\"testuser\",\"password\":\"Test123456\",\"email\":\"test@example.com\"}"
echo.
## 3. 用户登录
@echo.
@echo "=== 3. 用户登录admin ==="
curl -X POST http://localhost:8080/api/v1/auth/login ^
-H "Content-Type: application/json" ^
-d "{\"account\":\"%TEST_ADMIN_ACCOUNT%\",\"password\":\"%TEST_ADMIN_PASSWORD%\"}"
echo.
## 4. 获取用户信息需要token这里先跳过
@echo.
@echo "=== 4. 需要使用登录返回的token ==="
@echo "请复制上面登录返回的access_token然后手动测试"
@echo "curl -X GET http://localhost:8080/api/v1/auth/userinfo -H \"Authorization: Bearer YOUR_TOKEN\""
echo.
@echo.
@echo "测试完成!"
@pause

49
scripts/test/test_api.sh Normal file
View File

@@ -0,0 +1,49 @@
#!/bin/bash
# 用户管理系统 API 测试脚本
TEST_ADMIN_ACCOUNT="${TEST_ADMIN_ACCOUNT:-admin}"
if [ -z "${TEST_ADMIN_PASSWORD:-}" ]; then
echo "请先设置 TEST_ADMIN_PASSWORD"
exit 1
fi
echo "=== 1. 健康检查 ==="
curl http://localhost:8080/health
echo -e "\n"
echo "=== 2. 用户注册 ==="
curl -X POST http://localhost:8080/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{"username":"testuser","password":"Test123456","email":"test@example.com"}'
echo -e "\n"
echo "=== 3. 用户登录admin ==="
LOGIN_RESPONSE=$(curl -s -X POST http://localhost:8080/api/v1/auth/login \
-H "Content-Type: application/json" \
-d "{\"account\":\"${TEST_ADMIN_ACCOUNT}\",\"password\":\"${TEST_ADMIN_PASSWORD}\"}")
echo "$LOGIN_RESPONSE"
# 提取token
TOKEN=$(echo $LOGIN_RESPONSE | grep -o '"access_token":"[^"]*' | cut -d'"' -f4)
echo -e "\n=== 4. 获取用户信息 ==="
if [ -n "$TOKEN" ]; then
curl -X GET http://localhost:8080/api/v1/auth/userinfo \
-H "Authorization: Bearer $TOKEN"
echo -e "\n"
else
echo "无法获取token跳过此测试"
fi
echo -e "\n=== 5. 测试限流(连续快速请求) ==="
for i in {1..6}; do
echo "$i 次登录请求:"
curl -s -X POST http://localhost:8080/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"account":"wrong","password":"wrong"}'
echo ""
done
echo -e "\n测试完成"

399
scripts/test/test_full.sh Normal file
View File

@@ -0,0 +1,399 @@
#!/bin/bash
# 用户管理系统自动化测试脚本
# 用途:全面测试所有功能和接口
BASE_URL="http://localhost:8080"
ADMIN_TOKEN=""
USER_TOKEN=""
USER_ID=""
TEST_ADMIN_ACCOUNT="${TEST_ADMIN_ACCOUNT:-admin}"
TEST_ADMIN_PASSWORD="${TEST_ADMIN_PASSWORD:-}"
if [ -z "${TEST_ADMIN_PASSWORD}" ]; then
echo "请先设置 TEST_ADMIN_PASSWORD"
exit 1
fi
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 打印函数
print_success() {
echo -e "${GREEN}$1${NC}"
}
print_error() {
echo -e "${RED}$1${NC}"
}
print_info() {
echo -e "${YELLOW}$1${NC}"
}
# 测试1健康检查
test_health_check() {
print_info "测试1健康检查"
response=$(curl -s -w "\n%{http_code}" "${BASE_URL}/health")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" = "200" ]; then
print_success "健康检查通过 (200)"
echo "响应: $body"
else
print_error "健康检查失败 (HTTP $http_code)"
fi
echo ""
}
# 测试2用户注册
test_register() {
print_info "测试2用户注册"
# 测试正常注册
response=$(curl -s -w "\n%{http_code}" -X POST "${BASE_URL}/api/v1/auth/register" \
-H "Content-Type: application/json" \
-d '{"username":"testuser1","password":"Test123456","email":"test1@example.com"}')
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" = "200" ]; then
print_success "用户注册成功"
USER_ID=$(echo "$body" | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
echo "用户ID: $USER_ID"
else
print_error "用户注册失败 (HTTP $http_code)"
echo "响应: $body"
fi
echo ""
# 测试重复用户名
print_info "测试2.1:重复用户名注册"
response=$(curl -s -w "\n%{http_code}" -X POST "${BASE_URL}/api/v1/auth/register" \
-H "Content-Type: application/json" \
-d '{"username":"testuser1","password":"Test123456","email":"test2@example.com"}')
http_code=$(echo "$response" | tail -n1)
if [ "$http_code" = "400" ] || [ "$http_code" = "409" ]; then
print_success "重复用户名注册被正确拒绝 ($http_code)"
else
print_error "重复用户名验证失败 (HTTP $http_code)"
fi
echo ""
# 测试弱密码
print_info "测试2.2:弱密码注册"
response=$(curl -s -w "\n%{http_code}" -X POST "${BASE_URL}/api/v1/auth/register" \
-H "Content-Type: application/json" \
-d '{"username":"testuser2","password":"123","email":"test2@example.com"}')
http_code=$(echo "$response" | tail -n1)
if [ "$http_code" = "400" ]; then
print_success "弱密码注册被正确拒绝 (400)"
else
print_error "弱密码验证失败 (HTTP $http_code)"
fi
echo ""
# 测试无效邮箱
print_info "测试2.3:无效邮箱注册"
response=$(curl -s -w "\n%{http_code}" -X POST "${BASE_URL}/api/v1/auth/register" \
-H "Content-Type: application/json" \
-d '{"username":"testuser3","password":"Test123456","email":"invalid"}')
http_code=$(echo "$response" | tail -n1)
if [ "$http_code" = "400" ]; then
print_success "无效邮箱注册被正确拒绝 (400)"
else
print_error "邮箱验证失败 (HTTP $http_code)"
fi
echo ""
}
# 测试3用户登录
test_login() {
print_info "测试3用户登录"
# 测试正常登录(管理员)
response=$(curl -s -w "\n%{http_code}" -X POST "${BASE_URL}/api/v1/auth/login" \
-H "Content-Type: application/json" \
-d "{\"account\":\"${TEST_ADMIN_ACCOUNT}\",\"password\":\"${TEST_ADMIN_PASSWORD}\"}")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" = "200" ]; then
print_success "管理员登录成功"
ADMIN_TOKEN=$(echo "$body" | grep -o '"access_token":"[^"]*' | cut -d'"' -f4)
echo "获取到访问令牌"
else
print_error "管理员登录失败 (HTTP $http_code)"
echo "响应: $body"
fi
echo ""
# 测试错误密码
print_info "测试3.1:错误密码登录"
response=$(curl -s -w "\n%{http_code}" -X POST "${BASE_URL}/api/v1/auth/login" \
-H "Content-Type: application/json" \
-d '{"account":"admin","password":"wrong"}')
http_code=$(echo "$response" | tail -n1)
if [ "$http_code" = "401" ]; then
print_success "错误密码登录被正确拒绝 (401)"
else
print_error "错误密码验证失败 (HTTP $http_code)"
fi
echo ""
# 测试用户名登录
if [ -n "$USER_ID" ]; then
print_info "测试3.2:用户名登录(新注册用户)"
response=$(curl -s -w "\n%{http_code}" -X POST "${BASE_URL}/api/v1/auth/login" \
-H "Content-Type: application/json" \
-d '{"account":"testuser1","password":"Test123456"}')
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" = "200" ]; then
print_success "新用户登录成功"
USER_TOKEN=$(echo "$body" | grep -o '"access_token":"[^"]*' | cut -d'"' -f4)
else
print_error "新用户登录失败 (HTTP $http_code)"
fi
echo ""
fi
}
# 测试4获取用户信息
test_get_userinfo() {
print_info "测试4获取用户信息需要认证"
if [ -z "$ADMIN_TOKEN" ]; then
print_error "没有访问令牌,跳过测试"
return
fi
response=$(curl -s -w "\n%{http_code}" -X GET "${BASE_URL}/api/v1/auth/userinfo" \
-H "Authorization: Bearer ${ADMIN_TOKEN}")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" = "200" ]; then
print_success "获取用户信息成功"
echo "响应: $body"
else
print_error "获取用户信息失败 (HTTP $http_code)"
echo "响应: $body"
fi
echo ""
# 测试无令牌访问
print_info "测试4.1:无令牌访问"
response=$(curl -s -w "\n%{http_code}" -X GET "${BASE_URL}/api/v1/auth/userinfo")
http_code=$(echo "$response" | tail -n1)
if [ "$http_code" = "401" ]; then
print_success "无令牌访问被正确拒绝 (401)"
else
print_error "认证验证失败 (HTTP $http_code)"
fi
echo ""
# 测试无效令牌
print_info "测试4.2:无效令牌访问"
response=$(curl -s -w "\n%{http_code}" -X GET "${BASE_URL}/api/v1/auth/userinfo" \
-H "Authorization: Bearer invalid_token")
http_code=$(echo "$response" | tail -n1)
if [ "$http_code" = "401" ]; then
print_success "无效令牌访问被正确拒绝 (401)"
else
print_error "无效令牌验证失败 (HTTP $http_code)"
fi
echo ""
}
# 测试5获取用户列表
test_get_users() {
print_info "测试5获取用户列表需要认证"
if [ -z "$ADMIN_TOKEN" ]; then
print_error "没有访问令牌,跳过测试"
return
fi
response=$(curl -s -w "\n%{http_code}" -X GET "${BASE_URL}/api/v1/users" \
-H "Authorization: Bearer ${ADMIN_TOKEN}")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" = "200" ]; then
print_success "获取用户列表成功"
echo "响应: $body"
else
print_error "获取用户列表失败 (HTTP $http_code)"
echo "响应: $body"
fi
echo ""
}
# 测试6更新用户信息
test_update_user() {
print_info "测试6更新用户信息需要认证"
if [ -z "$ADMIN_TOKEN" ] || [ -z "$USER_ID" ]; then
print_error "缺少必要参数,跳过测试"
return
fi
response=$(curl -s -w "\n%{http_code}" -X PUT "${BASE_URL}/api/v1/users/${USER_ID}" \
-H "Authorization: Bearer ${ADMIN_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"nickname":"测试用户昵称","bio":"这是个人简介"}')
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" = "200" ]; then
print_success "更新用户信息成功"
echo "响应: $body"
else
print_error "更新用户信息失败 (HTTP $http_code)"
echo "响应: $body"
fi
echo ""
}
# 测试7令牌刷新
test_refresh_token() {
print_info "测试7令牌刷新"
if [ -z "$ADMIN_TOKEN" ]; then
print_error "没有访问令牌,跳过测试"
return
fi
response=$(curl -s -w "\n%{http_code}" -X POST "${BASE_URL}/api/v1/auth/refresh" \
-H "Content-Type: application/json" \
-d "{\"refresh_token\":\"${ADMIN_TOKEN}\"}")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" = "200" ] || [ "$http_code" = "401" ]; then
print_success "令牌刷新接口响应正常 (HTTP $http_code)"
echo "响应: $body"
else
print_error "令牌刷新失败 (HTTP $http_code)"
echo "响应: $body"
fi
echo ""
}
# 测试8限流测试
test_rate_limit() {
print_info "测试8限流功能测试"
print_info "快速发送6次请求测试限流..."
success_count=0
rate_limited=0
for i in {1..6}; do
response=$(curl -s -w "\n%{http_code}" -X POST "${BASE_URL}/api/v1/auth/login" \
-H "Content-Type: application/json" \
-d '{"account":"wrong","password":"wrong"}')
http_code=$(echo "$response" | tail -n1)
if [ "$http_code" = "429" ]; then
rate_limited=$((rate_limited + 1))
echo " 请求 $i: 被限流 (429)"
else
success_count=$((success_count + 1))
echo " 请求 $i: 正常 (HTTP $http_code)"
fi
done
if [ "$rate_limited" -gt 0 ]; then
print_success "限流功能正常生效,触发 $rate_limited 次限流"
else
print_error "限流功能未触发,建议检查配置"
fi
echo ""
}
# 测试9Prometheus 指标
test_metrics() {
print_info "测试9Prometheus 指标采集"
response=$(curl -s -w "\n%{http_code}" "${BASE_URL}/metrics")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" = "200" ]; then
print_success "Prometheus 指标端点正常"
# 检查关键指标
if echo "$body" | grep -q "http_requests_total"; then
print_success "✓ http_requests_total 指标存在"
fi
if echo "$body" | grep -q "http_request_duration_seconds"; then
print_success "✓ http_request_duration_seconds 指标存在"
fi
if echo "$body" | grep -q "user_logins_total"; then
print_success "✓ user_logins_total 指标存在"
fi
else
print_error "Prometheus 指标端点失败 (HTTP $http_code)"
fi
echo ""
}
# 测试10登出
test_logout() {
print_info "测试10用户登出"
if [ -z "$ADMIN_TOKEN" ]; then
print_error "没有访问令牌,跳过测试"
return
fi
response=$(curl -s -w "\n%{http_code}" -X POST "${BASE_URL}/api/v1/auth/logout" \
-H "Authorization: Bearer ${ADMIN_TOKEN}")
http_code=$(echo "$response" | tail -n1)
if [ "$http_code" = "200" ]; then
print_success "登出成功"
else
print_error "登出失败 (HTTP $http_code)"
fi
echo ""
}
# 主测试流程
main() {
echo "============================================"
echo " 用户管理系统自动化测试"
echo " 测试环境: ${BASE_URL}"
echo "============================================"
echo ""
test_health_check
test_register
test_login
test_get_userinfo
test_get_users
test_update_user
test_refresh_token
test_rate_limit
test_metrics
test_logout
echo "============================================"
echo " 测试完成"
echo "============================================"
}
# 执行测试
main

14
scripts/test/test_go.bat Normal file
View File

@@ -0,0 +1,14 @@
@echo off
echo Testing Go installation...
echo.
echo Checking Go version...
"C:\Program Files\Go\bin\go.exe" version
if %errorlevel% == 0 (
echo.
echo SUCCESS: Go is working!
) else (
echo.
echo ERROR: Go is not working
)
echo.
pause