-
Notifications
You must be signed in to change notification settings - Fork 321
fix(api): harden task automations for MCP/API clients #3044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
apps/api/src/tasks/automations/automations.service.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { ConflictException, NotFoundException } from '@nestjs/common'; | ||
|
|
||
| // Mock the DB layer before importing the service. We also provide a stand-in | ||
| // Prisma.PrismaClientKnownRequestError so the service's `instanceof` checks and | ||
| // error-code branches can be exercised without a real database. | ||
| jest.mock('@db', () => { | ||
| class PrismaClientKnownRequestError extends Error { | ||
| code: string; | ||
| constructor(message: string, { code }: { code: string }) { | ||
| super(message); | ||
| this.code = code; | ||
| this.name = 'PrismaClientKnownRequestError'; | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| db: { | ||
| $transaction: jest.fn(), | ||
| evidenceAutomationVersion: { create: jest.fn() }, | ||
| evidenceAutomation: { update: jest.fn() }, | ||
| }, | ||
| Prisma: { PrismaClientKnownRequestError }, | ||
| }; | ||
| }); | ||
|
|
||
| import { db, Prisma } from '@db'; | ||
| import { AutomationsService } from './automations.service'; | ||
|
|
||
| const prismaError = (code: string) => | ||
| new Prisma.PrismaClientKnownRequestError(code, { | ||
| code, | ||
| clientVersion: '5.0.0', | ||
| }); | ||
|
|
||
| describe('AutomationsService.createVersion — error mapping', () => { | ||
| let service: AutomationsService; | ||
| const input = { version: 1, scriptKey: 'org_1/tsk_1/aut_1.v1.js' }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| service = new AutomationsService(); | ||
| }); | ||
|
|
||
| it('records the version and returns it on success', async () => { | ||
| const created = { id: 'eav_1', version: 1, scriptKey: input.scriptKey }; | ||
| (db.$transaction as jest.Mock).mockResolvedValue([created, { id: 'aut_1' }]); | ||
|
|
||
| const result = await service.createVersion('aut_1', input); | ||
|
|
||
| expect(result).toEqual({ success: true, version: created }); | ||
| }); | ||
|
|
||
| it('maps a duplicate version (P2002) to a 409 ConflictException', async () => { | ||
| (db.$transaction as jest.Mock).mockRejectedValue(prismaError('P2002')); | ||
|
|
||
| await expect(service.createVersion('aut_1', input)).rejects.toBeInstanceOf( | ||
| ConflictException, | ||
| ); | ||
| }); | ||
|
|
||
| it('maps a missing automation (P2003 FK violation) to a 404 NotFoundException', async () => { | ||
| (db.$transaction as jest.Mock).mockRejectedValue(prismaError('P2003')); | ||
|
|
||
| await expect( | ||
| service.createVersion('missing', input), | ||
| ).rejects.toBeInstanceOf(NotFoundException); | ||
| }); | ||
|
|
||
| it('maps a missing automation (P2025 record not found) to a 404 NotFoundException', async () => { | ||
| (db.$transaction as jest.Mock).mockRejectedValue(prismaError('P2025')); | ||
|
|
||
| await expect( | ||
| service.createVersion('missing', input), | ||
| ).rejects.toBeInstanceOf(NotFoundException); | ||
| }); | ||
|
|
||
| it('rethrows unexpected errors untouched (no masking real 500s)', async () => { | ||
| const boom = new Error('db exploded'); | ||
| (db.$transaction as jest.Mock).mockRejectedValue(boom); | ||
|
|
||
| await expect(service.createVersion('aut_1', input)).rejects.toBe(boom); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
apps/api/src/tasks/automations/dto/create-version.dto.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { plainToInstance } from 'class-transformer'; | ||
| import { validate } from 'class-validator'; | ||
| import { CreateVersionDto } from './create-version.dto'; | ||
|
|
||
| /** | ||
| * The original endpoint accepted an inline, untyped `@Body()` — invisible to the | ||
| * ValidationPipe — so a missing `version`/`scriptKey` slipped through and blew up | ||
| * with a Prisma non-null violation (500). These tests prove the DTO now rejects | ||
| * those payloads at the validation layer (400) before they reach the service. | ||
| */ | ||
| describe('CreateVersionDto', () => { | ||
| async function validatePayload(payload: Record<string, unknown>) { | ||
| return validate(plainToInstance(CreateVersionDto, payload)); | ||
| } | ||
|
|
||
| it('accepts a valid payload', async () => { | ||
| const errors = await validatePayload({ | ||
| version: 1, | ||
| scriptKey: 'org_1/tsk_1/aut_1.v1.js', | ||
| changelog: 'initial publish', | ||
| }); | ||
| expect(errors).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('rejects a missing version (previously a 500)', async () => { | ||
| const errors = await validatePayload({ scriptKey: 'k' }); | ||
| expect(errors.some((e) => e.property === 'version')).toBe(true); | ||
| }); | ||
|
|
||
| it('rejects a missing scriptKey (previously a 500)', async () => { | ||
| const errors = await validatePayload({ version: 1 }); | ||
| expect(errors.some((e) => e.property === 'scriptKey')).toBe(true); | ||
| }); | ||
|
|
||
| it('rejects a version below 1', async () => { | ||
| const errors = await validatePayload({ version: 0, scriptKey: 'k' }); | ||
| expect(errors.some((e) => e.property === 'version')).toBe(true); | ||
| }); | ||
|
|
||
| it('rejects an empty scriptKey', async () => { | ||
| const errors = await validatePayload({ version: 1, scriptKey: '' }); | ||
| expect(errors.some((e) => e.property === 'scriptKey')).toBe(true); | ||
| }); | ||
|
|
||
| it('rejects a whitespace-only scriptKey (would otherwise persist a blank key)', async () => { | ||
| for (const scriptKey of [' ', '\t\n', ' ']) { | ||
| const errors = await validatePayload({ version: 1, scriptKey }); | ||
| expect(errors.some((e) => e.property === 'scriptKey')).toBe(true); | ||
| } | ||
| }); | ||
|
|
||
| it('trims surrounding whitespace from a valid scriptKey', async () => { | ||
| const dto = plainToInstance(CreateVersionDto, { | ||
| version: 1, | ||
| scriptKey: ' org_1/tsk_1/aut_1.v1.js ', | ||
| }); | ||
| const errors = await validate(dto); | ||
| expect(errors).toHaveLength(0); | ||
| expect(dto.scriptKey).toBe('org_1/tsk_1/aut_1.v1.js'); | ||
| }); | ||
|
|
||
| it('treats changelog as optional', async () => { | ||
| const errors = await validatePayload({ version: 2, scriptKey: 'k' }); | ||
| expect(errors.some((e) => e.property === 'changelog')).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { ApiProperty } from '@nestjs/swagger'; | ||
| import { Transform } from 'class-transformer'; | ||
| import { IsInt, IsNotEmpty, IsOptional, IsString, Min } from 'class-validator'; | ||
|
|
||
| /** | ||
| * Records that an automation script has been generated + published to S3. | ||
| * `version` and `scriptKey` are REQUIRED — the row references an already-stored | ||
| * script. The web UI's publish flow supplies both from the enterprise publish | ||
| * step; calling this without them used to 500 (Prisma non-null violation). | ||
| */ | ||
| export class CreateVersionDto { | ||
| @ApiProperty({ | ||
| description: 'Version number for this published script', | ||
| example: 1, | ||
| }) | ||
| @IsInt() | ||
| @Min(1) | ||
| version!: number; | ||
|
|
||
| @ApiProperty({ | ||
| description: | ||
| 'S3 key of the already-generated & published automation script (returned by the publish step).', | ||
| example: 'org_abc123/tsk_abc123/aut_abc123.v1.js', | ||
| }) | ||
| @IsString() | ||
| // Trim first so a whitespace-only key collapses to '' and @IsNotEmpty rejects | ||
| // it — otherwise a blank key would persist and the automation would later | ||
| // fail to fetch a script at that key. Non-strings pass through for @IsString. | ||
| @Transform(({ value }) => (typeof value === 'string' ? value.trim() : value)) | ||
| @IsNotEmpty() | ||
| scriptKey!: string; | ||
|
|
||
| @ApiProperty({ | ||
| description: 'Optional changelog describing this version', | ||
| required: false, | ||
| }) | ||
| @IsOptional() | ||
| @IsString() | ||
| changelog?: string; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| { | ||
| "openapi": "3.0.0", | ||
| "x-speakeasy-timeout": 120000, | ||
| "paths": { | ||
| "/v1/organization": { | ||
| "get": { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.