Board component, security, test updates
This commit is contained in:
1 parent
f525898b79
commit
ab5b1bf354
23 files changed
+1128
-180
No files matched your search
@@ -0,0 +1,125 @@
|
||||
import { expect, test, vi } from 'vitest';
|
||||
import { ENTITY_TYPE } from '@/lib/constants';
|
||||
import { canViewReport, getReportSection } from './report';
|
||||
import { canViewWebsite } from './website';
|
||||
|
||||
vi.mock('./website', () => ({
|
||||
canViewWebsite: vi.fn(),
|
||||
}));
|
||||
|
||||
test('getReportSection maps saved report types to share sections', () => {
|
||||
expect(getReportSection('goal')).toBe('goals');
|
||||
expect(getReportSection('funnel')).toBe('funnels');
|
||||
expect(getReportSection('journey')).toBe('journeys');
|
||||
expect(getReportSection('revenue')).toBe('revenue');
|
||||
expect(getReportSection('heatmap')).toBe(null);
|
||||
});
|
||||
|
||||
test('canViewReport allows board shares to fetch included saved goal reports', async () => {
|
||||
await expect(
|
||||
canViewReport(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.board,
|
||||
websiteIds: ['website-1'],
|
||||
parameters: {},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'report-1',
|
||||
userId: 'owner-1',
|
||||
websiteId: 'website-1',
|
||||
type: 'goal',
|
||||
} as any,
|
||||
),
|
||||
).resolves.toBe(true);
|
||||
});
|
||||
|
||||
test('canViewReport respects share section flags for saved funnel and goal reports', async () => {
|
||||
await expect(
|
||||
canViewReport(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.website,
|
||||
websiteId: 'website-1',
|
||||
parameters: {
|
||||
overview: true,
|
||||
funnels: false,
|
||||
goals: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'report-1',
|
||||
userId: 'owner-1',
|
||||
websiteId: 'website-1',
|
||||
type: 'funnel',
|
||||
} as any,
|
||||
),
|
||||
).resolves.toBe(false);
|
||||
|
||||
await expect(
|
||||
canViewReport(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.website,
|
||||
websiteId: 'website-1',
|
||||
parameters: {
|
||||
overview: true,
|
||||
funnels: false,
|
||||
goals: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'report-2',
|
||||
userId: 'owner-1',
|
||||
websiteId: 'website-1',
|
||||
type: 'goal',
|
||||
} as any,
|
||||
),
|
||||
).resolves.toBe(true);
|
||||
});
|
||||
|
||||
test('canViewReport denies share-token access to report types without a share section', async () => {
|
||||
await expect(
|
||||
canViewReport(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.website,
|
||||
websiteId: 'website-1',
|
||||
parameters: {},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'report-1',
|
||||
userId: 'owner-1',
|
||||
websiteId: 'website-1',
|
||||
type: 'heatmap',
|
||||
} as any,
|
||||
),
|
||||
).resolves.toBe(false);
|
||||
});
|
||||
|
||||
test('canViewReport falls back to website access for authenticated users', async () => {
|
||||
vi.mocked(canViewWebsite).mockResolvedValue(true);
|
||||
|
||||
await expect(
|
||||
canViewReport(
|
||||
{
|
||||
user: {
|
||||
id: 'user-1',
|
||||
username: 'user',
|
||||
role: 'user',
|
||||
isAdmin: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'report-1',
|
||||
userId: 'owner-1',
|
||||
websiteId: 'website-1',
|
||||
type: 'heatmap',
|
||||
} as any,
|
||||
),
|
||||
).resolves.toBe(true);
|
||||
});
|
||||
@@ -1,8 +1,34 @@
|
||||
import type { Report } from '@/generated/prisma/client';
|
||||
import type { Auth } from '@/lib/types';
|
||||
import type { ShareSection } from './share';
|
||||
import { canViewWebsiteSection } from './share';
|
||||
import { canDeleteWebsite, canUpdateWebsite, canViewWebsite } from './website';
|
||||
|
||||
export async function canViewReport(auth: Auth, report: Report) {
|
||||
export function getReportSection(type?: string): ShareSection | null {
|
||||
switch (type) {
|
||||
case 'attribution':
|
||||
case 'breakdown':
|
||||
case 'performance':
|
||||
case 'retention':
|
||||
case 'revenue':
|
||||
case 'utm':
|
||||
return type;
|
||||
case 'funnel':
|
||||
return 'funnels';
|
||||
case 'goal':
|
||||
return 'goals';
|
||||
case 'journey':
|
||||
return 'journeys';
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function canViewReport(auth: Auth, report: Report | null) {
|
||||
if (!report) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (auth.user?.isAdmin) {
|
||||
return true;
|
||||
}
|
||||
@@ -11,7 +37,13 @@ export async function canViewReport(auth: Auth, report: Report) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return !!(await canViewWebsite(auth, report.websiteId));
|
||||
const section = getReportSection(report.type);
|
||||
|
||||
if (section) {
|
||||
return !!(await canViewWebsiteSection(auth, report.websiteId, section));
|
||||
}
|
||||
|
||||
return !!auth.user && !!(await canViewWebsite(auth, report.websiteId));
|
||||
}
|
||||
|
||||
export async function canUpdateReport(auth: Auth, report: Report) {
|
||||
|
||||
Reference in new issue
Block a user