mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +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>
141 lines
3.9 KiB
TypeScript
141 lines
3.9 KiB
TypeScript
import { screen } from '@testing-library/react'
|
|
import { mockIntersectionObserver } from 'jsdom-testing-mocks'
|
|
import type { ReactNode } from 'react'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { BillingCustomerData } from './BillingCustomerData'
|
|
import { MANAGED_BY } from '@/lib/constants/infrastructure'
|
|
import { createMockOrganization, render } from '@/tests/helpers'
|
|
|
|
mockIntersectionObserver()
|
|
|
|
const mockSelectedOrganization = vi.hoisted(() => vi.fn())
|
|
|
|
vi.mock('common', async (importOriginal) => {
|
|
const original = (await importOriginal()) as typeof import('common')
|
|
return {
|
|
...original,
|
|
useParams: () => ({ slug: 'stripe-org' }),
|
|
}
|
|
})
|
|
|
|
vi.mock('next-themes', () => ({
|
|
useTheme: () => ({ resolvedTheme: 'dark' }),
|
|
}))
|
|
|
|
vi.mock('@stripe/react-stripe-js', () => ({
|
|
Elements: ({ children }: { children: ReactNode }) => <div>{children}</div>,
|
|
}))
|
|
|
|
vi.mock('@stripe/stripe-js', () => ({
|
|
loadStripe: vi.fn(() => Promise.resolve(null)),
|
|
}))
|
|
|
|
vi.mock('ui', async (importOriginal) => {
|
|
const original = (await importOriginal()) as typeof import('ui')
|
|
return {
|
|
...original,
|
|
Form: ({ children }: { children: ReactNode }) => <div>{children}</div>,
|
|
}
|
|
})
|
|
|
|
vi.mock('@/hooks/misc/useSelectedOrganization', () => ({
|
|
useSelectedOrganizationQuery: () => ({ data: mockSelectedOrganization() }),
|
|
}))
|
|
|
|
vi.mock('@/hooks/misc/useCheckPermissionsV2', async (importOriginal) => ({
|
|
...(await importOriginal<typeof import('@/hooks/misc/useCheckPermissionsV2')>()),
|
|
useAsyncCheckPermissionsV2: () => ({ can: true, isSuccess: true }),
|
|
}))
|
|
|
|
vi.mock('@/data/organizations/organization-customer-profile-query', () => ({
|
|
useOrganizationCustomerProfileQuery: () => ({
|
|
data: {
|
|
address: {
|
|
line1: '',
|
|
line2: '',
|
|
city: '',
|
|
state: '',
|
|
postal_code: '',
|
|
country: 'US',
|
|
},
|
|
billing_name: 'Toolshed',
|
|
},
|
|
error: null,
|
|
isPending: false,
|
|
isSuccess: true,
|
|
}),
|
|
}))
|
|
|
|
vi.mock('@/data/organizations/organization-tax-id-query', () => ({
|
|
useOrganizationTaxIdQuery: () => ({
|
|
data: null,
|
|
error: null,
|
|
isPending: false,
|
|
isSuccess: true,
|
|
}),
|
|
}))
|
|
|
|
vi.mock('@/data/organizations/organization-customer-profile-update-mutation', () => ({
|
|
useOrganizationCustomerProfileUpdateMutation: () => ({
|
|
mutateAsync: vi.fn(),
|
|
}),
|
|
}))
|
|
|
|
vi.mock('./useBillingCustomerDataForm', () => ({
|
|
useBillingCustomerDataForm: () => ({
|
|
form: {},
|
|
handleSubmit: vi.fn(),
|
|
handleReset: vi.fn(),
|
|
isDirty: false,
|
|
resetKey: 0,
|
|
onAddressChange: vi.fn(),
|
|
applyAddressElementValue: vi.fn(),
|
|
markCurrentValuesAsSaved: vi.fn(),
|
|
addressCountry: 'US',
|
|
addressOptions: {},
|
|
}),
|
|
}))
|
|
|
|
vi.mock('./BillingCustomerDataForm', () => ({
|
|
BillingCustomerDataForm: () => <div data-testid="billing-customer-data-form" />,
|
|
}))
|
|
|
|
vi.mock('@/components/ui/PartnerManagedResource', () => ({
|
|
default: () => <div data-testid="partner-managed-resource" />,
|
|
}))
|
|
|
|
describe('BillingCustomerData', () => {
|
|
beforeEach(() => {
|
|
mockSelectedOrganization.mockReturnValue(
|
|
createMockOrganization({
|
|
slug: 'stripe-org',
|
|
billing_partner: null,
|
|
integration_source: 'stripe_projects',
|
|
managed_by: MANAGED_BY.STRIPE_PROJECTS,
|
|
})
|
|
)
|
|
})
|
|
|
|
it('keeps Stripe-connected orgs editable in Studio', () => {
|
|
render(<BillingCustomerData />)
|
|
|
|
expect(screen.queryByTestId('partner-managed-resource')).not.toBeInTheDocument()
|
|
expect(screen.getByTestId('billing-customer-data-form')).toBeInTheDocument()
|
|
})
|
|
|
|
it('still blocks billing-partner orgs behind the partner-managed resource', () => {
|
|
mockSelectedOrganization.mockReturnValue(
|
|
createMockOrganization({
|
|
slug: 'vercel-org',
|
|
billing_partner: 'vercel_marketplace',
|
|
managed_by: MANAGED_BY.VERCEL_MARKETPLACE,
|
|
})
|
|
)
|
|
|
|
render(<BillingCustomerData />)
|
|
|
|
expect(screen.getByTestId('partner-managed-resource')).toBeInTheDocument()
|
|
})
|
|
})
|