fix: 部署测试test.sh使用HTTPS协议(4433/4434端口)+跳过mock支付测试生产环境
This commit is contained in:
40
lib/test.sh
40
lib/test.sh
@@ -16,9 +16,15 @@ rollback_and_exit() {
|
||||
}
|
||||
|
||||
test_basic_connectivity() {
|
||||
# 根据端口判定协议(4433/4434 生产环境使用 HTTPS)
|
||||
local proto="http"
|
||||
if [ "${BACKEND_PORT}" = "4433" ] || [ "${BACKEND_PORT}" = "4434" ]; then
|
||||
proto="https"
|
||||
fi
|
||||
local base_url="${proto}://127.0.0.1:${BACKEND_PORT}"
|
||||
local max_retries=3 retry=0
|
||||
while [ $retry -lt $max_retries ]; do
|
||||
retry=$((retry+1)); log_info "尝试 ${retry}/${max_retries}: 测试本地后端 (127.0.0.1:${BACKEND_PORT})"
|
||||
retry=$((retry+1)); log_info "尝试 ${retry}/${max_retries}: 测试本地后端 (${base_url})"
|
||||
local all=true
|
||||
for test in \
|
||||
"/health:200" \
|
||||
@@ -27,7 +33,7 @@ test_basic_connectivity() {
|
||||
"/:200"; do
|
||||
IFS=':' read -r path expected method extra <<< "$test"
|
||||
[ -z "$method" ] && method="GET"
|
||||
local code; code=$(remote "curl -s -o /dev/null -w '%{http_code}' --max-time 10 -X ${method} http://127.0.0.1:${BACKEND_PORT}${path} ${extra}" 2>/dev/null || echo "000")
|
||||
local code; code=$(remote "curl -sk -o /dev/null -w '%{http_code}' --max-time 10 -X ${method} ${base_url}${path} ${extra}" 2>/dev/null || echo "000")
|
||||
if [ "$code" != "$expected" ] && [ "$expected" != "200,400" ] && ! { [ "$expected" = "400,200" ] && { [ "$code" = "400" ] || [ "$code" = "200" ]; }; }; then
|
||||
if { [ "$expected" = "400" ] && [ "$code" = "200" ]; } || { [ "$expected" = "401" ] && [ "$code" = "200" ]; }; then
|
||||
log_info " ${path} 正常 (HTTP ${code})"; continue
|
||||
@@ -56,25 +62,25 @@ test_database_connection() {
|
||||
}
|
||||
|
||||
test_token_flow() {
|
||||
local login; login=$(remote "curl -s --max-time 10 -X POST http://127.0.0.1:${BACKEND_PORT}/api/login -H 'Content-Type: application/json' -d '{\"code\":\"test\"}'" 2>/dev/null || echo "")
|
||||
local login; login=$(remote "curl -sk --max-time 10 -X POST ${base_url}/api/login -H 'Content-Type: application/json' -d '{\"code\":\"test\"}'" 2>/dev/null || echo "")
|
||||
local token; token=$(echo "$login" | grep -o '"token":"[^"]*"' | head -1 | cut -d'"' -f4 || echo "")
|
||||
[ -z "$token" ] && log_warn " 未获取到 token(微信 code 无效是正常的)" || log_info " 获取到 token"
|
||||
|
||||
# 测试受保护端点
|
||||
local profile; profile=$(remote "curl -s --max-time 10 http://127.0.0.1:${BACKEND_PORT}/api/user/profile -H 'Authorization: Bearer ${token}'" 2>/dev/null || echo "")
|
||||
local profile; profile=$(remote "curl -sk --max-time 10 ${base_url}/api/user/profile -H 'Authorization: Bearer ${token}'" 2>/dev/null || echo "")
|
||||
echo "$profile" | grep -q '"success":true' && log_info " /api/user/profile JWT 验证成功" || log_warn " /api/user/profile 无权限(正常)"
|
||||
|
||||
local refresh; refresh=$(remote "curl -s --max-time 10 -X POST http://127.0.0.1:${BACKEND_PORT}/api/refresh-token -H 'Content-Type: application/json' -d '{\"refresh_token\":\"invalid\"}'" 2>/dev/null || echo "")
|
||||
local refresh; refresh=$(remote "curl -sk --max-time 10 -X POST ${base_url}/api/refresh-token -H 'Content-Type: application/json' -d '{\"refresh_token\":\"invalid\"}'" 2>/dev/null || echo "")
|
||||
log_info " refresh_token 端点响应正常"
|
||||
log_info "Token 流程测试通过"; return 0
|
||||
}
|
||||
|
||||
test_weather_details_jwt() {
|
||||
local login; login=$(remote "curl -s --max-time 10 -X POST http://127.0.0.1:${BACKEND_PORT}/api/login -H 'Content-Type: application/json' -d '{\"code\":\"test\"}'" 2>/dev/null || echo "")
|
||||
local login; login=$(remote "curl -sk --max-time 10 -X POST ${base_url}/api/login -H 'Content-Type: application/json' -d '{\"code\":\"test\"}'" 2>/dev/null || echo "")
|
||||
local token; token=$(echo "$login" | grep -o '"token":"[^"]*"' | head -1 | cut -d'"' -f4 || echo "")
|
||||
[ -z "$token" ] && { log_warn " 无 token,跳过 weather/details 测试(微信 code 无效是正常的)"; return 0; }
|
||||
|
||||
local resp; resp=$(remote "curl -s --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/weather/details?id=1' -H 'Authorization: Bearer ${token}'" 2>/dev/null || echo "")
|
||||
local resp; resp=$(remote "curl -sk --max-time 10 '${base_url}/weather/details?id=1' -H 'Authorization: Bearer ${token}'" 2>/dev/null || echo "")
|
||||
if echo "$resp" | grep -q '"success":true' || echo "$resp" | grep -q "Forbidden"; then
|
||||
log_info " weather/details JWT 验证通过"
|
||||
else
|
||||
@@ -87,7 +93,7 @@ test_invalid_token_401() {
|
||||
local bad="eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjB9.bad"
|
||||
local all=true
|
||||
for path in "/api/user/profile" "/weather/details?id=1" "/api/weather"; do
|
||||
local c; c=$(remote "curl -s -o /dev/null -w '%{http_code}' --max-time 10 'http://127.0.0.1:${BACKEND_PORT}${path}' -H 'Authorization: Bearer ${bad}'" 2>/dev/null || echo "000")
|
||||
local c; c=$(remote "curl -sk -o /dev/null -w '%{http_code}' --max-time 10 '${base_url}${path}' -H 'Authorization: Bearer ${bad}'" 2>/dev/null || echo "000")
|
||||
[ "$c" = "401" ] && log_info " ${path} 返回 401 ✓" || { log_warn " ${path} 返回 ${c}"; all=false; }
|
||||
done
|
||||
$all && log_info "无效 Token 401 测试通过" && return 0
|
||||
@@ -95,7 +101,7 @@ test_invalid_token_401() {
|
||||
}
|
||||
|
||||
test_response_content() {
|
||||
local r; r=$(remote "curl -s --max-time 10 http://127.0.0.1:${BACKEND_PORT}/health" 2>/dev/null || echo "")
|
||||
local r; r=$(remote "curl -sk --max-time 10 ${base_url}/health" 2>/dev/null || echo "")
|
||||
echo "$r" | grep -q '"status"' && echo "$r" | grep -q '"database"' && \
|
||||
log_info " /health JSON 格式正确 ✓" || { log_warn " /health 响应异常"; return 1; }
|
||||
echo "$r" | grep -q '"database":"connected"' && \
|
||||
@@ -113,13 +119,13 @@ test_mock_login() {
|
||||
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 resp; resp=$(remote "curl -sk --max-time 10 '${base_url}/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 "")
|
||||
local notif; notif=$(remote "curl -sk --max-time 10 '${base_url}/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"
|
||||
@@ -129,13 +135,13 @@ test_mock_login() {
|
||||
|
||||
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")
|
||||
local c; c=$(remote "curl -sk -o /dev/null -w '%{http_code}' --max-time 10 '${base_url}/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=$(remote "curl -sk -o /dev/null -w '%{http_code}' --max-time 10 '${base_url}/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=$(remote "curl -sk -o /dev/null -w '%{http_code}' --max-time 10 '${base_url}/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
|
||||
@@ -203,14 +209,14 @@ test_payment_flow() {
|
||||
|
||||
# 1. 获取 mock JWT
|
||||
local login_resp
|
||||
login_resp=$(remote "curl -s --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/api/mock-login' -H 'X-Mock-Key: ${mock_key}'" 2>/dev/null || echo "")
|
||||
login_resp=$(remote "curl -sk --max-time 10 '${base_url}/api/mock-login' -H 'X-Mock-Key: ${mock_key}'" 2>/dev/null || echo "")
|
||||
local token
|
||||
token=$(echo "$login_resp" | grep -o '"token":"[^"]*"' | head -1 | cut -d'"' -f4 || echo "")
|
||||
|
||||
if [ -n "$token" ]; then
|
||||
# 2. 创建月度订单
|
||||
local order_page
|
||||
order_page=$(remote "curl -s --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/payment/page?package=monthly&jwt=${token}'" 2>/dev/null || echo "")
|
||||
order_page=$(remote "curl -sk --max-time 10 '${base_url}/payment/page?package=monthly&jwt=${token}'" 2>/dev/null || echo "")
|
||||
|
||||
# 3. 从页面提取订单号
|
||||
local order_no
|
||||
@@ -219,7 +225,7 @@ test_payment_flow() {
|
||||
if [ -n "$order_no" ] && [ "$order_no" != "null" ]; then
|
||||
# 4. Mock 确认支付
|
||||
local confirm_resp
|
||||
confirm_resp=$(remote "curl -s --max-time 10 -X POST 'http://127.0.0.1:${BACKEND_PORT}/api/payment/mock-confirm' \
|
||||
confirm_resp=$(remote "curl -sk --max-time 10 -X POST '${base_url}/api/payment/mock-confirm' \
|
||||
-H 'Authorization: Bearer ${token}' \
|
||||
-H 'X-Mock-Key: ${mock_key}' \
|
||||
-H 'Content-Type: application/json' \
|
||||
|
||||
Reference in New Issue
Block a user