|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack-ai/objectstack#25894 — Home reads the pending-drafts ledger only |
| 5 | + * for a session the ledger admits. |
| 6 | + * |
| 7 | + * Measured on the 2026-10-25 browser pass: signed in as a member with no |
| 8 | + * metadata capability, the console sent `GET /api/v1/meta/_drafts` and logged |
| 9 | + * its 403 as a resource error. Home is every member's landing (the approvals |
| 10 | + * inbox lives here), and its drafts row read the env-wide ledger for whoever |
| 11 | + * opened it. The door admits `studio.access`, `setup.access` or |
| 12 | + * `manage_metadata` (`mayReadPendingDrafts`); a session holding none of them |
| 13 | + * is now sent no read. |
| 14 | + * |
| 15 | + * Drives the REAL `MePermissionsProvider` over a `/me/permissions` payload, as |
| 16 | + * `HomePage.authoringCapabilityGate.test.tsx` does, so the gate consumes the |
| 17 | + * server's answer through `hasCapabilities`. |
| 18 | + * |
| 19 | + * Pinned: |
| 20 | + * - a reported set holding none of the three: no `_drafts` read, no row; |
| 21 | + * - CONTROL: each of the three alone reads the ledger and renders the row; |
| 22 | + * - CONTROL: an unreported set (no `systemPermissions`) fails OPEN and reads. |
| 23 | + */ |
| 24 | + |
| 25 | +import '@testing-library/jest-dom/vitest'; |
| 26 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 27 | +import React from 'react'; |
| 28 | +import { act, render, screen, cleanup } from '@testing-library/react'; |
| 29 | +import { MePermissionsProvider, type MePermissionsResponse } from '@object-ui/permissions'; |
| 30 | + |
| 31 | +vi.mock('react-router-dom', async () => { |
| 32 | + const { createElement } = await import('react'); |
| 33 | + return { |
| 34 | + useNavigate: () => vi.fn(), |
| 35 | + Link: ({ to, ...rest }: { to: string } & Record<string, unknown>) => createElement('a', { ...rest, href: to }), |
| 36 | + }; |
| 37 | +}); |
| 38 | + |
| 39 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 40 | + ...(await importOriginal<Record<string, unknown>>()), |
| 41 | + useObjectTranslation: () => ({ t: (key: string) => `«${key}»`, language: 'en' }), |
| 42 | + useObjectLabel: () => ({ appLabel: (app: any) => String(app?.label ?? app?.name ?? '') }), |
| 43 | +})); |
| 44 | + |
| 45 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 46 | + ...(await importOriginal<Record<string, unknown>>()), |
| 47 | + useAuth: () => ({ user: { id: 'u1', name: 'Sales Manager', email: 'sales.manager@example.com' } }), |
| 48 | + useWorkspaceAdminStatus: () => ({ isAdmin: false, isResolved: true }), |
| 49 | +})); |
| 50 | + |
| 51 | +vi.mock('@object-ui/plugin-chatbot/core', async (importOriginal) => ({ |
| 52 | + ...(await importOriginal<typeof import('@object-ui/plugin-chatbot/core')>()), |
| 53 | + useAgents: () => ({ agents: [] }), |
| 54 | + isAskAgent: () => false, |
| 55 | + agentHasCapability: () => false, |
| 56 | +})); |
| 57 | + |
| 58 | +vi.mock('../../../providers/MetadataProvider', () => ({ |
| 59 | + useMetadata: () => ({ apps: [{ name: 'crm', label: 'CRM' }], loading: false }), |
| 60 | +})); |
| 61 | +vi.mock('../../../context/NavigationContext', () => ({ |
| 62 | + useNavigationContext: () => ({ currentAppName: 'crm' }), |
| 63 | +})); |
| 64 | +vi.mock('../../../hooks/useRecentItems', () => ({ useRecentItems: () => ({ recentItems: [] }) })); |
| 65 | +vi.mock('../../../hooks/useFavorites', () => ({ useFavorites: () => ({ favorites: [] }) })); |
| 66 | +vi.mock('../../../hooks/useHomeInbox', () => ({ |
| 67 | + useHomeInbox: () => ({ |
| 68 | + pendingApprovalsCount: 0, |
| 69 | + pendingApprovals: [], |
| 70 | + pendingApprovalsStatus: 'ready', |
| 71 | + pendingApprovalsReadable: true, |
| 72 | + notifications: [], |
| 73 | + unreadTopicCount: 0, |
| 74 | + notificationsStatus: 'ready', |
| 75 | + notificationsReadable: true, |
| 76 | + activity: { value: [], status: 'ready', readable: true }, |
| 77 | + }), |
| 78 | +})); |
| 79 | +vi.mock('../../../hooks/useAiSurface', () => ({ resolveAiApiBase: () => '' })); |
| 80 | +vi.mock('../../../preview/usePublishAllDrafts', () => ({ |
| 81 | + usePublishAllDrafts: () => ({ publishAll: async () => ({ ok: true }), publishing: false }), |
| 82 | +})); |
| 83 | +vi.mock('../../../runtime-config', () => ({ |
| 84 | + getRuntimeConfig: () => ({ branding: { productName: 'ObjectStack' } }), |
| 85 | + isMarketplaceEnabled: () => true, |
| 86 | + isAiStudioEnabled: () => true, |
| 87 | +})); |
| 88 | + |
| 89 | +import { HomePage } from '../HomePage'; |
| 90 | + |
| 91 | +const DRAFTS_ROUTE = '/api/v1/meta/_drafts'; |
| 92 | +const routeOf = (url: string) => url.split('?')[0]; |
| 93 | + |
| 94 | +/** Two drafts in two packages: the row names no package, so no package list is read. */ |
| 95 | +const DRAFTS = [ |
| 96 | + { type: 'object', name: 'visit', label: 'Visit', packageId: 'com.a' }, |
| 97 | + { type: 'object', name: 'invoice', label: 'Invoice', packageId: 'com.b' }, |
| 98 | +]; |
| 99 | + |
| 100 | +let fetchCalls: string[] = []; |
| 101 | + |
| 102 | +beforeEach(() => { |
| 103 | + fetchCalls = []; |
| 104 | + vi.stubGlobal( |
| 105 | + 'fetch', |
| 106 | + vi.fn(async (input: unknown) => { |
| 107 | + const url = String(input); |
| 108 | + fetchCalls.push(url); |
| 109 | + if (routeOf(url) === DRAFTS_ROUTE) return { ok: true, status: 200, json: async () => ({ drafts: DRAFTS }) }; |
| 110 | + return { ok: false, status: 404, json: async () => ({}) }; |
| 111 | + }), |
| 112 | + ); |
| 113 | +}); |
| 114 | + |
| 115 | +afterEach(() => { |
| 116 | + expect(fetchCalls.filter((url) => routeOf(url) !== DRAFTS_ROUTE)).toEqual([]); |
| 117 | + // Unmount before restoring the real `fetch` (objectui#7439). |
| 118 | + cleanup(); |
| 119 | + vi.unstubAllGlobals(); |
| 120 | +}); |
| 121 | + |
| 122 | +const payload = (systemPermissions?: string[]): MePermissionsResponse => |
| 123 | + ({ |
| 124 | + authenticated: true, |
| 125 | + userId: 'u1', |
| 126 | + tenantId: 'acme', |
| 127 | + roles: ['everyone'], |
| 128 | + permissionSets: ['member_default'], |
| 129 | + ...(systemPermissions ? { systemPermissions } : {}), |
| 130 | + objects: {}, |
| 131 | + fields: {}, |
| 132 | + }) as MePermissionsResponse; |
| 133 | + |
| 134 | +async function renderHome(permissions: MePermissionsResponse) { |
| 135 | + render( |
| 136 | + <MePermissionsProvider initialPermissions={permissions}> |
| 137 | + <HomePage /> |
| 138 | + </MePermissionsProvider>, |
| 139 | + ); |
| 140 | + await act(async () => { |
| 141 | + for (let i = 0; i < 20; i++) await new Promise((r) => setTimeout(r, 0)); |
| 142 | + }); |
| 143 | +} |
| 144 | + |
| 145 | +const draftsReads = () => fetchCalls.filter((url) => routeOf(url) === DRAFTS_ROUTE); |
| 146 | + |
| 147 | +describe('Home reads the drafts ledger only for a session it admits (objectstack-ai/objectstack#25894)', () => { |
| 148 | + it('a member holding none of the ledger capabilities: no `_drafts` read and no row', async () => { |
| 149 | + await renderHome(payload(['export_reports'])); |
| 150 | + |
| 151 | + expect(screen.getByRole('heading', { level: 1 }), 'Home never rendered: the harness is dead').toBeInTheDocument(); |
| 152 | + expect(draftsReads()).toEqual([]); |
| 153 | + expect(screen.queryByTestId('home-pending-drafts')).toBeNull(); |
| 154 | + }); |
| 155 | + |
| 156 | + it('a REPORTED empty set holds none of them either: no read', async () => { |
| 157 | + await renderHome(payload([])); |
| 158 | + |
| 159 | + expect(draftsReads()).toEqual([]); |
| 160 | + }); |
| 161 | + |
| 162 | + it.each(['setup.access', 'studio.access', 'manage_metadata'])( |
| 163 | + 'CONTROL: `%s` alone reads the ledger and shows the row', |
| 164 | + async (capability) => { |
| 165 | + await renderHome(payload([capability])); |
| 166 | + |
| 167 | + expect(draftsReads()).toEqual([DRAFTS_ROUTE]); |
| 168 | + expect(await screen.findByTestId('home-pending-drafts')).toBeInTheDocument(); |
| 169 | + }, |
| 170 | + ); |
| 171 | + |
| 172 | + it('CONTROL: an unreported capability set fails OPEN and reads', async () => { |
| 173 | + await renderHome(payload(undefined)); |
| 174 | + |
| 175 | + expect(draftsReads()).toEqual([DRAFTS_ROUTE]); |
| 176 | + expect(await screen.findByTestId('home-pending-drafts')).toBeInTheDocument(); |
| 177 | + }); |
| 178 | +}); |
0 commit comments