Skip to content

Commit 2aa01f9

Browse files
committed
docs(auth): add context-aware validation documentation
- Document context-aware authentication validation system - Add sections on context detection and validation scope differences - Update script documentation with context parameters - Add debugging guide for context-related validation issues - Include context-specific failure patterns and solutions Context-aware validation automatically detects build vs E2E environments and runs appropriate validations for each context.
1 parent 26d1aa6 commit 2aa01f9

File tree

2 files changed

+232
-1
lines changed

2 files changed

+232
-1
lines changed

docs/testing/AUTHENTICATION_TROUBLESHOOTING.md

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,185 @@ if (navigationTime > profile.maxDelay) {
294294
}
295295
```
296296

297+
## Context-Aware Authentication Validation
298+
299+
GitPulse implements intelligent context-aware authentication validation that automatically detects the execution environment and runs appropriate validations for each context.
300+
301+
### Validation Contexts
302+
303+
The system supports four validation contexts:
304+
305+
1. **Build Context** (`build`)
306+
- **When**: CI build environments without E2E setup
307+
- **Validations**: Environment variables, NextAuth configuration, endpoint health
308+
- **Purpose**: Verify core authentication components are working for build steps
309+
310+
2. **E2E Context** (`e2e`)
311+
- **When**: Full E2E test execution with storage state available
312+
- **Validations**: All build validations + storage state, JWT structure, session API, authentication flow
313+
- **Purpose**: Comprehensive authentication flow validation
314+
315+
3. **Monitoring Context** (`monitoring`)
316+
- **When**: Authentication health monitoring workflows
317+
- **Validations**: Core authentication components (same as build)
318+
- **Purpose**: Periodic health checks without E2E overhead
319+
320+
4. **Local Context** (`local`)
321+
- **When**: Development environment testing
322+
- **Validations**: Core authentication components
323+
- **Purpose**: Developer testing and debugging
324+
325+
### Context Detection Logic
326+
327+
The validation system automatically detects context using:
328+
329+
```bash
330+
# Manual context specification
331+
node scripts/ci/validate-auth-tokens.js http://localhost:3000 30000 build
332+
333+
# Auto-detection (default)
334+
node scripts/ci/validate-auth-tokens.js http://localhost:3000 30000 auto
335+
```
336+
337+
**Auto-detection rules:**
338+
1. **E2E context**: `e2e/storageState.json` exists OR `AUTH_CONTEXT=e2e`
339+
2. **Build context**: `CI=true` without storage state
340+
3. **Monitoring context**: `AUTH_CONTEXT=monitoring`
341+
4. **Local context**: Development environment (fallback)
342+
343+
### Context-Specific Validation Behavior
344+
345+
#### Build Context Validation
346+
```bash
347+
# Only core validations run
348+
✅ Environment Variables Validation
349+
✅ NextAuth Configuration Validation
350+
✅ Authentication Endpoints Validation
351+
⏭️ Storage State Validation (skipped)
352+
⏭️ JWT Token Structure Validation (skipped)
353+
⏭️ Session API Response Validation (skipped)
354+
⏭️ End-to-End Authentication Flow Validation (skipped)
355+
```
356+
357+
#### E2E Context Validation
358+
```bash
359+
# All validations run
360+
✅ Environment Variables Validation
361+
✅ NextAuth Configuration Validation
362+
✅ Authentication Endpoints Validation
363+
✅ Storage State Validation
364+
✅ JWT Token Structure Validation
365+
✅ Session API Response Validation
366+
✅ End-to-End Authentication Flow Validation
367+
```
368+
369+
### Context-Related Issues
370+
371+
#### Issue: Wrong Context Detection
372+
**Symptoms:**
373+
- Build workflows fail with "Storage state file not found" errors
374+
- E2E tests skip important validations
375+
376+
**Diagnosis:**
377+
```bash
378+
# Check what context is being detected
379+
node scripts/ci/validate-auth-tokens.js http://localhost:3000 30000 auto
380+
# Look for: "Detected [context] context" in output
381+
382+
# Check environment indicators
383+
echo "CI: $CI"
384+
echo "AUTH_CONTEXT: $AUTH_CONTEXT"
385+
ls -la e2e/storageState.json
386+
```
387+
388+
**Solutions:**
389+
1. **Explicit context**: Pass context parameter to validation script
390+
2. **Environment setup**: Ensure `AUTH_CONTEXT` is set in CI workflows
391+
3. **File cleanup**: Remove stale storage state files if they cause wrong detection
392+
393+
#### Issue: Context Mismatch in CI
394+
**Symptoms:**
395+
- Authentication validation passes locally but fails in CI
396+
- Different validation results between workflows
397+
398+
**Diagnosis:**
399+
```bash
400+
# Compare context detection between environments
401+
CI=true node scripts/ci/validate-auth-tokens.js http://localhost:3000 30000 auto
402+
node scripts/ci/validate-auth-tokens.js http://localhost:3000 30000 auto
403+
404+
# Check composite action context passing
405+
grep -A 5 "validate-auth-tokens" .github/actions/auth-setup/action.yml
406+
```
407+
408+
**Solutions:**
409+
1. **Composite action update**: Ensure context is passed to validation script
410+
2. **Environment alignment**: Set `AUTH_CONTEXT` environment variable
411+
3. **Manual override**: Specify context explicitly in problematic workflows
412+
413+
### Debugging Context Issues
414+
415+
#### Context Detection Debugging
416+
```typescript
417+
// In validation script
418+
console.log('Context detection factors:');
419+
console.log(' hasStorageState:', fs.existsSync('e2e/storageState.json'));
420+
console.log(' isCI:', process.env.CI === 'true');
421+
console.log(' authContextEnv:', process.env.AUTH_CONTEXT);
422+
console.log(' Detected context:', detectedContext);
423+
```
424+
425+
#### Validation Scope Verification
426+
```bash
427+
# Check what validations run for each context
428+
echo "=== Build Context ==="
429+
node scripts/ci/validate-auth-tokens.js http://localhost:3000 30000 build | grep "Will run"
430+
431+
echo "=== E2E Context ==="
432+
node scripts/ci/validate-auth-tokens.js http://localhost:3000 30000 e2e | grep "Will run"
433+
```
434+
435+
#### Context Override Testing
436+
```bash
437+
# Test different contexts manually
438+
for context in build e2e monitoring local; do
439+
echo "=== Testing $context context ==="
440+
NODE_ENV=test E2E_MOCK_AUTH_ENABLED=true NEXTAUTH_SECRET=test \
441+
node scripts/ci/validate-auth-tokens.js http://localhost:3000 10000 $context
442+
done
443+
```
444+
445+
### Context Configuration in CI
446+
447+
#### Composite Action Integration
448+
The authentication setup composite action automatically passes context:
449+
450+
```yaml
451+
# In .github/actions/auth-setup/action.yml
452+
- name: Validate Authentication Configuration
453+
run: |
454+
node scripts/ci/validate-auth-tokens.js http://localhost:3000 ${{ inputs.health_check_timeout }} ${{ inputs.auth_context }}
455+
env:
456+
AUTH_CONTEXT: ${{ inputs.auth_context }}
457+
```
458+
459+
#### Workflow Context Settings
460+
Ensure workflows pass appropriate context:
461+
462+
```yaml
463+
# Build-focused workflow
464+
- name: Setup Authentication Environment
465+
uses: ./.github/actions/auth-setup
466+
with:
467+
auth_context: "ci" # Will use build context
468+
469+
# E2E-focused workflow
470+
- name: Setup Authentication Environment
471+
uses: ./.github/actions/auth-setup
472+
with:
473+
auth_context: "e2e" # Will use E2E context
474+
```
475+
297476
## CI Workflow Configuration Issues
298477
299478
### Authentication Configuration Validation

docs/testing/auth-debug-quickref.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,48 @@ for var in NEXTAUTH_URL NEXTAUTH_SECRET E2E_MOCK_AUTH_ENABLED; do
2727
done
2828
```
2929

30+
## 🎯 Context-Aware Validation
31+
32+
### Context Testing Commands
33+
```bash
34+
# Test specific validation context
35+
node scripts/ci/validate-auth-tokens.js http://localhost:3000 30000 build
36+
node scripts/ci/validate-auth-tokens.js http://localhost:3000 30000 e2e
37+
38+
# Auto-detect context (default)
39+
node scripts/ci/validate-auth-tokens.js http://localhost:3000 30000 auto
40+
41+
# Test all contexts
42+
for context in build e2e monitoring local; do
43+
echo "=== $context context ==="
44+
NODE_ENV=test E2E_MOCK_AUTH_ENABLED=true NEXTAUTH_SECRET=test \
45+
node scripts/ci/validate-auth-tokens.js http://localhost:3000 10000 $context
46+
done
47+
```
48+
49+
### Context Detection Debugging
50+
```bash
51+
# Check context detection factors
52+
echo "CI: $CI"
53+
echo "AUTH_CONTEXT: $AUTH_CONTEXT"
54+
ls -la e2e/storageState.json
55+
56+
# Debug context detection
57+
NODE_ENV=test CI=true node scripts/ci/validate-auth-tokens.js http://localhost:3000 10000 auto | grep "Detected"
58+
```
59+
60+
### Context-Specific Issues
61+
```bash
62+
# Build context: Should skip E2E validations
63+
# ✅ 3 tests (env, config, endpoints) - ⏭️ 4 tests skipped
64+
65+
# E2E context: Should run all validations
66+
# ✅ 7 tests (all validations)
67+
68+
# Check which validations run per context
69+
node scripts/ci/validate-auth-tokens.js http://localhost:3000 30000 build | grep "Will run"
70+
```
71+
3072
## 🔧 Configuration Debugging
3173

3274
### Validate Configuration
@@ -169,6 +211,14 @@ COMPOSITE_ACTION_DRIFT: Workflow uses authentication but not composite actions
169211
```
170212
**Fix:** Check artifacts, run local validation, fix drift issues
171213
214+
### Context Validation Failures
215+
```
216+
❌ Storage state file not found at e2e/storageState.json
217+
❌ Build context validation failed for storage state
218+
❌ Wrong context detected: expected e2e, got build
219+
```
220+
**Fix:** Check context detection, verify AUTH_CONTEXT environment variable, ensure proper context in CI
221+
172222
### Test Authentication Failures
173223
```
174224
Authentication lost after navigation
@@ -225,7 +275,9 @@ NODE_ENV=test E2E_MOCK_AUTH_ENABLED=true npm run test:e2e -- --project=setup
225275
### Scripts and Utilities
226276
- `scripts/ci/validate-auth-configuration.js` - Configuration validation
227277
- `scripts/check-auth-health.js` - Authentication endpoint health check
228-
- `scripts/ci/validate-auth-tokens.js` - JWT token validation
278+
- `scripts/ci/validate-auth-tokens.js` - Context-aware JWT token and authentication validation
279+
- Usage: `node scripts/ci/validate-auth-tokens.js <url> <timeout> [context]`
280+
- Contexts: `build`, `e2e`, `monitoring`, `local`, `auto` (default)
229281
- `scripts/ci/verify-nextauth-initialization.js` - NextAuth initialization verification
230282

231283
### Test Helpers

0 commit comments

Comments
 (0)