mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem Nothing is tracked on the API keys pages (new and legacy), so we can't tell which keys people copy, how often they create or delete keys, or how quickly projects disable legacy keys. Closes FE-5030. ## Solution Adds five events, defined in `packages/common/telemetry-constants.ts` and fired through `useTrack()`: | Event | When | Properties | | --- | --- | --- | | `api_key_copied` | User copies a publishable, secret, or legacy anon/service_role key with its Copy button. Keyboard copies aren't counted. Not fired when blocked by permissions or when the copy fails. | `keyType` | | `api_key_reveal_clicked` | User reveals a secret or legacy service_role key (not on hide) | `keyType` | | `api_key_created` | Key created from the dialog, or by the first-time setup (fires once per key) | `keyType`, `source` | | `api_key_removed` | Key deleted after confirming in the modal | `keyType` | | `legacy_api_keys_updated` | Legacy keys disabled or re-enabled | `enabled` | - Create, delete, and legacy toggle fire in the mutation hook's `onSuccess`, so failed attempts aren't counted and the event still fires if the component unmounts mid-request. They're attributed to the mutation's own project. Copy and reveal fire in the click handler. - Delete is tracked in `SecretAPIKeys` / `PublishableAPIKeys`, where the confirm modal and mutation live, not in the dropdown item. - No key names or values in properties. - `data-attr` added to the copy, reveal, create, delete, and docs controls (static strings; Studio's PostHog autocapture is currently off, so these send nothing today). `DocsButton` gets an optional `data-attr` prop. - `ui-patterns` `Input` gets optional `onReveal` and `onCopied` callbacks. `onCopied` fires only for the Copy button, so legacy and new keys count copies the same way. - The empty-state sample keys are `inert`, so their buttons don't copy or track. - New `getLegacyApiKeyType` helper maps the legacy `anon` / `service_role` tags to the event's `keyType`. Tests cover copy, reveal, the no-event-on-hide and denied-grant cases, the legacy toggle, and the key-type helper. Success paths for the create dialogs and delete aren't covered. ## How to test - [ ] Open `/project/[ref]/settings/api-keys`, copy a publishable and a secret key, reveal a secret key, and check `api_key_copied` and `api_key_reveal_clicked` in PostHog (staging) with the expected `keyType` - [ ] Create and delete a publishable and a secret key and check `api_key_created` / `api_key_removed` - [ ] On the legacy page, copy anon and service_role, reveal service_role, then disable and re-enable the keys and check `legacy_api_keys_updated`
100 lines
3.4 KiB
TypeScript
100 lines
3.4 KiB
TypeScript
import { screen, waitFor } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import type { components } from 'api-types'
|
|
import { HttpResponse } from 'msw'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { CreateNewAPIKeysButton } from './CreateNewAPIKeysButton'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw'
|
|
import { createMockProfileContext } from '@/tests/lib/profile-helpers'
|
|
import { mockProjectGrantContext } from '@/tests/lib/project-grant-mocks'
|
|
|
|
type ApiKeyResponse = components['schemas']['ApiKeyResponse_Output']
|
|
|
|
const { mockTrack } = vi.hoisted(() => ({ mockTrack: vi.fn() }))
|
|
vi.mock('@/lib/telemetry/track', () => ({ useTrack: () => mockTrack }))
|
|
|
|
vi.mock('common', async () => {
|
|
const actual = await vi.importActual<Record<string, unknown>>('common')
|
|
return { ...actual, useIsLoggedIn: () => true, useParams: () => ({ ref: 'default' }) }
|
|
})
|
|
|
|
// Responds to each create request in order: `true` succeeds, `false` fails
|
|
function mockCreateResponses(results: boolean[]) {
|
|
const remaining = [...results]
|
|
addAPIMock({
|
|
method: 'post',
|
|
path: '/v1/projects/:ref/api-keys',
|
|
response: () => {
|
|
const succeeds = remaining.shift() ?? false
|
|
if (!succeeds) {
|
|
return HttpResponse.json<APIErrorBody>({ message: 'Failed' }, { status: 500 })
|
|
}
|
|
return HttpResponse.json<ApiKeyResponse>({
|
|
id: 'key',
|
|
name: 'default',
|
|
type: 'publishable',
|
|
api_key: 'sb_publishable_xyz',
|
|
})
|
|
},
|
|
})
|
|
}
|
|
|
|
async function createKeys() {
|
|
customRender(<CreateNewAPIKeysButton />, { profileContext: createMockProfileContext() })
|
|
const trigger = await screen.findByRole('button', { name: 'Create new API keys' })
|
|
await waitFor(() => expect(trigger).toBeEnabled())
|
|
await userEvent.click(trigger)
|
|
await userEvent.click(await screen.findByRole('button', { name: 'Create keys' }))
|
|
}
|
|
|
|
describe('CreateNewAPIKeysButton tracking', () => {
|
|
beforeEach(() => {
|
|
mockTrack.mockClear()
|
|
mockProjectGrantContext({ ref: 'default' })
|
|
})
|
|
|
|
it('tracks the publishable and secret key separately when both are created', async () => {
|
|
mockCreateResponses([true, true])
|
|
await createKeys()
|
|
|
|
await waitFor(() => expect(mockTrack).toHaveBeenCalledTimes(2))
|
|
expect(mockTrack).toHaveBeenNthCalledWith(
|
|
1,
|
|
'api_key_created',
|
|
{ keyType: 'publishable', source: 'initial_setup' },
|
|
{ project: 'default' }
|
|
)
|
|
expect(mockTrack).toHaveBeenNthCalledWith(
|
|
2,
|
|
'api_key_created',
|
|
{ keyType: 'secret', source: 'initial_setup' },
|
|
{ project: 'default' }
|
|
)
|
|
})
|
|
|
|
it('still tracks the publishable key when the secret key fails', async () => {
|
|
mockCreateResponses([true, false])
|
|
await createKeys()
|
|
|
|
expect(await screen.findByText(/default secret key failed/)).toBeInTheDocument()
|
|
expect(mockTrack).toHaveBeenCalledTimes(1)
|
|
expect(mockTrack).toHaveBeenCalledWith(
|
|
'api_key_created',
|
|
{ keyType: 'publishable', source: 'initial_setup' },
|
|
{ project: 'default' }
|
|
)
|
|
})
|
|
|
|
it('tracks nothing when the publishable key fails', async () => {
|
|
mockCreateResponses([false])
|
|
await createKeys()
|
|
|
|
expect(
|
|
await screen.findByText(/Failed to create the default publishable key/)
|
|
).toBeInTheDocument()
|
|
expect(mockTrack).not.toHaveBeenCalled()
|
|
})
|
|
})
|