-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1795 from automatisch/rest-get-user-apps
feat: Implement users get apps API endpoint
- Loading branch information
Showing
7 changed files
with
350 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { renderObject } from '../../../../helpers/renderer.js'; | ||
|
||
export default async (request, response) => { | ||
const apps = await request.currentUser.getApps(request.query.name); | ||
|
||
renderObject(response, apps, { serializer: 'App' }); | ||
}; |
210 changes: 210 additions & 0 deletions
210
packages/backend/src/controllers/api/v1/users/get-apps.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
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'; | ||
import { createRole } from '../../../../../test/factories/role'; | ||
import { createUser } from '../../../../../test/factories/user'; | ||
import { createPermission } from '../../../../../test/factories/permission.js'; | ||
import { createFlow } from '../../../../../test/factories/flow.js'; | ||
import { createStep } from '../../../../../test/factories/step.js'; | ||
import { createConnection } from '../../../../../test/factories/connection.js'; | ||
import getAppsMock from '../../../../../test/mocks/rest/api/v1/users/get-apps.js'; | ||
|
||
describe('GET /api/v1/users/:userId/apps', () => { | ||
let currentUser, currentUserRole, token; | ||
|
||
beforeEach(async () => { | ||
currentUserRole = await createRole(); | ||
currentUser = await createUser({ roleId: currentUserRole.id }); | ||
|
||
token = createAuthTokenByUserId(currentUser.id); | ||
}); | ||
|
||
it('should return all apps of the current user', async () => { | ||
await createPermission({ | ||
action: 'read', | ||
subject: 'Flow', | ||
roleId: currentUserRole.id, | ||
conditions: ['isCreator'], | ||
}); | ||
|
||
await createPermission({ | ||
action: 'read', | ||
subject: 'Connection', | ||
roleId: currentUserRole.id, | ||
conditions: ['isCreator'], | ||
}); | ||
|
||
const flowOne = await createFlow({ userId: currentUser.id }); | ||
|
||
await createStep({ | ||
flowId: flowOne.id, | ||
appKey: 'webhook', | ||
}); | ||
|
||
const flowOneActionStepConnection = await createConnection({ | ||
userId: currentUser.id, | ||
key: 'deepl', | ||
draft: false, | ||
}); | ||
|
||
await createStep({ | ||
connectionId: flowOneActionStepConnection.id, | ||
flowId: flowOne.id, | ||
appKey: 'deepl', | ||
}); | ||
|
||
const flowTwo = await createFlow({ userId: currentUser.id }); | ||
|
||
const flowTwoTriggerStepConnection = await createConnection({ | ||
userId: currentUser.id, | ||
key: 'github', | ||
draft: false, | ||
}); | ||
|
||
await createStep({ | ||
connectionId: flowTwoTriggerStepConnection.id, | ||
flowId: flowTwo.id, | ||
appKey: 'github', | ||
}); | ||
|
||
await createStep({ | ||
flowId: flowTwo.id, | ||
appKey: 'slack', | ||
}); | ||
|
||
const response = await request(app) | ||
.get(`/api/v1/users/${currentUser.id}/apps`) | ||
.set('Authorization', token) | ||
.expect(200); | ||
|
||
const expectedPayload = getAppsMock(); | ||
expect(response.body).toEqual(expectedPayload); | ||
}); | ||
|
||
it('should return all apps of the another user', async () => { | ||
const anotherUser = await createUser(); | ||
|
||
await createPermission({ | ||
action: 'read', | ||
subject: 'Flow', | ||
roleId: currentUserRole.id, | ||
conditions: [], | ||
}); | ||
|
||
await createPermission({ | ||
action: 'read', | ||
subject: 'Connection', | ||
roleId: currentUserRole.id, | ||
conditions: [], | ||
}); | ||
|
||
const flowOne = await createFlow({ userId: anotherUser.id }); | ||
|
||
await createStep({ | ||
flowId: flowOne.id, | ||
appKey: 'webhook', | ||
}); | ||
|
||
const flowOneActionStepConnection = await createConnection({ | ||
userId: anotherUser.id, | ||
key: 'deepl', | ||
draft: false, | ||
}); | ||
|
||
await createStep({ | ||
connectionId: flowOneActionStepConnection.id, | ||
flowId: flowOne.id, | ||
appKey: 'deepl', | ||
}); | ||
|
||
const flowTwo = await createFlow({ userId: anotherUser.id }); | ||
|
||
const flowTwoTriggerStepConnection = await createConnection({ | ||
userId: anotherUser.id, | ||
key: 'github', | ||
draft: false, | ||
}); | ||
|
||
await createStep({ | ||
connectionId: flowTwoTriggerStepConnection.id, | ||
flowId: flowTwo.id, | ||
appKey: 'github', | ||
}); | ||
|
||
await createStep({ | ||
flowId: flowTwo.id, | ||
appKey: 'slack', | ||
}); | ||
|
||
const response = await request(app) | ||
.get(`/api/v1/users/${currentUser.id}/apps`) | ||
.set('Authorization', token) | ||
.expect(200); | ||
|
||
const expectedPayload = getAppsMock(); | ||
expect(response.body).toEqual(expectedPayload); | ||
}); | ||
|
||
it('should return specified app of the current user', async () => { | ||
await createPermission({ | ||
action: 'read', | ||
subject: 'Flow', | ||
roleId: currentUserRole.id, | ||
conditions: ['isCreator'], | ||
}); | ||
|
||
await createPermission({ | ||
action: 'read', | ||
subject: 'Connection', | ||
roleId: currentUserRole.id, | ||
conditions: ['isCreator'], | ||
}); | ||
|
||
const flowOne = await createFlow({ userId: currentUser.id }); | ||
|
||
await createStep({ | ||
flowId: flowOne.id, | ||
appKey: 'webhook', | ||
}); | ||
|
||
const flowOneActionStepConnection = await createConnection({ | ||
userId: currentUser.id, | ||
key: 'deepl', | ||
draft: false, | ||
}); | ||
|
||
await createStep({ | ||
connectionId: flowOneActionStepConnection.id, | ||
flowId: flowOne.id, | ||
appKey: 'deepl', | ||
}); | ||
|
||
const flowTwo = await createFlow({ userId: currentUser.id }); | ||
|
||
const flowTwoTriggerStepConnection = await createConnection({ | ||
userId: currentUser.id, | ||
key: 'github', | ||
draft: false, | ||
}); | ||
|
||
await createStep({ | ||
connectionId: flowTwoTriggerStepConnection.id, | ||
flowId: flowTwo.id, | ||
appKey: 'github', | ||
}); | ||
|
||
await createStep({ | ||
flowId: flowTwo.id, | ||
appKey: 'slack', | ||
}); | ||
|
||
const response = await request(app) | ||
.get(`/api/v1/users/${currentUser.id}/apps?name=deepl`) | ||
.set('Authorization', token) | ||
.expect(200); | ||
|
||
expect(response.body.data.length).toEqual(1); | ||
expect(response.body.data[0].key).toEqual('deepl'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,22 @@ | ||
const appSerializer = (app) => { | ||
return { | ||
name: app.name, | ||
let appData = { | ||
key: app.key, | ||
name: app.name, | ||
iconUrl: app.iconUrl, | ||
primaryColor: app.primaryColor, | ||
authDocUrl: app.authDocUrl, | ||
supportsConnections: app.supportsConnections, | ||
primaryColor: app.primaryColor, | ||
}; | ||
|
||
if (app.connectionCount) { | ||
appData.connectionCount = app.connectionCount; | ||
} | ||
|
||
if (app.flowCount) { | ||
appData.flowCount = app.flowCount; | ||
} | ||
|
||
return appData; | ||
}; | ||
|
||
export default appSerializer; |
Oops, something went wrong.