|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack-ai/objectstack#29187 — where the form routes sit in `App`'s |
| 5 | + * route tree, which decides what `FormPage`'s up-front write refusal reads. |
| 6 | + * |
| 7 | + * The app-less `/forms/:name` used to sit beside the console's permissions |
| 8 | + * layout, under `ProtectedRoute` alone, while its in-app twin |
| 9 | + * `/apps/:appName/forms/:name` sat under it. The refusal reads the caller's |
| 10 | + * grants from that layout's `/auth/me/permissions` answer, so on the app-less |
| 11 | + * URL it would have had none and the same form would behave differently by |
| 12 | + * URL. `/forms/:name` is now a child of the same pathless layout route as |
| 13 | + * `/home` and `/apps/:appName/*`: one answer, asked once per session load, |
| 14 | + * held across a hop between them. `/f/:slug` stays outside it: anonymous, |
| 15 | + * no answer, no request. |
| 16 | + * |
| 17 | + * What is real: `App`'s route tree, `ConsolePermissionsScope` and the |
| 18 | + * `MePermissionsProvider` it mounts. The network is the console's |
| 19 | + * authenticated fetcher, mocked. The probes stand where `InternalFormRoute`, |
| 20 | + * the public `FormPage` and Home's page render, and report what they see. |
| 21 | + * `ProtectedRoute` is reduced to the metadata context it provides. |
| 22 | + */ |
| 23 | + |
| 24 | +import '@testing-library/jest-dom/vitest'; |
| 25 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 26 | +import { render, screen, cleanup, waitFor, act } from '@testing-library/react'; |
| 27 | +import { useContext, type ReactNode } from 'react'; |
| 28 | +import { useNavigate } from 'react-router-dom'; |
| 29 | +import { MetadataCtx, type MetadataContextValue } from '@object-ui/react'; |
| 30 | +import { usePermissions, type MePermissionsAuthenticatedResponse } from '@object-ui/permissions'; |
| 31 | +// Not on the package's entry: the context object itself, read by its module id |
| 32 | +// (`@object-ui/permissions` resolves to `packages/permissions/src`, so this is |
| 33 | +// the same module instance the provider writes). |
| 34 | +import { PermCtx } from '../../../../packages/permissions/src/PermissionContext'; |
| 35 | + |
| 36 | +const net = vi.hoisted(() => ({ |
| 37 | + /** Every URL the console's authenticated fetcher was asked for. */ |
| 38 | + calls: [] as string[], |
| 39 | +})); |
| 40 | + |
| 41 | +const METADATA: MetadataContextValue = { |
| 42 | + apps: [{ name: 'crm', label: 'CRM', active: true, navigation: [] }] as never, |
| 43 | + objects: [], |
| 44 | + dashboards: [], |
| 45 | + reports: [], |
| 46 | + pages: [], |
| 47 | + loading: false, |
| 48 | + error: null, |
| 49 | + refresh: async () => {}, |
| 50 | + invalidate: () => {}, |
| 51 | + ensureType: async () => [], |
| 52 | + getItem: async () => null, |
| 53 | + getItemsByType: () => [], |
| 54 | + getTypeStatus: () => 'ready', |
| 55 | +} as unknown as MetadataContextValue; |
| 56 | + |
| 57 | +/** A member who may read `qar8_fsb` and not create it. */ |
| 58 | +function permissionsAnswer(): MePermissionsAuthenticatedResponse { |
| 59 | + return { |
| 60 | + authenticated: true, |
| 61 | + userId: 'u_member', |
| 62 | + tenantId: null, |
| 63 | + positions: [], |
| 64 | + hasAnyQueue: false, |
| 65 | + permissionSets: ['member'], |
| 66 | + systemPermissions: [], |
| 67 | + objects: { qar8_fsb: { allowRead: true } }, |
| 68 | + fields: {}, |
| 69 | + } as MePermissionsAuthenticatedResponse; |
| 70 | +} |
| 71 | + |
| 72 | +function respond(input: unknown): Promise<Response> { |
| 73 | + const url = String(input); |
| 74 | + const body = url.includes('/auth/me/permissions') ? permissionsAnswer() : {}; |
| 75 | + return Promise.resolve({ |
| 76 | + ok: true, |
| 77 | + status: 200, |
| 78 | + headers: { get: () => null }, |
| 79 | + json: async () => body, |
| 80 | + } as unknown as Response); |
| 81 | +} |
| 82 | + |
| 83 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 84 | + ...(await importOriginal<Record<string, unknown>>()), |
| 85 | + AuthProvider: ({ children }: { children?: ReactNode }) => <>{children}</>, |
| 86 | + useAuth: () => ({ user: { id: 'u_member', name: 'Member', email: 'member@example.com' }, isAuthenticated: true, isLoading: false }), |
| 87 | + createAuthenticatedFetch: () => (input: unknown) => { |
| 88 | + net.calls.push(String(input)); |
| 89 | + return respond(input); |
| 90 | + }, |
| 91 | +})); |
| 92 | + |
| 93 | +vi.mock('../components/ProtectedRoute', () => ({ |
| 94 | + ProtectedRoute: ({ children }: { children?: ReactNode }) => ( |
| 95 | + <MetadataCtx.Provider value={METADATA}>{children}</MetadataCtx.Provider> |
| 96 | + ), |
| 97 | +})); |
| 98 | + |
| 99 | +/** What a form route sees of the caller's grants on the form's object. */ |
| 100 | +function GrantProbe({ testId }: { testId: string }) { |
| 101 | + const ctx = useContext(PermCtx); |
| 102 | + const { isLoaded, can } = usePermissions(); |
| 103 | + const verdict = ctx === null ? 'no-perm-ctx' : isLoaded ? `create:${String(can('qar8_fsb', 'create'))}` : 'not-loaded'; |
| 104 | + return <div data-testid={testId}>{verdict}</div>; |
| 105 | +} |
| 106 | + |
| 107 | +/** Stands where Home's page renders, with a way to follow a form link. */ |
| 108 | +function HomeProbe() { |
| 109 | + const navigate = useNavigate(); |
| 110 | + return ( |
| 111 | + <button type="button" onClick={() => navigate('/forms/qar8_fsb.form')}> |
| 112 | + open form |
| 113 | + </button> |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +vi.mock('@object-ui/app-shell', async (importOriginal) => ({ |
| 118 | + ...(await importOriginal<Record<string, unknown>>()), |
| 119 | + ConsoleShell: ({ children }: { children?: ReactNode }) => <>{children}</>, |
| 120 | + ConsoleToaster: () => null, |
| 121 | + LoadingScreen: () => <div data-testid="loading-screen" />, |
| 122 | + DefaultHomePage: () => <HomeProbe />, |
| 123 | + DefaultAppContent: () => null, |
| 124 | + BuilderLanding: () => null, |
| 125 | + StudioDesignSurface: () => null, |
| 126 | + getProductName: () => 'ObjectOS', |
| 127 | + getFaviconUrl: () => '', |
| 128 | +})); |
| 129 | + |
| 130 | +vi.mock('../../../../packages/app-shell/src/layout/AppHeader', () => ({ AppHeader: () => null })); |
| 131 | +vi.mock('../../../../packages/app-shell/src/chrome/CommandPalette', () => ({ CommandPalette: () => null })); |
| 132 | +vi.mock('../../../../packages/app-shell/src/hooks/useAiSurface', async (importOriginal) => ({ |
| 133 | + ...(await importOriginal<Record<string, unknown>>()), |
| 134 | + useAiSurfaceEnabled: () => ({ enabled: false, isLoading: false }), |
| 135 | +})); |
| 136 | + |
| 137 | +vi.mock('../LocalizationFetchProvider', () => ({ |
| 138 | + LocalizationFetchProvider: ({ children }: { children?: ReactNode }) => <>{children}</>, |
| 139 | +})); |
| 140 | +vi.mock('../pages/settings/WorkspaceTimezonePrompt', () => ({ WorkspaceTimezonePrompt: () => null })); |
| 141 | +vi.mock('../components/RootLandingRedirect', () => ({ RootLandingRedirect: () => null })); |
| 142 | +vi.mock('../components/SetupRoute', () => ({ SetupRoute: () => null })); |
| 143 | +vi.mock('../components/FormPage', () => ({ FormPage: () => <GrantProbe testId="public-form" /> })); |
| 144 | +vi.mock('../components/InternalFormRoute', () => ({ |
| 145 | + InternalFormRoute: () => <GrantProbe testId="internal-form" />, |
| 146 | + InAppFormRoute: () => <GrantProbe testId="in-app-form" />, |
| 147 | +})); |
| 148 | +vi.mock('../components/MetadataHmrReloader', () => ({ MetadataHmrReloader: () => null })); |
| 149 | +vi.mock('../components/FaviconSync', () => ({ FaviconSync: () => null })); |
| 150 | +vi.mock('../pages/SharedRecordPage', () => ({ default: () => null })); |
| 151 | +vi.mock('../pages/auth/LoginPage', () => ({ LoginPage: () => null })); |
| 152 | +vi.mock('../pages/auth/RegisterPage', () => ({ RegisterPage: () => null })); |
| 153 | +vi.mock('../pages/auth/ForgotPasswordPage', () => ({ ForgotPasswordPage: () => null })); |
| 154 | +vi.mock('../pages/auth/ResetPasswordPage', () => ({ ResetPasswordPage: () => null })); |
| 155 | +vi.mock('../pages/auth/SetPasswordPage', () => ({ SetPasswordPage: () => null })); |
| 156 | +vi.mock('../pages/auth/VerifyEmailPage', () => ({ VerifyEmailPage: () => null })); |
| 157 | +vi.mock('../pages/auth/VerifyEmailPromptPage', () => ({ VerifyEmailPromptPage: () => null })); |
| 158 | +vi.mock('../pages/auth/OAuthConsentPage', () => ({ OAuthConsentPage: () => null })); |
| 159 | +vi.mock('../pages/auth/DeviceAuthPage', () => ({ DeviceAuthPage: () => null })); |
| 160 | +vi.mock('../dev/DevMasterDetail', () => ({ DevMasterDetail: () => null })); |
| 161 | +vi.mock('../dev/DevLists', () => ({ DevLists: () => null })); |
| 162 | +vi.mock('../dev/DevModal', () => ({ DevModal: () => null })); |
| 163 | +vi.mock('../dev/DevLookup', () => ({ DevLookup: () => null })); |
| 164 | +vi.mock('../dev/DevRowActions', () => ({ DevRowActions: () => null })); |
| 165 | + |
| 166 | +import { App } from '../App'; |
| 167 | + |
| 168 | +const permissionsCalls = () => net.calls.filter((u) => u.includes('/auth/me/permissions')); |
| 169 | + |
| 170 | +function renderAt(url: string) { |
| 171 | + window.history.pushState({}, '', url); |
| 172 | + return render(<App />); |
| 173 | +} |
| 174 | + |
| 175 | +beforeEach(() => { |
| 176 | + net.calls = []; |
| 177 | + localStorage.clear(); |
| 178 | + sessionStorage.clear(); |
| 179 | +}); |
| 180 | +afterEach(() => { |
| 181 | + cleanup(); |
| 182 | + window.history.pushState({}, '', '/'); |
| 183 | +}); |
| 184 | + |
| 185 | +describe('the form routes and the permissions answer (#29187)', () => { |
| 186 | + it('/forms/NAME renders under the permissions answer, asked once', async () => { |
| 187 | + renderAt('/forms/qar8_fsb.form'); |
| 188 | + await waitFor(() => expect(screen.getByTestId('internal-form')).toHaveTextContent('create:false')); |
| 189 | + expect(permissionsCalls()).toHaveLength(1); |
| 190 | + }); |
| 191 | + |
| 192 | + it('/forms/NAME waits on the answer the way /home and /apps do: the loading screen first, no form', async () => { |
| 193 | + renderAt('/forms/qar8_fsb.form'); |
| 194 | + expect(screen.getByTestId('loading-screen')).toBeInTheDocument(); |
| 195 | + expect(screen.queryByTestId('internal-form')).not.toBeInTheDocument(); |
| 196 | + await waitFor(() => expect(screen.getByTestId('internal-form')).toBeInTheDocument()); |
| 197 | + }); |
| 198 | + |
| 199 | + it('/home then a form link keeps the one answer: /auth/me/permissions asked ONCE in total', async () => { |
| 200 | + renderAt('/home'); |
| 201 | + await waitFor(() => expect(screen.getByRole('button', { name: 'open form' })).toBeInTheDocument()); |
| 202 | + await act(async () => { |
| 203 | + screen.getByRole('button', { name: 'open form' }).click(); |
| 204 | + }); |
| 205 | + await waitFor(() => expect(screen.getByTestId('internal-form')).toHaveTextContent('create:false')); |
| 206 | + expect(permissionsCalls()).toHaveLength(1); |
| 207 | + }); |
| 208 | + |
| 209 | + it('/f/SLUG stays outside it: no permission context and no /auth/me/permissions request', async () => { |
| 210 | + renderAt('/f/feedback'); |
| 211 | + await waitFor(() => expect(screen.getByTestId('public-form')).toHaveTextContent('no-perm-ctx')); |
| 212 | + expect(permissionsCalls()).toHaveLength(0); |
| 213 | + }); |
| 214 | +}); |
0 commit comments