mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## Problem Components and fetchers are moving to endpoint-bound FGA grants, so more tests need to mock `/platform/profile/permissions/v2` and mint grants. Each test file currently defines its own permissions response type and mock, and `testGrant` lives inside `guarded-fetch.test.ts`. Resolves [FE-4692](https://linear.app/supabase/issue/FE-4692/test-infra-global-msw-handler-for-v2-permissions-and-testgrant). ## Solution * `tests/lib/permissions-fixtures.ts`: shared `PermissionsV2Response` type, `ALL_FGA_PERMISSIONS`, `allPermissionsFor(slugs)` (grants everything at the org level), and `NO_PERMISSIONS`. * `tests/lib/msw-permissions.ts`: `mockPermissions(response)` and `mockPermissionsError({ status, message })` on top of `addAPIMock`. * `tests/lib/test-grant.ts`: `testGrant(endpoint, { variant, organizationSlug, projectRef, permissionsProjectRef })` for unit tests of fetchers and query options, moved out of `guarded-fetch.test.ts`. * Existing tests that mocked the v2 endpoint now use the shared helpers. `IndirectTaxDeclarationModal.test.tsx` keeps its own `addAPIMock` because a test asserts the request was served. * Documented in `apps/studio/tests/README.md` and the `studio-mock-api-tests` skill.
29 lines
973 B
TypeScript
29 lines
973 B
TypeScript
import { HttpResponse } from 'msw'
|
|
|
|
import { addAPIMock, type APIErrorBody } from './msw'
|
|
import type { PermissionsV2Response } from './permissions-fixtures'
|
|
|
|
/**
|
|
* Mocks the current user's FGA permissions. Use `allPermissionsFor([slug])` to grant everything,
|
|
* `NO_PERMISSIONS` to deny every check, or a hand-built response for specific permissions.
|
|
*/
|
|
export function mockPermissions(response: PermissionsV2Response) {
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/profile/permissions/v2',
|
|
response: () => HttpResponse.json<PermissionsV2Response>(response),
|
|
})
|
|
}
|
|
|
|
/** Makes the permissions request fail, so grants resolve to an error. */
|
|
export function mockPermissionsError({
|
|
status = 500,
|
|
message = 'Failed to retrieve permissions',
|
|
}: { status?: number; message?: string } = {}) {
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/profile/permissions/v2',
|
|
response: () => HttpResponse.json<APIErrorBody>({ message }, { status }),
|
|
})
|
|
}
|