import type { SafeSqlFragment } from '@supabase/pg-meta' import { screen } from '@testing-library/react' import { describe, expect, it, vi } from 'vitest' import type { Policy } from '@/components/interfaces/Auth/Policies/PolicyTableRow/PolicyTableRow.utils' import type { ParseQueryResults } from '@/components/interfaces/Auth/RLSTester/RLSTester.types' import { RLSTesterResults } from '@/components/interfaces/Auth/RLSTester/RLSTesterResults' import { render } from '@/tests/helpers' vi.mock('@/components/interfaces/Auth/RLSTester/useTestQueryRLS', () => ({ useTestQueryRLS: () => ({ limit: 100 }), })) vi.mock('@/components/interfaces/Auth/RLSTester/RLSTableCard', () => ({ RLSTableCard: () =>
, })) vi.mock('@/components/interfaces/SQLEditor/UtilityPanel/Results', () => ({ Results: () =>
, })) const sql = (s: string) => s as unknown as SafeSqlFragment const makePolicy = (definition: string | null = null): Policy => ({ definition: definition !== null ? sql(definition) : null }) as Policy const makeTable = ( overrides?: Partial ): ParseQueryResults['tables'][number] => ({ schema: 'public', table: 'items', isRLSEnabled: true, tablePolicies: [], ...overrides, }) const defaultProps = { results: [], autoLimit: false, handleSelectEditPolicy: vi.fn(), } describe('RLSTesterResults', () => { describe('access badge', () => { it('shows "No access" badge when table has RLS enabled but no policies', () => { render( ) expect(screen.getByText('No access')).toBeInTheDocument() }) it('shows "No access" badge when a policy definition is false', () => { render( ) expect(screen.getByText('No access')).toBeInTheDocument() }) it('shows "Has access" badge when results are empty and user has access', () => { render( ) expect(screen.getByText('Has access')).toBeInTheDocument() }) it('shows "Can access" badge when results are returned', () => { render( ) expect(screen.getByText('Can access')).toBeInTheDocument() }) }) describe('policy admonitions', () => { it('shows service role admonition for postgres role', () => { render( ) expect(screen.getByText(/bypasses all RLS policies/)).toBeInTheDocument() }) it('shows "no policies" admonition when RLS is enabled but no policies exist', () => { render( ) expect(screen.getByText(/no policies set up/)).toBeInTheDocument() expect(screen.getByText(/public.profiles/)).toBeInTheDocument() }) it('shows "policy false" admonition when a policy evaluates to false', () => { render( ) expect(screen.getByText(/evaluates to/)).toBeInTheDocument() expect(screen.getByText(/public.secrets/)).toBeInTheDocument() }) }) describe('"Ran as" section', () => { it('shows postgres for service role', () => { render( ) expect(screen.getAllByText('postgres').length).toBeGreaterThan(0) }) it('shows "an Anonymous user" for anon role', () => { render( ) expect(screen.getByText('an Anonymous user')).toBeInTheDocument() expect(screen.getByText('Not logged in user')).toBeInTheDocument() }) it('shows user email and ID when a user is present', () => { render( ) expect(screen.getByText('alice@example.com')).toBeInTheDocument() expect(screen.getByText('ID: user-123')).toBeInTheDocument() }) }) })