|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockGetSession, mockRedirect, baseUrl } = vi.hoisted(() => ({ |
| 7 | + mockGetSession: vi.fn(), |
| 8 | + mockRedirect: vi.fn((url: string) => { |
| 9 | + throw new Error(`NEXT_REDIRECT:${url}`) |
| 10 | + }), |
| 11 | + /** Mutable so a test can give the deployment a trailing-slash base URL. */ |
| 12 | + baseUrl: { value: 'https://sim.test' }, |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock('@/lib/auth', () => ({ |
| 16 | + auth: { api: { getSession: mockGetSession } }, |
| 17 | + getSession: vi.fn(), |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock('@/lib/auth/auth-client', () => ({ |
| 21 | + client: { oauth2: { link: vi.fn() } }, |
| 22 | + signOut: vi.fn(), |
| 23 | +})) |
| 24 | + |
| 25 | +vi.mock('@/lib/core/utils/urls', () => ({ |
| 26 | + getBaseUrl: () => baseUrl.value, |
| 27 | +})) |
| 28 | + |
| 29 | +/** Keeps the landing-page barrel the real shell pulls in out of this graph. */ |
| 30 | +vi.mock('@/app/desktop/components/desktop-handoff-shell', () => ({ |
| 31 | + DesktopHandoffShell: () => null, |
| 32 | +})) |
| 33 | + |
| 34 | +vi.mock('next/navigation', () => ({ |
| 35 | + redirect: mockRedirect, |
| 36 | +})) |
| 37 | + |
| 38 | +vi.mock('next/headers', () => ({ |
| 39 | + headers: vi.fn(async () => new Headers()), |
| 40 | +})) |
| 41 | + |
| 42 | +import DesktopConnectPage from '@/app/desktop/connect/page' |
| 43 | + |
| 44 | +const VALID_STATE = 'a'.repeat(32) |
| 45 | +const PORT = '57979' |
| 46 | + |
| 47 | +function pageProps(params: Record<string, string>) { |
| 48 | + return { searchParams: Promise.resolve(params) } |
| 49 | +} |
| 50 | + |
| 51 | +async function renderPage(params: Record<string, string>) { |
| 52 | + const result = (await DesktopConnectPage(pageProps(params))) as unknown as { |
| 53 | + type: { name: string } |
| 54 | + props: Record<string, unknown> |
| 55 | + } |
| 56 | + return result |
| 57 | +} |
| 58 | + |
| 59 | +describe('DesktopConnectPage', () => { |
| 60 | + beforeEach(() => { |
| 61 | + vi.clearAllMocks() |
| 62 | + baseUrl.value = 'https://sim.test' |
| 63 | + mockGetSession.mockResolvedValue({ user: { id: 'user-1', email: 'user@example.com' } }) |
| 64 | + }) |
| 65 | + |
| 66 | + it('hands the launcher an absolute complete URL so the callback can read the draft back', async () => { |
| 67 | + // Better Auth stores `callbackURL` verbatim, and the OAuth callback parses it |
| 68 | + // with `new URL`. A bare path threw there, failing the whole callback with a |
| 69 | + // 500 after the provider had already authorized. |
| 70 | + const result = await renderPage({ |
| 71 | + provider: 'google-email', |
| 72 | + state: VALID_STATE, |
| 73 | + port: PORT, |
| 74 | + draftId: 'draft-1', |
| 75 | + }) |
| 76 | + |
| 77 | + expect(result.type.name).toBe('ConnectLauncher') |
| 78 | + expect(result.props.providerId).toBe('google-email') |
| 79 | + |
| 80 | + const completeUrl = new URL(result.props.completeUrl as string) |
| 81 | + expect(completeUrl.origin).toBe('https://sim.test') |
| 82 | + expect(completeUrl.pathname).toBe('/desktop/connect/complete') |
| 83 | + expect(completeUrl.searchParams.get('state')).toBe(VALID_STATE) |
| 84 | + expect(completeUrl.searchParams.get('port')).toBe(PORT) |
| 85 | + expect(completeUrl.searchParams.get('credentialDraftId')).toBe('draft-1') |
| 86 | + }) |
| 87 | + |
| 88 | + it('keeps the complete URL absolute when no draft rides along', async () => { |
| 89 | + const result = await renderPage({ |
| 90 | + provider: 'google-email', |
| 91 | + state: VALID_STATE, |
| 92 | + port: PORT, |
| 93 | + }) |
| 94 | + |
| 95 | + expect(result.type.name).toBe('ConnectLauncher') |
| 96 | + expect(() => new URL(result.props.completeUrl as string)).not.toThrow() |
| 97 | + }) |
| 98 | + |
| 99 | + it('keeps the completion route intact when the deployment base URL has a trailing slash', async () => { |
| 100 | + // `//desktop/connect/complete` matches no route, so the provider result |
| 101 | + // would never reach the loopback and the connect would hang. |
| 102 | + baseUrl.value = 'https://sim.test/' |
| 103 | + |
| 104 | + const launcher = await renderPage({ |
| 105 | + provider: 'google-email', |
| 106 | + state: VALID_STATE, |
| 107 | + port: PORT, |
| 108 | + }) |
| 109 | + expect(new URL(launcher.props.completeUrl as string).pathname).toBe('/desktop/connect/complete') |
| 110 | + |
| 111 | + await expect( |
| 112 | + DesktopConnectPage( |
| 113 | + pageProps({ |
| 114 | + provider: 'google-email', |
| 115 | + state: VALID_STATE, |
| 116 | + port: PORT, |
| 117 | + workspaceId: 'workspace-1', |
| 118 | + }) |
| 119 | + ) |
| 120 | + ).rejects.toThrow('NEXT_REDIRECT:') |
| 121 | + const callbackUrl = new URL(mockRedirect.mock.calls[0][0]).searchParams.get('callbackURL') |
| 122 | + expect(new URL(callbackUrl as string).pathname).toBe('/desktop/connect/complete') |
| 123 | + }) |
| 124 | + |
| 125 | + it('sends a workspace-scoped connect to the authorize route with an absolute callback', async () => { |
| 126 | + await expect( |
| 127 | + DesktopConnectPage( |
| 128 | + pageProps({ |
| 129 | + provider: 'google-email', |
| 130 | + state: VALID_STATE, |
| 131 | + port: PORT, |
| 132 | + workspaceId: 'workspace-1', |
| 133 | + }) |
| 134 | + ) |
| 135 | + ).rejects.toThrow('NEXT_REDIRECT:') |
| 136 | + |
| 137 | + const authorize = new URL(mockRedirect.mock.calls[0][0]) |
| 138 | + expect(authorize.pathname).toBe('/api/auth/oauth2/authorize') |
| 139 | + expect(authorize.searchParams.get('providerId')).toBe('google-email') |
| 140 | + expect(authorize.searchParams.get('workspaceId')).toBe('workspace-1') |
| 141 | + expect(authorize.searchParams.get('callbackURL')).toBe( |
| 142 | + `https://sim.test/desktop/connect/complete?state=${VALID_STATE}&port=${PORT}` |
| 143 | + ) |
| 144 | + }) |
| 145 | + |
| 146 | + it('rejects a malformed request without reading the session', async () => { |
| 147 | + const invalid = [ |
| 148 | + { provider: 'Google', state: VALID_STATE, port: PORT }, |
| 149 | + { provider: 'google-email', state: 'short', port: PORT }, |
| 150 | + { provider: 'google-email', state: VALID_STATE }, |
| 151 | + { provider: 'google-email', state: VALID_STATE, port: PORT, draftId: 'bad draft' }, |
| 152 | + ] |
| 153 | + |
| 154 | + for (const params of invalid) { |
| 155 | + const result = await renderPage(params) |
| 156 | + expect(result.type.name).toBe('InvalidRequest') |
| 157 | + } |
| 158 | + expect(mockGetSession).not.toHaveBeenCalled() |
| 159 | + }) |
| 160 | +}) |
0 commit comments