mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +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>
90 lines
2.8 KiB
TypeScript
90 lines
2.8 KiB
TypeScript
import { screen } from '@testing-library/react'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { Usage } from './Usage'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
|
|
const {
|
|
mockUseAsyncCheckPermissions,
|
|
mockUseOrgDailyStatsQuery,
|
|
mockUseOrgSubscriptionQuery,
|
|
mockUseProjectDetailQuery,
|
|
} = vi.hoisted(() => ({
|
|
mockUseAsyncCheckPermissions: vi.fn(),
|
|
mockUseOrgDailyStatsQuery: vi.fn(),
|
|
mockUseOrgSubscriptionQuery: vi.fn(),
|
|
mockUseProjectDetailQuery: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('common', async (importOriginal) => ({
|
|
...(await importOriginal<typeof import('common')>()),
|
|
useParams: () => ({ slug: 'test-org' }),
|
|
}))
|
|
|
|
vi.mock('@/hooks/misc/useCheckPermissionsV2', async (importOriginal) => ({
|
|
...(await importOriginal<typeof import('@/hooks/misc/useCheckPermissionsV2')>()),
|
|
useAsyncCheckPermissionsV2: mockUseAsyncCheckPermissions,
|
|
}))
|
|
|
|
vi.mock('@/data/analytics/org-daily-stats-query', async (importOriginal) => ({
|
|
...(await importOriginal<typeof import('@/data/analytics/org-daily-stats-query')>()),
|
|
useOrgDailyStatsQuery: mockUseOrgDailyStatsQuery,
|
|
}))
|
|
|
|
vi.mock('@/data/projects/project-detail-query', () => ({
|
|
useProjectDetailQuery: mockUseProjectDetailQuery,
|
|
}))
|
|
|
|
vi.mock('@/data/subscriptions/org-subscription-query', () => ({
|
|
useOrgSubscriptionQuery: mockUseOrgSubscriptionQuery,
|
|
}))
|
|
|
|
describe('Usage', () => {
|
|
beforeEach(() => {
|
|
mockUseAsyncCheckPermissions.mockReturnValue({ can: true, isLoading: false })
|
|
mockUseProjectDetailQuery.mockReturnValue({
|
|
data: {
|
|
ref: 'ha-project',
|
|
name: 'HA project',
|
|
high_availability: true,
|
|
},
|
|
isPending: false,
|
|
})
|
|
mockUseOrgSubscriptionQuery.mockReturnValue({
|
|
data: undefined,
|
|
error: undefined,
|
|
isPending: false,
|
|
isError: false,
|
|
isSuccess: false,
|
|
})
|
|
mockUseOrgDailyStatsQuery.mockReturnValue({
|
|
data: undefined,
|
|
error: undefined,
|
|
isPending: false,
|
|
isError: false,
|
|
})
|
|
})
|
|
|
|
it('shows a coming-soon empty state for a High Availability project', () => {
|
|
customRender(<Usage />, {
|
|
nuqs: { searchParams: { projectRef: 'ha-project' } },
|
|
})
|
|
|
|
expect(screen.getByText('Usage unavailable on High Availability projects')).toBeInTheDocument()
|
|
expect(
|
|
screen.getByText('Usage insights for High Availability projects are coming soon.')
|
|
).toBeInTheDocument()
|
|
expect(screen.queryByText('Usage filtered by project')).not.toBeInTheDocument()
|
|
|
|
expect(mockUseProjectDetailQuery).toHaveBeenCalledWith({ ref: 'ha-project' })
|
|
expect(mockUseOrgSubscriptionQuery).toHaveBeenCalledWith(
|
|
{ orgSlug: 'test-org' },
|
|
{ enabled: false }
|
|
)
|
|
expect(mockUseOrgDailyStatsQuery).toHaveBeenCalledWith(
|
|
expect.objectContaining({ orgSlug: 'test-org', projectRef: 'ha-project' }),
|
|
{ enabled: false }
|
|
)
|
|
})
|
|
})
|