|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Integration test script for nuxt-users module |
| 4 | +# Tests playground middleware functionality locally |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +echo "🚀 Starting nuxt-users integration tests..." |
| 9 | + |
| 10 | +# Colors for output |
| 11 | +RED='\033[0;31m' |
| 12 | +GREEN='\033[0;32m' |
| 13 | +YELLOW='\033[1;33m' |
| 14 | +BLUE='\033[0;34m' |
| 15 | +NC='\033[0m' # No Color |
| 16 | + |
| 17 | +# Function to print colored output |
| 18 | +print_status() { |
| 19 | + local color=$1 |
| 20 | + local message=$2 |
| 21 | + echo -e "${color}${message}${NC}" |
| 22 | +} |
| 23 | + |
| 24 | +# Function to cleanup background processes |
| 25 | +cleanup() { |
| 26 | + if [ ! -z "$SERVER_PID" ]; then |
| 27 | + print_status $YELLOW "🧹 Cleaning up server (PID: $SERVER_PID)..." |
| 28 | + kill $SERVER_PID 2>/dev/null || true |
| 29 | + wait $SERVER_PID 2>/dev/null || true |
| 30 | + fi |
| 31 | +} |
| 32 | + |
| 33 | +# Set up trap for cleanup on script exit |
| 34 | +trap cleanup EXIT |
| 35 | + |
| 36 | +# Check if we're in the right directory |
| 37 | +if [ ! -f "package.json" ] || [ ! -d "playground" ]; then |
| 38 | + print_status $RED "❌ Please run this script from the nuxt-users root directory" |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +print_status $BLUE "📦 Preparing development environment..." |
| 43 | + |
| 44 | +# Prepare the module |
| 45 | +npm run dev:prepare |
| 46 | + |
| 47 | +print_status $BLUE "🏗️ Building playground..." |
| 48 | + |
| 49 | +# Build the playground |
| 50 | +cd playground |
| 51 | +npm run build |
| 52 | + |
| 53 | +print_status $BLUE "🖥️ Starting production server..." |
| 54 | + |
| 55 | +# Start the server in background |
| 56 | +node .output/server/index.mjs & |
| 57 | +SERVER_PID=$! |
| 58 | + |
| 59 | +# Wait for server to start |
| 60 | +sleep 3 |
| 61 | + |
| 62 | +# Check if server is running |
| 63 | +if ! kill -0 $SERVER_PID 2>/dev/null; then |
| 64 | + print_status $RED "❌ Server failed to start" |
| 65 | + exit 1 |
| 66 | +fi |
| 67 | + |
| 68 | +print_status $GREEN "✅ Server started successfully (PID: $SERVER_PID)" |
| 69 | + |
| 70 | +# Wait a bit more for server to be fully ready |
| 71 | +sleep 2 |
| 72 | + |
| 73 | +print_status $BLUE "🧪 Running integration tests..." |
| 74 | + |
| 75 | +# Test 1: Protected endpoint should return 401 |
| 76 | +print_status $YELLOW "🔐 Testing protected endpoint /api/nuxt-users/me..." |
| 77 | +response=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:3000/api/nuxt-users/me" || echo "000") |
| 78 | + |
| 79 | +if [ "$response" = "401" ]; then |
| 80 | + print_status $GREEN "✅ Middleware working - protected endpoint returns 401" |
| 81 | +else |
| 82 | + print_status $RED "❌ Middleware NOT working - expected 401, got $response" |
| 83 | + print_status $RED " This means server middleware is not properly registered!" |
| 84 | + exit 1 |
| 85 | +fi |
| 86 | + |
| 87 | +# Test 2: Session endpoint should be accessible (not 401) |
| 88 | +print_status $YELLOW "📝 Testing session endpoint /api/nuxt-users/session..." |
| 89 | +response=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:3000/api/nuxt-users/session" -X POST || echo "000") |
| 90 | + |
| 91 | +if [ "$response" != "401" ]; then |
| 92 | + print_status $GREEN "✅ Session endpoint accessible (HTTP $response - auth not blocking)" |
| 93 | +else |
| 94 | + print_status $RED "❌ Session endpoint blocked by auth - should be accessible for login" |
| 95 | + exit 1 |
| 96 | +fi |
| 97 | + |
| 98 | +# Test 3: Registration endpoint should be auto-whitelisted |
| 99 | +print_status $YELLOW "📋 Testing registration endpoint /api/nuxt-users/register..." |
| 100 | +response=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:3000/api/nuxt-users/register" -X POST || echo "000") |
| 101 | + |
| 102 | +if [ "$response" != "401" ]; then |
| 103 | + print_status $GREEN "✅ Registration endpoint accessible (HTTP $response - auto-whitelisted)" |
| 104 | +else |
| 105 | + print_status $RED "❌ Registration endpoint blocked - auto-whitelisting not working" |
| 106 | + exit 1 |
| 107 | +fi |
| 108 | + |
| 109 | +# Test 4: Test a few more protected endpoints if they exist |
| 110 | +print_status $YELLOW "🔍 Testing other protected endpoints..." |
| 111 | + |
| 112 | +endpoints=( |
| 113 | + "/api/nuxt-users/profile" |
| 114 | + "/api/nuxt-users/users" |
| 115 | +) |
| 116 | + |
| 117 | +for endpoint in "${endpoints[@]}"; do |
| 118 | + response=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:3000$endpoint" || echo "000") |
| 119 | + |
| 120 | + if [ "$response" = "401" ]; then |
| 121 | + print_status $GREEN "✅ $endpoint properly protected (401)" |
| 122 | + elif [ "$response" = "404" ]; then |
| 123 | + print_status $YELLOW "⚠️ $endpoint not found (404) - endpoint may not exist" |
| 124 | + else |
| 125 | + print_status $YELLOW "⚠️ $endpoint returned $response - verify this is expected" |
| 126 | + fi |
| 127 | +done |
| 128 | + |
| 129 | +print_status $GREEN "🎉 All integration tests passed!" |
| 130 | +print_status $BLUE "📊 Test Summary:" |
| 131 | +print_status $BLUE " • Server middleware registration: ✅ Working" |
| 132 | +print_status $BLUE " • Authentication enforcement: ✅ Working" |
| 133 | +print_status $BLUE " • Auto-whitelisting: ✅ Working" |
| 134 | +print_status $BLUE " • Endpoint accessibility: ✅ Working" |
| 135 | + |
| 136 | +print_status $GREEN "✨ Your nuxt-users module is ready for production!" |
0 commit comments