Files
chinese-family-tree-2/test-with-session.sh
T
freedakgmail e92884ae53 0.0.1.0
2025-11-23 03:07:39 +08:00

106 lines
2.7 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# API 测试脚本(带 session
BASE_URL="http://localhost:3000"
echo "🧪 API 测试需要先登录获取 session"
echo ""
# 1. 测试登录(获取重定向后的 session)
echo "1️⃣ 测试登录..."
echo "提示:请在浏览器中访问 $BASE_URL/auth/signin 完成登录"
echo ""
# 2. 创建测试用户(如果不存在)
echo "2️⃣ 创建测试用户..."
curl -s -X POST "$BASE_URL/api/auth/register" \
-H "Content-Type: application/json" \
-d '{
"email": "api-test@example.com",
"password": "password123",
"name": "API测试用户"
}' | jq . || echo "用户可能已存在"
echo ""
# 3. 模拟登录获取 session
echo "3️⃣ 模拟登录..."
LOGIN_RESPONSE=$(curl -v -X POST "$BASE_URL/api/auth/signin" \
-H "Content-Type: application/json" \
-d '{
"email": "api-test@example.com",
"password": "password123"
}' 2>&1 | grep -i "set-cookie")
echo "登录响应: $LOGIN_RESPONSE"
# 提取 session token
SESSION_TOKEN=$(echo "$LOGIN_RESPONSE" | grep -o 'next-auth\.session-token=[^;]*' | cut -d'=' -f2)
if [ -z "$SESSION_TOKEN" ]; then
echo "❌ 无法获取 session token"
echo "请手动在浏览器登录后获取 cookies"
exit 1
fi
echo "✅ 获取到 session token: ${SESSION_TOKEN:0:20}..."
echo ""
# 4. 测试创建家族树
echo "4️⃣ 测试创建家族树..."
TREE_RESPONSE=$(curl -s -X POST "$BASE_URL/api/trees" \
-H "Content-Type: application/json" \
-H "Cookie: next-auth.session-token=$SESSION_TOKEN" \
-d '{
"name": "API测试家族树",
"description": "通过API创建的测试家族树"
}')
echo "创建家族树响应:"
echo "$TREE_RESPONSE" | jq . || echo "$TREE_RESPONSE"
# 提取 tree ID
TREE_ID=$(echo "$TREE_RESPONSE" | jq -r '.tree.id // empty')
if [ -z "$TREE_ID" ]; then
echo "❌ 无法获取家族树 ID"
exit 1
fi
echo "✅ 家族树 ID: $TREE_ID"
echo ""
# 5. 测试获取家族树列表
echo "5️⃣ 测试获取家族树列表..."
curl -s -X GET "$BASE_URL/api/trees" \
-H "Cookie: next-auth.session-token=$SESSION_TOKEN" | jq .
echo ""
# 6. 测试创建成员
echo "6️⃣ 测试创建成员..."
MEMBER_RESPONSE=$(curl -s -X POST "$BASE_URL/api/trees/$TREE_ID/members" \
-H "Content-Type: application/json" \
-H "Cookie: next-auth.session-token=$SESSION_TOKEN" \
-d '{
"surname": "李",
"givenName": "明",
"fullName": "李明",
"gender": "MALE",
"generation": 1,
"birthDate": "1990-01-01"
}')
echo "创建成员响应:"
echo "$MEMBER_RESPONSE" | jq . || echo "$MEMBER_RESPONSE"
echo ""
# 7. 测试获取成员列表
echo "7️⃣ 测试获取成员列表..."
curl -s -X GET "$BASE_URL/api/trees/$TREE_ID/members" \
-H "Cookie: next-auth.session-token=$SESSION_TOKEN" | jq .
echo ""
echo "✅ API 测试完成!"