141 lines
5.0 KiB
Bash
141 lines
5.0 KiB
Bash
#!/bin/bash
|
|
# =============================================================================
|
|
# Presence Service - Quick Test Script
|
|
# =============================================================================
|
|
# Quickly verify core API functionality in local environment.
|
|
# =============================================================================
|
|
|
|
set -e
|
|
|
|
echo "🚀 Starting quick API tests..."
|
|
echo ""
|
|
|
|
# Colors
|
|
GREEN='\033[0;32m'
|
|
RED='\033[0;31m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m'
|
|
|
|
BASE_URL="http://localhost:3011/api/v1"
|
|
|
|
# Test counters
|
|
PASS=0
|
|
FAIL=0
|
|
|
|
# Test function
|
|
test_api() {
|
|
local test_name=$1
|
|
local method=$2
|
|
local endpoint=$3
|
|
local data=$4
|
|
local expected_status=$5
|
|
local token=$6
|
|
|
|
echo -n "Testing: $test_name ... "
|
|
|
|
if [ -n "$token" ]; then
|
|
response=$(curl -s -w "\n%{http_code}" -X $method "$BASE_URL$endpoint" \
|
|
-H "Content-Type: application/json" \
|
|
-H "Authorization: Bearer $token" \
|
|
-d "$data" 2>/dev/null)
|
|
elif [ -n "$data" ]; then
|
|
response=$(curl -s -w "\n%{http_code}" -X $method "$BASE_URL$endpoint" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$data" 2>/dev/null)
|
|
else
|
|
response=$(curl -s -w "\n%{http_code}" -X $method "$BASE_URL$endpoint" \
|
|
-H "Content-Type: application/json" 2>/dev/null)
|
|
fi
|
|
|
|
status=$(echo "$response" | tail -n1)
|
|
body=$(echo "$response" | head -n-1)
|
|
|
|
if [ "$status" -eq "$expected_status" ]; then
|
|
echo -e "${GREEN}✓ PASS${NC}"
|
|
PASS=$((PASS + 1))
|
|
if [ -n "$body" ]; then
|
|
echo "$body" | head -c 200
|
|
echo ""
|
|
fi
|
|
else
|
|
echo -e "${RED}✗ FAIL${NC} (Expected: $expected_status, Got: $status)"
|
|
FAIL=$((FAIL + 1))
|
|
echo "$body"
|
|
fi
|
|
echo ""
|
|
}
|
|
|
|
# Generate test data
|
|
INSTALL_ID="test-install-$(date +%s)"
|
|
USER_ID="test-user-$(date +%s)"
|
|
|
|
# =============================================================================
|
|
# 1. Health Check
|
|
# =============================================================================
|
|
echo -e "${YELLOW}=== 1. Health Check ===${NC}"
|
|
test_api "Health endpoint" "GET" "/health" "" 200 ""
|
|
|
|
# =============================================================================
|
|
# 2. Presence API (requires auth - should return 401)
|
|
# =============================================================================
|
|
echo -e "${YELLOW}=== 2. Presence API (Auth Required) ===${NC}"
|
|
test_api "Heartbeat without auth" "POST" "/presence/heartbeat" \
|
|
"{\"installId\": \"$INSTALL_ID\"}" \
|
|
401 ""
|
|
|
|
test_api "Online count without auth" "GET" "/presence/online-count" "" 401 ""
|
|
|
|
test_api "Online history without auth" "GET" "/presence/online-history?startDate=2024-01-01&endDate=2024-01-31" "" 401 ""
|
|
|
|
# =============================================================================
|
|
# 3. Analytics API (requires auth - should return 401)
|
|
# =============================================================================
|
|
echo -e "${YELLOW}=== 3. Analytics API (Auth Required) ===${NC}"
|
|
test_api "Track event without auth" "POST" "/analytics/events" \
|
|
"{\"installId\": \"$INSTALL_ID\", \"eventName\": \"app_open\", \"eventData\": {}}" \
|
|
401 ""
|
|
|
|
test_api "DAU stats without auth" "GET" "/analytics/dau?date=2024-01-01" "" 401 ""
|
|
|
|
# =============================================================================
|
|
# 4. API with Mock Token (will fail auth validation but test routing)
|
|
# =============================================================================
|
|
echo -e "${YELLOW}=== 4. API Routing Test (Invalid Token) ===${NC}"
|
|
FAKE_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IlRlc3QiLCJpYXQiOjE1MTYyMzkwMjJ9.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
|
|
|
|
test_api "Heartbeat with invalid token" "POST" "/presence/heartbeat" \
|
|
"{\"installId\": \"$INSTALL_ID\"}" \
|
|
401 "$FAKE_TOKEN"
|
|
|
|
# =============================================================================
|
|
# 5. Error Handling
|
|
# =============================================================================
|
|
echo -e "${YELLOW}=== 5. Error Handling ===${NC}"
|
|
test_api "Non-existent endpoint" "GET" "/non-existent" "" 404 ""
|
|
|
|
test_api "Invalid JSON body" "POST" "/analytics/events" \
|
|
"invalid-json" \
|
|
400 ""
|
|
|
|
# =============================================================================
|
|
# Summary
|
|
# =============================================================================
|
|
echo ""
|
|
echo -e "${YELLOW}======================================${NC}"
|
|
echo -e "${YELLOW}Test Summary${NC}"
|
|
echo -e "${GREEN}Passed: $PASS${NC}"
|
|
echo -e "${RED}Failed: $FAIL${NC}"
|
|
echo -e "${YELLOW}======================================${NC}"
|
|
|
|
if [ $FAIL -eq 0 ]; then
|
|
echo -e "${GREEN}✓ All tests passed!${NC}"
|
|
echo ""
|
|
echo -e "${BLUE}Note:${NC} Most endpoints require authentication."
|
|
echo "For full testing, use: npm run test:e2e"
|
|
exit 0
|
|
else
|
|
echo -e "${RED}✗ Some tests failed!${NC}"
|
|
exit 1
|
|
fi
|