|
| 1 | +/** @vitest-environment node */ |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | + |
| 4 | +const mocks = vi.hoisted(() => ({ session: vi.fn(), authorize: vi.fn() })) |
| 5 | +vi.mock('next/navigation', () => ({ |
| 6 | + redirect: (href: string) => { |
| 7 | + throw new Error(`redirect:${href}`) |
| 8 | + }, |
| 9 | + notFound: () => { |
| 10 | + throw new Error('not-found') |
| 11 | + }, |
| 12 | +})) |
| 13 | +vi.mock('@/lib/auth', () => ({ getSession: mocks.session })) |
| 14 | +vi.mock('@/lib/settings/application/organization-section-access', () => ({ |
| 15 | + authorizeOrganizationSettingsSection: mocks.authorize, |
| 16 | +})) |
| 17 | +vi.mock('@/components/settings/account-settings-renderer', () => ({ |
| 18 | + AccountSettingsRenderer: () => null, |
| 19 | +})) |
| 20 | +vi.mock('@/components/settings/prefetch-standalone-general', () => ({ |
| 21 | + prefetchStandaloneGeneral: vi.fn(), |
| 22 | +})) |
| 23 | +vi.mock('@/app/o/[organizationId]/settings/[section]/settings', () => ({ |
| 24 | + OrganizationSettings: () => null, |
| 25 | +})) |
| 26 | + |
| 27 | +import OrganizationSettingsSectionPage from '@/app/o/[organizationId]/settings/[section]/page' |
| 28 | + |
| 29 | +describe('organization request settings routing', () => { |
| 30 | + beforeEach(() => { |
| 31 | + vi.clearAllMocks() |
| 32 | + mocks.session.mockResolvedValue({ user: { id: 'viewer' } }) |
| 33 | + mocks.authorize.mockResolvedValue(true) |
| 34 | + }) |
| 35 | + |
| 36 | + it('renders the canonical request section only through the shared organization gate', async () => { |
| 37 | + const page = await OrganizationSettingsSectionPage({ |
| 38 | + params: Promise.resolve({ organizationId: 'organization', section: 'requests' }), |
| 39 | + }) |
| 40 | + expect(page.props.section).toBe('requests') |
| 41 | + expect(mocks.authorize).toHaveBeenCalledWith({ |
| 42 | + organizationId: 'organization', |
| 43 | + userId: 'viewer', |
| 44 | + section: 'requests', |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + it('authorizes saved review tabs as Requests and preserves their selected request', async () => { |
| 49 | + await expect( |
| 50 | + OrganizationSettingsSectionPage({ |
| 51 | + params: Promise.resolve({ organizationId: 'organization', section: 'access-control' }), |
| 52 | + searchParams: Promise.resolve({ |
| 53 | + 'access-view': 'requests', |
| 54 | + 'request-id': 'selected', |
| 55 | + 'request-status': 'all', |
| 56 | + 'group-id': 'old-group', |
| 57 | + }), |
| 58 | + }) |
| 59 | + ).rejects.toThrow( |
| 60 | + 'redirect:/o/organization/settings/requests?request-id=selected&request-status=all' |
| 61 | + ) |
| 62 | + expect(mocks.authorize).toHaveBeenCalledWith({ |
| 63 | + organizationId: 'organization', |
| 64 | + userId: 'viewer', |
| 65 | + section: 'requests', |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + it('conceals requests from viewers rejected by the organization gate', async () => { |
| 70 | + mocks.authorize.mockResolvedValue(false) |
| 71 | + await expect( |
| 72 | + OrganizationSettingsSectionPage({ |
| 73 | + params: Promise.resolve({ organizationId: 'organization', section: 'access-control' }), |
| 74 | + searchParams: Promise.resolve({ 'access-view': 'requests' }), |
| 75 | + }) |
| 76 | + ).rejects.toThrow('not-found') |
| 77 | + }) |
| 78 | +}) |
0 commit comments