From 813646e392665057c13b88fe4caa5a882339ff90 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Fri, 6 Sep 2024 17:30:10 +0000 Subject: [PATCH 1/7] feat: write REST API endpoint to create step --- .../controllers/api/v1/flows/create-step.js | 14 +++ .../api/v1/flows/create-step.test.js | 113 ++++++++++++++++++ packages/backend/src/helpers/authorization.js | 4 + packages/backend/src/models/flow.js | 25 ++++ packages/backend/src/models/step.js | 2 + packages/backend/src/routes/api/v1/flows.js | 7 ++ packages/backend/test/factories/role.js | 79 ++++++++++++ .../mocks/rest/api/v1/flows/create-step.js | 26 ++++ 8 files changed, 270 insertions(+) create mode 100644 packages/backend/src/controllers/api/v1/flows/create-step.js create mode 100644 packages/backend/src/controllers/api/v1/flows/create-step.test.js create mode 100644 packages/backend/test/mocks/rest/api/v1/flows/create-step.js diff --git a/packages/backend/src/controllers/api/v1/flows/create-step.js b/packages/backend/src/controllers/api/v1/flows/create-step.js new file mode 100644 index 00000000..f0bb71f7 --- /dev/null +++ b/packages/backend/src/controllers/api/v1/flows/create-step.js @@ -0,0 +1,14 @@ +import { renderObject } from '../../../../helpers/renderer.js'; + +export default async (request, response) => { + const flow = await request.currentUser.authorizedFlows + .clone() + .findById(request.params.flowId) + .throwIfNotFound(); + + const createdActionStep = await flow.createActionStep( + request.body.previousStepId + ); + + renderObject(response, createdActionStep, { status: 201 }); +}; diff --git a/packages/backend/src/controllers/api/v1/flows/create-step.test.js b/packages/backend/src/controllers/api/v1/flows/create-step.test.js new file mode 100644 index 00000000..358831dd --- /dev/null +++ b/packages/backend/src/controllers/api/v1/flows/create-step.test.js @@ -0,0 +1,113 @@ +import Crypto from 'node:crypto'; +import { describe, it, expect, beforeEach } from 'vitest'; +import request from 'supertest'; + +import app from '../../../../app.js'; +import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-user-id.js'; +import { createUser } from '../../../../../test/factories/user.js'; +import { createAdminRole } from '../../../../../test/factories/role.js'; +import { createFlow } from '../../../../../test/factories/flow.js'; +import { createStep } from '../../../../../test/factories/step.js'; +import createStepMock from '../../../../../test/mocks/rest/api/v1/flows/create-step.js'; +import { createPermission } from '../../../../../test/factories/permission.js'; + +describe('POST /api/v1/flows/:flowId/steps', () => { + let currentUser, flow, triggerStep, token; + + beforeEach(async () => { + const adminRole = await createAdminRole(); + currentUser = await createUser({ roleId: adminRole.id }); + + flow = await createFlow({ userId: currentUser.id }); + + triggerStep = await createStep({ flowId: flow.id, type: 'trigger' }); + await createStep({ flowId: flow.id, type: 'action' }); + + token = await createAuthTokenByUserId(currentUser.id); + }); + + it('should return created step', async () => { + const response = await request(app) + .post(`/api/v1/flows/${flow.id}/steps`) + .set('Authorization', token) + .send({ + previousStepId: triggerStep.id, + }) + .expect(201); + + const expectedPayload = await createStepMock({ + id: response.body.data.id, + position: 2, + }); + + expect(response.body).toMatchObject(expectedPayload); + }); + + it('should return created step for another user', async () => { + const anotherUser = await createUser(); + const anotherUsertoken = await createAuthTokenByUserId(anotherUser.id); + + await createPermission({ + roleId: anotherUser.roleId, + subject: 'Flow', + action: 'read', + conditions: [], + }); + + await createPermission({ + roleId: anotherUser.roleId, + subject: 'Flow', + action: 'update', + conditions: [], + }); + + const response = await request(app) + .post(`/api/v1/flows/${flow.id}/steps`) + .set('Authorization', anotherUsertoken) + .send({ + previousStepId: triggerStep.id, + }) + .expect(201); + + const expectedPayload = await createStepMock({ + id: response.body.data.id, + position: 2, + }); + + expect(response.body).toMatchObject(expectedPayload); + }); + + it('should return bad request response for invalid flow UUID', async () => { + await request(app) + .post('/api/v1/flows/invalidFlowUUID/steps') + .set('Authorization', token) + .send({ + previousStepId: triggerStep.id, + }) + .expect(400); + }); + + it('should return not found response for invalid flow UUID', async () => { + const notExistingFlowUUID = Crypto.randomUUID(); + + await request(app) + .post(`/api/v1/flows/${notExistingFlowUUID}/steps`) + .set('Authorization', token) + .send({ + previousStepId: triggerStep.id, + }) + .expect(404); + }); + + it('should return not found response for invalid flow UUID', async () => { + const notExistingStepUUID = Crypto.randomUUID(); + + await request(app) + .post(`/api/v1/flows/${flow.id}/steps`) + .set('Authorization', token) + .send({ + previousStepId: notExistingStepUUID, + }) + .expect(404); + }); +}); diff --git a/packages/backend/src/helpers/authorization.js b/packages/backend/src/helpers/authorization.js index eb71fda1..36210615 100644 --- a/packages/backend/src/helpers/authorization.js +++ b/packages/backend/src/helpers/authorization.js @@ -93,6 +93,10 @@ const authorizationList = { action: 'update', subject: 'Flow', }, + 'POST /api/v1/flows/:flowId/steps': { + action: 'update', + subject: 'Flow', + }, }; export const authorizeUser = async (request, response, next) => { diff --git a/packages/backend/src/models/flow.js b/packages/backend/src/models/flow.js index 7e657fbb..3c8a2811 100644 --- a/packages/backend/src/models/flow.js +++ b/packages/backend/src/models/flow.js @@ -135,6 +135,31 @@ class Flow extends Base { return this.$query().withGraphFetched('steps'); } + async createActionStep(previousStepId) { + const previousStep = await this.$relatedQuery('steps') + .findById(previousStepId) + .throwIfNotFound(); + + const createdStep = await this.$relatedQuery('steps').insertAndFetch({ + type: 'action', + position: previousStep.position + 1, + }); + + const nextSteps = await this.$relatedQuery('steps') + .where('position', '>=', createdStep.position) + .whereNot('id', createdStep.id); + + const nextStepQueries = nextSteps.map(async (nextStep, index) => { + return await nextStep.$query().patchAndFetch({ + position: createdStep.position + index + 1, + }); + }); + + await Promise.all(nextStepQueries); + + return createdStep; + } + async $beforeUpdate(opt, queryContext) { await super.$beforeUpdate(opt, queryContext); diff --git a/packages/backend/src/models/step.js b/packages/backend/src/models/step.js index 01f46190..4781d8b6 100644 --- a/packages/backend/src/models/step.js +++ b/packages/backend/src/models/step.js @@ -82,6 +82,8 @@ class Step extends Base { }); get webhookUrl() { + if (!this.webhookPath) return null; + return new URL(this.webhookPath, appConfig.webhookUrl).toString(); } diff --git a/packages/backend/src/routes/api/v1/flows.js b/packages/backend/src/routes/api/v1/flows.js index 38ce3201..e7d21e3b 100644 --- a/packages/backend/src/routes/api/v1/flows.js +++ b/packages/backend/src/routes/api/v1/flows.js @@ -5,6 +5,7 @@ import getFlowsAction from '../../../controllers/api/v1/flows/get-flows.js'; import getFlowAction from '../../../controllers/api/v1/flows/get-flow.js'; import updateFlowAction from '../../../controllers/api/v1/flows/update-flow.js'; import createFlowAction from '../../../controllers/api/v1/flows/create-flow.js'; +import createStepAction from '../../../controllers/api/v1/flows/create-step.js'; const router = Router(); @@ -12,5 +13,11 @@ router.get('/', authenticateUser, authorizeUser, getFlowsAction); router.get('/:flowId', authenticateUser, authorizeUser, getFlowAction); router.post('/', authenticateUser, authorizeUser, createFlowAction); router.patch('/:flowId', authenticateUser, authorizeUser, updateFlowAction); +router.post( + '/:flowId/steps', + authenticateUser, + authorizeUser, + createStepAction +); export default router; diff --git a/packages/backend/test/factories/role.js b/packages/backend/test/factories/role.js index 28ac9960..3d3b165a 100644 --- a/packages/backend/test/factories/role.js +++ b/packages/backend/test/factories/role.js @@ -1,5 +1,6 @@ import { faker } from '@faker-js/faker'; import Role from '../../src/models/role'; +import { createPermission } from './permission'; export const createRole = async (params = {}) => { const name = faker.lorem.word(); @@ -16,3 +17,81 @@ export const createRole = async (params = {}) => { return role; }; + +export const createAdminRole = async (params = {}) => { + const adminRole = await createRole({ ...params, name: 'Admin' }); + + await createPermission({ + roleId: adminRole.id, + action: 'read', + subject: 'Flow', + }); + + await createPermission({ + roleId: adminRole.id, + action: 'create', + subject: 'Flow', + }); + + await createPermission({ + roleId: adminRole.id, + action: 'update', + subject: 'Flow', + }); + + await createPermission({ + roleId: adminRole.id, + action: 'delete', + subject: 'Flow', + }); + + await createPermission({ + roleId: adminRole.id, + action: 'publish', + subject: 'Flow', + }); + + await createPermission({ + roleId: adminRole.id, + action: 'read', + subject: 'Connection', + }); + + await createPermission({ + roleId: adminRole.id, + action: 'create', + subject: 'Connection', + }); + + await createPermission({ + roleId: adminRole.id, + action: 'update', + subject: 'Connection', + }); + + await createPermission({ + roleId: adminRole.id, + action: 'delete', + subject: 'Connection', + }); + + await createPermission({ + roleId: adminRole.id, + action: 'read', + subject: 'Execution', + }); + + await createPermission({ + roleId: adminRole.id, + action: 'create', + subject: 'Execution', + }); + + await createPermission({ + roleId: adminRole.id, + action: 'update', + subject: 'Execution', + }); + + return adminRole; +}; diff --git a/packages/backend/test/mocks/rest/api/v1/flows/create-step.js b/packages/backend/test/mocks/rest/api/v1/flows/create-step.js new file mode 100644 index 00000000..da23f51c --- /dev/null +++ b/packages/backend/test/mocks/rest/api/v1/flows/create-step.js @@ -0,0 +1,26 @@ +const createStepMock = async (step) => { + const data = { + id: step.id, + type: step.type || 'action', + key: step.key || null, + appKey: step.appKey || null, + iconUrl: step.iconUrl || null, + webhookUrl: step.webhookUrl || null, + status: step.status || 'incomplete', + position: step.position, + parameters: step.parameters || {}, + }; + + return { + data, + meta: { + type: 'Step', + count: 1, + isArray: false, + currentPage: null, + totalPages: null, + }, + }; +}; + +export default createStepMock; From af56fa28305cd08c6a8c9ca5dd12c3d80a8689b9 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Fri, 6 Sep 2024 17:30:34 +0000 Subject: [PATCH 2/7] feat(Editor): use REST API endpoint to create step --- packages/web/src/components/Editor/index.jsx | 27 +++++----------- .../src/components/EditorNew/EditorNew.jsx | 31 ++++++------------- packages/web/src/hooks/useCreateStep.js | 24 ++++++++++++++ 3 files changed, 42 insertions(+), 40 deletions(-) create mode 100644 packages/web/src/hooks/useCreateStep.js diff --git a/packages/web/src/components/Editor/index.jsx b/packages/web/src/components/Editor/index.jsx index 0995d300..51de3de9 100644 --- a/packages/web/src/components/Editor/index.jsx +++ b/packages/web/src/components/Editor/index.jsx @@ -4,7 +4,7 @@ import Box from '@mui/material/Box'; import IconButton from '@mui/material/IconButton'; import AddIcon from '@mui/icons-material/Add'; -import { CREATE_STEP } from 'graphql/mutations/create-step'; +import useCreateStep from 'hooks/useCreateStep'; import { UPDATE_STEP } from 'graphql/mutations/update-step'; import FlowStep from 'components/FlowStep'; import { FlowPropType } from 'propTypes/propTypes'; @@ -12,9 +12,9 @@ import { useQueryClient } from '@tanstack/react-query'; function Editor(props) { const [updateStep] = useMutation(UPDATE_STEP); - const [createStep, { loading: creationInProgress }] = - useMutation(CREATE_STEP); const { flow } = props; + const { mutateAsync: createStep, isPending: isCreateStepPending } = + useCreateStep(flow?.id); const [triggerStep] = flow.steps; const [currentStepId, setCurrentStepId] = React.useState(triggerStep.id); const queryClient = useQueryClient(); @@ -48,24 +48,13 @@ function Editor(props) { const addStep = React.useCallback( async (previousStepId) => { - const mutationInput = { - previousStep: { - id: previousStepId, - }, - flow: { - id: flow.id, - }, - }; - - const createdStep = await createStep({ - variables: { input: mutationInput }, + const { data: createdStep } = await createStep({ + previousStepId, }); - const createdStepId = createdStep.data.createStep.id; - setCurrentStepId(createdStepId); - await queryClient.invalidateQueries({ queryKey: ['flows', flow.id] }); + setCurrentStepId(createdStep.id); }, - [createStep, flow.id, queryClient], + [createStep], ); const openNextStep = React.useCallback((nextStep) => { @@ -101,7 +90,7 @@ function Editor(props) { addStep(step.id)} color="primary" - disabled={creationInProgress || flow.active} + disabled={isCreateStepPending || flow.active} > diff --git a/packages/web/src/components/EditorNew/EditorNew.jsx b/packages/web/src/components/EditorNew/EditorNew.jsx index 73d29c51..2ad2db92 100644 --- a/packages/web/src/components/EditorNew/EditorNew.jsx +++ b/packages/web/src/components/EditorNew/EditorNew.jsx @@ -4,8 +4,9 @@ import { useQueryClient } from '@tanstack/react-query'; import { FlowPropType } from 'propTypes/propTypes'; import ReactFlow, { useNodesState, useEdgesState } from 'reactflow'; import 'reactflow/dist/style.css'; + import { UPDATE_STEP } from 'graphql/mutations/update-step'; -import { CREATE_STEP } from 'graphql/mutations/create-step'; +import useCreateStep from 'hooks/useCreateStep'; import { useAutoLayout } from './useAutoLayout'; import { useScrollBoundaries } from './useScrollBoundaries'; @@ -36,8 +37,9 @@ const edgeTypes = { const EditorNew = ({ flow }) => { const [updateStep] = useMutation(UPDATE_STEP); const queryClient = useQueryClient(); - const [createStep, { loading: stepCreationInProgress }] = - useMutation(CREATE_STEP); + + const { mutateAsync: createStep, isPending: isCreateStepPending } = + useCreateStep(flow?.id); const [nodes, setNodes, onNodesChange] = useNodesState( generateInitialNodes(flow), @@ -111,26 +113,13 @@ const EditorNew = ({ flow }) => { const onAddStep = useCallback( async (previousStepId) => { - const mutationInput = { - previousStep: { - id: previousStepId, - }, - flow: { - id: flow.id, - }, - }; + const { data: createdStep } = await createStep({ previousStepId }); - const { - data: { createStep: createdStep }, - } = await createStep({ - variables: { input: mutationInput }, - }); + console.log('CHECK THIS OUT!', createdStep); - const createdStepId = createdStep.id; - await queryClient.invalidateQueries({ queryKey: ['flows', flow.id] }); - createdStepIdRef.current = createdStepId; + createdStepIdRef.current = createdStep.id; }, - [flow.id, createStep, queryClient], + [createStep], ); useEffect(() => { @@ -260,7 +249,7 @@ const EditorNew = ({ flow }) => { > { + const { data } = await api.post(`/v1/flows/${flowId}/steps`, { + previousStepId, + }); + + return data; + }, + + onSuccess: () => { + queryClient.invalidateQueries({ + queryKey: ['flows', flowId], + }); + }, + }); + + return query; +} From 852d4bba0ad24c4d991ecf8dec460220ffe626dc Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Fri, 6 Sep 2024 17:30:56 +0000 Subject: [PATCH 3/7] chore: remove redundant create step mutation --- .../backend/src/graphql/mutation-resolvers.js | 2 - .../src/graphql/mutations/create-step.js | 56 ------------------- packages/backend/src/graphql/schema.graphql | 12 ---- .../web/src/graphql/mutations/create-step.js | 19 ------- 4 files changed, 89 deletions(-) delete mode 100644 packages/backend/src/graphql/mutations/create-step.js delete mode 100644 packages/web/src/graphql/mutations/create-step.js diff --git a/packages/backend/src/graphql/mutation-resolvers.js b/packages/backend/src/graphql/mutation-resolvers.js index 60c8b8ea..fffc1b04 100644 --- a/packages/backend/src/graphql/mutation-resolvers.js +++ b/packages/backend/src/graphql/mutation-resolvers.js @@ -1,5 +1,4 @@ import createConnection from './mutations/create-connection.js'; -import createStep from './mutations/create-step.js'; import createUser from './mutations/create-user.ee.js'; import deleteFlow from './mutations/delete-flow.js'; import deleteRole from './mutations/delete-role.ee.js'; @@ -23,7 +22,6 @@ import deleteCurrentUser from './mutations/delete-current-user.ee.js'; const mutationResolvers = { createConnection, createFlow, - createStep, createUser, deleteCurrentUser, deleteFlow, diff --git a/packages/backend/src/graphql/mutations/create-step.js b/packages/backend/src/graphql/mutations/create-step.js deleted file mode 100644 index 05b8d364..00000000 --- a/packages/backend/src/graphql/mutations/create-step.js +++ /dev/null @@ -1,56 +0,0 @@ -import App from '../../models/app.js'; -import Flow from '../../models/flow.js'; - -const createStep = async (_parent, params, context) => { - const conditions = context.currentUser.can('update', 'Flow'); - const userFlows = context.currentUser.$relatedQuery('flows'); - const allFlows = Flow.query(); - const flowsQuery = conditions.isCreator ? userFlows : allFlows; - - const { input } = params; - - if (input.appKey && input.key) { - await App.checkAppAndAction(input.appKey, input.key); - } - - if (input.appKey && !input.key) { - await App.findOneByKey(input.appKey); - } - - const flow = await flowsQuery - .findOne({ - id: input.flow.id, - }) - .throwIfNotFound(); - - const previousStep = await flow - .$relatedQuery('steps') - .findOne({ - id: input.previousStep.id, - }) - .throwIfNotFound(); - - const step = await flow.$relatedQuery('steps').insertAndFetch({ - key: input.key, - appKey: input.appKey, - type: 'action', - position: previousStep.position + 1, - }); - - const nextSteps = await flow - .$relatedQuery('steps') - .where('position', '>=', step.position) - .whereNot('id', step.id); - - const nextStepQueries = nextSteps.map(async (nextStep, index) => { - await nextStep.$query().patchAndFetch({ - position: step.position + index + 1, - }); - }); - - await Promise.all(nextStepQueries); - - return step; -}; - -export default createStep; diff --git a/packages/backend/src/graphql/schema.graphql b/packages/backend/src/graphql/schema.graphql index 4958b2f0..de30c7a9 100644 --- a/packages/backend/src/graphql/schema.graphql +++ b/packages/backend/src/graphql/schema.graphql @@ -4,7 +4,6 @@ type Query { type Mutation { createConnection(input: CreateConnectionInput): Connection createFlow(input: CreateFlowInput): Flow - createStep(input: CreateStepInput): Step createUser(input: CreateUserInput): UserWithAcceptInvitationUrl deleteCurrentUser: Boolean deleteFlow(input: DeleteFlowInput): Boolean @@ -266,17 +265,6 @@ input DuplicateFlowInput { id: String! } -input CreateStepInput { - id: String - previousStepId: String - key: String - appKey: String - connection: StepConnectionInput - flow: StepFlowInput - parameters: JSONObject - previousStep: PreviousStepInput -} - input UpdateStepInput { id: String previousStepId: String diff --git a/packages/web/src/graphql/mutations/create-step.js b/packages/web/src/graphql/mutations/create-step.js deleted file mode 100644 index 9b77b3f3..00000000 --- a/packages/web/src/graphql/mutations/create-step.js +++ /dev/null @@ -1,19 +0,0 @@ -import { gql } from '@apollo/client'; -export const CREATE_STEP = gql` - mutation CreateStep($input: CreateStepInput) { - createStep(input: $input) { - id - type - key - appKey - parameters - iconUrl - position - webhookUrl - status - connection { - id - } - } - } -`; From 1bcaec144b5f2f44881e8ed37df726d8d1458675 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Tue, 10 Sep 2024 10:15:59 +0000 Subject: [PATCH 4/7] test(create-step): use non-admin user --- .../api/v1/flows/create-step.test.js | 16 +++- packages/backend/test/factories/role.js | 79 ------------------- 2 files changed, 13 insertions(+), 82 deletions(-) diff --git a/packages/backend/src/controllers/api/v1/flows/create-step.test.js b/packages/backend/src/controllers/api/v1/flows/create-step.test.js index 358831dd..4d90dfba 100644 --- a/packages/backend/src/controllers/api/v1/flows/create-step.test.js +++ b/packages/backend/src/controllers/api/v1/flows/create-step.test.js @@ -5,7 +5,6 @@ import request from 'supertest'; import app from '../../../../app.js'; import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-user-id.js'; import { createUser } from '../../../../../test/factories/user.js'; -import { createAdminRole } from '../../../../../test/factories/role.js'; import { createFlow } from '../../../../../test/factories/flow.js'; import { createStep } from '../../../../../test/factories/step.js'; import createStepMock from '../../../../../test/mocks/rest/api/v1/flows/create-step.js'; @@ -15,8 +14,19 @@ describe('POST /api/v1/flows/:flowId/steps', () => { let currentUser, flow, triggerStep, token; beforeEach(async () => { - const adminRole = await createAdminRole(); - currentUser = await createUser({ roleId: adminRole.id }); + currentUser = await createUser(); + + await createPermission({ + roleId: currentUser.roleId, + subject: 'Flow', + action: 'read', + }); + + await createPermission({ + roleId: currentUser.roleId, + subject: 'Flow', + action: 'update', + }); flow = await createFlow({ userId: currentUser.id }); diff --git a/packages/backend/test/factories/role.js b/packages/backend/test/factories/role.js index 3d3b165a..28ac9960 100644 --- a/packages/backend/test/factories/role.js +++ b/packages/backend/test/factories/role.js @@ -1,6 +1,5 @@ import { faker } from '@faker-js/faker'; import Role from '../../src/models/role'; -import { createPermission } from './permission'; export const createRole = async (params = {}) => { const name = faker.lorem.word(); @@ -17,81 +16,3 @@ export const createRole = async (params = {}) => { return role; }; - -export const createAdminRole = async (params = {}) => { - const adminRole = await createRole({ ...params, name: 'Admin' }); - - await createPermission({ - roleId: adminRole.id, - action: 'read', - subject: 'Flow', - }); - - await createPermission({ - roleId: adminRole.id, - action: 'create', - subject: 'Flow', - }); - - await createPermission({ - roleId: adminRole.id, - action: 'update', - subject: 'Flow', - }); - - await createPermission({ - roleId: adminRole.id, - action: 'delete', - subject: 'Flow', - }); - - await createPermission({ - roleId: adminRole.id, - action: 'publish', - subject: 'Flow', - }); - - await createPermission({ - roleId: adminRole.id, - action: 'read', - subject: 'Connection', - }); - - await createPermission({ - roleId: adminRole.id, - action: 'create', - subject: 'Connection', - }); - - await createPermission({ - roleId: adminRole.id, - action: 'update', - subject: 'Connection', - }); - - await createPermission({ - roleId: adminRole.id, - action: 'delete', - subject: 'Connection', - }); - - await createPermission({ - roleId: adminRole.id, - action: 'read', - subject: 'Execution', - }); - - await createPermission({ - roleId: adminRole.id, - action: 'create', - subject: 'Execution', - }); - - await createPermission({ - roleId: adminRole.id, - action: 'update', - subject: 'Execution', - }); - - return adminRole; -}; From 82161f028e55e59befc6147a905b45585f9323a7 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Tue, 10 Sep 2024 10:21:42 +0000 Subject: [PATCH 5/7] test(create-step): state permission conditions explicitly --- .../backend/src/controllers/api/v1/flows/create-step.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/controllers/api/v1/flows/create-step.test.js b/packages/backend/src/controllers/api/v1/flows/create-step.test.js index 4d90dfba..d41408ba 100644 --- a/packages/backend/src/controllers/api/v1/flows/create-step.test.js +++ b/packages/backend/src/controllers/api/v1/flows/create-step.test.js @@ -20,12 +20,14 @@ describe('POST /api/v1/flows/:flowId/steps', () => { roleId: currentUser.roleId, subject: 'Flow', action: 'read', + conditions: ['isCreator'], }); await createPermission({ roleId: currentUser.roleId, subject: 'Flow', action: 'update', + conditions: ['isCreator'], }); flow = await createFlow({ userId: currentUser.id }); @@ -36,7 +38,7 @@ describe('POST /api/v1/flows/:flowId/steps', () => { token = await createAuthTokenByUserId(currentUser.id); }); - it('should return created step', async () => { + it('should return created step for current user', async () => { const response = await request(app) .post(`/api/v1/flows/${flow.id}/steps`) .set('Authorization', token) From 2992236be438f5e6bf9c65d912b3c3dd28f7a4a9 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Tue, 10 Sep 2024 10:35:19 +0000 Subject: [PATCH 6/7] test(create-step): make current and another user explicit --- .../api/v1/flows/create-step.test.js | 81 +++++++++++++++---- 1 file changed, 66 insertions(+), 15 deletions(-) diff --git a/packages/backend/src/controllers/api/v1/flows/create-step.test.js b/packages/backend/src/controllers/api/v1/flows/create-step.test.js index d41408ba..efc599b5 100644 --- a/packages/backend/src/controllers/api/v1/flows/create-step.test.js +++ b/packages/backend/src/controllers/api/v1/flows/create-step.test.js @@ -16,6 +16,16 @@ describe('POST /api/v1/flows/:flowId/steps', () => { beforeEach(async () => { currentUser = await createUser(); + flow = await createFlow({ userId: currentUser.id }); + + triggerStep = await createStep({ flowId: flow.id, type: 'trigger' }); + + await createStep({ flowId: flow.id, type: 'action' }); + + token = await createAuthTokenByUserId(currentUser.id); + }); + + it('should return created step for current user', async () => { await createPermission({ roleId: currentUser.roleId, subject: 'Flow', @@ -30,15 +40,6 @@ describe('POST /api/v1/flows/:flowId/steps', () => { conditions: ['isCreator'], }); - flow = await createFlow({ userId: currentUser.id }); - - triggerStep = await createStep({ flowId: flow.id, type: 'trigger' }); - await createStep({ flowId: flow.id, type: 'action' }); - - token = await createAuthTokenByUserId(currentUser.id); - }); - - it('should return created step for current user', async () => { const response = await request(app) .post(`/api/v1/flows/${flow.id}/steps`) .set('Authorization', token) @@ -57,27 +58,35 @@ describe('POST /api/v1/flows/:flowId/steps', () => { it('should return created step for another user', async () => { const anotherUser = await createUser(); - const anotherUsertoken = await createAuthTokenByUserId(anotherUser.id); + + const anotherUserFlow = await createFlow({ userId: anotherUser.id }); + + const anotherUserFlowTriggerStep = await createStep({ + flowId: anotherUserFlow.id, + type: 'trigger', + }); + + await createStep({ flowId: anotherUserFlow.id, type: 'action' }); await createPermission({ - roleId: anotherUser.roleId, + roleId: currentUser.roleId, subject: 'Flow', action: 'read', conditions: [], }); await createPermission({ - roleId: anotherUser.roleId, + roleId: currentUser.roleId, subject: 'Flow', action: 'update', conditions: [], }); const response = await request(app) - .post(`/api/v1/flows/${flow.id}/steps`) - .set('Authorization', anotherUsertoken) + .post(`/api/v1/flows/${anotherUserFlow.id}/steps`) + .set('Authorization', token) .send({ - previousStepId: triggerStep.id, + previousStepId: anotherUserFlowTriggerStep.id, }) .expect(201); @@ -90,6 +99,20 @@ describe('POST /api/v1/flows/:flowId/steps', () => { }); it('should return bad request response for invalid flow UUID', async () => { + await createPermission({ + roleId: currentUser.roleId, + subject: 'Flow', + action: 'read', + conditions: ['isCreator'], + }); + + await createPermission({ + roleId: currentUser.roleId, + subject: 'Flow', + action: 'update', + conditions: ['isCreator'], + }); + await request(app) .post('/api/v1/flows/invalidFlowUUID/steps') .set('Authorization', token) @@ -100,6 +123,20 @@ describe('POST /api/v1/flows/:flowId/steps', () => { }); it('should return not found response for invalid flow UUID', async () => { + await createPermission({ + roleId: currentUser.roleId, + subject: 'Flow', + action: 'read', + conditions: ['isCreator'], + }); + + await createPermission({ + roleId: currentUser.roleId, + subject: 'Flow', + action: 'update', + conditions: ['isCreator'], + }); + const notExistingFlowUUID = Crypto.randomUUID(); await request(app) @@ -112,6 +149,20 @@ describe('POST /api/v1/flows/:flowId/steps', () => { }); it('should return not found response for invalid flow UUID', async () => { + await createPermission({ + roleId: currentUser.roleId, + subject: 'Flow', + action: 'read', + conditions: ['isCreator'], + }); + + await createPermission({ + roleId: currentUser.roleId, + subject: 'Flow', + action: 'update', + conditions: ['isCreator'], + }); + const notExistingStepUUID = Crypto.randomUUID(); await request(app) From 3ba4c8b3bfccfea7f07485f0f9575f379eee50eb Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Tue, 10 Sep 2024 10:50:11 +0000 Subject: [PATCH 7/7] chore(EditorNew): remove console log --- packages/web/src/components/EditorNew/EditorNew.jsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/web/src/components/EditorNew/EditorNew.jsx b/packages/web/src/components/EditorNew/EditorNew.jsx index 2ad2db92..8858af6e 100644 --- a/packages/web/src/components/EditorNew/EditorNew.jsx +++ b/packages/web/src/components/EditorNew/EditorNew.jsx @@ -115,8 +115,6 @@ const EditorNew = ({ flow }) => { async (previousStepId) => { const { data: createdStep } = await createStep({ previousStepId }); - console.log('CHECK THIS OUT!', createdStep); - createdStepIdRef.current = createdStep.id; }, [createStep],