Skip to content
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

fix(core): Ensure executions cannot resume if already running #10014

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/cli/src/WaitingWebhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export class WaitingWebhooks implements IWebhookManager {
throw new NotFoundError(`The execution "${executionId} does not exist.`);
}

if (execution.status === 'running') {
throw new ConflictError(`The execution "${executionId} is running already.`);
}

if (execution.finished || execution.data.resultData.error) {
throw new ConflictError(`The execution "${executionId} has finished already.`);
}
Expand Down
80 changes: 80 additions & 0 deletions packages/cli/src/__tests__/waiting-webhooks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { mock } from 'jest-mock-extended';
import { WaitingWebhooks } from '@/WaitingWebhooks';
import { ConflictError } from '@/errors/response-errors/conflict.error';
import { NotFoundError } from '@/errors/response-errors/not-found.error';
import type { IExecutionResponse, WaitingWebhookRequest } from '@/Interfaces';
import type express from 'express';
import type { ExecutionRepository } from '@/databases/repositories/execution.repository';

describe('WaitingWebhooks', () => {
const executionRepository = mock<ExecutionRepository>();
const waitingWebhooks = new WaitingWebhooks(mock(), mock(), executionRepository);

beforeEach(() => {
jest.restoreAllMocks();
});

it('should throw NotFoundError if there is no execution to resume', async () => {
/**
* Arrange
*/
executionRepository.findSingleExecution.mockResolvedValue(undefined);

/**
* Act
*/
const promise = waitingWebhooks.executeWebhook(
mock<WaitingWebhookRequest>(),
mock<express.Response>(),
);

/**
* Assert
*/
await expect(promise).rejects.toThrowError(NotFoundError);
});

it('should throw ConflictError if the execution to resume is already running', async () => {
/**
* Arrange
*/
executionRepository.findSingleExecution.mockResolvedValue(
mock<IExecutionResponse>({ status: 'running' }),
);

/**
* Act
*/
const promise = waitingWebhooks.executeWebhook(
mock<WaitingWebhookRequest>(),
mock<express.Response>(),
);

/**
* Assert
*/
await expect(promise).rejects.toThrowError(ConflictError);
});

it('should throw ConflictError if the execution to resume already finished', async () => {
/**
* Arrange
*/
executionRepository.findSingleExecution.mockResolvedValue(
mock<IExecutionResponse>({ finished: true }),
);

/**
* Act
*/
const promise = waitingWebhooks.executeWebhook(
mock<WaitingWebhookRequest>(),
mock<express.Response>(),
);

/**
* Assert
*/
await expect(promise).rejects.toThrowError(ConflictError);
});
});
Loading