#!/bin/bash # Whale Town API Test Script (Linux/macOS) # 测试邮箱验证码和用户注册登录功能 BASE_URL="${1:-http://localhost:3000}" TEST_EMAIL="${2:-test@example.com}" echo "=== Whale Town API Test (Linux/macOS) ===" echo "Testing without database and email server" echo "Base URL: $BASE_URL" echo "Test Email: $TEST_EMAIL" # Test 1: Send verification code echo "" echo "1. Sending email verification code..." SEND_RESPONSE=$(curl -s -X POST "$BASE_URL/auth/send-email-verification" \ -H "Content-Type: application/json" \ -d "{\"email\":\"$TEST_EMAIL\"}") if echo "$SEND_RESPONSE" | grep -q '"success"'; then echo "✅ Verification code sent successfully" VERIFICATION_CODE=$(echo "$SEND_RESPONSE" | grep -o '"verification_code":"[^"]*"' | cut -d'"' -f4) IS_TEST_MODE=$(echo "$SEND_RESPONSE" | grep -o '"is_test_mode":[^,}]*' | cut -d':' -f2) echo " Code: $VERIFICATION_CODE" echo " Test Mode: $IS_TEST_MODE" else echo "❌ Failed to send verification code" echo " Response: $SEND_RESPONSE" exit 1 fi # Test 2: Verify email code echo "" echo "2. Verifying email code..." VERIFY_RESPONSE=$(curl -s -X POST "$BASE_URL/auth/verify-email" \ -H "Content-Type: application/json" \ -d "{\"email\":\"$TEST_EMAIL\",\"verification_code\":\"$VERIFICATION_CODE\"}") if echo "$VERIFY_RESPONSE" | grep -q '"success":true'; then echo "✅ Email verification successful" else echo "❌ Email verification failed" echo " Response: $VERIFY_RESPONSE" fi # Test 3: User registration echo "" echo "3. Testing user registration..." RANDOM_NUM=$((RANDOM % 9999)) USERNAME="testuser_$RANDOM_NUM" REGISTER_RESPONSE=$(curl -s -X POST "$BASE_URL/auth/register" \ -H "Content-Type: application/json" \ -d "{\"username\":\"$USERNAME\",\"password\":\"Test123456\",\"nickname\":\"Test User\",\"email\":\"$TEST_EMAIL\",\"email_verification_code\":\"$VERIFICATION_CODE\"}") if echo "$REGISTER_RESPONSE" | grep -q '"success":true'; then echo "✅ User registration successful" USER_ID=$(echo "$REGISTER_RESPONSE" | grep -o '"id":"[^"]*"' | cut -d'"' -f4) REGISTERED_USERNAME=$(echo "$REGISTER_RESPONSE" | grep -o '"username":"[^"]*"' | cut -d'"' -f4) echo " User ID: $USER_ID" echo " Username: $REGISTERED_USERNAME" else echo "❌ User registration failed" echo " Response: $REGISTER_RESPONSE" REGISTERED_USERNAME="" fi # Test 4: User login if [ -n "$REGISTERED_USERNAME" ]; then echo "" echo "4. Testing user login..." LOGIN_RESPONSE=$(curl -s -X POST "$BASE_URL/auth/login" \ -H "Content-Type: application/json" \ -d "{\"identifier\":\"$REGISTERED_USERNAME\",\"password\":\"Test123456\"}") if echo "$LOGIN_RESPONSE" | grep -q '"success":true'; then echo "✅ User login successful" LOGIN_USERNAME=$(echo "$LOGIN_RESPONSE" | grep -o '"username":"[^"]*"' | cut -d'"' -f4) LOGIN_NICKNAME=$(echo "$LOGIN_RESPONSE" | grep -o '"nickname":"[^"]*"' | cut -d'"' -f4) echo " Username: $LOGIN_USERNAME" echo " Nickname: $LOGIN_NICKNAME" else echo "❌ User login failed" echo " Response: $LOGIN_RESPONSE" fi fi echo "" echo "=== Test Summary ===" echo "✅ Redis file storage: Working" echo "✅ Email test mode: Working" echo "✅ Memory user storage: Working" echo "" echo "💡 Check redis-data/redis.json for stored verification data" echo "💡 Check server console for email content output"