feat: write POST /v1/connections/:connectionId/reset
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
import { renderObject } from '../../../../helpers/renderer.js';
|
||||||
|
|
||||||
|
export default async (request, response) => {
|
||||||
|
let connection = await request.currentUser
|
||||||
|
.$relatedQuery('connections')
|
||||||
|
.findOne({
|
||||||
|
id: request.params.connectionId,
|
||||||
|
})
|
||||||
|
.throwIfNotFound();
|
||||||
|
|
||||||
|
if (!connection.formattedData) {
|
||||||
|
return renderObject(response, connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
connection = await connection.$query().patchAndFetch({
|
||||||
|
formattedData: { screenName: connection.formattedData.screenName },
|
||||||
|
});
|
||||||
|
|
||||||
|
renderObject(response, connection);
|
||||||
|
};
|
@@ -0,0 +1,138 @@
|
|||||||
|
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.js';
|
||||||
|
import { createUser } from '../../../../../test/factories/user.js';
|
||||||
|
import { createConnection } from '../../../../../test/factories/connection.js';
|
||||||
|
import { createPermission } from '../../../../../test/factories/permission.js';
|
||||||
|
import resetConnectionMock from '../../../../../test/mocks/rest/api/v1/connections/reset-connection.js';
|
||||||
|
|
||||||
|
describe('POST /api/v1/connections/:connectionId/reset', () => {
|
||||||
|
let currentUser, currentUserRole, token;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
currentUser = await createUser();
|
||||||
|
currentUserRole = await currentUser.$relatedQuery('role');
|
||||||
|
|
||||||
|
token = await createAuthTokenByUserId(currentUser.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`should reset the connection's formatted data`, async () => {
|
||||||
|
const currentUserConnection = await createConnection({
|
||||||
|
userId: currentUser.id,
|
||||||
|
key: 'deepl',
|
||||||
|
verified: true,
|
||||||
|
formattedData: {
|
||||||
|
screenName: 'Connection name',
|
||||||
|
clientSecret: 'secret',
|
||||||
|
clientId: 'id',
|
||||||
|
token: 'token',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await createPermission({
|
||||||
|
action: 'read',
|
||||||
|
subject: 'Connection',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: ['isCreator'],
|
||||||
|
});
|
||||||
|
|
||||||
|
await createPermission({
|
||||||
|
action: 'create',
|
||||||
|
subject: 'Connection',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: ['isCreator'],
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await request(app)
|
||||||
|
.post(`/api/v1/connections/${currentUserConnection.id}/reset`)
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
const refetchedCurrentUserConnection = await currentUserConnection.$query();
|
||||||
|
|
||||||
|
const expectedPayload = resetConnectionMock({
|
||||||
|
...refetchedCurrentUserConnection,
|
||||||
|
reconnectable: refetchedCurrentUserConnection.reconnectable,
|
||||||
|
formattedData: {
|
||||||
|
screenName: 'Connection name',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.body).toMatchObject(expectedPayload);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return not found response for another user', async () => {
|
||||||
|
const anotherUser = await createUser();
|
||||||
|
|
||||||
|
const anotherUserConnection = await createConnection({
|
||||||
|
userId: anotherUser.id,
|
||||||
|
key: 'deepl',
|
||||||
|
verified: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
await createPermission({
|
||||||
|
action: 'read',
|
||||||
|
subject: 'Connection',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
await createPermission({
|
||||||
|
action: 'create',
|
||||||
|
subject: 'Connection',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
await request(app)
|
||||||
|
.post(`/api/v1/connections/${anotherUserConnection.id}/reset`)
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(404);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return not found response for not existing connection UUID', async () => {
|
||||||
|
const notExistingConnectionUUID = Crypto.randomUUID();
|
||||||
|
|
||||||
|
await createPermission({
|
||||||
|
action: 'read',
|
||||||
|
subject: 'Connection',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: ['isCreator'],
|
||||||
|
});
|
||||||
|
|
||||||
|
await createPermission({
|
||||||
|
action: 'create',
|
||||||
|
subject: 'Connection',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: ['isCreator'],
|
||||||
|
});
|
||||||
|
|
||||||
|
await request(app)
|
||||||
|
.post(`/api/v1/connections/${notExistingConnectionUUID}/reset`)
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(404);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return bad request response for invalid UUID', async () => {
|
||||||
|
await createPermission({
|
||||||
|
action: 'read',
|
||||||
|
subject: 'Connection',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: ['isCreator'],
|
||||||
|
});
|
||||||
|
|
||||||
|
await createPermission({
|
||||||
|
action: 'create',
|
||||||
|
subject: 'Connection',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: ['isCreator'],
|
||||||
|
});
|
||||||
|
|
||||||
|
await request(app)
|
||||||
|
.post('/api/v1/connections/invalidConnectionUUID/reset')
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(400);
|
||||||
|
});
|
||||||
|
});
|
@@ -75,6 +75,10 @@ const authorizationList = {
|
|||||||
action: 'delete',
|
action: 'delete',
|
||||||
subject: 'Connection',
|
subject: 'Connection',
|
||||||
},
|
},
|
||||||
|
'POST /api/v1/connections/:connectionId/reset': {
|
||||||
|
action: 'create',
|
||||||
|
subject: 'Connection',
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const authorizeUser = async (request, response, next) => {
|
export const authorizeUser = async (request, response, next) => {
|
||||||
|
@@ -5,6 +5,7 @@ import getFlowsAction from '../../../controllers/api/v1/connections/get-flows.js
|
|||||||
import testConnectionAction from '../../../controllers/api/v1/connections/test-connection.js';
|
import testConnectionAction from '../../../controllers/api/v1/connections/test-connection.js';
|
||||||
import verifyConnectionAction from '../../../controllers/api/v1/connections/verify-connection.js';
|
import verifyConnectionAction from '../../../controllers/api/v1/connections/verify-connection.js';
|
||||||
import deleteConnectionAction from '../../../controllers/api/v1/connections/delete-connection.js';
|
import deleteConnectionAction from '../../../controllers/api/v1/connections/delete-connection.js';
|
||||||
|
import resetConnectionAction from '../../../controllers/api/v1/connections/reset-connection.js';
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
@@ -29,6 +30,13 @@ router.post(
|
|||||||
testConnectionAction
|
testConnectionAction
|
||||||
);
|
);
|
||||||
|
|
||||||
|
router.post(
|
||||||
|
'/:connectionId/reset',
|
||||||
|
authenticateUser,
|
||||||
|
authorizeUser,
|
||||||
|
resetConnectionAction
|
||||||
|
);
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
'/:connectionId/verify',
|
'/:connectionId/verify',
|
||||||
authenticateUser,
|
authenticateUser,
|
||||||
|
@@ -0,0 +1,27 @@
|
|||||||
|
const resetConnectionMock = (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 resetConnectionMock;
|
Reference in New Issue
Block a user