Files
supabase/apps/studio/components/interfaces/Billing/Payment/PaymentMethods/PaymentMethods.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

200 lines
5.9 KiB
TypeScript

import { screen } from '@testing-library/react'
import { mockIntersectionObserver } from 'jsdom-testing-mocks'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import PaymentMethods from './PaymentMethods'
import { MANAGED_BY } from '@/lib/constants/infrastructure'
import { createMockOrganization, render } from '@/tests/helpers'
mockIntersectionObserver()
const { mockSelectedOrganization, mockPaymentMethodsQuery, mockSubscription } = vi.hoisted(() => ({
mockSelectedOrganization: vi.fn(),
mockPaymentMethodsQuery: vi.fn(),
mockSubscription: vi.fn(),
}))
vi.mock('common', async (importOriginal) => {
const original = (await importOriginal()) as typeof import('common')
return {
...original,
useParams: () => ({ slug: 'stripe-org' }),
}
})
vi.mock('@/hooks/misc/useSelectedOrganization', () => ({
useSelectedOrganizationQuery: () => ({ data: mockSelectedOrganization() }),
}))
vi.mock('@/data/subscriptions/org-subscription-query', () => ({
useOrgSubscriptionQuery: () => ({ data: mockSubscription() }),
}))
vi.mock('@/data/organizations/organization-payment-methods-query', () => ({
useOrganizationPaymentMethodsQuery: () => mockPaymentMethodsQuery(),
}))
vi.mock('@/hooks/misc/useCheckPermissionsV2', async (importOriginal) => ({
...(await importOriginal<typeof import('@/hooks/misc/useCheckPermissionsV2')>()),
useAsyncCheckPermissionsV2: () => ({ can: true, isSuccess: true }),
}))
vi.mock('@/components/ui/PartnerManagedResource', () => ({
default: () => <div data-testid="partner-managed-resource" />,
}))
vi.mock('@/components/interfaces/Billing/Payment/AddNewPaymentMethodModal', () => ({
default: () => null,
}))
vi.mock('./ChangePaymentMethodModal', () => ({
default: () => null,
}))
vi.mock('./DeletePaymentMethodModal', () => ({
default: () => null,
}))
describe('PaymentMethods', () => {
beforeEach(() => {
mockSelectedOrganization.mockReturnValue(
createMockOrganization({
slug: 'stripe-org',
billing_partner: null,
integration_source: 'stripe_projects',
managed_by: MANAGED_BY.STRIPE_PROJECTS,
})
)
mockSubscription.mockReturnValue({
payment_method_type: 'card',
plan: { id: 'pro', name: 'Pro' },
})
mockPaymentMethodsQuery.mockReturnValue({
data: { data: [] },
error: null,
isPending: false,
isError: false,
isSuccess: true,
})
})
it('does not treat Stripe-connected orgs as partner-billed', () => {
render(<PaymentMethods />)
expect(screen.queryByTestId('partner-managed-resource')).not.toBeInTheDocument()
expect(screen.getByText('No payment methods')).toBeInTheDocument()
})
it('uses Stripe Projects guidance instead of support for invoice-based Stripe orgs', () => {
mockSubscription.mockReturnValue({
payment_method_type: 'invoice',
plan: { id: 'pro', name: 'Pro' },
})
render(<PaymentMethods />)
expect(screen.getByText('Payment is currently by invoice')).toBeInTheDocument()
expect(screen.getByText(/Manage payment methods through Stripe Projects/)).toBeInTheDocument()
expect(
screen.getByRole('link', {
name: 'View Stripe Projects docs',
})
).toBeInTheDocument()
expect(screen.queryByText('Contact support')).not.toBeInTheDocument()
})
it('still shows the partner-managed fallback for billing partners', () => {
mockSelectedOrganization.mockReturnValue(
createMockOrganization({
slug: 'aws-org',
billing_partner: 'aws_marketplace',
managed_by: MANAGED_BY.AWS_MARKETPLACE,
})
)
render(<PaymentMethods />)
expect(screen.getByTestId('partner-managed-resource')).toBeInTheDocument()
})
it('keeps the standard card row copy for non-Stripe payment methods', () => {
mockSelectedOrganization.mockReturnValue(
createMockOrganization({
slug: 'self-serve-org',
billing_partner: null,
integration_source: null,
managed_by: MANAGED_BY.SUPABASE,
})
)
mockPaymentMethodsQuery.mockReturnValue({
data: {
data: [
{
id: 'pm_123',
type: 'card',
is_default: true,
has_address: true,
created: 1_776_700_000,
card: {
brand: 'visa',
exp_month: 12,
exp_year: 2028,
last4: '4242',
},
shared_payment_token: null,
},
],
},
error: null,
isPending: false,
isError: false,
isSuccess: true,
})
render(<PaymentMethods />)
expect(screen.getByText('Expires: 12/2028')).toBeInTheDocument()
expect(screen.queryByText(/Managed via Stripe Projects/i)).not.toBeInTheDocument()
})
it('shows Stripe Projects payment method details separately from the underlying card', () => {
mockPaymentMethodsQuery.mockReturnValue({
data: {
data: [
{
id: 'spt._1T0w6VJDPojXS6LNK3clVOLZ',
type: 'shared_payment_token',
is_default: true,
has_address: true,
created: 1_776_700_000,
card: {
brand: 'visa',
exp_month: 12,
exp_year: 2028,
last4: '4242',
},
shared_payment_token: {
last4: 'VOLZ',
expires_at: 1_860_883_200,
is_expired: false,
},
},
],
},
error: null,
isPending: false,
isError: false,
isSuccess: true,
})
render(<PaymentMethods />)
expect(screen.getByText('**** **** **** 4242')).toBeInTheDocument()
expect(screen.getByText('Expires: 12/2028')).toBeInTheDocument()
expect(screen.getByText(/Managed via Stripe Projects/i)).toBeInTheDocument()
expect(screen.getByText('VOLZ')).toBeInTheDocument()
expect(screen.getByText(/Expires 12\/2028/i, { selector: 'span' })).toBeInTheDocument()
expect(screen.getByText('Active')).toBeInTheDocument()
})
})