mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +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`
36 lines
1.4 KiB
TypeScript
36 lines
1.4 KiB
TypeScript
import { screen } from '@testing-library/react'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { ApiKeysCreateCallout } from './ApiKeysIllustrations'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
import { createMockProfileContext } from '@/tests/lib/profile-helpers'
|
|
import { mockProjectGrantContext } from '@/tests/lib/project-grant-mocks'
|
|
|
|
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' }) }
|
|
})
|
|
|
|
describe('ApiKeysCreateCallout', () => {
|
|
beforeEach(() => mockTrack.mockClear())
|
|
|
|
it('renders the sample keys inert so their Copy and Reveal buttons are not interactive', () => {
|
|
mockProjectGrantContext({ ref: 'default' })
|
|
customRender(<ApiKeysCreateCallout />, { profileContext: createMockProfileContext() })
|
|
|
|
const sampleButtons = [
|
|
...screen.getAllByRole('button', { name: 'Copy API key' }),
|
|
...screen.getAllByRole('button', { name: 'Reveal API key' }),
|
|
]
|
|
|
|
expect(sampleButtons.length).toBeGreaterThan(0)
|
|
for (const button of sampleButtons) {
|
|
expect(button.closest('[inert]')).not.toBeNull()
|
|
}
|
|
expect(mockTrack).not.toHaveBeenCalled()
|
|
})
|
|
})
|