|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectstack#26971 item 1 — a viewer who may not read the data behind a |
| 11 | + * dataset tile does not see a permission-error tile, and the other tiles |
| 12 | + * still render. |
| 13 | + * |
| 14 | + * Measured on HotCRM's Sales Performance dashboard: a viewer holding only a |
| 15 | + * marketing permission set saw 14 tiles answer and the 15th draw "You don't |
| 16 | + * have permission to view the data behind this widget" (its dataset query |
| 17 | + * answered 403). Outside design mode `DashboardRenderer` now leaves that tile |
| 18 | + * out of the layout, Card frame and title included, once the server refuses |
| 19 | + * the query as a READ refusal (`DatasetWidget`'s `onForbidden`). The 403 stays |
| 20 | + * the enforcement: nothing is hidden before the server answers. |
| 21 | + * |
| 22 | + * CONTROLS: in design mode the refused tile stays and draws the no-access |
| 23 | + * state (objectui#10899), so the author can still see and select it; and a |
| 24 | + * failure that is not a read refusal keeps the tile and its error alert. |
| 25 | + * |
| 26 | + * The refusal carries the two fields `@object-ui/data-objectstack`'s |
| 27 | + * `AnalyticsForbiddenError` declares (`httpStatus: 403`, |
| 28 | + * `code: 'PERMISSION_DENIED'`), as `DatasetWidget.forbiddenState-10899.test.tsx` |
| 29 | + * builds it. |
| 30 | + */ |
| 31 | + |
| 32 | +import '@testing-library/jest-dom/vitest'; |
| 33 | +import * as React from 'react'; |
| 34 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 35 | +import { render, screen, cleanup, waitFor } from '@testing-library/react'; |
| 36 | +import type { DashboardComponentSchema } from '@object-ui/types'; |
| 37 | +// Module scope (AGENTS.md 测试纪律): the table tile renders onward through the |
| 38 | +// registry this barrel populates. |
| 39 | +import '@object-ui/components'; |
| 40 | +import { DashboardRenderer } from '../DashboardRenderer'; |
| 41 | + |
| 42 | +beforeEach(() => { |
| 43 | + // The dimension-label metadata probe (`useDatasetDimensionMeta`) falls back |
| 44 | + // to the global fetch; answer it locally so no case reaches the network. |
| 45 | + vi.stubGlobal('fetch', vi.fn(async () => ({ ok: true, status: 200, json: async () => ({}), text: async () => '{}' }))); |
| 46 | +}); |
| 47 | +afterEach(() => { |
| 48 | + vi.unstubAllGlobals(); |
| 49 | + cleanup(); |
| 50 | +}); |
| 51 | + |
| 52 | +const dashboard = (...widgets: Record<string, unknown>[]): DashboardComponentSchema => |
| 53 | + ({ type: 'dashboard', name: 'sales_performance', widgets }) as unknown as DashboardComponentSchema; |
| 54 | + |
| 55 | +const READABLE = { |
| 56 | + id: 'pipeline_by_stage', |
| 57 | + type: 'table', |
| 58 | + title: 'Pipeline by Stage', |
| 59 | + dataset: 'opportunity_metrics', |
| 60 | + dimensions: ['stage'], |
| 61 | + values: ['deal_count'], |
| 62 | +}; |
| 63 | +const REFUSED = { |
| 64 | + id: 'quota_attainment', |
| 65 | + type: 'table', |
| 66 | + title: 'Quota Attainment by Rep', |
| 67 | + dataset: 'forecast_metrics', |
| 68 | + dimensions: ['owner'], |
| 69 | + values: ['attainment'], |
| 70 | +}; |
| 71 | + |
| 72 | +function readRefusal() { |
| 73 | + return Object.assign(new Error('Analytics query refused: this user may not read the data behind dataset "forecast_metrics".'), { |
| 74 | + name: 'AnalyticsForbiddenError', |
| 75 | + httpStatus: 403, |
| 76 | + code: 'PERMISSION_DENIED', |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +/** Answers every dataset but `forecast_metrics`, which it refuses with `failure()`. */ |
| 81 | +function source(failure: () => Error = readRefusal) { |
| 82 | + return { |
| 83 | + queryDataset: vi.fn(async (dataset: string) => { |
| 84 | + if (dataset === 'forecast_metrics') throw failure(); |
| 85 | + return { |
| 86 | + rows: [{ stage: 'Closed Won', deal_count: 7 }], |
| 87 | + fields: [ |
| 88 | + { name: 'stage', label: 'Stage', type: 'text' }, |
| 89 | + { name: 'deal_count', label: 'Deals', type: 'number' }, |
| 90 | + ], |
| 91 | + object: 'opportunity', |
| 92 | + }; |
| 93 | + }), |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +describe('DashboardRenderer — a tile whose data the viewer may not read is left out (objectstack#26971)', () => { |
| 98 | + it('drops the refused tile, frame and title included, and keeps the readable one', async () => { |
| 99 | + const src = source(); |
| 100 | + render(<DashboardRenderer schema={dashboard(READABLE, REFUSED)} dataSource={src} />); |
| 101 | + |
| 102 | + // The readable tile answers. |
| 103 | + expect(await screen.findByText('Closed Won')).toBeInTheDocument(); |
| 104 | + expect(screen.getByText('Pipeline by Stage')).toBeInTheDocument(); |
| 105 | + // The refused tile was asked (no client-side guess), and once the server |
| 106 | + // refused it, nothing of it is left: no no-access state, no titled frame. |
| 107 | + expect(src.queryDataset).toHaveBeenCalledWith('forecast_metrics', expect.anything()); |
| 108 | + await waitFor(() => expect(screen.queryByText('Quota Attainment by Rep')).toBeNull()); |
| 109 | + expect(screen.queryByTestId('dataset-widget-forbidden')).toBeNull(); |
| 110 | + expect(screen.queryByText(/don’t have (access|permission)/)).toBeNull(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('CONTROL — in design mode the refused tile stays and draws the no-access state', async () => { |
| 114 | + render( |
| 115 | + <DashboardRenderer |
| 116 | + schema={dashboard(READABLE, REFUSED)} |
| 117 | + dataSource={source()} |
| 118 | + designMode |
| 119 | + onWidgetClick={() => {}} |
| 120 | + />, |
| 121 | + ); |
| 122 | + |
| 123 | + expect(await screen.findByTestId('dataset-widget-forbidden')).toHaveTextContent('You don’t have access'); |
| 124 | + expect(screen.getByTestId('dashboard-preview-widget-quota_attainment')).toHaveTextContent('Quota Attainment by Rep'); |
| 125 | + expect(await screen.findByText('Closed Won')).toBeInTheDocument(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('CONTROL — a failure that is not a read refusal keeps the tile and its error', async () => { |
| 129 | + render( |
| 130 | + <DashboardRenderer |
| 131 | + schema={dashboard(READABLE, REFUSED)} |
| 132 | + dataSource={source(() => new Error('Dataset query failed: 400 Bad Request — relationship not declared in include'))} |
| 133 | + />, |
| 134 | + ); |
| 135 | + |
| 136 | + expect(await screen.findByText(/relationship not declared in include/)).toBeInTheDocument(); |
| 137 | + expect(screen.getByText('Quota Attainment by Rep')).toBeInTheDocument(); |
| 138 | + expect(await screen.findByText('Closed Won')).toBeInTheDocument(); |
| 139 | + }); |
| 140 | +}); |
0 commit comments