|
| 1 | +/** |
| 2 | + * #26473 — the console's own `/login` and `/register`, opened signed out from |
| 3 | + * an invitation link, greet the visitor by WHO invited them and to WHAT. |
| 4 | + * |
| 5 | + * The read is app-shell's `useInvitationPreview` over the server's signed-out |
| 6 | + * `GET {authUrl}/organization/invitation-preview?id=ID`; the package's default |
| 7 | + * pages carry the same pins in |
| 8 | + * `packages/app-shell/src/console/auth/__tests__/invitationPreview-26473.test.tsx`. |
| 9 | + * Pinned here for the pages the console actually routes: |
| 10 | + * |
| 11 | + * - found: the card names the inviter and the organization and shows the |
| 12 | + * MASKED address as the one to use; the email field stays empty; |
| 13 | + * - 404: today's generic invitation copy; |
| 14 | + * - a failed read: today's generic copy too, and the form still renders. |
| 15 | + * |
| 16 | + * Rendered with a real `AuthProvider` over a real `createAuthClient`; only |
| 17 | + * `fetch` is a stub. Generic copy is read from the en pack. |
| 18 | + */ |
| 19 | + |
| 20 | +import '@testing-library/jest-dom/vitest'; |
| 21 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 22 | +import { act, render, screen, cleanup, waitFor } from '@testing-library/react'; |
| 23 | +import { MemoryRouter, Routes, Route } from 'react-router-dom'; |
| 24 | +import { I18nProvider } from '@object-ui/i18n'; |
| 25 | +import { builtInLocales } from '@object-ui/i18n/locales'; |
| 26 | +import { AuthProvider, createAuthClient } from '@object-ui/auth'; |
| 27 | +import type { AuthPublicConfig } from '@object-ui/auth'; |
| 28 | +import { LoginPage } from '../LoginPage'; |
| 29 | +import { RegisterPage } from '../RegisterPage'; |
| 30 | + |
| 31 | +const AUTH_URL = 'http://localhost/api/v1/auth'; |
| 32 | +const en = builtInLocales.en.auth; |
| 33 | +const INVITATION_ID = 'inv_26473'; |
| 34 | +const INVITE_QUERY = `?redirect=${encodeURIComponent(`/accept-invitation/${INVITATION_ID}`)}`; |
| 35 | +const ORG = 'Xiaolin Decoration Studio'; |
| 36 | +const INVITER = 'Lin Xiao'; |
| 37 | +const MASKED = 'z***@example.com'; |
| 38 | +const MASKED_TEXT = new RegExp(MASKED.replace(/[*.]/g, '\\$&')); |
| 39 | + |
| 40 | +const CONFIG: AuthPublicConfig = { |
| 41 | + emailPassword: { enabled: true, disableSignUp: false, requireEmailVerification: false }, |
| 42 | + features: { audiencePosture: 'invite_only' }, |
| 43 | +}; |
| 44 | + |
| 45 | +type PreviewAnswer = 'found' | 404 | 500; |
| 46 | +let previewRequests: string[]; |
| 47 | + |
| 48 | +function stubServer(answer: PreviewAnswer): typeof fetch { |
| 49 | + previewRequests = []; |
| 50 | + const json = (body: unknown, status = 200) => |
| 51 | + new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }); |
| 52 | + return (async (input: string | URL | Request) => { |
| 53 | + const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url; |
| 54 | + if (url.includes('/organization/invitation-preview')) { |
| 55 | + previewRequests.push(url); |
| 56 | + if (answer === 'found') { |
| 57 | + return json({ success: true, data: { organizationName: ORG, inviterName: INVITER, email: MASKED } }); |
| 58 | + } |
| 59 | + return json( |
| 60 | + { |
| 61 | + success: false, |
| 62 | + error: |
| 63 | + answer === 404 |
| 64 | + ? { code: 'RESOURCE_NOT_FOUND', message: 'Invitation not found.' } |
| 65 | + : { code: 'INTERNAL_ERROR', message: 'The invitation preview is unavailable.' }, |
| 66 | + }, |
| 67 | + answer, |
| 68 | + ); |
| 69 | + } |
| 70 | + if (url.endsWith('/bootstrap-status')) return json({ hasOwner: true }); |
| 71 | + if (url.endsWith('/config')) return json({ success: true, data: CONFIG }); |
| 72 | + return json(null); |
| 73 | + }) as typeof fetch; |
| 74 | +} |
| 75 | + |
| 76 | +function renderAt(path: string, answer: PreviewAnswer) { |
| 77 | + const fetchFn = stubServer(answer); |
| 78 | + vi.stubGlobal('fetch', fetchFn); |
| 79 | + window.history.replaceState({}, '', path); |
| 80 | + const client = createAuthClient({ baseURL: AUTH_URL, fetchFn }); |
| 81 | + return render( |
| 82 | + <I18nProvider config={{ defaultLanguage: 'en', detectBrowserLanguage: false }} persistLanguage={false}> |
| 83 | + <AuthProvider authUrl={AUTH_URL} client={client}> |
| 84 | + <MemoryRouter initialEntries={[path]}> |
| 85 | + <Routes> |
| 86 | + <Route path="/login" element={<LoginPage />} /> |
| 87 | + <Route path="/register" element={<RegisterPage />} /> |
| 88 | + </Routes> |
| 89 | + </MemoryRouter> |
| 90 | + </AuthProvider> |
| 91 | + </I18nProvider>, |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +async function previewAnswered() { |
| 96 | + await waitFor(() => expect(previewRequests).toHaveLength(1)); |
| 97 | + await act(async () => { |
| 98 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +beforeEach(() => { |
| 103 | + window.localStorage.clear(); |
| 104 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 105 | +}); |
| 106 | + |
| 107 | +afterEach(() => { |
| 108 | + cleanup(); |
| 109 | + vi.unstubAllGlobals(); |
| 110 | + vi.restoreAllMocks(); |
| 111 | + window.history.replaceState({}, '', '/'); |
| 112 | +}); |
| 113 | + |
| 114 | +describe('console /login — the invitation landing reads the signed-out preview (#26473)', () => { |
| 115 | + it('found: names the inviter and the organization, shows the masked address, prefills nothing', async () => { |
| 116 | + renderAt(`/login${INVITE_QUERY}`, 'found'); |
| 117 | + |
| 118 | + const heading = await screen.findByRole('heading', { name: new RegExp(ORG) }); |
| 119 | + expect(heading).toHaveTextContent(INVITER); |
| 120 | + expect(screen.getByText(MASKED_TEXT)).toBeInTheDocument(); |
| 121 | + expect(screen.queryByRole('heading', { name: en.login.invitationTitle })).toBeNull(); |
| 122 | + expect(previewRequests).toEqual([`${AUTH_URL}/organization/invitation-preview?id=${INVITATION_ID}`]); |
| 123 | + expect(await screen.findByLabelText(en.login.emailLabel)).toHaveValue(''); |
| 124 | + }); |
| 125 | + |
| 126 | + it('404: keeps the generic invitation copy', async () => { |
| 127 | + renderAt(`/login${INVITE_QUERY}`, 404); |
| 128 | + await previewAnswered(); |
| 129 | + |
| 130 | + expect(await screen.findByRole('heading', { name: en.login.invitationTitle })).toBeInTheDocument(); |
| 131 | + expect(screen.queryByText(new RegExp(ORG))).toBeNull(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('a failed read: keeps the generic copy and the sign-in form', async () => { |
| 135 | + renderAt(`/login${INVITE_QUERY}`, 500); |
| 136 | + await previewAnswered(); |
| 137 | + |
| 138 | + expect(await screen.findByRole('heading', { name: en.login.invitationTitle })).toBeInTheDocument(); |
| 139 | + expect(await screen.findByLabelText(en.login.emailLabel)).toBeInTheDocument(); |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +describe('console /register — the sign-up step reads the same preview (#26473)', () => { |
| 144 | + it('found: names the inviter and the organization, shows the masked address, prefills nothing', async () => { |
| 145 | + renderAt(`/register${INVITE_QUERY}`, 'found'); |
| 146 | + |
| 147 | + const heading = await screen.findByRole('heading', { name: new RegExp(ORG) }); |
| 148 | + expect(heading).toHaveTextContent(INVITER); |
| 149 | + expect(screen.getByText(MASKED_TEXT)).toBeInTheDocument(); |
| 150 | + expect(screen.getByLabelText(en.register.emailLabel)).toHaveValue(''); |
| 151 | + }); |
| 152 | + |
| 153 | + it('404: keeps the generic invitation copy', async () => { |
| 154 | + renderAt(`/register${INVITE_QUERY}`, 404); |
| 155 | + await previewAnswered(); |
| 156 | + |
| 157 | + expect(await screen.findByRole('heading', { name: en.register.invitationTitle })).toBeInTheDocument(); |
| 158 | + }); |
| 159 | + |
| 160 | + it('a failed read: keeps the generic copy and the form', async () => { |
| 161 | + renderAt(`/register${INVITE_QUERY}`, 500); |
| 162 | + await previewAnswered(); |
| 163 | + |
| 164 | + expect(await screen.findByRole('heading', { name: en.register.invitationTitle })).toBeInTheDocument(); |
| 165 | + expect(screen.getByLabelText(en.register.emailLabel)).toBeInTheDocument(); |
| 166 | + }); |
| 167 | +}); |
0 commit comments