|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#29174 — signing out leaves the list's state behind. |
| 5 | + * |
| 6 | + * A list mirrors its toolbar state into the URL (`uf__filter`, `uf__search`, |
| 7 | + * `uf__sort`, `uf__group`, a quick filter's `uf_<field>`, the tab preset's |
| 8 | + * `uf__tab`; `views/userFilterUrlState.ts` in app-shell owns the family). When |
| 9 | + * the session ends, `LoginRedirect` sends the visitor to `/login` with a |
| 10 | + * `?redirect=` return link, and the login page follows that link after ANY |
| 11 | + * successful sign-in. So whoever signs in next on that browser used to open |
| 12 | + * the previous user's filtered, searched and sorted list — on a shared desk or |
| 13 | + * kiosk, the next person. |
| 14 | + * |
| 15 | + * Pinned here, through the REAL composition: the real `AuthProvider` over a |
| 16 | + * scripted auth client (the session is A's until A signs out, then B's once B |
| 17 | + * signs in on the form), the real `ProtectedRoute` (its `AuthGuard` and |
| 18 | + * `LoginRedirect`), and the real Console `LoginPage` with its post-login |
| 19 | + * hand-off. Only the shell's provider stack is a pass-through. |
| 20 | + * |
| 21 | + * - A signs out of a list carrying every kind of list state; the login page's |
| 22 | + * URL carries none of it, and B lands on the same path with the other |
| 23 | + * params kept (the card's reproduction, steps 4 to 7); |
| 24 | + * - an invitation redirect still lands on the invitation; |
| 25 | + * - a signed-out arrival on a deep link with no list state keeps it byte for |
| 26 | + * byte; |
| 27 | + * - a signed-out arrival on a link WITH list state loses that state too: the |
| 28 | + * guard cannot tell who will sign in, so it never forwards it. |
| 29 | + * |
| 30 | + * What B's list then opens with at that landing URL — B's own stored state or |
| 31 | + * the view's default, and nothing of A's in the first data request — is pinned |
| 32 | + * in app-shell's `ObjectView.listUrlState-11860.test.tsx`. |
| 33 | + */ |
| 34 | + |
| 35 | +import '@testing-library/jest-dom/vitest'; |
| 36 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 37 | +import { render, screen, waitFor, cleanup } from '@testing-library/react'; |
| 38 | +import userEvent from '@testing-library/user-event'; |
| 39 | +import { MemoryRouter, Routes, Route, useLocation } from 'react-router-dom'; |
| 40 | +import type { ReactNode } from 'react'; |
| 41 | +import { AuthProvider, useAuth } from '@object-ui/auth'; |
| 42 | +import type { AuthClient } from '@object-ui/auth'; |
| 43 | + |
| 44 | +vi.mock('@object-ui/app-shell', async (importOriginal) => ({ |
| 45 | + ...(await importOriginal<Record<string, unknown>>()), |
| 46 | + // Pass-through: the shell's provider stack does not decide where the guard |
| 47 | + // sends a signed-out visitor. |
| 48 | + ConnectedShell: ({ children }: { children?: ReactNode }) => <>{children}</>, |
| 49 | + RequireOrganization: ({ children }: { children?: ReactNode }) => <>{children}</>, |
| 50 | + LoadingFallback: () => <div data-testid="loading" />, |
| 51 | +})); |
| 52 | + |
| 53 | +import { ProtectedRoute } from './ProtectedRoute'; |
| 54 | +import { LoginPage } from '../pages/auth/LoginPage'; |
| 55 | +import { withConsoleBase } from '../utils/consoleBase'; |
| 56 | + |
| 57 | +const LIST_PATH = '/apps/demo/demo_task/view/in_progress'; |
| 58 | + |
| 59 | +/** A's list, as the list writes it: two Filter panel conditions, a search, a sort, a grouping, a chip, a tab, and a record open beside it. */ |
| 60 | +const A_LIST_SEARCH = new URLSearchParams([ |
| 61 | + [ |
| 62 | + 'uf__filter', |
| 63 | + JSON.stringify({ |
| 64 | + logic: 'and', |
| 65 | + conditions: [ |
| 66 | + { field: 'estimate_hours', operator: 'greater_than', value: 10 }, |
| 67 | + { field: 'progress', operator: 'less_than', value: 80 }, |
| 68 | + ], |
| 69 | + }), |
| 70 | + ], |
| 71 | + ['uf__search', 'pipeline'], |
| 72 | + ['uf__sort', JSON.stringify([{ field: 'due_date', order: 'desc' }])], |
| 73 | + ['uf__group', JSON.stringify({ fields: [{ field: 'status', order: 'asc', collapsed: false }] })], |
| 74 | + ['uf_priority', 'urgent'], |
| 75 | + ['uf__tab', 'mine'], |
| 76 | + ['recordId', 'r1'], |
| 77 | +]).toString(); |
| 78 | + |
| 79 | +const A_LIST_URL = `${LIST_PATH}?${A_LIST_SEARCH}`; |
| 80 | + |
| 81 | +const USER_A = { id: 'user_a', name: 'Ada', email: 'ada@example.com' }; |
| 82 | +const USER_B = { id: 'user_b', name: 'Ben', email: 'ben@example.com' }; |
| 83 | + |
| 84 | +/** The session the scripted server holds, newest state wins. */ |
| 85 | +let serverUser: typeof USER_A | null; |
| 86 | + |
| 87 | +function createClient(): AuthClient { |
| 88 | + return { |
| 89 | + getSession: vi.fn(async () => (serverUser ? { user: serverUser, session: { token: `tok_${serverUser.id}` } } : null)), |
| 90 | + getConfig: vi.fn().mockResolvedValue({ emailPassword: { enabled: true, disableSignUp: true } }), |
| 91 | + signOut: vi.fn(async () => { |
| 92 | + serverUser = null; |
| 93 | + }), |
| 94 | + signIn: vi.fn(async ({ email }: { email: string }) => { |
| 95 | + serverUser = email === USER_B.email ? USER_B : USER_A; |
| 96 | + return { user: serverUser, session: { token: `tok_${serverUser.id}` } }; |
| 97 | + }), |
| 98 | + } as unknown as AuthClient; |
| 99 | +} |
| 100 | + |
| 101 | +/** Every location the router settles on, oldest first. */ |
| 102 | +const seen: string[] = []; |
| 103 | +function LocationProbe() { |
| 104 | + const location = useLocation(); |
| 105 | + seen.push(location.pathname + location.search); |
| 106 | + return null; |
| 107 | +} |
| 108 | + |
| 109 | +/** The list page, reduced to the account menu's sign-out. */ |
| 110 | +function SignedInList() { |
| 111 | + const { user, signOut } = useAuth(); |
| 112 | + return ( |
| 113 | + <div data-testid="list-page" data-user={user?.id ?? ''}> |
| 114 | + <button type="button" onClick={() => void signOut()}> |
| 115 | + Sign out |
| 116 | + </button> |
| 117 | + </div> |
| 118 | + ); |
| 119 | +} |
| 120 | + |
| 121 | +function renderConsole(entry: string, client: AuthClient) { |
| 122 | + return render( |
| 123 | + <AuthProvider authUrl="/api/v1/auth" client={client}> |
| 124 | + <MemoryRouter initialEntries={[entry]}> |
| 125 | + <LocationProbe /> |
| 126 | + <Routes> |
| 127 | + <Route path="/login" element={<LoginPage />} /> |
| 128 | + <Route |
| 129 | + path="/apps/*" |
| 130 | + element={ |
| 131 | + <ProtectedRoute requireOrganization={false}> |
| 132 | + <SignedInList /> |
| 133 | + </ProtectedRoute> |
| 134 | + } |
| 135 | + /> |
| 136 | + </Routes> |
| 137 | + </MemoryRouter> |
| 138 | + </AuthProvider>, |
| 139 | + ); |
| 140 | +} |
| 141 | + |
| 142 | +/** The login page the router settled on, and its `?redirect=` decoded. */ |
| 143 | +async function loginRedirect(): Promise<{ url: string; redirect: string | null }> { |
| 144 | + await screen.findByLabelText('Email'); |
| 145 | + const url = seen[seen.length - 1]!; |
| 146 | + expect(url.startsWith('/login')).toBe(true); |
| 147 | + return { url, redirect: new URLSearchParams(url.slice('/login'.length)).get('redirect') }; |
| 148 | +} |
| 149 | + |
| 150 | +async function signInAs(email: string) { |
| 151 | + const user = userEvent.setup(); |
| 152 | + await user.type(await screen.findByLabelText('Email'), email); |
| 153 | + await user.type(screen.getByLabelText('Password'), 'pw-29174'); |
| 154 | + await user.click(screen.getByRole('button', { name: 'Sign In' })); |
| 155 | +} |
| 156 | + |
| 157 | +/** The URL the login page sends the browser to once someone is signed in. */ |
| 158 | +async function landing(): Promise<string> { |
| 159 | + const assign = vi.mocked(window.location.assign); |
| 160 | + await waitFor(() => expect(assign).toHaveBeenCalled()); |
| 161 | + expect(assign).toHaveBeenCalledTimes(1); |
| 162 | + return String(assign.mock.calls[0]![0]); |
| 163 | +} |
| 164 | + |
| 165 | +beforeEach(() => { |
| 166 | + seen.length = 0; |
| 167 | + serverUser = null; |
| 168 | + window.localStorage.clear(); |
| 169 | + window.sessionStorage.clear(); |
| 170 | + vi.spyOn(window.location, 'assign').mockImplementation(() => undefined); |
| 171 | +}); |
| 172 | + |
| 173 | +afterEach(() => { |
| 174 | + cleanup(); |
| 175 | + vi.restoreAllMocks(); |
| 176 | +}); |
| 177 | + |
| 178 | +describe('signing out leaves the list state behind (objectstack#29174)', () => { |
| 179 | + it('A signs out of a filtered, searched, sorted list; B signs in and lands on the path with none of it', async () => { |
| 180 | + serverUser = USER_A; |
| 181 | + const client = createClient(); |
| 182 | + renderConsole(A_LIST_URL, client); |
| 183 | + |
| 184 | + // A is on the list, and the URL carries A's list state (the control). |
| 185 | + expect((await screen.findByTestId('list-page')).dataset.user).toBe(USER_A.id); |
| 186 | + expect(seen[seen.length - 1]).toBe(A_LIST_URL); |
| 187 | + |
| 188 | + await userEvent.setup().click(screen.getByRole('button', { name: 'Sign out' })); |
| 189 | + expect(client.signOut).toHaveBeenCalledTimes(1); |
| 190 | + |
| 191 | + // The login page's own URL carries none of it: not the params, not a value. |
| 192 | + const { url, redirect } = await loginRedirect(); |
| 193 | + expect(redirect).toBe(`${LIST_PATH}?recordId=r1`); |
| 194 | + expect(decodeURIComponent(url)).not.toContain('uf_'); |
| 195 | + expect(decodeURIComponent(url)).not.toContain('pipeline'); |
| 196 | + expect(decodeURIComponent(url)).not.toContain('estimate_hours'); |
| 197 | + |
| 198 | + await signInAs(USER_B.email); |
| 199 | + expect(client.signIn).toHaveBeenLastCalledWith( |
| 200 | + expect.objectContaining({ email: USER_B.email }), |
| 201 | + ); |
| 202 | + |
| 203 | + // B lands on the list's path, with the record param kept. |
| 204 | + const landed = await landing(); |
| 205 | + expect(landed).toBe(withConsoleBase(`${LIST_PATH}?recordId=r1`)); |
| 206 | + expect(landed).not.toContain('uf_'); |
| 207 | + }); |
| 208 | + |
| 209 | + it('an invitation redirect still lands on the invitation', async () => { |
| 210 | + const client = createClient(); |
| 211 | + renderConsole(`/login?redirect=${encodeURIComponent('/accept-invitation/inv_1')}`, client); |
| 212 | + |
| 213 | + await signInAs(USER_B.email); |
| 214 | + |
| 215 | + expect(await landing()).toBe(withConsoleBase('/accept-invitation/inv_1')); |
| 216 | + }); |
| 217 | + |
| 218 | + it('a signed-out arrival on a deep link with no list state keeps it byte for byte', async () => { |
| 219 | + const deepLink = '/apps/demo/demo_task/record/r1?tab=details¬e=a%20b'; |
| 220 | + const client = createClient(); |
| 221 | + renderConsole(deepLink, client); |
| 222 | + |
| 223 | + const { url, redirect } = await loginRedirect(); |
| 224 | + expect(url).toBe(`/login?redirect=${encodeURIComponent(deepLink)}`); |
| 225 | + expect(redirect).toBe(deepLink); |
| 226 | + |
| 227 | + await signInAs(USER_B.email); |
| 228 | + expect(await landing()).toBe(withConsoleBase(deepLink)); |
| 229 | + }); |
| 230 | + |
| 231 | + it('a signed-out arrival on a link carrying list state loses the list state: the guard cannot tell who signs in', async () => { |
| 232 | + const client = createClient(); |
| 233 | + renderConsole(A_LIST_URL, client); |
| 234 | + |
| 235 | + const { redirect } = await loginRedirect(); |
| 236 | + expect(redirect).toBe(`${LIST_PATH}?recordId=r1`); |
| 237 | + }); |
| 238 | +}); |
0 commit comments