Files
supabase/apps/studio/components/interfaces/ProjectHome/ProjectConnectionPopover.platform.test.tsx
Charis 1d0398db4b feat(studio): gate connect sheet and project connection popover on api-keys grants (#52964)
Part 4 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 #52963.

## 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
Moves the last publishable-key consumers onto the plain `GET
/v1/projects/{ref}/api-keys` grant:
- `ConnectSheet`: shows `ApiKeysAccessNote` under the mode selector
(except the warehouse and server modes; the server panel has its own
note)
- `useConnectServerEnv`: `canReadAPIKeys` and its loading state come
from the grant, and the hook returns the grant. The on-demand secret
reveal is unchanged here (part 5 moves it).
- Server env content: shows the note next to the disabled copy buttons
- `ProjectConnectionPopover`: only fetches while open. The nested
ternary for the key display is replaced with a flat helper covering
loading, denied, error, and the key.

With no callers left, the deprecated transitional `useAPIKeys` /
`useAPIKeysQuery` hooks are removed from
`data/api-keys/api-keys-query.ts`.

The old guards matched the annotation.

## Testing
- `ProjectConnectionPopover.platform.test.tsx` and
`content/server/env/content.platform.test.tsx`: granted, denied (no
request), and permission-check error.
- `useConnectServerEnv.test.ts` updated for the grant.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-11-05 12:40:49 -05:00

155 lines
4.4 KiB
TypeScript

import { screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import type { components } from 'api-types'
import { HttpResponse } from 'msw'
import { describe, expect, it, vi } from 'vitest'
import { ProjectConnectionPopover } from './ProjectConnectionPopover'
import { FGA_PERMISSIONS } from '@/hooks/misc/useCheckPermissionsV2'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock } from '@/tests/lib/msw'
import { mockPermissionsError } from '@/tests/lib/msw-permissions'
import { ALL_FGA_PERMISSIONS, type PermissionsV2Response } 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 withoutKeysRead = (): PermissionsV2Response => ({
organizations: [
{
slug: 'acme',
role: 'developer',
permissions: ALL_FGA_PERMISSIONS.filter(
(permission) => permission !== FGA_PERMISSIONS.PROJECT.API_GATEWAY_KEYS_READ
),
projects: [],
},
],
})
const profileContext = createMockProfileContext()
type ApiKeyResponse = components['schemas']['ApiKeyResponse_Output']
const apiKeys: ApiKeyResponse[] = [
{ type: 'legacy' as const, name: 'anon', api_key: 'anon-jwt', id: 'anon-id' },
{
type: 'publishable' as const,
name: 'default',
api_key: 'sb_publishable_abc',
id: 'pub-id',
inserted_at: '2024-01-01',
},
]
const mockApiKeys = () => {
const handler = vi.fn()
addAPIMock({
method: 'get',
path: '/v1/projects/:ref/api-keys',
response: () => {
handler()
return HttpResponse.json<ApiKeyResponse[]>(apiKeys)
},
})
return handler
}
const mockSettings = () =>
addAPIMock({
method: 'get',
path: '/platform/projects/:ref/settings',
response: {
app_config: {
endpoint: 'default.supabase.co',
db_schema: 'public',
storage_endpoint: 'default.supabase.co',
},
cloud_provider: 'AWS',
db_dns_name: 'db.default.supabase.co',
db_host: 'db.default.supabase.co',
db_name: 'postgres',
db_port: 5432,
db_user: 'postgres',
inserted_at: '2026-01-01T00:00:00.000Z',
name: 'Production',
ref: 'default',
region: 'us-east-1',
ssl_enforced: false,
status: 'ACTIVE_HEALTHY',
},
})
const mockEndpointDeps = () => {
addAPIMock({
method: 'get',
path: '/platform/profile/access-tokens',
response: [],
})
addAPIMock({
method: 'get',
path: '/platform/projects/:ref/billing/addons',
response: { selected_addons: [], available_addons: [], ref: 'default' },
})
addAPIMock({
method: 'get',
path: '/v1/projects/:ref/custom-hostname',
response: {
status: '1_not_started',
custom_hostname: '',
data: {
errors: [],
messages: [],
success: true,
result: { hostname: 'api.example.com', id: 'hostname-id' },
},
},
})
}
const renderPopover = async () => {
const user = userEvent.setup()
customRender(<ProjectConnectionPopover projectRef="default" />, { profileContext })
await user.click(await screen.findByRole('button', { name: /Copy/ }))
}
describe('ProjectConnectionPopover', () => {
it('shows the publishable key when api-keys are readable', async () => {
mockProjectGrantContext({ ref: 'default' })
mockApiKeys()
mockSettings()
mockEndpointDeps()
await renderPopover()
expect(await screen.findByText('sb_publishable_abc')).toBeVisible()
})
it('explains the missing permission and skips the request when denied', async () => {
mockProjectGrantContext({ ref: 'default', permissions: withoutKeysRead() })
const requests = mockApiKeys()
mockSettings()
mockEndpointDeps()
await renderPopover()
expect(await screen.findByText(/don't have permission to view API keys/)).toBeVisible()
expect(requests).not.toHaveBeenCalled()
})
it('shows an error when permissions fail to load', async () => {
mockProjectGrantContext({ ref: 'default' })
mockPermissionsError()
mockSettings()
mockEndpointDeps()
await renderPopover()
expect(await screen.findByText(/Failed to check API key permissions/)).toBeVisible()
})
})