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

This commit is contained in:
Iván Ovejero 2024-07-11 15:49:05 +02:00 committed by GitHub
parent c2adaa58cd
commit d651be4e01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 84 additions and 0 deletions

View file

@ -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.`);
}

View file

@ -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);
});
});