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

feat: Implement API endpoint to get flows of the specified app #1708

Merged
merged 1 commit into from
Mar 9, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/backend/src/controllers/api/v1/apps/get-flows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { renderObject } from '../../../../helpers/renderer.js';
import App from '../../../../models/app.js';
import paginateRest from '../../../../helpers/pagination-rest.js';

export default async (request, response) => {
const app = await App.findOneByKey(request.params.appKey);

const flowsQuery = request.currentUser.authorizedFlows
.clone()
.joinRelated({
steps: true,
})
.withGraphFetched({
steps: true,
})
.where('steps.app_key', app.key)
.orderBy('active', 'desc')
.orderBy('updated_at', 'desc');

const flows = await paginateRest(flowsQuery, request.query.page);

renderObject(response, flows);
};
129 changes: 129 additions & 0 deletions packages/backend/src/controllers/api/v1/apps/get-flows.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
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 { createFlow } from '../../../../../test/factories/flow.js';
import { createStep } from '../../../../../test/factories/step.js';
import { createPermission } from '../../../../../test/factories/permission.js';
import getFlowsMock from '../../../../../test/mocks/rest/api/v1/flows/get-flows.js';

describe('GET /api/v1/apps/:appKey/flows', () => {
let currentUser, currentUserRole, token;

beforeEach(async () => {
currentUser = await createUser();
currentUserRole = await currentUser.$relatedQuery('role');

token = createAuthTokenByUserId(currentUser.id);
});

it('should return the flows data of specified app for current user', async () => {
const currentUserFlowOne = await createFlow({ userId: currentUser.id });

const triggerStepFlowOne = await createStep({
flowId: currentUserFlowOne.id,
type: 'trigger',
appKey: 'webhook',
});

const actionStepFlowOne = await createStep({
flowId: currentUserFlowOne.id,
type: 'action',
});

const currentUserFlowTwo = await createFlow({ userId: currentUser.id });

await createStep({
flowId: currentUserFlowTwo.id,
type: 'trigger',
appKey: 'github',
});

await createStep({
flowId: currentUserFlowTwo.id,
type: 'action',
});

await createPermission({
action: 'read',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: ['isCreator'],
});

const response = await request(app)
.get('/api/v1/apps/webhook/flows')
.set('Authorization', token)
.expect(200);

const expectedPayload = await getFlowsMock(
[currentUserFlowOne],
[triggerStepFlowOne, actionStepFlowOne]
);

expect(response.body).toEqual(expectedPayload);
});

it('should return the flows data of specified app for another user', async () => {
const anotherUser = await createUser();
const anotherUserFlowOne = await createFlow({ userId: anotherUser.id });

const triggerStepFlowOne = await createStep({
flowId: anotherUserFlowOne.id,
type: 'trigger',
appKey: 'webhook',
});

const actionStepFlowOne = await createStep({
flowId: anotherUserFlowOne.id,
type: 'action',
});

const anotherUserFlowTwo = await createFlow({ userId: anotherUser.id });

await createStep({
flowId: anotherUserFlowTwo.id,
type: 'trigger',
appKey: 'github',
});

await createStep({
flowId: anotherUserFlowTwo.id,
type: 'action',
});

await createPermission({
action: 'read',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: [],
});

const response = await request(app)
.get('/api/v1/apps/webhook/flows')
.set('Authorization', token)
.expect(200);

const expectedPayload = await getFlowsMock(
[anotherUserFlowOne],
[triggerStepFlowOne, actionStepFlowOne]
);

expect(response.body).toEqual(expectedPayload);
});

it('should return not found response for invalid app key', async () => {
await createPermission({
action: 'read',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: ['isCreator'],
});

await request(app)
.get('/api/v1/apps/invalid-app-key/flows')
.set('Authorization', token)
.expect(404);
});
});
4 changes: 4 additions & 0 deletions packages/backend/src/helpers/authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const authorizationList = {
action: 'read',
subject: 'Flow',
},
'GET /api/v1/apps/:appKey/flows': {
action: 'read',
subject: 'Flow',
},
'GET /api/v1/executions/:executionId': {
action: 'read',
subject: 'Execution',
Expand Down
9 changes: 9 additions & 0 deletions packages/backend/src/routes/api/v1/apps.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { authenticateUser } from '../../../helpers/authentication.js';
import { authorizeUser } from '../../../helpers/authorization.js';
import getAppAction from '../../../controllers/api/v1/apps/get-app.js';
import getAppsAction from '../../../controllers/api/v1/apps/get-apps.js';
import getAuthAction from '../../../controllers/api/v1/apps/get-auth.js';
import getTriggersAction from '../../../controllers/api/v1/apps/get-triggers.js';
import getTriggerSubstepsAction from '../../../controllers/api/v1/apps/get-trigger-substeps.js';
import getActionsAction from '../../../controllers/api/v1/apps/get-actions.js';
import getActionSubstepsAction from '../../../controllers/api/v1/apps/get-action-substeps.js';
import getFlowsAction from '../../../controllers/api/v1/apps/get-flows.js';

const router = Router();

Expand Down Expand Up @@ -39,4 +41,11 @@ router.get(
asyncHandler(getActionSubstepsAction)
);

router.get(
'/:appKey/flows',
authenticateUser,
authorizeUser,
asyncHandler(getFlowsAction)
);

export default router;