|
| 1 | +import type { Page } from '@playwright/test'; |
| 2 | +import { expect } from '@playwright/test'; |
| 3 | +import type { Event } from '@sentry/core'; |
| 4 | + |
| 5 | +import { sentryTest } from '../../../../utils/fixtures'; |
| 6 | +import { |
| 7 | + getFirstSentryEnvelopeRequest, |
| 8 | + getMultipleSentryEnvelopeRequests, |
| 9 | + shouldSkipTracingTest, |
| 10 | +} from '../../../../utils/helpers'; |
| 11 | + |
| 12 | +async function mockSupabaseAuthRoutesSuccess(page: Page) { |
| 13 | + await page.route('**/auth/v1/token?grant_type=password**', route => { |
| 14 | + return route.fulfill({ |
| 15 | + status: 200, |
| 16 | + body: JSON.stringify({ |
| 17 | + access_token: 'test-access-token', |
| 18 | + refresh_token: 'test-refresh-token', |
| 19 | + token_type: 'bearer', |
| 20 | + expires_in: 3600, |
| 21 | + }), |
| 22 | + headers: { |
| 23 | + 'Content-Type': 'application/json', |
| 24 | + }, |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + await page.route('**/auth/v1/logout**', route => { |
| 29 | + return route.fulfill({ |
| 30 | + status: 200, |
| 31 | + body: JSON.stringify({ |
| 32 | + message: 'Logged out', |
| 33 | + }), |
| 34 | + headers: { |
| 35 | + 'Content-Type': 'application/json', |
| 36 | + }, |
| 37 | + }); |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +async function mockSupabaseAuthRoutesFailure(page: Page) { |
| 42 | + await page.route('**/auth/v1/token?grant_type=password**', route => { |
| 43 | + return route.fulfill({ |
| 44 | + status: 400, |
| 45 | + body: JSON.stringify({ |
| 46 | + error_description: 'Invalid email or password', |
| 47 | + error: 'invalid_grant', |
| 48 | + }), |
| 49 | + headers: { |
| 50 | + 'Content-Type': 'application/json', |
| 51 | + }, |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + await page.route('**/auth/v1/logout**', route => { |
| 56 | + return route.fulfill({ |
| 57 | + status: 400, |
| 58 | + body: JSON.stringify({ |
| 59 | + error_description: 'Invalid refresh token', |
| 60 | + error: 'invalid_grant', |
| 61 | + }), |
| 62 | + headers: { |
| 63 | + 'Content-Type': 'application/json', |
| 64 | + }, |
| 65 | + }); |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +const bundle = process.env.PW_BUNDLE || ''; |
| 71 | +// We only want to run this in non-CDN bundle mode |
| 72 | +if (bundle.startsWith('bundle')) { |
| 73 | + sentryTest.skip(); |
| 74 | +} |
| 75 | + |
| 76 | +sentryTest('should capture Supabase authentication spans', async ({ getLocalTestUrl, page }) => { |
| 77 | + if (shouldSkipTracingTest()) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + await mockSupabaseAuthRoutesSuccess(page); |
| 82 | + |
| 83 | + const url = await getLocalTestUrl({ testDir: __dirname }); |
| 84 | + |
| 85 | + const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); |
| 86 | + const supabaseSpans = eventData.spans?.filter(({ op }) => op?.startsWith('db.auth')); |
| 87 | + |
| 88 | + expect(supabaseSpans).toHaveLength(2); |
| 89 | + expect(supabaseSpans![0]).toMatchObject({ |
| 90 | + description: 'signInWithPassword', |
| 91 | + parent_span_id: eventData.contexts?.trace?.span_id, |
| 92 | + span_id: expect.any(String), |
| 93 | + start_timestamp: expect.any(Number), |
| 94 | + timestamp: expect.any(Number), |
| 95 | + trace_id: eventData.contexts?.trace?.trace_id, |
| 96 | + status: 'ok', |
| 97 | + data: expect.objectContaining({ |
| 98 | + 'sentry.op': 'db.auth.signInWithPassword', |
| 99 | + 'sentry.origin': 'auto.db.supabase', |
| 100 | + }), |
| 101 | + }); |
| 102 | + |
| 103 | + expect(supabaseSpans![1]).toMatchObject({ |
| 104 | + description: 'signOut', |
| 105 | + parent_span_id: eventData.contexts?.trace?.span_id, |
| 106 | + span_id: expect.any(String), |
| 107 | + start_timestamp: expect.any(Number), |
| 108 | + timestamp: expect.any(Number), |
| 109 | + trace_id: eventData.contexts?.trace?.trace_id, |
| 110 | + status: 'ok', |
| 111 | + data: expect.objectContaining({ |
| 112 | + 'sentry.op': 'db.auth.signOut', |
| 113 | + 'sentry.origin': 'auto.db.supabase', |
| 114 | + }), |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +sentryTest('should capture Supabase authentication errors', async ({ getLocalTestUrl, page }) => { |
| 119 | + if (shouldSkipTracingTest()) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + await mockSupabaseAuthRoutesFailure(page); |
| 124 | + |
| 125 | + const url = await getLocalTestUrl({ testDir: __dirname }); |
| 126 | + |
| 127 | + const [errorEvent, transactionEvent] = await getMultipleSentryEnvelopeRequests<Event>(page, 2, { url }); |
| 128 | + |
| 129 | + const supabaseSpans = transactionEvent.spans?.filter(({ op }) => op?.startsWith('db.auth')); |
| 130 | + |
| 131 | + expect(errorEvent.exception?.values?.[0].value).toBe('Invalid email or password'); |
| 132 | + |
| 133 | + expect(supabaseSpans).toHaveLength(2); |
| 134 | + expect(supabaseSpans![0]).toMatchObject({ |
| 135 | + description: 'signInWithPassword', |
| 136 | + parent_span_id: transactionEvent.contexts?.trace?.span_id, |
| 137 | + span_id: expect.any(String), |
| 138 | + start_timestamp: expect.any(Number), |
| 139 | + timestamp: expect.any(Number), |
| 140 | + trace_id: transactionEvent.contexts?.trace?.trace_id, |
| 141 | + status: 'unknown_error', |
| 142 | + data: expect.objectContaining({ |
| 143 | + 'sentry.op': 'db.auth.signInWithPassword', |
| 144 | + 'sentry.origin': 'auto.db.supabase', |
| 145 | + }), |
| 146 | + }); |
| 147 | +}); |
0 commit comments