Files
supabase/apps/studio/tests/lib/branch-test-utils.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

78 lines
2.3 KiB
TypeScript

import type { platformComponents } from 'api-types'
import type { Branch } from '@/data/branches/branches-query'
import type { FgaPermissions } from '@/hooks/misc/useCheckPermissionsV2'
import { createMockOrganizationResponse } from '@/tests/helpers'
import { addAPIMock } from '@/tests/lib/msw'
import { mockPermissions } from '@/tests/lib/msw-permissions'
type ProjectDetailResponse = platformComponents['schemas']['ProjectDetailResponse_Output']
export const createTestBranch = (overrides: Partial<Branch> = {}): Branch => ({
created_at: '2026-01-01T00:00:00Z',
id: 'branch-id',
is_default: false,
name: 'branch',
parent_project_ref: 'parent-ref',
persistent: false,
project_ref: 'branch-ref',
status: 'MIGRATIONS_PASSED',
updated_at: '2026-01-01T00:00:00Z',
with_data: false,
...overrides,
})
export const BRANCH_TEST_SCOPE = {
organizationSlug: 'acme',
parentRef: 'parent-ref',
branchRef: 'branch-ref',
} as const
export function mockBranchProject() {
const { organizationSlug, parentRef, branchRef } = BRANCH_TEST_SCOPE
addAPIMock({
method: 'get',
path: '/platform/projects/:ref',
response: {
cloud_provider: 'AWS',
connectionString: null,
db_host: `db.${branchRef}.supabase.co`,
dbVersion: 'supabase-postgres-15.1.0',
high_availability: false,
id: 2,
infra_compute_size: 'micro',
inserted_at: '2026-01-01T00:00:00.000Z',
integration_source: null,
is_branch_enabled: true,
is_physical_backups_enabled: false,
name: 'branch',
organization_id: 1,
parent_project_ref: parentRef,
ref: branchRef,
region: 'us-east-1',
restUrl: `https://${branchRef}.supabase.co`,
status: 'ACTIVE_HEALTHY',
subscription_id: 'subscription-1',
updated_at: '2026-01-01T00:00:00.000Z',
} satisfies ProjectDetailResponse,
})
addAPIMock({
method: 'get',
path: '/platform/organizations',
response: [createMockOrganizationResponse({ id: 1, slug: organizationSlug })],
})
}
export function mockParentProjectPermissions(permissions: FgaPermissions[]) {
mockPermissions({
organizations: [
{
slug: BRANCH_TEST_SCOPE.organizationSlug,
role: 'developer',
permissions: [],
projects: [{ ref: BRANCH_TEST_SCOPE.parentRef, role: 'developer', permissions }],
},
],
})
}