|
| 1 | +/** |
| 2 | + * #27509 — an invitation link the server no longer knows, on the console's |
| 3 | + * own `/login` and `/register`. The package's default pages carry the same |
| 4 | + * pins in |
| 5 | + * `packages/app-shell/src/console/auth/__tests__/invitationGone-27509.test.tsx`. |
| 6 | + * |
| 7 | + * The signed-out preview door answers ONE 404 body |
| 8 | + * (`RESOURCE_NOT_FOUND`) for an unknown, expired, accepted, rejected or |
| 9 | + * cancelled id (ADR-0138 D2). Both pages used to read it as "no preview" and |
| 10 | + * still offer the accept-and-register flow, whose refusal then told the link |
| 11 | + * holder to ask for an invitation. Pinned here: |
| 12 | + * |
| 13 | + * - the door's 404: the "no longer valid" notice, with no sign-up prompt and |
| 14 | + * no form, on both pages; |
| 15 | + * - a sign-up refused with `SELF_REGISTRATION_CLOSED` from an invitation |
| 16 | + * link re-reads the preview: an answer keeps the form and the refusal names |
| 17 | + * the masked address the invitation was sent to; the door's 404 turns the |
| 18 | + * page into the notice. |
| 19 | + * |
| 20 | + * A failed read and a server without the route keep today's generic copy; |
| 21 | + * those pins live in `invitationPreview-26473.test.tsx` beside this file. |
| 22 | + * |
| 23 | + * Rendered with a real `AuthProvider` over a real `createAuthClient` and a |
| 24 | + * real `I18nProvider`; only `fetch` is a stub. Copy is read from the en pack. |
| 25 | + */ |
| 26 | + |
| 27 | +import '@testing-library/jest-dom/vitest'; |
| 28 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 29 | +import { render, screen, cleanup, waitFor } from '@testing-library/react'; |
| 30 | +import userEvent from '@testing-library/user-event'; |
| 31 | +import { MemoryRouter, Routes, Route } from 'react-router-dom'; |
| 32 | +import { I18nProvider } from '@object-ui/i18n'; |
| 33 | +import { builtInLocales } from '@object-ui/i18n/locales'; |
| 34 | +import { AuthProvider, createAuthClient } from '@object-ui/auth'; |
| 35 | +import type { AuthPublicConfig } from '@object-ui/auth'; |
| 36 | +import { LoginPage } from '../LoginPage'; |
| 37 | +import { RegisterPage } from '../RegisterPage'; |
| 38 | + |
| 39 | +const AUTH_URL = 'http://localhost/api/v1/auth'; |
| 40 | +const en = builtInLocales.en.auth; |
| 41 | +const INVITATION_ID = 'inv_27509'; |
| 42 | +const INVITE_QUERY = `?redirect=${encodeURIComponent(`/accept-invitation/${INVITATION_ID}`)}`; |
| 43 | +const ORG = 'Default Organization'; |
| 44 | +const MASKED = 'i***@example.com'; |
| 45 | + |
| 46 | +const CONFIG: AuthPublicConfig = { |
| 47 | + emailPassword: { enabled: true, disableSignUp: false, requireEmailVerification: false }, |
| 48 | + features: { audiencePosture: 'invite_only' }, |
| 49 | +}; |
| 50 | + |
| 51 | +/** What the preview door answers, one entry per read; the last repeats. */ |
| 52 | +type PreviewAnswer = 'found' | 'gone'; |
| 53 | + |
| 54 | +let previewRequests: string[]; |
| 55 | +let signUpRequests: number; |
| 56 | + |
| 57 | +function stubServer(answers: PreviewAnswer[]): typeof fetch { |
| 58 | + previewRequests = []; |
| 59 | + signUpRequests = 0; |
| 60 | + const json = (body: unknown, status = 200) => |
| 61 | + new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }); |
| 62 | + return (async (input: string | URL | Request) => { |
| 63 | + const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url; |
| 64 | + if (url.includes('/organization/invitation-preview')) { |
| 65 | + const answer = answers[Math.min(previewRequests.length, answers.length - 1)]; |
| 66 | + previewRequests.push(url); |
| 67 | + return answer === 'found' |
| 68 | + ? json({ success: true, data: { organizationName: ORG, inviterName: 'Dev Admin', email: MASKED } }) |
| 69 | + : json( |
| 70 | + { |
| 71 | + success: false, |
| 72 | + error: { |
| 73 | + code: 'RESOURCE_NOT_FOUND', |
| 74 | + message: 'Invitation not found. It may have expired, been used, or been withdrawn.', |
| 75 | + }, |
| 76 | + }, |
| 77 | + 404, |
| 78 | + ); |
| 79 | + } |
| 80 | + if (url.includes('/sign-up/email')) { |
| 81 | + signUpRequests += 1; |
| 82 | + return json( |
| 83 | + { code: 'SELF_REGISTRATION_CLOSED', message: 'Self-registration is closed on this environment.' }, |
| 84 | + 403, |
| 85 | + ); |
| 86 | + } |
| 87 | + if (url.endsWith('/bootstrap-status')) return json({ hasOwner: true }); |
| 88 | + if (url.endsWith('/config')) return json({ success: true, data: CONFIG }); |
| 89 | + return json(null); |
| 90 | + }) as typeof fetch; |
| 91 | +} |
| 92 | + |
| 93 | +function renderAt(path: string, answers: PreviewAnswer[]) { |
| 94 | + const fetchFn = stubServer(answers); |
| 95 | + vi.stubGlobal('fetch', fetchFn); |
| 96 | + window.history.replaceState({}, '', path); |
| 97 | + const client = createAuthClient({ baseURL: AUTH_URL, fetchFn }); |
| 98 | + return render( |
| 99 | + <I18nProvider config={{ defaultLanguage: 'en', detectBrowserLanguage: false }} persistLanguage={false}> |
| 100 | + <AuthProvider authUrl={AUTH_URL} client={client}> |
| 101 | + <MemoryRouter initialEntries={[path]}> |
| 102 | + <Routes> |
| 103 | + <Route path="/login" element={<LoginPage />} /> |
| 104 | + <Route path="/register" element={<RegisterPage />} /> |
| 105 | + </Routes> |
| 106 | + </MemoryRouter> |
| 107 | + </AuthProvider> |
| 108 | + </I18nProvider>, |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +async function submitRegistration(email: string) { |
| 113 | + const user = userEvent.setup(); |
| 114 | + await user.type(screen.getByLabelText(en.register.nameLabel), 'Invitee'); |
| 115 | + await user.type(screen.getByLabelText(en.register.emailLabel), email); |
| 116 | + await user.type(screen.getByLabelText(en.register.passwordLabel), 'correct-horse-1'); |
| 117 | + await user.type(screen.getByLabelText(en.register.confirmPasswordLabel), 'correct-horse-1'); |
| 118 | + await user.click(screen.getByRole('button', { name: en.register.submitButton })); |
| 119 | +} |
| 120 | + |
| 121 | +beforeEach(() => { |
| 122 | + window.localStorage.clear(); |
| 123 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 124 | +}); |
| 125 | + |
| 126 | +afterEach(() => { |
| 127 | + cleanup(); |
| 128 | + vi.unstubAllGlobals(); |
| 129 | + vi.restoreAllMocks(); |
| 130 | + window.history.replaceState({}, '', '/'); |
| 131 | +}); |
| 132 | + |
| 133 | +describe('the door\'s 404: the invitation link is no longer valid (#27509)', () => { |
| 134 | + it('console /login: the notice, with no sign-up prompt and no sign-in form', async () => { |
| 135 | + renderAt(`/login${INVITE_QUERY}`, ['gone']); |
| 136 | + |
| 137 | + expect(await screen.findByTestId('invitation-gone')).toBeInTheDocument(); |
| 138 | + expect(screen.getByRole('heading', { name: en.login.invitationGoneTitle })).toBeInTheDocument(); |
| 139 | + expect(screen.queryByText(en.login.invitationSignUpButton)).toBeNull(); |
| 140 | + expect(screen.queryByLabelText(en.login.emailLabel)).toBeNull(); |
| 141 | + expect(screen.queryByRole('heading', { name: en.login.invitationTitle })).toBeNull(); |
| 142 | + // The way on is the plain sign-in, not back into the invitation. |
| 143 | + expect(screen.getByRole('link', { name: en.login.invitationGoneSignIn })).toHaveAttribute('href', '/login'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('console /register: the notice, with no registration form', async () => { |
| 147 | + renderAt(`/register${INVITE_QUERY}`, ['gone']); |
| 148 | + |
| 149 | + expect(await screen.findByTestId('invitation-gone')).toBeInTheDocument(); |
| 150 | + expect(screen.getByRole('heading', { name: en.login.invitationGoneTitle })).toBeInTheDocument(); |
| 151 | + expect(screen.queryByLabelText(en.register.emailLabel)).toBeNull(); |
| 152 | + expect(screen.queryByRole('heading', { name: en.register.invitationTitle })).toBeNull(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('a live invitation keeps the prompt and the form (the control)', async () => { |
| 156 | + renderAt(`/login${INVITE_QUERY}`, ['found']); |
| 157 | + |
| 158 | + expect(await screen.findByRole('heading', { name: new RegExp(ORG) })).toBeInTheDocument(); |
| 159 | + expect(await screen.findByText(en.login.invitationSignUpButton)).toBeInTheDocument(); |
| 160 | + expect(screen.getByLabelText(en.login.emailLabel)).toBeInTheDocument(); |
| 161 | + expect(screen.queryByTestId('invitation-gone')).toBeNull(); |
| 162 | + }); |
| 163 | +}); |
| 164 | + |
| 165 | +describe('console /register — a refused invitation sign-up re-reads the preview (#27509)', () => { |
| 166 | + it('still pending: keeps the form, and the refusal names the masked invited address', async () => { |
| 167 | + renderAt(`/register${INVITE_QUERY}`, ['found', 'found']); |
| 168 | + await screen.findByRole('heading', { name: new RegExp(ORG) }); |
| 169 | + |
| 170 | + await submitRegistration('someone@example.com'); |
| 171 | + |
| 172 | + const sentTo = en.register.errors.invitationSentTo.replace('{{email}}', MASKED); |
| 173 | + expect(await screen.findByText(sentTo)).toBeInTheDocument(); |
| 174 | + expect(screen.queryByText(en.register.errors.selfRegistrationClosed)).toBeNull(); |
| 175 | + expect(signUpRequests).toBe(1); |
| 176 | + await waitFor(() => expect(previewRequests).toHaveLength(2)); |
| 177 | + expect(screen.getByLabelText(en.register.emailLabel)).toBeInTheDocument(); |
| 178 | + }); |
| 179 | + |
| 180 | + it('gone meanwhile: the page becomes the notice', async () => { |
| 181 | + renderAt(`/register${INVITE_QUERY}`, ['found', 'gone']); |
| 182 | + await screen.findByRole('heading', { name: new RegExp(ORG) }); |
| 183 | + |
| 184 | + await submitRegistration('invitee2@example.com'); |
| 185 | + |
| 186 | + expect(await screen.findByTestId('invitation-gone')).toBeInTheDocument(); |
| 187 | + expect(previewRequests).toHaveLength(2); |
| 188 | + expect(screen.queryByLabelText(en.register.emailLabel)).toBeNull(); |
| 189 | + }); |
| 190 | +}); |
0 commit comments