Skip to content

Commit

Permalink
fix(wf-def): disallow template editing (#2661)
Browse files Browse the repository at this point in the history
  • Loading branch information
alonp99 authored Sep 2, 2024
1 parent 6900f0b commit 61919ac
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,19 @@ export class WorkflowDefinitionRepository {
projectIds: TProjectIds,
noValidate = false,
): Promise<Prisma.BatchPayload> {
const workflowDefinition = await this.prisma.workflowDefinition.findUnique({
where: { id },
select: { isPublic: true },
});

if (workflowDefinition?.isPublic) {
throw new Error('Cannot update public workflow definition templates');
}

const scopedArgs = this.scopeService.scopeUpdateMany(
{
...args,
where: { id },
where: { id, isPublic: false },
},
projectIds,
);
Expand All @@ -142,6 +151,14 @@ export class WorkflowDefinitionRepository {
args: Prisma.SelectSubset<T, Omit<Prisma.WorkflowDefinitionDeleteArgs, 'where'>>,
projectIds: TProjectIds,
): Promise<WorkflowDefinition> {
const workflowDefinition = await this.prisma.workflowDefinition.findUnique({
where: { id },
select: { isPublic: true },
});

if (workflowDefinition?.isPublic) {
throw new Error('Cannot delete public workflow definition templates');
}
return await this.prisma.workflowDefinition.delete(
this.scopeService.scopeDelete(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ClsService } from 'nestjs-cls';
import { ApiKeyService } from '@/customer/api-key/api-key.service';
import { ApiKeyRepository } from '@/customer/api-key/api-key.repository';

const buildWorkflowDefinition = (sequenceNum: number, projectId?: string) => {
const buildWorkflowDefinition = (sequenceNum: number, projectId?: string, isPublic = false) => {
return {
id: sequenceNum.toString(),
name: `name ${sequenceNum}`,
Expand All @@ -42,7 +42,7 @@ const buildWorkflowDefinition = (sequenceNum: number, projectId?: string) => {
schema: {},
},
projectId: projectId,
isPublic: false,
isPublic: isPublic,
};
};

Expand Down Expand Up @@ -83,7 +83,7 @@ describe('WorkflowDefinitionService', () => {

beforeEach(async () => {
await prismaService.workflowDefinition.create({
data: buildWorkflowDefinition(1),
data: buildWorkflowDefinition(Math.floor(Math.random() * 1000) + 1),
});

const customer = await createCustomer(
Expand Down Expand Up @@ -242,4 +242,41 @@ describe('WorkflowDefinitionService', () => {
expect(latestWorkflowVersion.id).toEqual(updatedWorkflowDefintiion.id);
});
});

describe('Public records (templates)', () => {
it('should not allow editing of public records', async () => {
// Arrange
const publicWorkflowDefinition = await prismaService.workflowDefinition.create({
data: buildWorkflowDefinition(11, undefined, true),
});

const updateArgs = {
definition: { some: 'new definition' },
};

// Act & Assert
await expect(
workflowDefinitionService.updateById(publicWorkflowDefinition.id, updateArgs as any, [
project.id,
]),
).rejects.toThrow('Cannot update public workflow definition templates');
});

it('should allow reading of public records', async () => {
// Arrange
const publicWorkflowDefinition = await prismaService.workflowDefinition.create({
data: buildWorkflowDefinition(23, undefined, true),
});

// Act
const result = await workflowDefinitionService.getLatestVersion(publicWorkflowDefinition.id, [
project.id,
]);

// Assert
expect(result).toBeDefined();
expect(result.id).toEqual(publicWorkflowDefinition.id);
expect(result.isPublic).toBe(true);
});
});
});

0 comments on commit 61919ac

Please sign in to comment.