Files
supabase/apps/studio/hooks/branches/useBranchGrant.test.ts
Charis 7ce15123ab feat(studio): grant infra for production and development branch variants (#52372)
Part 1 of 7 in a stack that splits #52331 into reviewable pieces.
Resolves part of
[FE-4693](https://linear.app/supabase/issue/FE-4693/pilot-migrate-branches-to-grants-8-endpoints-createbranchmodal).
There are no functional changes beyond #52331, and the top of the stack
is identical to it. See #52331 for the full rationale.

## Stack
1. 👉 #52372 Grant infra for branch variants
2. #52373 Branches list query
3. #52374 Page gates on the list grant
4. #52375 Branch creation
5. #52376 Branch read, delete, reset, and restore
6. #52377 Branch update and push
7. #52378 Branch diff and merge

## Changes
Adds `lib/fga-refinements.ts` entries: `production`/`development`
variants for the 8 single-branch endpoints, with a new `variantRequired`
flag. The fetch wrapper and `assertGrant` reject a grant without a
variant. Also adds the `production` variant for `POST
/v1/projects/{ref}/branches` when the body sets `is_default` or
`with_data`. Adds `GrantForVariants`, the `useBranchGrant` hook, and
shared fixtures in `tests/lib/branch-test-utils.ts`. Behavior-neutral:
existing branch calls still use `unguarded.migrating`, which skips these
checks.

## Testing
`guarded-fetch.test.ts` and `useBranchGrant.test.ts`.
2026-10-27 10:48:24 -04:00

67 lines
2.3 KiB
TypeScript

// @vitest-environment jsdom
import { waitFor } from '@testing-library/react'
import { describe, expect, test, vi } from 'vitest'
import { useBranchGrant } from './useBranchGrant'
import { FGA_PERMISSIONS } from '@/hooks/misc/useCheckPermissionsV2'
import {
BRANCH_TEST_SCOPE,
mockBranchProject,
mockParentProjectPermissions,
} from '@/tests/lib/branch-test-utils'
import { customRenderHook } from '@/tests/lib/custom-render'
import { createMockProfileContext } from '@/tests/lib/profile-helpers'
vi.mock('common', async (importOriginal) => {
const actual = await importOriginal<typeof import('common')>()
return {
...actual,
useIsLoggedIn: () => true,
useParams: () => ({ ref: BRANCH_TEST_SCOPE.branchRef }),
}
})
vi.mock('@/lib/constants', async (importOriginal) => {
const actual = await importOriginal<typeof import('@/lib/constants')>()
return { ...actual, IS_PLATFORM: true }
})
const UPDATE_BRANCH = 'PATCH /v1/branches/{branch_id_or_ref}'
const renderBranchGrant = (isDefault: boolean | undefined) =>
customRenderHook(
() => useBranchGrant(UPDATE_BRANCH, { projectRef: BRANCH_TEST_SCOPE.parentRef, isDefault }),
{ profileContext: createMockProfileContext() }
)
describe('useBranchGrant', () => {
test('grants a preview branch with development permissions only', async () => {
mockBranchProject()
mockParentProjectPermissions([FGA_PERMISSIONS.PROJECT.BRANCHING_DEVELOPMENT_WRITE])
const { result } = renderBranchGrant(false)
await waitFor(() => expect(result.current.status).toBe('granted'))
expect(result.current.grant?.variant).toBe('development')
})
test('denies the default branch without production permissions', async () => {
mockBranchProject()
mockParentProjectPermissions([FGA_PERMISSIONS.PROJECT.BRANCHING_DEVELOPMENT_WRITE])
const { result } = renderBranchGrant(true)
await waitFor(() => expect(result.current.status).toBe('denied'))
})
test('grants the default branch with production permissions', async () => {
mockBranchProject()
mockParentProjectPermissions([FGA_PERMISSIONS.PROJECT.BRANCHING_PRODUCTION_WRITE])
const { result } = renderBranchGrant(true)
await waitFor(() => expect(result.current.status).toBe('granted'))
expect(result.current.grant?.variant).toBe('production')
})
})