Files
supabase/apps/studio/components/interfaces/APIKeys/PublishableAPIKeys.test.tsx
T
fffb1e23ca feat: implement profile permissions v2 api (#50966)
## Problem

Migrating Studio's permission checks from the old RLS json logic to
openfga backed permissions.

### New hooks and helpers

- `usePermissionsQueryV2` fetches the new profile openfga permisisons 
- `useAsyncCheckPermissionsV2` the replacement for the old check hook;
takes an FGA permission string (or a list)
- `FGA_PERMISSIONS` permission constants derived from
`@supabase/shared-types`. At the moment, the hook also accepts plain
strings for permissions not yet published in the package
- `useAsyncCheckUserContentPermissions` new hook for permission checks
on user content (SQL snippets, reports, log snippets). This wraps
``useAsyncCheckPermissionsV2` and adds existing content level rules on
top of the fga permission check

### Notes

- ~~Need api to support new permissions: project_operations_write,
billing_read/write, audit_logs_read~~ [DONE]
- ~~Still need to support: organization_config_read/write, auth hooks
and users permissions~~ [DONE]

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Charis Lam <26616127+charislam@users.noreply.github.com>
2026-10-21 06:54:16 -04:00

162 lines
4.3 KiB
TypeScript

import { screen } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { PublishableAPIKeys } from './PublishableAPIKeys'
import { customRender as render } from '@/tests/lib/custom-render'
const {
mockIsPlatform,
mockUseAPIKeysQuery,
mockUseAsyncCheckPermissions,
mockUseAPIKeyDeleteMutation,
} = vi.hoisted(() => ({
mockIsPlatform: { value: true },
mockUseAPIKeysQuery: vi.fn(),
mockUseAsyncCheckPermissions: vi.fn(),
mockUseAPIKeyDeleteMutation: vi.fn(),
}))
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('@/data/api-keys/api-keys-query', () => ({
useAPIKeysQuery: mockUseAPIKeysQuery,
}))
vi.mock('@/data/api-keys/api-key-delete-mutation', () => ({
useAPIKeyDeleteMutation: mockUseAPIKeyDeleteMutation,
}))
vi.mock('@/hooks/misc/useCheckPermissionsV2', () => ({
useAsyncCheckPermissionsV2: mockUseAsyncCheckPermissions,
FGA_PERMISSIONS: { PROJECT: { API_GATEWAY_KEYS_READ: 'api_gateway_keys_read' } },
}))
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 = {
id: 'pk-1',
name: 'default-publishable',
type: 'publishable' as const,
api_key: 'sb_publishable_xyz',
}
const secretKey = {
id: 'sk-1',
name: 'default-secret',
type: 'secret' as const,
api_key: 'sb_secret_xyz',
}
describe('PublishableAPIKeys', () => {
beforeEach(() => {
mockIsPlatform.value = true
mockUseAsyncCheckPermissions.mockReturnValue({ can: true, isLoading: false })
mockUseAPIKeyDeleteMutation.mockReturnValue({
mutate: vi.fn(),
isPending: false,
isSuccess: false,
})
})
it('renders the table with publishable keys', () => {
mockUseAPIKeysQuery.mockReturnValue({
data: [publishableKey],
isPending: false,
isSuccess: true,
isError: false,
})
render(<PublishableAPIKeys />)
expect(screen.getByText('api-key-row:default-publishable')).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', () => {
mockUseAPIKeysQuery.mockReturnValue({
data: [secretKey],
isPending: false,
isSuccess: true,
isError: false,
})
render(<PublishableAPIKeys />)
expect(screen.getByText('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', () => {
mockUseAPIKeysQuery.mockReturnValue({
data: [],
isPending: false,
isSuccess: true,
isError: false,
})
render(<PublishableAPIKeys />)
expect(screen.queryByText('No publishable API keys found')).not.toBeInTheDocument()
})
it('on self-hosted with no publishable keys, shows the empty-state card', () => {
mockIsPlatform.value = false
mockUseAPIKeysQuery.mockReturnValue({
data: [],
isPending: false,
isSuccess: true,
isError: false,
})
render(<PublishableAPIKeys />)
expect(screen.getByText('No publishable API keys found')).toBeInTheDocument()
})
it('hides the create button on non-platform', () => {
mockIsPlatform.value = false
mockUseAPIKeysQuery.mockReturnValue({
data: [publishableKey],
isPending: false,
isSuccess: true,
isError: false,
})
render(<PublishableAPIKeys />)
expect(screen.queryByText('CreatePublishableAPIKeyDialog')).not.toBeInTheDocument()
})
it('shows the create button on platform', () => {
mockUseAPIKeysQuery.mockReturnValue({
data: [publishableKey],
isPending: false,
isSuccess: true,
isError: false,
})
render(<PublishableAPIKeys />)
expect(screen.getByText('CreatePublishableAPIKeyDialog')).toBeInTheDocument()
})
})