Merge pull request #2055 from automatisch/rest-test-step
feat: Implement rest api endpoint to test step
This commit is contained in:
12
packages/backend/src/controllers/api/v1/steps/test-step.js
Normal file
12
packages/backend/src/controllers/api/v1/steps/test-step.js
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { renderObject } from '../../../../helpers/renderer.js';
|
||||||
|
|
||||||
|
export default async (request, response) => {
|
||||||
|
let step = await request.currentUser.authorizedSteps
|
||||||
|
.clone()
|
||||||
|
.findById(request.params.stepId)
|
||||||
|
.throwIfNotFound();
|
||||||
|
|
||||||
|
step = await step.test();
|
||||||
|
|
||||||
|
renderObject(response, step);
|
||||||
|
};
|
209
packages/backend/src/controllers/api/v1/steps/test-step.test.js
Normal file
209
packages/backend/src/controllers/api/v1/steps/test-step.test.js
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
import { describe, it, expect, beforeEach } from 'vitest';
|
||||||
|
import request from 'supertest';
|
||||||
|
import Crypto from 'crypto';
|
||||||
|
import app from '../../../../app.js';
|
||||||
|
import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-user-id';
|
||||||
|
import { createUser } from '../../../../../test/factories/user';
|
||||||
|
import { createConnection } from '../../../../../test/factories/connection';
|
||||||
|
import { createFlow } from '../../../../../test/factories/flow';
|
||||||
|
import { createStep } from '../../../../../test/factories/step';
|
||||||
|
import { createExecution } from '../../../../../test/factories/execution.js';
|
||||||
|
import { createExecutionStep } from '../../../../../test/factories/execution-step.js';
|
||||||
|
import { createPermission } from '../../../../../test/factories/permission';
|
||||||
|
import testStepMock from '../../../../../test/mocks/rest/api/v1/steps/test-step.js';
|
||||||
|
|
||||||
|
describe('POST /api/v1/steps/:stepId/test', () => {
|
||||||
|
let currentUser, currentUserRole, token;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
currentUser = await createUser();
|
||||||
|
currentUserRole = await currentUser.$relatedQuery('role');
|
||||||
|
|
||||||
|
token = await createAuthTokenByUserId(currentUser.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should test the step of the current user and return step data', async () => {
|
||||||
|
const currentUserFlow = await createFlow({ userId: currentUser.id });
|
||||||
|
const currentUserConnection = await createConnection();
|
||||||
|
|
||||||
|
const triggerStep = await createStep({
|
||||||
|
flowId: currentUserFlow.id,
|
||||||
|
connectionId: currentUserConnection.id,
|
||||||
|
appKey: 'webhook',
|
||||||
|
key: 'catchRawWebhook',
|
||||||
|
type: 'trigger',
|
||||||
|
parameters: {
|
||||||
|
workSynchronously: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const actionStep = await createStep({
|
||||||
|
flowId: currentUserFlow.id,
|
||||||
|
connectionId: currentUserConnection.id,
|
||||||
|
appKey: 'formatter',
|
||||||
|
key: 'text',
|
||||||
|
type: 'action',
|
||||||
|
parameters: {
|
||||||
|
input: `{{step.${triggerStep.id}.body.name}}`,
|
||||||
|
transform: 'capitalize',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const execution = await createExecution({
|
||||||
|
flowId: currentUserFlow.id,
|
||||||
|
testRun: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
await createExecutionStep({
|
||||||
|
dataIn: { workSynchronously: false },
|
||||||
|
dataOut: { body: { name: 'john doe' } },
|
||||||
|
stepId: triggerStep.id,
|
||||||
|
executionId: execution.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
await createPermission({
|
||||||
|
action: 'read',
|
||||||
|
subject: 'Flow',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: ['isCreator'],
|
||||||
|
});
|
||||||
|
|
||||||
|
await createPermission({
|
||||||
|
action: 'update',
|
||||||
|
subject: 'Flow',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: ['isCreator'],
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await request(app)
|
||||||
|
.post(`/api/v1/steps/${actionStep.id}/test`)
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
const expectedLastExecutionStep = await actionStep.$relatedQuery(
|
||||||
|
'lastExecutionStep'
|
||||||
|
);
|
||||||
|
|
||||||
|
const expectedPayload = await testStepMock(
|
||||||
|
actionStep,
|
||||||
|
expectedLastExecutionStep
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(response.body).toMatchObject(expectedPayload);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should test the step of the another user and return step data', async () => {
|
||||||
|
const anotherUser = await createUser();
|
||||||
|
const anotherUserFlow = await createFlow({ userId: anotherUser.id });
|
||||||
|
const anotherUserConnection = await createConnection();
|
||||||
|
|
||||||
|
const triggerStep = await createStep({
|
||||||
|
flowId: anotherUserFlow.id,
|
||||||
|
connectionId: anotherUserConnection.id,
|
||||||
|
appKey: 'webhook',
|
||||||
|
key: 'catchRawWebhook',
|
||||||
|
type: 'trigger',
|
||||||
|
parameters: {
|
||||||
|
workSynchronously: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const actionStep = await createStep({
|
||||||
|
flowId: anotherUserFlow.id,
|
||||||
|
connectionId: anotherUserConnection.id,
|
||||||
|
appKey: 'formatter',
|
||||||
|
key: 'text',
|
||||||
|
type: 'action',
|
||||||
|
parameters: {
|
||||||
|
input: `{{step.${triggerStep.id}.body.name}}`,
|
||||||
|
transform: 'capitalize',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const execution = await createExecution({
|
||||||
|
flowId: anotherUserFlow.id,
|
||||||
|
testRun: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
await createExecutionStep({
|
||||||
|
dataIn: { workSynchronously: false },
|
||||||
|
dataOut: { body: { name: 'john doe' } },
|
||||||
|
stepId: triggerStep.id,
|
||||||
|
executionId: execution.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
await createPermission({
|
||||||
|
action: 'read',
|
||||||
|
subject: 'Flow',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
await createPermission({
|
||||||
|
action: 'update',
|
||||||
|
subject: 'Flow',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await request(app)
|
||||||
|
.post(`/api/v1/steps/${actionStep.id}/test`)
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
const expectedLastExecutionStep = await actionStep.$relatedQuery(
|
||||||
|
'lastExecutionStep'
|
||||||
|
);
|
||||||
|
|
||||||
|
const expectedPayload = await testStepMock(
|
||||||
|
actionStep,
|
||||||
|
expectedLastExecutionStep
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(response.body).toMatchObject(expectedPayload);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return not found response for not existing step UUID', async () => {
|
||||||
|
await createPermission({
|
||||||
|
action: 'update',
|
||||||
|
subject: 'Flow',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
await createPermission({
|
||||||
|
action: 'read',
|
||||||
|
subject: 'Flow',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
const notExistingStepUUID = Crypto.randomUUID();
|
||||||
|
|
||||||
|
await request(app)
|
||||||
|
.post(`/api/v1/steps/${notExistingStepUUID}/test`)
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(404);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return bad request response for invalid step UUID', async () => {
|
||||||
|
await createPermission({
|
||||||
|
action: 'update',
|
||||||
|
subject: 'Flow',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
await createPermission({
|
||||||
|
action: 'read',
|
||||||
|
subject: 'Flow',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
await request(app)
|
||||||
|
.post('/api/v1/steps/invalidStepUUID/test')
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(400);
|
||||||
|
});
|
||||||
|
});
|
@@ -6,7 +6,6 @@ import deleteCurrentUser from './mutations/delete-current-user.ee.js';
|
|||||||
import deleteFlow from './mutations/delete-flow.js';
|
import deleteFlow from './mutations/delete-flow.js';
|
||||||
import deleteRole from './mutations/delete-role.ee.js';
|
import deleteRole from './mutations/delete-role.ee.js';
|
||||||
import duplicateFlow from './mutations/duplicate-flow.js';
|
import duplicateFlow from './mutations/duplicate-flow.js';
|
||||||
import executeFlow from './mutations/execute-flow.js';
|
|
||||||
import generateAuthUrl from './mutations/generate-auth-url.js';
|
import generateAuthUrl from './mutations/generate-auth-url.js';
|
||||||
import registerUser from './mutations/register-user.ee.js';
|
import registerUser from './mutations/register-user.ee.js';
|
||||||
import resetConnection from './mutations/reset-connection.js';
|
import resetConnection from './mutations/reset-connection.js';
|
||||||
@@ -19,6 +18,7 @@ import upsertSamlAuthProvider from './mutations/upsert-saml-auth-provider.ee.js'
|
|||||||
import upsertSamlAuthProvidersRoleMappings from './mutations/upsert-saml-auth-providers-role-mappings.ee.js';
|
import upsertSamlAuthProvidersRoleMappings from './mutations/upsert-saml-auth-providers-role-mappings.ee.js';
|
||||||
|
|
||||||
// Converted mutations
|
// Converted mutations
|
||||||
|
import executeFlow from './mutations/execute-flow.js';
|
||||||
import updateUser from './mutations/update-user.ee.js';
|
import updateUser from './mutations/update-user.ee.js';
|
||||||
import deleteStep from './mutations/delete-step.js';
|
import deleteStep from './mutations/delete-step.js';
|
||||||
import verifyConnection from './mutations/verify-connection.js';
|
import verifyConnection from './mutations/verify-connection.js';
|
||||||
|
@@ -29,6 +29,10 @@ const authorizationList = {
|
|||||||
action: 'read',
|
action: 'read',
|
||||||
subject: 'Flow',
|
subject: 'Flow',
|
||||||
},
|
},
|
||||||
|
'POST /api/v1/steps/:stepId/test': {
|
||||||
|
action: 'update',
|
||||||
|
subject: 'Flow',
|
||||||
|
},
|
||||||
'GET /api/v1/steps/:stepId/previous-steps': {
|
'GET /api/v1/steps/:stepId/previous-steps': {
|
||||||
action: 'update',
|
action: 'update',
|
||||||
subject: 'Flow',
|
subject: 'Flow',
|
||||||
|
@@ -9,6 +9,7 @@ import Telemetry from '../helpers/telemetry/index.js';
|
|||||||
import appConfig from '../config/app.js';
|
import appConfig from '../config/app.js';
|
||||||
import globalVariable from '../helpers/global-variable.js';
|
import globalVariable from '../helpers/global-variable.js';
|
||||||
import computeParameters from '../helpers/compute-parameters.js';
|
import computeParameters from '../helpers/compute-parameters.js';
|
||||||
|
import testRun from '../services/test-run.js';
|
||||||
|
|
||||||
class Step extends Base {
|
class Step extends Base {
|
||||||
static tableName = 'steps';
|
static tableName = 'steps';
|
||||||
@@ -59,6 +60,17 @@ class Step extends Base {
|
|||||||
to: 'connections.id',
|
to: 'connections.id',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
lastExecutionStep: {
|
||||||
|
relation: Base.HasOneRelation,
|
||||||
|
modelClass: ExecutionStep,
|
||||||
|
join: {
|
||||||
|
from: 'steps.id',
|
||||||
|
to: 'execution_steps.step_id',
|
||||||
|
},
|
||||||
|
filter(builder) {
|
||||||
|
builder.orderBy('created_at', 'desc').limit(1).first();
|
||||||
|
},
|
||||||
|
},
|
||||||
executionSteps: {
|
executionSteps: {
|
||||||
relation: Base.HasManyRelation,
|
relation: Base.HasManyRelation,
|
||||||
modelClass: ExecutionStep,
|
modelClass: ExecutionStep,
|
||||||
@@ -145,6 +157,16 @@ class Step extends Base {
|
|||||||
return await App.findOneByKey(this.appKey);
|
return await App.findOneByKey(this.appKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async test() {
|
||||||
|
await testRun({ stepId: this.id });
|
||||||
|
|
||||||
|
const updatedStep = await this.$query()
|
||||||
|
.withGraphFetched('lastExecutionStep')
|
||||||
|
.patchAndFetch({ status: 'completed' });
|
||||||
|
|
||||||
|
return updatedStep;
|
||||||
|
}
|
||||||
|
|
||||||
async getLastExecutionStep() {
|
async getLastExecutionStep() {
|
||||||
const lastExecutionStep = await this.$relatedQuery('executionSteps')
|
const lastExecutionStep = await this.$relatedQuery('executionSteps')
|
||||||
.orderBy('created_at', 'desc')
|
.orderBy('created_at', 'desc')
|
||||||
|
@@ -2,6 +2,7 @@ import { Router } from 'express';
|
|||||||
import { authenticateUser } from '../../../helpers/authentication.js';
|
import { authenticateUser } from '../../../helpers/authentication.js';
|
||||||
import { authorizeUser } from '../../../helpers/authorization.js';
|
import { authorizeUser } from '../../../helpers/authorization.js';
|
||||||
import getConnectionAction from '../../../controllers/api/v1/steps/get-connection.js';
|
import getConnectionAction from '../../../controllers/api/v1/steps/get-connection.js';
|
||||||
|
import testStepAction from '../../../controllers/api/v1/steps/test-step.js';
|
||||||
import getPreviousStepsAction from '../../../controllers/api/v1/steps/get-previous-steps.js';
|
import getPreviousStepsAction from '../../../controllers/api/v1/steps/get-previous-steps.js';
|
||||||
import createDynamicFieldsAction from '../../../controllers/api/v1/steps/create-dynamic-fields.js';
|
import createDynamicFieldsAction from '../../../controllers/api/v1/steps/create-dynamic-fields.js';
|
||||||
import createDynamicDataAction from '../../../controllers/api/v1/steps/create-dynamic-data.js';
|
import createDynamicDataAction from '../../../controllers/api/v1/steps/create-dynamic-data.js';
|
||||||
@@ -16,6 +17,8 @@ router.get(
|
|||||||
getConnectionAction
|
getConnectionAction
|
||||||
);
|
);
|
||||||
|
|
||||||
|
router.post('/:stepId/test', authenticateUser, authorizeUser, testStepAction);
|
||||||
|
|
||||||
router.get(
|
router.get(
|
||||||
'/:stepId/previous-steps',
|
'/:stepId/previous-steps',
|
||||||
authenticateUser,
|
authenticateUser,
|
||||||
|
@@ -13,6 +13,12 @@ const stepSerializer = (step) => {
|
|||||||
parameters: step.parameters,
|
parameters: step.parameters,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (step.lastExecutionStep) {
|
||||||
|
stepData.lastExecutionStep = executionStepSerializer(
|
||||||
|
step.lastExecutionStep
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (step.executionSteps?.length > 0) {
|
if (step.executionSteps?.length > 0) {
|
||||||
stepData.executionSteps = step.executionSteps.map((executionStep) =>
|
stepData.executionSteps = step.executionSteps.map((executionStep) =>
|
||||||
executionStepSerializer(executionStep)
|
executionStepSerializer(executionStep)
|
||||||
|
@@ -27,6 +27,18 @@ describe('stepSerializer', () => {
|
|||||||
expect(stepSerializer(step)).toEqual(expectedPayload);
|
expect(stepSerializer(step)).toEqual(expectedPayload);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return step data with the last execution step', async () => {
|
||||||
|
const executionStep = await createExecutionStep({ stepId: step.id });
|
||||||
|
|
||||||
|
step.lastExecutionStep = executionStep;
|
||||||
|
|
||||||
|
const expectedPayload = {
|
||||||
|
lastExecutionStep: executionStepSerializer(executionStep),
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(stepSerializer(step)).toMatchObject(expectedPayload);
|
||||||
|
});
|
||||||
|
|
||||||
it('should return step data with the execution steps', async () => {
|
it('should return step data with the execution steps', async () => {
|
||||||
const executionStepOne = await createExecutionStep({ stepId: step.id });
|
const executionStepOne = await createExecutionStep({ stepId: step.id });
|
||||||
const executionStepTwo = await createExecutionStep({ stepId: step.id });
|
const executionStepTwo = await createExecutionStep({ stepId: step.id });
|
||||||
|
34
packages/backend/test/mocks/rest/api/v1/steps/test-step.js
Normal file
34
packages/backend/test/mocks/rest/api/v1/steps/test-step.js
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
const testStepMock = async (step, lastExecutionStep) => {
|
||||||
|
const data = {
|
||||||
|
id: step.id,
|
||||||
|
appKey: step.appKey,
|
||||||
|
key: step.key,
|
||||||
|
iconUrl: step.iconUrl,
|
||||||
|
lastExecutionStep: {
|
||||||
|
id: lastExecutionStep.id,
|
||||||
|
status: lastExecutionStep.status,
|
||||||
|
dataIn: lastExecutionStep.dataIn,
|
||||||
|
dataOut: lastExecutionStep.dataOut,
|
||||||
|
errorDetails: lastExecutionStep.errorDetails,
|
||||||
|
createdAt: lastExecutionStep.createdAt.getTime(),
|
||||||
|
updatedAt: lastExecutionStep.updatedAt.getTime(),
|
||||||
|
},
|
||||||
|
parameters: step.parameters,
|
||||||
|
position: step.position,
|
||||||
|
status: step.status,
|
||||||
|
type: step.type,
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
data: data,
|
||||||
|
meta: {
|
||||||
|
count: 1,
|
||||||
|
currentPage: null,
|
||||||
|
isArray: false,
|
||||||
|
totalPages: null,
|
||||||
|
type: 'Step',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default testStepMock;
|
Reference in New Issue
Block a user