mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## 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>
156 lines
5.2 KiB
TypeScript
156 lines
5.2 KiB
TypeScript
import { screen } from '@testing-library/react'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { ConnectionPooling } from './ConnectionPooling'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
|
|
const {
|
|
mockUpdatePoolerConfig,
|
|
mockUseAsyncCheckPermissions,
|
|
mockUseCheckEntitlements,
|
|
mockUseHighAvailability,
|
|
mockUseMaxConnectionsQuery,
|
|
mockUsePgbouncerConfigQuery,
|
|
mockUsePgbouncerConfigurationUpdateMutation,
|
|
mockUseProjectAddonsQuery,
|
|
mockUseSelectedProjectQuery,
|
|
} = vi.hoisted(() => ({
|
|
mockUpdatePoolerConfig: vi.fn(),
|
|
mockUseAsyncCheckPermissions: vi.fn(),
|
|
mockUseCheckEntitlements: vi.fn(),
|
|
mockUseHighAvailability: vi.fn(),
|
|
mockUseMaxConnectionsQuery: vi.fn(),
|
|
mockUsePgbouncerConfigQuery: vi.fn(),
|
|
mockUsePgbouncerConfigurationUpdateMutation: vi.fn(),
|
|
mockUseProjectAddonsQuery: vi.fn(),
|
|
mockUseSelectedProjectQuery: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('common', async (importOriginal) => ({
|
|
...(await importOriginal<typeof import('common')>()),
|
|
useParams: () => ({ ref: 'ha-project' }),
|
|
}))
|
|
|
|
vi.mock('@/hooks/misc/useCheckPermissionsV2', () => ({
|
|
useAsyncCheckPermissionsV2: mockUseAsyncCheckPermissions,
|
|
FGA_PERMISSIONS: { PROJECT: { DATABASE_POOLING_CONFIG_WRITE: 'database_pooling_config_write' } },
|
|
}))
|
|
|
|
vi.mock('@/hooks/misc/useCheckEntitlements', () => ({
|
|
useCheckEntitlements: mockUseCheckEntitlements,
|
|
}))
|
|
|
|
vi.mock('@/hooks/misc/useHighAvailability', () => ({
|
|
useHighAvailability: mockUseHighAvailability,
|
|
}))
|
|
|
|
vi.mock('@/hooks/misc/useSelectedProject', () => ({
|
|
useSelectedProjectQuery: mockUseSelectedProjectQuery,
|
|
useIsHighAvailability: () => mockUseHighAvailability().isHighAvailability ?? false,
|
|
useIsAwsCloudProvider: () => false,
|
|
}))
|
|
|
|
vi.mock('@/data/database/max-connections-query', () => ({
|
|
useMaxConnectionsQuery: mockUseMaxConnectionsQuery,
|
|
}))
|
|
|
|
vi.mock('@/data/database/pgbouncer-config-query', () => ({
|
|
usePgbouncerConfigQuery: mockUsePgbouncerConfigQuery,
|
|
}))
|
|
|
|
vi.mock('@/data/database/pgbouncer-config-update-mutation', () => ({
|
|
usePgbouncerConfigurationUpdateMutation: mockUsePgbouncerConfigurationUpdateMutation,
|
|
}))
|
|
|
|
vi.mock('@/data/subscriptions/project-addons-query', () => ({
|
|
useProjectAddonsQuery: mockUseProjectAddonsQuery,
|
|
}))
|
|
|
|
const expectEveryQueryCall = (queryMock: ReturnType<typeof vi.fn>, enabled: boolean) => {
|
|
expect(queryMock).toHaveBeenCalled()
|
|
for (const [, options] of queryMock.mock.calls) {
|
|
expect(options).toEqual({ enabled })
|
|
}
|
|
}
|
|
|
|
describe('ConnectionPooling', () => {
|
|
beforeEach(() => {
|
|
mockUseSelectedProjectQuery.mockReturnValue({
|
|
data: {
|
|
id: 1,
|
|
ref: 'ha-project',
|
|
infra_compute_size: 'small',
|
|
connectionString: 'postgresql://example',
|
|
},
|
|
})
|
|
mockUseHighAvailability.mockReturnValue({ isHighAvailability: true, isPending: false })
|
|
mockUseAsyncCheckPermissions.mockReturnValue({ can: true })
|
|
mockUseCheckEntitlements.mockReturnValue({ hasAccess: true })
|
|
mockUsePgbouncerConfigQuery.mockReturnValue({
|
|
data: undefined,
|
|
error: undefined,
|
|
isPending: false,
|
|
isError: false,
|
|
isSuccess: false,
|
|
})
|
|
mockUseMaxConnectionsQuery.mockReturnValue({ data: undefined })
|
|
mockUseProjectAddonsQuery.mockReturnValue({
|
|
data: {
|
|
selected_addons: [
|
|
{
|
|
type: 'compute_instance',
|
|
variant: { name: 'Small', identifier: 'ci_small' },
|
|
},
|
|
],
|
|
},
|
|
isSuccess: true,
|
|
})
|
|
mockUsePgbouncerConfigurationUpdateMutation.mockReturnValue({
|
|
mutate: mockUpdatePoolerConfig,
|
|
isPending: false,
|
|
})
|
|
})
|
|
|
|
it('renders High Availability pooling settings as read-only', () => {
|
|
customRender(<ConnectionPooling />)
|
|
|
|
expect(
|
|
screen.getAllByText(
|
|
'Connection pooling settings are managed automatically on High Availability projects'
|
|
)
|
|
).toHaveLength(1)
|
|
expect(screen.getByRole('link', { name: 'Learn more' })).toHaveAttribute(
|
|
'href',
|
|
'https://multigres.com/blog/pooling-without-choosing-a-mode'
|
|
)
|
|
|
|
expect(screen.queryByText('Enable IPv4 add-on')).not.toBeInTheDocument()
|
|
expect(screen.getByPlaceholderText('Managed automatically')).toBeDisabled()
|
|
expect(screen.getByDisplayValue('100000')).toBeDisabled()
|
|
expect(screen.queryByRole('button', { name: 'Cancel' })).not.toBeInTheDocument()
|
|
expect(screen.queryByRole('button', { name: 'Save' })).not.toBeInTheDocument()
|
|
expect(mockUpdatePoolerConfig).not.toHaveBeenCalled()
|
|
|
|
expectEveryQueryCall(mockUsePgbouncerConfigQuery, false)
|
|
expectEveryQueryCall(mockUseMaxConnectionsQuery, false)
|
|
})
|
|
|
|
it('does not fetch pooling config while the high availability state is pending', () => {
|
|
mockUseHighAvailability.mockReturnValue({ isHighAvailability: false, isPending: true })
|
|
|
|
customRender(<ConnectionPooling />)
|
|
|
|
expectEveryQueryCall(mockUsePgbouncerConfigQuery, false)
|
|
expectEveryQueryCall(mockUseMaxConnectionsQuery, false)
|
|
})
|
|
|
|
it('fetches pooling config for non high availability projects', () => {
|
|
mockUseHighAvailability.mockReturnValue({ isHighAvailability: false, isPending: false })
|
|
|
|
customRender(<ConnectionPooling />)
|
|
|
|
expectEveryQueryCall(mockUsePgbouncerConfigQuery, true)
|
|
expectEveryQueryCall(mockUseMaxConnectionsQuery, true)
|
|
})
|
|
})
|