|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Settings hub — an empty list says who it is empty for. |
| 5 | + * |
| 6 | + * `GET /api/settings` lists only the manifests the caller may read, and a |
| 7 | + * manifest that declares no `readPermission` reads at the spec's default, so a |
| 8 | + * plain member is answered an empty list while the settings exist. The hub drew |
| 9 | + * one developer sentence for every empty list ("No settings registered. Plugins |
| 10 | + * can register settings manifests via the SettingsService."). Now: |
| 11 | + * |
| 12 | + * - a caller without the default read capability is told an administrator |
| 13 | + * manages the settings; |
| 14 | + * - a caller who holds it and still gets none keeps a no-settings message; |
| 15 | + * - a caller with manifests sees the list, unchanged. |
| 16 | + * |
| 17 | + * The capability answer is the real `MePermissionsProvider`'s, fed the |
| 18 | + * `/auth/me/permissions` payload it would fetch. No i18n provider is mounted, |
| 19 | + * so each call site renders its key, as in the sibling `pageHeader` suite. |
| 20 | + */ |
| 21 | + |
| 22 | +import '@testing-library/jest-dom/vitest'; |
| 23 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 24 | +import { cleanup, render, screen } from '@testing-library/react'; |
| 25 | +import { MemoryRouter } from 'react-router-dom'; |
| 26 | +import { |
| 27 | + MePermissionsProvider, |
| 28 | + type MePermissionsAuthenticatedResponse, |
| 29 | + type MePermissionsResponse, |
| 30 | +} from '@object-ui/permissions'; |
| 31 | +import { SettingsManifestSchema } from '@objectstack/spec/system'; |
| 32 | + |
| 33 | +vi.mock('sonner', () => ({ toast: { error: vi.fn(), success: vi.fn() } })); |
| 34 | + |
| 35 | +import { SettingsHub, SETTINGS_DEFAULT_READ_CAPABILITY } from '../SettingsHub'; |
| 36 | + |
| 37 | +const MANIFESTS = [ |
| 38 | + { namespace: 'localization', version: 1, label: 'Localization', category: 'Platform', specifiers: [] }, |
| 39 | +]; |
| 40 | + |
| 41 | +const MEMBER: string[] = []; |
| 42 | +const ADMIN = ['manage_org_users', 'setup.access', 'setup.write']; |
| 43 | + |
| 44 | +function serveManifests(manifests: unknown[]) { |
| 45 | + vi.stubGlobal( |
| 46 | + 'fetch', |
| 47 | + vi.fn(async () => ({ |
| 48 | + ok: true, |
| 49 | + status: 200, |
| 50 | + statusText: 'OK', |
| 51 | + json: async () => ({ success: true, data: { manifests } }), |
| 52 | + })), |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +function permissionsPayload(systemPermissions: string[]): MePermissionsResponse { |
| 57 | + const base: MePermissionsAuthenticatedResponse = { |
| 58 | + authenticated: true, |
| 59 | + userId: 'u_1', |
| 60 | + tenantId: 'org_1', |
| 61 | + positions: [], |
| 62 | + hasAnyQueue: false, |
| 63 | + permissionSets: [], |
| 64 | + objects: {}, |
| 65 | + fields: {}, |
| 66 | + }; |
| 67 | + return { ...base, systemPermissions }; |
| 68 | +} |
| 69 | + |
| 70 | +function renderHub(systemPermissions: string[]) { |
| 71 | + return render( |
| 72 | + <MemoryRouter> |
| 73 | + <MePermissionsProvider initialPermissions={permissionsPayload(systemPermissions)}> |
| 74 | + <SettingsHub /> |
| 75 | + </MePermissionsProvider> |
| 76 | + </MemoryRouter>, |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +afterEach(() => { |
| 81 | + cleanup(); |
| 82 | + vi.unstubAllGlobals(); |
| 83 | +}); |
| 84 | + |
| 85 | +describe('Settings hub — an empty list says who it is empty for', () => { |
| 86 | + it('the capability the hub asks is the one a manifest reads at by default in the spec', () => { |
| 87 | + expect(SettingsManifestSchema.shape.readPermission.parse(undefined)).toBe(SETTINGS_DEFAULT_READ_CAPABILITY); |
| 88 | + }); |
| 89 | + |
| 90 | + it('a member answered no manifests is told an administrator manages the settings', async () => { |
| 91 | + serveManifests([]); |
| 92 | + renderHub(MEMBER); |
| 93 | + const panel = await screen.findByTestId('settings-hub-admin-managed'); |
| 94 | + expect(panel).toHaveTextContent('console.settingsHub.adminManaged'); |
| 95 | + expect(panel).toHaveTextContent('console.settingsHub.adminManagedDescription'); |
| 96 | + expect(screen.queryByTestId('settings-hub-empty')).toBeNull(); |
| 97 | + expect(screen.queryByText('console.settingsHub.empty')).toBeNull(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('an admin answered no manifests is told none are available, not that an administrator manages them', async () => { |
| 101 | + serveManifests([]); |
| 102 | + renderHub(ADMIN); |
| 103 | + expect(await screen.findByTestId('settings-hub-empty')).toHaveTextContent('console.settingsHub.empty'); |
| 104 | + expect(screen.queryByTestId('settings-hub-admin-managed')).toBeNull(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('an admin answered manifests sees the list', async () => { |
| 108 | + serveManifests(MANIFESTS); |
| 109 | + renderHub(ADMIN); |
| 110 | + expect(await screen.findByText('Localization')).toBeInTheDocument(); |
| 111 | + expect(screen.queryByTestId('settings-hub-empty')).toBeNull(); |
| 112 | + expect(screen.queryByTestId('settings-hub-admin-managed')).toBeNull(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('a member answered manifests (a namespace it may read) sees the list too', async () => { |
| 116 | + serveManifests(MANIFESTS); |
| 117 | + renderHub(MEMBER); |
| 118 | + expect(await screen.findByText('Localization')).toBeInTheDocument(); |
| 119 | + expect(screen.queryByTestId('settings-hub-admin-managed')).toBeNull(); |
| 120 | + }); |
| 121 | +}); |
0 commit comments