Files
supabase/apps/studio/components/interfaces/APIKeys/PublishableAPIKeys.test.tsx
CharisandClaude 7bc32a82fd feat(studio): gate api keys settings UI on api-keys grants (#52724)
Part 2 of 4 in a stack for
[FE-4694](https://linear.app/supabase/issue/FE-4694/pilot-split-api-keys-queries-and-add-first-refinement-reveal-variant).
Based on #52723.

## Stack
1. #52723 Reveal refinement and split api-keys queries
2. 👉 #52724 API keys settings UI and command menu
3. #52727 Database and edge functions consumers that reveal secret keys
4. #52728 Storage, cron, and Realtime consumers; remove the transitional
reveal path

## Changes
Moves the API keys settings UI off the V2 permission checks and onto
`useGrant('GET /v1/projects/{ref}/api-keys')` with the new query
options:
- `PublishableAPIKeys`, `SecretAPIKeys`, `Project/APIKeys`, and the
api-keys settings page use a plain grant. They never requested revealed
keys; individual secrets still come from the per-key endpoint.
- The command menu's API keys commands use the `reveal` grant, since
they copy secret key values.
- Each consumer renders loading, denied (`NoPermission`),
permission-check error (`AlertError`), and the existing data states.
`Project/APIKeys` now shows `NoPermission` when denied instead of a
skeleton that never resolved.

## Testing
- Migrates `PublishableAPIKeys.test.tsx`,
`CommandMenu/ApiKeys.test.tsx`, and the api-keys settings page test to
MSW and `customRender`, without mocking the data module or the V2
permissions hook.
- Adds `PublishableAPIKeys.platform.test.tsx` and
`CommandMenu/ApiKeys.platform.test.tsx` for granted, denied, and error
states. The denied test waits for the denied grant before asserting that
no api-keys request was made.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-04 10:35:39 -05:00

130 lines
4.0 KiB
TypeScript

import { screen } from '@testing-library/react'
import type { components } from 'api-types'
import { HttpResponse } from 'msw'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { PublishableAPIKeys } from './PublishableAPIKeys'
import { customRender as render } from '@/tests/lib/custom-render'
import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw'
import { mockProjectGrantContext } from '@/tests/lib/project-grant-mocks'
type ApiKeyResponse = components['schemas']['ApiKeyResponse_Output']
const { mockIsPlatform } = vi.hoisted(() => ({ mockIsPlatform: { value: true } }))
vi.mock('common', async () => {
const actual = await vi.importActual<Record<string, unknown>>('common')
return {
...actual,
get IS_PLATFORM() {
return mockIsPlatform.value
},
useParams: () => ({ ref: 'default' }),
}
})
vi.mock('./CreatePublishableAPIKeyDialog', () => ({
CreatePublishableAPIKeyDialog: () => <div>CreatePublishableAPIKeyDialog</div>,
}))
vi.mock('./APIKeyRow', () => ({
APIKeyRow: ({ apiKey }: { apiKey: { id: string; name: string } }) => (
<tr>
<td>api-key-row:{apiKey.name}</td>
</tr>
),
}))
const publishableKey: ApiKeyResponse = {
id: 'pk-1',
name: 'default-publishable',
type: 'publishable',
api_key: 'sb_publishable_xyz',
hash: 'hash',
inserted_at: '2025-02-16T22:24:42.115195Z',
}
const secretKey: ApiKeyResponse = {
id: 'sk-1',
name: 'default-secret',
type: 'secret',
api_key: 'sb_secret_xyz',
hash: 'hash',
inserted_at: '2025-02-16T22:24:42.115195Z',
}
function mockApiKeys(keys: ApiKeyResponse[]) {
addAPIMock({
method: 'get',
path: '/v1/projects/:ref/api-keys',
response: () => HttpResponse.json<ApiKeyResponse[]>(keys),
})
}
describe('PublishableAPIKeys', () => {
beforeEach(() => {
mockIsPlatform.value = true
mockProjectGrantContext({ ref: 'default' })
})
it('renders the table with publishable keys and the create button on platform', async () => {
mockApiKeys([publishableKey])
render(<PublishableAPIKeys />)
expect(await screen.findByText('api-key-row:default-publishable')).toBeInTheDocument()
expect(screen.getByText('CreatePublishableAPIKeyDialog')).toBeInTheDocument()
expect(screen.queryByText('No publishable API keys found')).not.toBeInTheDocument()
})
it('on platform with secret-only keys, shows the inline "No publishable keys created yet" admonition', async () => {
mockApiKeys([secretKey])
render(<PublishableAPIKeys />)
expect(await screen.findByText('No publishable keys created yet')).toBeInTheDocument()
expect(screen.queryByText('No publishable API keys found')).not.toBeInTheDocument()
})
it('on platform with no keys, does NOT show the self-hosted empty-state card', async () => {
mockApiKeys([])
render(<PublishableAPIKeys />)
expect(await screen.findByText('Publishable key')).toBeInTheDocument()
expect(await screen.findByText('Publishable keys can be safely shared publicly')).toBeVisible()
expect(screen.queryByText('No publishable API keys found')).not.toBeInTheDocument()
})
it('on self-hosted with no publishable keys, shows the empty-state card', async () => {
mockIsPlatform.value = false
mockApiKeys([])
render(<PublishableAPIKeys />)
expect(await screen.findByText('No publishable API keys found')).toBeInTheDocument()
})
it('hides the create button on non-platform', async () => {
mockIsPlatform.value = false
mockApiKeys([publishableKey])
render(<PublishableAPIKeys />)
expect(await screen.findByText('api-key-row:default-publishable')).toBeInTheDocument()
expect(screen.queryByText('CreatePublishableAPIKeyDialog')).not.toBeInTheDocument()
})
it('shows an error when the keys request fails', async () => {
addAPIMock({
method: 'get',
path: '/v1/projects/:ref/api-keys',
response: () => HttpResponse.json<APIErrorBody>({ message: 'boom' }, { status: 500 }),
})
render(<PublishableAPIKeys />)
expect(await screen.findByText(/Failed to load API keys/)).toBeVisible()
})
})