|
| 1 | +import { vi } from 'vitest'; |
| 2 | + |
| 3 | +vi.mock('@/utils/auth', async () => { |
| 4 | + const { mockAuth } = await import('@/test-utils/mocks/auth'); |
| 5 | + return { auth: mockAuth }; |
| 6 | +}); |
| 7 | + |
| 8 | +const { mockServerApiGet, mockCreateSetupSession } = vi.hoisted(() => ({ |
| 9 | + mockServerApiGet: vi.fn(), |
| 10 | + mockCreateSetupSession: vi.fn(async () => ({ id: 'setup_1' })), |
| 11 | +})); |
| 12 | +vi.mock('@/lib/api-server', () => ({ serverApi: { get: mockServerApiGet } })); |
| 13 | +vi.mock('./lib/setup-session', () => ({ createSetupSession: mockCreateSetupSession })); |
| 14 | + |
| 15 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 16 | +import { NextRequest } from 'next/server'; |
| 17 | +import { redirect } from 'next/navigation'; |
| 18 | +import { GET } from './route'; |
| 19 | +import { mockAuthApi, createMockSession, createMockUser } from '@/test-utils/mocks/auth'; |
| 20 | + |
| 21 | +const mockRedirect = vi.mocked(redirect); |
| 22 | + |
| 23 | +interface MeData { |
| 24 | + organizations: unknown[]; |
| 25 | + pendingInvitation: { id: string } | null; |
| 26 | + hasInactiveMembership?: boolean; |
| 27 | +} |
| 28 | +const mockMe = (data: MeData) => mockServerApiGet.mockResolvedValue({ data, status: 200 }); |
| 29 | +const call = (url: string) => GET(new NextRequest(url)); |
| 30 | + |
| 31 | +describe('/setup route — CS-569 offboard guard vs invite precedence', () => { |
| 32 | + beforeEach(() => { |
| 33 | + vi.clearAllMocks(); |
| 34 | + mockCreateSetupSession.mockResolvedValue({ id: 'setup_1' }); |
| 35 | + mockRedirect.mockImplementation((url: string) => { |
| 36 | + throw new Error(`REDIRECT:${url}`); |
| 37 | + }); |
| 38 | + mockAuthApi.getSession.mockResolvedValue({ |
| 39 | + session: createMockSession(), |
| 40 | + user: createMockUser(), |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('redirects an offboarded user with no invite to access-removed', async () => { |
| 45 | + mockMe({ organizations: [], pendingInvitation: null, hasInactiveMembership: true }); |
| 46 | + |
| 47 | + await expect(call('http://localhost/setup')).rejects.toThrow('REDIRECT:/auth/access-removed'); |
| 48 | + expect(mockCreateSetupSession).not.toHaveBeenCalled(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('does NOT block an offboarded user arriving via ?inviteCode= (invite handled downstream)', async () => { |
| 52 | + // Regression (cubic): the guard must not pre-empt the legacy invite entry. |
| 53 | + mockMe({ organizations: [], pendingInvitation: null, hasInactiveMembership: true }); |
| 54 | + |
| 55 | + await expect(call('http://localhost/setup?inviteCode=inv_abc')).rejects.toThrow( |
| 56 | + 'REDIRECT:/setup/setup_1?inviteCode=inv_abc', |
| 57 | + ); |
| 58 | + // Guard is skipped entirely when an invite code is present. |
| 59 | + expect(mockServerApiGet).not.toHaveBeenCalled(); |
| 60 | + expect(mockRedirect).not.toHaveBeenCalledWith('/auth/access-removed'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('routes an offboarded user with a pending invitation to the invite, not access-removed', async () => { |
| 64 | + mockMe({ |
| 65 | + organizations: [], |
| 66 | + pendingInvitation: { id: 'inv_xyz' }, |
| 67 | + hasInactiveMembership: true, |
| 68 | + }); |
| 69 | + |
| 70 | + await expect(call('http://localhost/setup')).rejects.toThrow('REDIRECT:/invite/inv_xyz'); |
| 71 | + expect(mockRedirect).not.toHaveBeenCalledWith('/auth/access-removed'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('lets a genuinely new user through to onboarding', async () => { |
| 75 | + mockMe({ organizations: [], pendingInvitation: null, hasInactiveMembership: false }); |
| 76 | + |
| 77 | + await expect(call('http://localhost/setup')).rejects.toThrow('REDIRECT:/setup/setup_1'); |
| 78 | + expect(mockCreateSetupSession).toHaveBeenCalled(); |
| 79 | + }); |
| 80 | +}); |
0 commit comments