|
1 | 1 | import { expect, test } from '@playwright/test'; |
| 2 | +import { authenticateTestUser } from './utils/auth-helpers'; |
2 | 3 |
|
3 | 4 | test('simple auth flow', async ({ page, context, browserName }) => { |
4 | | - // Create a test user and authenticate |
5 | | - const testEmail = `test-${Date.now()}@example.com`; |
6 | | - |
7 | | - console.log(`[${browserName}] Starting test with email: ${testEmail}`); |
8 | | - |
9 | | - const response = await context.request.post('http://localhost:3000/api/auth/test-login', { |
10 | | - data: { |
11 | | - email: testEmail, |
12 | | - name: 'Test User', |
13 | | - }, |
14 | | - timeout: 30000, // 30 second timeout |
| 5 | + const testEmail = `test-${Date.now()}-${Math.random().toString(36).substring(7)}-${browserName}@example.com`; |
| 6 | + |
| 7 | + // Authenticate user |
| 8 | + await authenticateTestUser(page, { |
| 9 | + email: testEmail, |
| 10 | + name: 'Test User', |
| 11 | + skipOrg: false, |
| 12 | + hasAccess: true, |
15 | 13 | }); |
16 | 14 |
|
17 | | - // Add debugging for all browsers |
18 | | - if (!response.ok()) { |
19 | | - console.error(`[${browserName}] Test login failed:`, { |
20 | | - status: response.status(), |
21 | | - statusText: response.statusText(), |
22 | | - }); |
23 | | - try { |
24 | | - const body = await response.text(); |
25 | | - console.error(`[${browserName}] Response body:`, body); |
26 | | - } catch (e) { |
27 | | - console.error(`[${browserName}] Could not read response body`); |
28 | | - } |
29 | | - } |
30 | | - |
31 | | - expect(response.ok()).toBeTruthy(); |
32 | | - const data = await response.json(); |
33 | | - expect(data.success).toBe(true); |
34 | | - expect(data.user).toBeDefined(); |
35 | | - expect(data.user.email).toBe(testEmail); |
36 | | - expect(data.user.emailVerified).toBe(true); |
37 | | - |
38 | 15 | // Verify session cookie was set |
39 | 16 | const cookies = await context.cookies(); |
40 | 17 | const sessionCookie = cookies.find((c) => c.name === 'better-auth.session_token'); |
41 | 18 | expect(sessionCookie).toBeDefined(); |
42 | | - expect(sessionCookie?.httpOnly).toBe(true); |
43 | 19 |
|
44 | | - // Navigate to auth page - should be redirected since we're authenticated |
45 | | - await page.goto('http://localhost:3000/auth', { waitUntil: 'domcontentloaded' }); |
| 20 | + // Navigate to root first to let the user settle into their authenticated state |
| 21 | + await page.goto('/', { waitUntil: 'domcontentloaded' }); |
| 22 | + await page.waitForTimeout(3000); // Wait for all redirects to complete |
| 23 | + |
| 24 | + const afterRootUrl = page.url(); |
| 25 | + console.log('URL after navigating to root:', afterRootUrl); |
| 26 | + |
| 27 | + // Now navigate to auth page - should be redirected since we're authenticated |
| 28 | + await page.goto('/auth', { waitUntil: 'domcontentloaded' }); |
46 | 29 |
|
47 | | - // Wait for the redirect to happen |
48 | | - // Since we know we should be redirected, wait for URL change |
49 | | - let retries = 0; |
50 | | - const maxRetries = 10; |
| 30 | + // Wait for redirect away from auth |
| 31 | + await page.waitForURL((url) => !url.toString().includes('/auth'), { timeout: 5000 }); |
51 | 32 |
|
52 | | - while (page.url().includes('/auth') && retries < maxRetries) { |
53 | | - await page.waitForTimeout(500); |
54 | | - retries++; |
| 33 | + // If we're on root, wait for the subsequent redirect to final destination |
| 34 | + if (new URL(page.url()).pathname === '/') { |
| 35 | + console.log('On root route, waiting for final redirect...'); |
| 36 | + await page.waitForURL((url) => new URL(url).pathname !== '/', { timeout: 5000 }); |
55 | 37 | } |
56 | 38 |
|
57 | 39 | const currentUrl = page.url(); |
| 40 | + console.log('Final URL after auth redirect:', currentUrl); |
58 | 41 |
|
59 | | - // Verify we're redirected to an authenticated route |
60 | 42 | expect(currentUrl).not.toContain('/auth'); |
61 | 43 |
|
62 | | - // Common authenticated routes include /setup, /dashboard, /upgrade, or organization-specific routes |
| 44 | + // User should be on one of these meaningful authenticated routes |
63 | 45 | const isAuthenticatedRoute = |
64 | | - currentUrl.includes('/setup') || |
65 | | - currentUrl.includes('/dashboard') || |
66 | | - currentUrl.includes('/upgrade') || |
67 | | - currentUrl.includes('/org_'); |
| 46 | + currentUrl.includes('/setup') || // Setup flow |
| 47 | + currentUrl.match(/\/org_[a-zA-Z0-9]+\//) !== null || // Organization pages |
| 48 | + currentUrl.includes('/upgrade') || // Upgrade page |
| 49 | + currentUrl.includes('/no-access') || // No access page |
| 50 | + currentUrl.includes('/onboarding'); // Onboarding flow |
68 | 51 |
|
69 | 52 | expect(isAuthenticatedRoute).toBeTruthy(); |
70 | 53 | }); |
0 commit comments