feat: write DELETE /v1/connections/:connectionId

This commit is contained in:
Ali BARIN
2024-08-28 13:16:16 +00:00
parent 456f8a30cc
commit c413ae06dc
4 changed files with 100 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
export default async (request, response) => {
await request.currentUser
.$relatedQuery('connections')
.delete()
.findOne({
id: request.params.connectionId,
})
.throwIfNotFound();
response.status(204).end();
};

View File

@@ -0,0 +1,77 @@
import { describe, it, 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.js';
import { createUser } from '../../../../../test/factories/user.js';
import { createConnection } from '../../../../../test/factories/connection.js';
import { createPermission } from '../../../../../test/factories/permission.js';
describe('DELETE /api/v1/connections/:connectionId', () => {
let currentUser, currentUserRole, token;
beforeEach(async () => {
currentUser = await createUser();
currentUserRole = await currentUser.$relatedQuery('role');
await createPermission({
action: 'delete',
subject: 'Connection',
roleId: currentUserRole.id,
conditions: ['isCreator'],
});
await createPermission({
action: 'update',
subject: 'Connection',
roleId: currentUserRole.id,
conditions: ['isCreator'],
});
token = await createAuthTokenByUserId(currentUser.id);
});
it('should delete the connection for current user', async () => {
const currentUserConnection = await createConnection({
userId: currentUser.id,
key: 'deepl',
verified: true,
});
await request(app)
.delete(`/api/v1/connections/${currentUserConnection.id}`)
.set('Authorization', token)
.expect(204);
});
it(`should return not found for other users' connections`, async () => {
const anotherUser = await createUser();
const anotherUserConnection = await createConnection({
userId: anotherUser.id,
key: 'deepl',
verified: true,
});
await request(app)
.post(`/api/v1/connections/${anotherUserConnection.id}`)
.set('Authorization', token)
.expect(404);
});
it('should return not found response for not existing connection UUID', async () => {
const notExistingConnectionUUID = Crypto.randomUUID();
await request(app)
.delete(`/api/v1/connections/${notExistingConnectionUUID}`)
.set('Authorization', token)
.expect(404);
});
it('should return bad request response for invalid UUID', async () => {
await request(app)
.delete('/api/v1/connections/invalidConnectionUUID')
.set('Authorization', token)
.expect(400);
});
});

View File

@@ -71,6 +71,10 @@ const authorizationList = {
action: 'update',
subject: 'Flow',
},
'DELETE /api/v1/connections/:connectionId': {
action: 'delete',
subject: 'Connection',
},
};
export const authorizeUser = async (request, response, next) => {

View File

@@ -4,9 +4,17 @@ import { authorizeUser } from '../../../helpers/authorization.js';
import getFlowsAction from '../../../controllers/api/v1/connections/get-flows.js';
import testConnectionAction from '../../../controllers/api/v1/connections/test-connection.js';
import verifyConnectionAction from '../../../controllers/api/v1/connections/verify-connection.js';
import deleteConnectionAction from '../../../controllers/api/v1/connections/delete-connection.js';
const router = Router();
router.delete(
'/:connectionId',
authenticateUser,
authorizeUser,
deleteConnectionAction
);
router.get(
'/:connectionId/flows',
authenticateUser,