feat: 部署测试扩增-支付接口+mock登录+通知+迁移验证(9项)
This commit is contained in:
54
lib/test.sh
54
lib/test.sh
@@ -103,9 +103,58 @@ test_response_content() {
|
||||
log_info "响应内容验证通过"; return 0
|
||||
}
|
||||
|
||||
# ---------- 新增测试 ----------
|
||||
|
||||
test_mock_login() {
|
||||
if [ "${APP_ENV}" != "development" ]; then
|
||||
log_info " mock-login 仅在开发环境中测试"
|
||||
return 0
|
||||
fi
|
||||
local key; key=$(remote "grep 'MOCK_LOGIN_KEY' /etc/systemd/system/${SERVICE_NAME} 2>/dev/null | head -1 | sed 's/.*MOCK_LOGIN_KEY=//'" 2>/dev/null || echo "")
|
||||
[ -z "$key" ] && { log_warn " 未配置 MOCK_LOGIN_KEY,跳过"; return 0; }
|
||||
|
||||
local resp; resp=$(remote "curl -s --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/api/mock-login' -H 'X-Mock-Key: ${key}'" 2>/dev/null || echo "")
|
||||
local token; token=$(echo "$resp" | grep -o '"token":"[^"]*"' | head -1 | cut -d'"' -f4 || echo "")
|
||||
if [ -n "$token" ]; then
|
||||
log_info " mock-login 返回有效 JWT ✅"
|
||||
|
||||
# 用获取到的 token 测试通知接口
|
||||
local notif; notif=$(remote "curl -s --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/api/notifications/unread-count' -H 'Authorization: Bearer ${token}'" 2>/dev/null || echo "")
|
||||
echo "$notif" | grep -q '"success":true' && log_info " /api/notifications/unread-count 正常 ✅" || log_warn " 通知接口响应异常"
|
||||
else
|
||||
log_warn " mock-login 未返回 token"
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
test_payment_endpoints() {
|
||||
local all=true
|
||||
local c; c=$(remote "curl -s -o /dev/null -w '%{http_code}' --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/payment'" 2>/dev/null || echo "000")
|
||||
[ "$c" = "200" ] && log_info " /payment 返回 200 ✅" || { log_warn " /payment 返回 ${c}"; all=false; }
|
||||
|
||||
c=$(remote "curl -s -o /dev/null -w '%{http_code}' --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/payment/page?package=monthly&jwt=bad'" 2>/dev/null || echo "000")
|
||||
[ "$c" = "401" ] && log_info " /payment/page (bad JWT) 返回 401 ✅" || { log_warn " /payment/page 返回 ${c}"; all=false; }
|
||||
|
||||
c=$(remote "curl -s -o /dev/null -w '%{http_code}' --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/api/user/quota' -H 'Authorization: Bearer bad'" 2>/dev/null || echo "000")
|
||||
[ "$c" = "401" ] && log_info " /api/user/quota (bad JWT) 返回 401 ✅" || { log_warn " /api/user/quota 返回 ${c}"; all=false; }
|
||||
|
||||
$all && log_info "支付端点测试通过" && return 0
|
||||
log_warn "支付端点测试有异常(部分失败)"; return 0
|
||||
}
|
||||
|
||||
test_migration_status() {
|
||||
local r; r=$(remote "docker exec ${DB_CONTAINER} psql -U ${DB_USER} -d ${DB_NAME} -tAc 'SELECT COUNT(*) FROM _migrations'" 2>/dev/null || echo "0")
|
||||
[ "$r" -gt "0" ] 2>/dev/null && log_info " _migrations 表存在,已执行 ${r} 个迁移 ✅" || { log_warn " _migrations 表不存在或无记录"; return 0; }
|
||||
|
||||
local last; last=$(remote "docker exec ${DB_CONTAINER} psql -U ${DB_USER} -d ${DB_NAME} -tAc 'SELECT name FROM _migrations ORDER BY applied_at DESC LIMIT 1'" 2>/dev/null || echo "")
|
||||
[ -n "$last" ] && log_info " 最新迁移: ${last} ✅"
|
||||
return 0
|
||||
}
|
||||
|
||||
# ---------- 主测试入口 ----------
|
||||
run_tests() {
|
||||
log_step "执行部署后测试..."
|
||||
[ "$DRY_RUN" = true ] && { log_dry "完整测试套件(6 项)"; return 0; }
|
||||
[ "$DRY_RUN" = true ] && { log_dry "完整测试套件(9 项)"; return 0; }
|
||||
[ "$SKIP_TESTS" = true ] && { log_warn "跳过测试"; return 0; }
|
||||
|
||||
local tests=(
|
||||
@@ -115,6 +164,9 @@ run_tests() {
|
||||
"test_weather_details_jwt:weather/details JWT"
|
||||
"test_invalid_token_401:无效 Token 401"
|
||||
"test_response_content:响应内容验证"
|
||||
"test_mock_login:Mock 登录 + 通知接口"
|
||||
"test_payment_endpoints:支付端点"
|
||||
"test_migration_status:迁移状态"
|
||||
)
|
||||
local i=1 total=${#tests[@]}
|
||||
for entry in "${tests[@]}"; do
|
||||
|
||||
Reference in New Issue
Block a user