Files
supabase/apps/studio/components/interfaces/Database/Backups/BackupItem.test.tsx
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

70 lines
1.9 KiB
TypeScript

import { fireEvent, screen } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { BackupItem } from './BackupItem'
import type { DatabaseBackup } from '@/data/database/backups-query'
import { customRender } from '@/tests/lib/custom-render'
const { mockUseAsyncCheckPermissions } = vi.hoisted(() => ({
mockUseAsyncCheckPermissions: vi.fn(),
}))
vi.mock('@/hooks/misc/useCheckPermissionsV2', () => ({
useAsyncCheckPermissionsV2: mockUseAsyncCheckPermissions,
FGA_PERMISSIONS: { PROJECT: { BACKUPS_WRITE: 'backups_write' } },
}))
const backup: DatabaseBackup = {
id: 1,
inserted_at: '2024-01-01T00:00:00Z',
isPhysicalBackup: false,
project_id: 1,
status: 'COMPLETED',
}
describe('BackupItem', () => {
beforeEach(() => {
vi.clearAllMocks()
mockUseAsyncCheckPermissions.mockReturnValue({ can: true })
})
it('enables restoring for a healthy project that is not High Availability', () => {
customRender(
<BackupItem
index={0}
isHealthy={true}
isHighAvailability={false}
backup={backup}
onSelectBackup={vi.fn()}
/>
)
expect(screen.getByRole('button', { name: 'Restore' })).toBeEnabled()
})
it('disables restoring with a tooltip on High Availability projects', async () => {
customRender(
<BackupItem
index={0}
isHealthy={true}
isHighAvailability={true}
backup={backup}
onSelectBackup={vi.fn()}
/>
)
const button = screen.getByRole('button', { name: 'Restore' })
expect(button).toBeAriaDisabled()
// Radix opens the tooltip on pointermove; focusable disabled buttons support this
fireEvent.pointerMove(button)
expect(
await screen.findAllByText(
'Restoring from a backup is unavailable on High Availability projects',
{},
{ timeout: 2000 }
)
).not.toHaveLength(0)
})
})