harden report references in boards

This commit is contained in:
Francis Cao committed 2026-07-06 10:58:43 -07:00
1 parent a4a0167515
commit 7bdc2f215e
8 files changed
+346 -10

No files matched your search

+175 -1
View File
@@ -1,12 +1,18 @@
import { beforeEach, expect, test, vi } from 'vitest';
import { BOARD_TYPES } from '@/lib/boards';
import { canViewBoardEntities } from './board';
import { getReport } from '@/queries/prisma';
import {
canViewBoardEntities,
hasValidBoardReports,
stripInvalidBoardReports,
} from './board';
import { canViewLink } from './link';
import { canViewPixel } from './pixel';
import { canViewWebsite } from './website';
vi.mock('@/queries/prisma', () => ({
getBoard: vi.fn(),
getReport: vi.fn(),
getTeamUser: vi.fn(),
}));
@@ -42,6 +48,7 @@ beforeEach(() => {
vi.mocked(canViewWebsite).mockReset();
vi.mocked(canViewPixel).mockReset();
vi.mocked(canViewLink).mockReset();
vi.mocked(getReport).mockReset();
});
test('canViewBoardEntities validates board IDs with user auth only', async () => {
@@ -82,3 +89,170 @@ test('canViewBoardEntities rejects IDs not accessible to the user', async () =>
}),
).resolves.toBe(false);
});
test('hasValidBoardReports allows saved reports for the board website', async () => {
vi.mocked(getReport).mockResolvedValue({
id: 'report-1',
websiteId: 'website-1',
type: 'goal',
} as any);
await expect(
hasValidBoardReports(BOARD_TYPES.website, {
websiteId: 'website-1',
rows: [
{
id: 'row-1',
columns: [
{
id: 'column-1',
component: {
type: 'Goal',
props: {
reportId: 'report-1',
},
},
},
],
},
],
}),
).resolves.toBe(true);
});
test('hasValidBoardReports rejects saved reports from a different website', async () => {
vi.mocked(getReport).mockResolvedValue({
id: 'report-1',
websiteId: 'website-2',
type: 'goal',
} as any);
await expect(
hasValidBoardReports(BOARD_TYPES.website, {
websiteId: 'website-1',
rows: [
{
id: 'row-1',
columns: [
{
id: 'column-1',
component: {
type: 'Goal',
props: {
reportId: 'report-1',
},
},
},
],
},
],
}),
).resolves.toBe(false);
});
test('hasValidBoardReports rejects saved reports with the wrong type', async () => {
vi.mocked(getReport).mockResolvedValue({
id: 'report-1',
websiteId: 'website-1',
type: 'funnel',
} as any);
await expect(
hasValidBoardReports(BOARD_TYPES.website, {
websiteId: 'website-1',
rows: [
{
id: 'row-1',
columns: [
{
id: 'column-1',
component: {
type: 'Goal',
props: {
reportId: 'report-1',
},
},
},
],
},
],
}),
).resolves.toBe(false);
});
test('hasValidBoardReports rejects report widgets without a website context', async () => {
await expect(
hasValidBoardReports(BOARD_TYPES.pixel, {
pixelId: 'pixel-1',
rows: [
{
id: 'row-1',
columns: [
{
id: 'column-1',
component: {
type: 'Goal',
props: {
reportId: 'report-1',
},
},
},
],
},
],
}),
).resolves.toBe(false);
});
test('stripInvalidBoardReports removes only invalid report IDs', async () => {
vi.mocked(getReport).mockResolvedValue({
id: 'report-1',
websiteId: 'website-2',
type: 'goal',
} as any);
await expect(
stripInvalidBoardReports(BOARD_TYPES.website, {
websiteId: 'website-1',
rows: [
{
id: 'row-1',
columns: [
{
id: 'column-1',
component: {
type: 'Goal',
title: 'Saved signup goal',
description: 'Should stay on the board',
props: {
reportId: 'report-1',
customValue: 'keep-me',
},
},
},
],
},
],
}),
).resolves.toEqual({
websiteId: 'website-1',
rows: [
{
id: 'row-1',
columns: [
{
id: 'column-1',
component: {
type: 'Goal',
title: 'Saved signup goal',
description: 'Should stay on the board',
props: {
customValue: 'keep-me',
},
},
},
],
},
],
});
});