mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
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>
104 lines
3.4 KiB
TypeScript
104 lines
3.4 KiB
TypeScript
import { screen } from '@testing-library/react'
|
|
import type { components } from 'api-types'
|
|
import { HttpResponse } from 'msw'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import { PublishableAPIKeys } from './PublishableAPIKeys'
|
|
import { customRender as render } from '@/tests/lib/custom-render'
|
|
import { addAPIMock } from '@/tests/lib/msw'
|
|
import { mockPermissionsError } from '@/tests/lib/msw-permissions'
|
|
import { ALL_FGA_PERMISSIONS, NO_PERMISSIONS } from '@/tests/lib/permissions-fixtures'
|
|
import { createMockProfileContext } from '@/tests/lib/profile-helpers'
|
|
import { mockProjectGrantContext } from '@/tests/lib/project-grant-mocks'
|
|
|
|
vi.mock('common', async () => {
|
|
const actual = await vi.importActual<Record<string, unknown>>('common')
|
|
return { ...actual, useIsLoggedIn: () => true, 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>
|
|
),
|
|
}))
|
|
|
|
type ApiKeyResponse = components['schemas']['ApiKeyResponse_Output']
|
|
|
|
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',
|
|
}
|
|
|
|
function mockApiKeys(keys: ApiKeyResponse[], onRequest?: () => void) {
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/v1/projects/:ref/api-keys',
|
|
response: () => {
|
|
onRequest?.()
|
|
return HttpResponse.json<ApiKeyResponse[]>(keys)
|
|
},
|
|
})
|
|
}
|
|
|
|
describe('PublishableAPIKeys grants', () => {
|
|
it('grants access with the project permissions', async () => {
|
|
mockProjectGrantContext({ ref: 'default' })
|
|
mockApiKeys([publishableKey])
|
|
|
|
render(<PublishableAPIKeys />, { profileContext: createMockProfileContext() })
|
|
|
|
expect(await screen.findByText('api-key-row:default-publishable')).toBeInTheDocument()
|
|
})
|
|
|
|
it('shows a no-permission message without requesting keys when the grant is denied', async () => {
|
|
const requests = vi.fn()
|
|
mockProjectGrantContext({ ref: 'default', permissions: NO_PERMISSIONS })
|
|
mockApiKeys([], requests)
|
|
|
|
render(<PublishableAPIKeys />, { profileContext: createMockProfileContext() })
|
|
|
|
expect(await screen.findByText(/need additional permissions to view API keys/)).toBeVisible()
|
|
expect(requests).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('denies when only unrelated permissions are held', async () => {
|
|
mockProjectGrantContext({
|
|
ref: 'default',
|
|
permissions: {
|
|
organizations: [
|
|
{
|
|
slug: 'acme',
|
|
role: 'owner',
|
|
permissions: ALL_FGA_PERMISSIONS.filter((p) => !p.startsWith('api_gateway_keys_')),
|
|
projects: [],
|
|
},
|
|
],
|
|
},
|
|
})
|
|
mockApiKeys([publishableKey])
|
|
|
|
render(<PublishableAPIKeys />, { profileContext: createMockProfileContext() })
|
|
|
|
expect(await screen.findByText(/need additional permissions to view API keys/)).toBeVisible()
|
|
})
|
|
|
|
it('shows an error when permissions fail to load', async () => {
|
|
mockProjectGrantContext({ ref: 'default' })
|
|
mockPermissionsError()
|
|
|
|
render(<PublishableAPIKeys />, { profileContext: createMockProfileContext() })
|
|
|
|
expect(await screen.findByText(/Failed to check API key permissions/)).toBeVisible()
|
|
})
|
|
})
|