Files
supabase/apps/studio/components/interfaces/APIKeys/APIKeyDeleteDialog.platform.test.tsx
Charis 121de3dff7 feat(studio): gate api key create and delete on endpoint grants (#52966)
Part 6 of 6 in a stack for
[FE-4731](https://linear.app/supabase/issue/FE-4731/grants-rollout-api-keys-consumers-that-only-need-publishable-keys).
Based on #52965.

## Stack
1. #52961 Docs and project API docs consumers
2. #52962 Edge functions and vector bucket examples
3. #52963 Integrations landing data
4. #52964 Connect sheet and project connection popover; remove the
transitional api-keys hooks
5. #52965 `reveal` refinement for single api-key reads; API key pill
6. 👉 #52966 API key create and delete

## Changes
Gates the api-key write endpoints (both require
`api_gateway_keys_write`) on grants instead of `unguarded.migrating`:
- `api-key-create-mutation` and `api-key-delete-mutation` take the grant
in their variables.
- `APIKeyDeleteDialog` uses the `DELETE` grant instead of its V2 check,
and is disabled with a tooltip when denied or errored. `SecretAPIKeys`
and `PublishableAPIKeys` pass the grant to the mutation.
- `CreateNewAPIKeysButton` and the create publishable and secret key
dialogs mint the `POST` grant. Their triggers and submit buttons are
disabled with a tooltip unless granted.
- `useConnectedResourceMutations`: the api-key delete call takes the
grant. Without it, the mutation shows an error toast and throws.

**New guard:** creating API keys had no UI guard. Users without
`api_gateway_keys_write` could open the dialogs and get a 403; the
controls are now disabled for them. The delete guard already matched the
annotation.

Out of scope: the legacy API key toggle and status (FE-4732).

## Testing
- `CreateAPIKeyDialogs.platform.test.tsx` (renamed from
`CreateAPIKeyDialogs.test.tsx`, the mutation mock removed): denied and
permission-check error disable the triggers and the submit button.
- `APIKeyDeleteDialog.platform.test.tsx`: granted, denied, and
permission-check error.
- `data/api-keys/api-key-write-mutations.test.ts`: create and delete
send their requests with a grant.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-11-10 14:01:40 +00:00

81 lines
2.9 KiB
TypeScript

import { screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from 'ui'
import { describe, expect, it, vi } from 'vitest'
import { APIKeyDeleteDialog } from './APIKeyDeleteDialog'
import type { APIKeysData } from '@/data/api-keys/api-keys-query'
import { customRender as render } from '@/tests/lib/custom-render'
import { mockPermissionsError } from '@/tests/lib/msw-permissions'
import { 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' }) }
})
const apiKey = { id: 'key-1', name: 'default', type: 'secret', api_key: 'sb_secret_x' } as Extract<
APIKeysData[number],
{ type: 'secret' }
>
function renderDialog(setKeyToDelete = vi.fn()) {
render(
<DropdownMenu open>
<DropdownMenuTrigger>menu</DropdownMenuTrigger>
<DropdownMenuContent>
<APIKeyDeleteDialog apiKey={apiKey} setKeyToDelete={setKeyToDelete} />
</DropdownMenuContent>
</DropdownMenu>,
{ profileContext: createMockProfileContext() }
)
return setKeyToDelete
}
const item = () => screen.getByRole('menuitem', { name: /Delete API key/ })
describe('APIKeyDeleteDialog grants', () => {
it('selects the key for deletion when granted', async () => {
mockProjectGrantContext({ ref: 'default' })
const setKeyToDelete = renderDialog()
await waitFor(() => expect(item()).not.toHaveAttribute('data-disabled'))
item().click()
expect(setKeyToDelete).toHaveBeenCalledWith('key-1')
})
it('disables the item without selecting the key when denied', async () => {
mockProjectGrantContext({ ref: 'default', permissions: NO_PERMISSIONS })
const setKeyToDelete = renderDialog()
// Wait for the grant to settle: the item is also disabled while loading
await userEvent.hover(item())
expect(
(await screen.findAllByText('You need additional permissions to delete API keys')).length
).toBeGreaterThan(0)
expect(item()).toHaveAttribute('data-disabled')
item().click()
expect(setKeyToDelete).not.toHaveBeenCalled()
})
it('disables the item when permissions fail to load', async () => {
mockProjectGrantContext({ ref: 'default' })
mockPermissionsError()
const setKeyToDelete = renderDialog()
// Wait for the grant to settle: the item is also disabled while loading
await userEvent.hover(item())
expect(
(await screen.findAllByText('Failed to check permissions to delete API keys')).length
).toBeGreaterThan(0)
expect(item()).toHaveAttribute('data-disabled')
item().click()
expect(setKeyToDelete).not.toHaveBeenCalled()
})
})