|
| 1 | +/** |
| 2 | + * #30125 — registering from an invitation link joins that invitation's |
| 3 | + * organization and enters the console, instead of stopping on the accept page |
| 4 | + * ("Accept invitation") and then on "Invitation accepted … Continue". |
| 5 | + * |
| 6 | + * Pinned on the console's own `/register`, the page the console routes: |
| 7 | + * |
| 8 | + * - the sign-up accepts the invitation its `?redirect=` names, switches the |
| 9 | + * session to that organization, and leaves for the console root once; |
| 10 | + * - the server refusing the read (an address that is not the invited one): |
| 11 | + * nothing is accepted and the page opens the accept page, as before; |
| 12 | + * - a person already signed in when the page opens accepts nothing here. |
| 13 | + * |
| 14 | + * The real `AuthProvider` runs over a mock client, so the session appears the |
| 15 | + * way a sign-up makes it appear. The Console is mounted as the shipped build |
| 16 | + * is (`<base href="/_console/">`), so "the root" is read where the browser |
| 17 | + * would land. The package's `DefaultRegisterPage` carries the same pins in |
| 18 | + * `packages/app-shell/src/console/auth/__tests__/invitationSignUpJoin-30125.test.tsx`. |
| 19 | + */ |
| 20 | + |
| 21 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 22 | +import { render, screen, cleanup, waitFor } from '@testing-library/react'; |
| 23 | +import userEvent from '@testing-library/user-event'; |
| 24 | +import { MemoryRouter } from 'react-router-dom'; |
| 25 | +import { AuthProvider } from '@object-ui/auth'; |
| 26 | +import type { AuthClient } from '@object-ui/auth'; |
| 27 | +import { RegisterPage } from '../RegisterPage'; |
| 28 | + |
| 29 | +const INVITATION_ID = 'inv_30125'; |
| 30 | +const ACCEPT_PATH = `/accept-invitation/${INVITATION_ID}`; |
| 31 | +const ORG = { id: 'org_acme', name: 'Acme', slug: 'acme' }; |
| 32 | +const USER = { id: 'u_new', name: 'Zhang', email: 'zhang@example.com' }; |
| 33 | + |
| 34 | +let baseEl: HTMLBaseElement | null = null; |
| 35 | +let assign: ReturnType<typeof vi.fn>; |
| 36 | + |
| 37 | +beforeEach(() => { |
| 38 | + window.localStorage.clear(); |
| 39 | + baseEl = document.createElement('base'); |
| 40 | + baseEl.setAttribute('href', '/_console/'); |
| 41 | + document.head.appendChild(baseEl); |
| 42 | + vi.stubEnv('BASE_URL', './'); |
| 43 | + assign = vi.fn(); |
| 44 | + vi.spyOn(window.location, 'assign').mockImplementation(assign as never); |
| 45 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 46 | +}); |
| 47 | + |
| 48 | +afterEach(() => { |
| 49 | + cleanup(); |
| 50 | + baseEl?.remove(); |
| 51 | + baseEl = null; |
| 52 | + vi.unstubAllEnvs(); |
| 53 | + vi.restoreAllMocks(); |
| 54 | +}); |
| 55 | + |
| 56 | +interface ServerOptions { |
| 57 | + /** Signed in already when the page opens. */ |
| 58 | + signedInOnArrival?: boolean; |
| 59 | + /** `get-invitation` refuses this account (better-auth's recipient check). */ |
| 60 | + refuseRecipient?: boolean; |
| 61 | +} |
| 62 | + |
| 63 | +function createMockClient({ signedInOnArrival = false, refuseRecipient = false }: ServerOptions = {}) { |
| 64 | + let signedIn = signedInOnArrival; |
| 65 | + let member = false; |
| 66 | + let active: typeof ORG | null = null; |
| 67 | + const session = () => ({ user: USER, session: { token: 't', createdAt: new Date() } }); |
| 68 | + const client = { |
| 69 | + getSession: vi.fn(async () => (signedIn ? session() : null)), |
| 70 | + getConfig: vi.fn().mockResolvedValue({ |
| 71 | + emailPassword: { enabled: true, disableSignUp: false, requireEmailVerification: false }, |
| 72 | + features: { audiencePosture: 'invite_only' }, |
| 73 | + }), |
| 74 | + getInvitationPreview: vi.fn().mockResolvedValue({ |
| 75 | + organizationName: ORG.name, |
| 76 | + inviterName: 'Lin', |
| 77 | + email: 'z***@example.com', |
| 78 | + }), |
| 79 | + signUp: vi.fn(async () => { |
| 80 | + signedIn = true; |
| 81 | + return { user: USER, session: undefined, requiresVerification: false }; |
| 82 | + }), |
| 83 | + getInvitation: vi.fn(async (id: string) => { |
| 84 | + if (refuseRecipient) { |
| 85 | + throw Object.assign(new Error('You are not the recipient of the invitation'), { |
| 86 | + code: 'YOU_ARE_NOT_THE_RECIPIENT_OF_THE_INVITATION', |
| 87 | + status: 403, |
| 88 | + }); |
| 89 | + } |
| 90 | + return { id, organizationId: ORG.id, email: USER.email, role: 'member', status: 'pending' }; |
| 91 | + }), |
| 92 | + acceptInvitation: vi.fn(async () => { |
| 93 | + member = true; |
| 94 | + }), |
| 95 | + listOrganizations: vi.fn(async () => (member ? [ORG] : [])), |
| 96 | + getActiveOrganization: vi.fn(async () => active), |
| 97 | + getActiveMember: vi.fn().mockResolvedValue(null), |
| 98 | + setActiveOrganization: vi.fn(async (id: string) => { |
| 99 | + active = id === ORG.id ? ORG : null; |
| 100 | + return active; |
| 101 | + }), |
| 102 | + }; |
| 103 | + return client as typeof client & AuthClient; |
| 104 | +} |
| 105 | + |
| 106 | +function renderRegister(client: AuthClient) { |
| 107 | + const search = `?redirect=${encodeURIComponent(ACCEPT_PATH)}`; |
| 108 | + window.history.replaceState({}, '', `/_console/register${search}`); |
| 109 | + return render( |
| 110 | + <AuthProvider authUrl="/api/v1/auth" client={client}> |
| 111 | + <MemoryRouter initialEntries={[`/register${search}`]}> |
| 112 | + <RegisterPage /> |
| 113 | + </MemoryRouter> |
| 114 | + </AuthProvider>, |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +async function signUpOnTheForm() { |
| 119 | + const user = userEvent.setup(); |
| 120 | + await user.type(await screen.findByLabelText('Name'), USER.name); |
| 121 | + await user.type(screen.getByLabelText('Email'), USER.email); |
| 122 | + await user.type(screen.getByLabelText('Password'), 'hunter2hunter2'); |
| 123 | + await user.type(screen.getByLabelText('Confirm Password'), 'hunter2hunter2'); |
| 124 | + await user.click(screen.getByRole('button', { name: 'Create Account' })); |
| 125 | +} |
| 126 | + |
| 127 | +/** Where a full-page navigation lands, per the browser's own resolution. */ |
| 128 | +const lands = (target: string) => new URL(target, document.baseURI).pathname; |
| 129 | + |
| 130 | +describe('console /register — a sign-up from an invitation link joins it (#30125)', () => { |
| 131 | + it('accepts the invitation the link names, switches to its organization, and enters the root once', async () => { |
| 132 | + const client = createMockClient(); |
| 133 | + renderRegister(client); |
| 134 | + await signUpOnTheForm(); |
| 135 | + |
| 136 | + await waitFor(() => expect(assign).toHaveBeenCalled()); |
| 137 | + expect(client.getInvitation).toHaveBeenCalledWith(INVITATION_ID); |
| 138 | + expect(client.acceptInvitation).toHaveBeenCalledTimes(1); |
| 139 | + expect(client.acceptInvitation).toHaveBeenCalledWith(INVITATION_ID); |
| 140 | + expect(client.setActiveOrganization).toHaveBeenCalledWith(ORG.id); |
| 141 | + expect(assign).toHaveBeenCalledTimes(1); |
| 142 | + // The root, never the accept page and its "Invitation accepted" step. |
| 143 | + expect(lands(assign.mock.calls[0][0] as string)).toBe('/_console/'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('the server refuses this account: accepts nothing and opens the accept page, as before', async () => { |
| 147 | + const client = createMockClient({ refuseRecipient: true }); |
| 148 | + renderRegister(client); |
| 149 | + await signUpOnTheForm(); |
| 150 | + |
| 151 | + await waitFor(() => expect(assign).toHaveBeenCalled()); |
| 152 | + expect(client.acceptInvitation).not.toHaveBeenCalled(); |
| 153 | + expect(assign).toHaveBeenCalledTimes(1); |
| 154 | + expect(lands(assign.mock.calls[0][0] as string)).toBe(`/_console${ACCEPT_PATH}`); |
| 155 | + }); |
| 156 | + |
| 157 | + it('CONTROL: already signed in when the page opens — nothing is accepted, the accept page decides', async () => { |
| 158 | + const client = createMockClient({ signedInOnArrival: true }); |
| 159 | + renderRegister(client); |
| 160 | + |
| 161 | + await waitFor(() => expect(assign).toHaveBeenCalled()); |
| 162 | + expect(client.getInvitation).not.toHaveBeenCalled(); |
| 163 | + expect(client.acceptInvitation).not.toHaveBeenCalled(); |
| 164 | + expect(lands(assign.mock.calls[0][0] as string)).toBe(`/_console${ACCEPT_PATH}`); |
| 165 | + }); |
| 166 | +}); |
0 commit comments