4.3 KiB
4.3 KiB
Docker Auth/Session Test Fixes Summary
Problem
The docker-compose.test.yml setup had 18 failing tests related to authentication session issues. The main problems were:
- Service dependency issues: Website container started before deps container finished installing
- Cookie domain problems: Mock API cookies weren't working properly in Docker environment
- Network connectivity: Website couldn't reach API due to timing and configuration issues
Root Causes
1. Missing Service Dependencies
- Website container didn't wait for deps container to complete
- API container didn't wait for deps container
- This caused "next: not found" and module resolution errors
2. Cookie Domain Issues
- Mock API set cookies without domain specification
- In Docker, cookies need proper domain settings to work across containers
- Browser at localhost:3100 couldn't access cookies from API at localhost:3101
3. Slow npm Install
- deps container took too long to install packages
- Website container would timeout waiting
- No proper health checks or completion signals
Fixes Applied
1. Updated docker-compose.test.yml
Before:
website:
depends_on:
api:
condition: service_healthy
After:
deps:
command: ["sh", "-lc", "echo '[deps] Ready' && sleep infinity"]
# Simple command that completes immediately
api:
depends_on:
deps:
condition: service_started
# Added deps dependency
website:
depends_on:
deps:
condition: service_started # Wait for deps
api:
condition: service_healthy # Wait for API
command:
- sh
- -lc
- |
# Check if node_modules exist, install if needed
if [ ! -d "node_modules" ] || [ ! -f "node_modules/.bin/next" ]; then
echo "[website] Installing dependencies..."
npm install --no-package-lock --include-workspace-root --no-audit --fund=false --prefer-offline
else
echo "[website] node_modules already present"
fi
echo "[website] Starting Next.js dev server..."
npm run dev
2. Fixed testing/mock-api-server.cjs
Before:
const cookies = [
`gp_session=${encodeURIComponent(gpSessionValue)}; Path=/; HttpOnly`,
`gridpilot_demo_mode=${encodeURIComponent(mode)}; Path=/`,
];
After:
// Set cookies with proper domain for Docker environment
const domain = 'localhost';
const cookies = [
`gp_session=${encodeURIComponent(gpSessionValue)}; Path=/; HttpOnly; Domain=${domain}`,
`gridpilot_demo_mode=${encodeURIComponent(mode)}; Path=/; Domain=${domain}`,
];
3. Verified playwright.website.config.ts
- Already correctly configured for Docker
- Uses
http://localhost:3100whenDOCKER_SMOKE=true - Proper timeout and retry settings
Key Configuration Changes
Environment Variables
API_BASE_URL=http://api:3000(internal Docker network)NEXT_PUBLIC_API_BASE_URL=http://localhost:3101(external for browser)DOCKER_SMOKE=true(tells tests to use Docker ports)
Cookie Settings
- Added
Domain=localhostto all Set-Cookie headers - Ensures cookies work across localhost:3100 and localhost:3101
Service Dependencies
- deps → api → website (proper startup order)
- Health checks ensure services are ready before dependent services start
Testing the Fixes
Quick Test
# Start services
docker-compose -f docker-compose.test.yml up -d
# Wait for startup
sleep 30
# Run tests
DOCKER_SMOKE=true npx playwright test --config=playwright.website.config.ts
Verification Steps
- Check deps container starts immediately
- API container waits for deps and becomes healthy
- Website container waits for both deps and API
- Cookies are set with proper domain
- Tests can access both website and API
Expected Results
- All 93 tests should pass
- No "next: not found" errors
- No connection refused errors
- Auth sessions work properly in Docker
- Cookie-based authentication flows correctly
Files Modified
docker-compose.test.yml- Service dependencies and startup logictesting/mock-api-server.cjs- Cookie domain settingstest-docker-fix.sh- Verification script (new)
Notes
- The fixes address the core infrastructure issues that were causing auth/session failures
- The mock API now properly simulates real authentication flows
- Docker networking is properly configured for cross-container communication