Files
supabase/apps/studio/tests/lib/project-grant-mocks.ts
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

53 lines
1.7 KiB
TypeScript

import type { platformComponents as components } from 'api-types'
import { createMockOrganizationResponse } from '@/tests/helpers'
import { addAPIMock } from '@/tests/lib/msw'
import { mockPermissions } from '@/tests/lib/msw-permissions'
import { allPermissionsFor, type PermissionsV2Response } from '@/tests/lib/permissions-fixtures'
type ProjectDetailResponse = components['schemas']['ProjectDetailResponse_Output']
/**
* Mocks what `useGrant` resolves for a project page: the selected project and organization, and
* the user's permissions (everything by default). Render with `useParams` returning `ref`.
*/
export function mockProjectGrantContext({
ref,
slug = 'acme',
permissions = allPermissionsFor([slug]),
}: {
ref: string
slug?: string
permissions?: PermissionsV2Response
}) {
const project: ProjectDetailResponse = {
cloud_provider: 'AWS',
connectionString: `postgresql://postgres:password@db.${ref}.supabase.co:5432/postgres`,
db_host: `db.${ref}.supabase.co`,
dbVersion: 'supabase-postgres-15.1.0',
high_availability: false,
id: 1,
infra_compute_size: 'micro',
inserted_at: '2026-01-01T00:00:00.000Z',
integration_source: null,
is_branch_enabled: false,
is_physical_backups_enabled: false,
name: 'Production',
organization_id: 1,
ref,
region: 'us-east-1',
restUrl: `https://${ref}.supabase.co`,
status: 'ACTIVE_HEALTHY',
subscription_id: 'subscription-1',
updated_at: '2026-01-01T00:00:00.000Z',
}
addAPIMock({ method: 'get', path: '/platform/projects/:ref', response: project })
addAPIMock({
method: 'get',
path: '/platform/organizations',
response: [createMockOrganizationResponse({ id: 1, slug })],
})
mockPermissions(permissions)
}