Skip to content

Commit e6cfbfb

Browse files
committed
test: add integration tests runner
1 parent a0a371c commit e6cfbfb

File tree

3 files changed

+187
-3
lines changed

3 files changed

+187
-3
lines changed

.github/workflows/nuxt4.yml

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,54 @@ jobs:
3939
npm install $(npm pack .. | tail -1)
4040
echo "export default defineNuxtConfig({ modules: ['../dist/module.mjs'] })" > nuxt.config.ts
4141
42-
- name: Build Nuxt 4 app
42+
- name: Test Nuxt 4 app builds
4343
run: |
4444
cd nuxt4-app
4545
npm run build
46+
47+
- name: Test playground integration
48+
run: |
49+
cd playground
50+
npm run build
51+
52+
# Start the built server in background
53+
node .output/server/index.mjs &
54+
SERVER_PID=$!
55+
sleep 10
56+
57+
# Test that middleware is working - protected endpoint should return 401
58+
echo "Testing protected endpoint /api/nuxt-users/me..."
59+
response=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:3000/api/nuxt-users/me")
60+
echo "Response code: $response"
61+
if [ "$response" != "401" ]; then
62+
echo "❌ Middleware NOT working - /me endpoint should return 401, got $response"
63+
kill $SERVER_PID
64+
exit 1
65+
fi
66+
echo "✅ Middleware is working - /me endpoint properly protected"
67+
68+
# Test that session endpoint is accessible (not blocked by auth)
69+
echo "Testing session endpoint /api/nuxt-users/session..."
70+
response=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:3000/api/nuxt-users/session" -X POST)
71+
echo "Response code: $response"
72+
if [ "$response" = "401" ]; then
73+
echo "❌ Session endpoint blocked by auth - should be accessible for login"
74+
kill $SERVER_PID
75+
exit 1
76+
fi
77+
echo "✅ Session endpoint is accessible (got $response - not 401 unauthorized)"
78+
79+
# Test that registration endpoint is accessible (auto-whitelisted)
80+
echo "Testing registration endpoint /api/nuxt-users/register..."
81+
response=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:3000/api/nuxt-users/register" -X POST)
82+
echo "Response code: $response"
83+
if [ "$response" = "401" ]; then
84+
echo "❌ Registration endpoint blocked by auth - auto-whitelisting not working"
85+
kill $SERVER_PID
86+
exit 1
87+
fi
88+
echo "✅ Registration endpoint is accessible (got $response - not 401 unauthorized)"
89+
90+
# Clean up
91+
kill $SERVER_PID
92+
echo "🎉 All playground integration tests passed!"

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,14 @@
6666
"lint": "eslint .",
6767
"lint:fix": "eslint . --fix",
6868
"pretest": "node -v | grep -q 'v22' || (echo 'Please use Node.js v22 for testing' && exit 1)",
69-
"test:release": "yarn test:types && yarn test:unit && yarn test:sqlite && yarn build && yarn lint:fix",
69+
"test:release": "yarn test:types && yarn test:unit && yarn test:integration && yarn test:sqlite && yarn build && yarn lint:fix",
7070
"test:types": "yarn dev:prepare && vue-tsc --noEmit && cd playground && vue-tsc --noEmit",
7171
"test:unit": "./scripts/test-unit.sh",
72+
"test:integration": "./scripts/test-integration.sh",
7273
"test:sqlite": "./scripts/test-sqlite.sh",
7374
"test:mysql": "./scripts/test-mysql.sh",
7475
"test:postgresql": "./scripts/test-postgresql.sh",
75-
"test": "yarn test:types && yarn test:unit && yarn test:sqlite && yarn test:mysql && yarn test:postgresql",
76+
"test": "yarn test:types && yarn test:unit && yarn test:integration && yarn test:sqlite && yarn test:mysql && yarn test:postgresql",
7677
"test:watch": "vitest watch",
7778
"db:create-users-table": "tsx src/cli/create-users-table.ts",
7879
"db:create-user": "tsx src/cli/create-user.ts",

scripts/test-integration.sh

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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

Comments
 (0)