feat: Implement get step connection API endpoint

This commit is contained in:
Faruk AYDIN
2024-03-13 15:24:50 +01:00
parent d6923a2ff0
commit 9a0434be32
7 changed files with 186 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
import { renderObject } from '../../../../helpers/renderer.js';
export default async (request, response) => {
const step = await request.currentUser.authorizedSteps
.findById(request.params.stepId)
.throwIfNotFound();
const connection = await step.$relatedQuery('connection').throwIfNotFound();
renderObject(response, connection);
};

View File

@@ -0,0 +1,121 @@
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 { createPermission } from '../../../../../test/factories/permission';
import getConnectionMock from '../../../../../test/mocks/rest/api/v1/steps/get-connection';
describe('GET /api/v1/steps/:stepId/connection', () => {
let currentUser, currentUserRole, token;
beforeEach(async () => {
currentUser = await createUser();
currentUserRole = await currentUser.$relatedQuery('role');
token = createAuthTokenByUserId(currentUser.id);
});
it('should return the current user connection data of specified step', async () => {
const currentUserflow = await createFlow({ userId: currentUser.id });
const currentUserConnection = await createConnection();
const triggerStep = await createStep({
flowId: currentUserflow.id,
connectionId: currentUserConnection.id,
});
await createPermission({
action: 'read',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: ['isCreator'],
});
const response = await request(app)
.get(`/api/v1/steps/${triggerStep.id}/connection`)
.set('Authorization', token)
.expect(200);
const expectedPayload = await getConnectionMock(currentUserConnection);
expect(response.body).toEqual(expectedPayload);
});
it('should return the current user connection data of specified step', 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,
});
await createPermission({
action: 'read',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: [],
});
const response = await request(app)
.get(`/api/v1/steps/${triggerStep.id}/connection`)
.set('Authorization', token)
.expect(200);
const expectedPayload = await getConnectionMock(anotherUserConnection);
expect(response.body).toEqual(expectedPayload);
});
it('should return not found response for not existing step without connection', async () => {
const stepWithoutConnection = await createStep();
await createPermission({
action: 'read',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: [],
});
await request(app)
.get(`/api/v1/steps/${stepWithoutConnection.id}/connection`)
.set('Authorization', token)
.expect(404);
});
it('should return not found response for not existing step UUID', async () => {
await createPermission({
action: 'read',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: [],
});
const notExistingFlowUUID = Crypto.randomUUID();
await request(app)
.get(`/api/v1/steps/${notExistingFlowUUID}/connection`)
.set('Authorization', token)
.expect(404);
});
it('should return bad request response for invalid UUID', async () => {
await createPermission({
action: 'read',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: [],
});
await request(app)
.get('/api/v1/steps/invalidFlowUUID/connection')
.set('Authorization', token)
.expect(400);
});
});

View File

@@ -15,6 +15,10 @@ const authorizationList = {
action: 'read', action: 'read',
subject: 'Flow', subject: 'Flow',
}, },
'GET /api/v1/steps/:stepId/connection': {
action: 'read',
subject: 'Flow',
},
'GET /api/v1/connections/:connectionId/flows': { 'GET /api/v1/connections/:connectionId/flows': {
action: 'read', action: 'read',
subject: 'Flow', subject: 'Flow',

View File

@@ -149,6 +149,11 @@ class User extends Base {
return conditions.isCreator ? this.$relatedQuery('flows') : Flow.query(); return conditions.isCreator ? this.$relatedQuery('flows') : Flow.query();
} }
get authorizedSteps() {
const conditions = this.can('read', 'Flow');
return conditions.isCreator ? this.$relatedQuery('steps') : Step.query();
}
get authorizedExecutions() { get authorizedExecutions() {
const conditions = this.can('read', 'Execution'); const conditions = this.can('read', 'Execution');
return conditions.isCreator return conditions.isCreator

View File

@@ -0,0 +1,16 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { authenticateUser } from '../../../helpers/authentication.js';
import { authorizeUser } from '../../../helpers/authorization.js';
import getConnectionAction from '../../../controllers/api/v1/steps/get-connection.js';
const router = Router();
router.get(
'/:stepId/connection',
authenticateUser,
authorizeUser,
asyncHandler(getConnectionAction)
);
export default router;

View File

@@ -9,6 +9,7 @@ import paymentRouter from './api/v1/payment.ee.js';
import appAuthClientsRouter from './api/v1/app-auth-clients.js'; import appAuthClientsRouter from './api/v1/app-auth-clients.js';
import appConfigsRouter from './api/v1/app-configs.ee.js'; import appConfigsRouter from './api/v1/app-configs.ee.js';
import flowsRouter from './api/v1/flows.js'; import flowsRouter from './api/v1/flows.js';
import stepsRouter from './api/v1/steps.js';
import appsRouter from './api/v1/apps.js'; import appsRouter from './api/v1/apps.js';
import connectionsRouter from './api/v1/connections.js'; import connectionsRouter from './api/v1/connections.js';
import executionsRouter from './api/v1/executions.js'; import executionsRouter from './api/v1/executions.js';
@@ -30,6 +31,7 @@ router.use('/api/v1/payment', paymentRouter);
router.use('/api/v1/app-auth-clients', appAuthClientsRouter); router.use('/api/v1/app-auth-clients', appAuthClientsRouter);
router.use('/api/v1/app-configs', appConfigsRouter); router.use('/api/v1/app-configs', appConfigsRouter);
router.use('/api/v1/flows', flowsRouter); router.use('/api/v1/flows', flowsRouter);
router.use('/api/v1/steps', stepsRouter);
router.use('/api/v1/apps', appsRouter); router.use('/api/v1/apps', appsRouter);
router.use('/api/v1/connections', connectionsRouter); router.use('/api/v1/connections', connectionsRouter);
router.use('/api/v1/executions', executionsRouter); router.use('/api/v1/executions', executionsRouter);

View File

@@ -0,0 +1,27 @@
const getConnectionMock = async (connection) => {
const data = {
id: connection.id,
key: connection.key,
verified: connection.verified,
reconnectable: connection.reconnectable,
appAuthClientId: connection.appAuthClientId,
formattedData: {
screenName: connection.formattedData.screenName,
},
createdAt: connection.createdAt.getTime(),
updatedAt: connection.updatedAt.getTime(),
};
return {
data: data,
meta: {
count: 1,
currentPage: null,
isArray: false,
totalPages: null,
type: 'Connection',
},
};
};
export default getConnectionMock;