|
| 1 | +name: Auth Server Tests |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - feat/** |
| 8 | + paths: |
| 9 | + - 'auth-server/**' |
| 10 | + - '.github/workflows/test-auth.yml' |
| 11 | + pull_request: |
| 12 | + branches: |
| 13 | + - main |
| 14 | + paths: |
| 15 | + - 'auth-server/**' |
| 16 | + - '.github/workflows/test-auth.yml' |
| 17 | + |
| 18 | +jobs: |
| 19 | + test: |
| 20 | + name: Run Auth Server Tests |
| 21 | + runs-on: ubuntu-latest |
| 22 | + |
| 23 | + # PostgreSQL service for database tests |
| 24 | + services: |
| 25 | + postgres: |
| 26 | + image: postgres:16 |
| 27 | + env: |
| 28 | + POSTGRES_USER: postgres |
| 29 | + POSTGRES_PASSWORD: postgres |
| 30 | + POSTGRES_DB: auth_test |
| 31 | + options: >- |
| 32 | + --health-cmd pg_isready |
| 33 | + --health-interval 10s |
| 34 | + --health-timeout 5s |
| 35 | + --health-retries 5 |
| 36 | + ports: |
| 37 | + - 5432:5432 |
| 38 | + |
| 39 | + defaults: |
| 40 | + run: |
| 41 | + working-directory: ./auth-server |
| 42 | + |
| 43 | + steps: |
| 44 | + - name: Checkout code |
| 45 | + uses: actions/checkout@v4 |
| 46 | + |
| 47 | + - name: Setup pnpm |
| 48 | + uses: pnpm/action-setup@v4 |
| 49 | + with: |
| 50 | + version: 9 |
| 51 | + |
| 52 | + - name: Setup Node.js |
| 53 | + uses: actions/setup-node@v4 |
| 54 | + with: |
| 55 | + node-version: '20' |
| 56 | + cache: 'pnpm' |
| 57 | + cache-dependency-path: './auth-server/pnpm-lock.yaml' |
| 58 | + |
| 59 | + - name: Install dependencies |
| 60 | + run: pnpm install --frozen-lockfile |
| 61 | + |
| 62 | + - name: Setup environment variables |
| 63 | + run: | |
| 64 | + cat > .env.local << EOF |
| 65 | + # Database |
| 66 | + DATABASE_URL=postgresql://postgres:postgres@localhost:5432/auth_test |
| 67 | +
|
| 68 | + # Better Auth |
| 69 | + BETTER_AUTH_SECRET=${{ secrets.BETTER_AUTH_SECRET || 'test-secret-key-for-ci-do-not-use-in-production-min-32-chars' }} |
| 70 | + BETTER_AUTH_URL=http://localhost:3001 |
| 71 | +
|
| 72 | + # CORS Configuration |
| 73 | + ALLOWED_ORIGINS=http://localhost:3000,http://localhost:3001 |
| 74 | +
|
| 75 | + # Client-side configuration |
| 76 | + NEXT_PUBLIC_BETTER_AUTH_URL=http://localhost:3001 |
| 77 | + NEXT_PUBLIC_APP_NAME=Auth Server CI |
| 78 | + NEXT_PUBLIC_ORG_NAME=RoboLearn |
| 79 | +
|
| 80 | + # Email (test mode - no actual emails sent) |
| 81 | + |
| 82 | + EMAIL_PROVIDER=console |
| 83 | +
|
| 84 | + # Node environment |
| 85 | + NODE_ENV=test |
| 86 | +
|
| 87 | + # Disable email verification for automated testing |
| 88 | + DISABLE_EMAIL_VERIFICATION=true |
| 89 | + EOF |
| 90 | +
|
| 91 | + - name: Run database migrations |
| 92 | + run: pnpm db:push |
| 93 | + |
| 94 | + - name: Seed test data |
| 95 | + run: pnpm seed:setup |
| 96 | + |
| 97 | + - name: Start auth server in background |
| 98 | + run: | |
| 99 | + pnpm dev & |
| 100 | + echo $! > .server.pid |
| 101 | + # Wait for server to be ready (max 120 seconds for first compilation) |
| 102 | + echo "Waiting for Next.js to compile and server to be ready..." |
| 103 | + for i in {1..120}; do |
| 104 | + # Check health endpoint (verifies server AND database connection) |
| 105 | + if curl -sf http://localhost:3001/api/health > /dev/null 2>&1; then |
| 106 | + echo "✅ Server is healthy and ready!" |
| 107 | + curl -s http://localhost:3001/api/health |
| 108 | + break |
| 109 | + fi |
| 110 | + if [ $i -eq 120 ]; then |
| 111 | + echo "❌ Server failed to start within 120 seconds" |
| 112 | + echo "Last 50 lines of server logs:" |
| 113 | + tail -50 .next/trace || echo "No trace file found" |
| 114 | + exit 1 |
| 115 | + fi |
| 116 | + # Show progress every 10 seconds |
| 117 | + if [ $((i % 10)) -eq 0 ]; then |
| 118 | + echo "Still waiting... ($i/120 seconds)" |
| 119 | + fi |
| 120 | + sleep 1 |
| 121 | + done |
| 122 | +
|
| 123 | + - name: Create admin user via Better Auth API |
| 124 | + run: | |
| 125 | + echo "Creating admin user through Better Auth signup..." |
| 126 | + curl -X POST http://localhost:3001/api/auth/sign-up/email \ |
| 127 | + -H "Content-Type: application/json" \ |
| 128 | + -d '{"email":"[email protected]","password":"RoboLearnAdmin2024!SecureTest","name":"Admin User"}' \ |
| 129 | + -v || echo "Admin user might already exist (that's okay)" |
| 130 | +
|
| 131 | + - name: Run API tests |
| 132 | + run: pnpm test-api |
| 133 | + timeout-minutes: 5 |
| 134 | + |
| 135 | + - name: Run E2E tests |
| 136 | + run: pnpm test-e2e |
| 137 | + timeout-minutes: 5 |
| 138 | + |
| 139 | + - name: Stop auth server |
| 140 | + if: always() |
| 141 | + run: | |
| 142 | + if [ -f .server.pid ]; then |
| 143 | + kill $(cat .server.pid) || true |
| 144 | + rm .server.pid |
| 145 | + fi |
| 146 | +
|
| 147 | + - name: Upload test results |
| 148 | + if: always() |
| 149 | + uses: actions/upload-artifact@v4 |
| 150 | + with: |
| 151 | + name: test-results |
| 152 | + path: | |
| 153 | + auth-server/test-results.json |
| 154 | + auth-server/tests/**/*.log |
| 155 | + retention-days: 7 |
| 156 | + |
| 157 | + security-audit: |
| 158 | + name: Security Audit |
| 159 | + runs-on: ubuntu-latest |
| 160 | + |
| 161 | + defaults: |
| 162 | + run: |
| 163 | + working-directory: ./auth-server |
| 164 | + |
| 165 | + steps: |
| 166 | + - name: Checkout code |
| 167 | + uses: actions/checkout@v4 |
| 168 | + |
| 169 | + - name: Setup pnpm |
| 170 | + uses: pnpm/action-setup@v4 |
| 171 | + with: |
| 172 | + version: 9 |
| 173 | + |
| 174 | + - name: Setup Node.js |
| 175 | + uses: actions/setup-node@v4 |
| 176 | + with: |
| 177 | + node-version: '20' |
| 178 | + cache: 'pnpm' |
| 179 | + cache-dependency-path: './auth-server/pnpm-lock.yaml' |
| 180 | + |
| 181 | + - name: Install dependencies |
| 182 | + run: pnpm install --frozen-lockfile |
| 183 | + |
| 184 | + - name: Run security audit |
| 185 | + run: pnpm audit --audit-level=moderate |
| 186 | + continue-on-error: true |
| 187 | + |
| 188 | + - name: Check for outdated dependencies |
| 189 | + run: pnpm outdated |
| 190 | + continue-on-error: true |
0 commit comments