Merge pull request #1675 from automatisch/get-app-auth
feat: Implement get app auth API endpoint
This commit is contained in:
8
packages/backend/src/controllers/api/v1/apps/get-auth.js
Normal file
8
packages/backend/src/controllers/api/v1/apps/get-auth.js
Normal file
@@ -0,0 +1,8 @@
|
||||
import App from '../../../../models/app.js';
|
||||
import { renderObject } from '../../../../helpers/renderer.js';
|
||||
|
||||
export default async (request, response) => {
|
||||
const auth = await App.findAuthByKey(request.params.appKey);
|
||||
|
||||
renderObject(response, auth, { serializer: 'Auth' });
|
||||
};
|
@@ -0,0 +1,35 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import request from 'supertest';
|
||||
import App from '../../../../models/app';
|
||||
import app from '../../../../app.js';
|
||||
import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-user-id';
|
||||
import { createUser } from '../../../../../test/factories/user';
|
||||
import getAuthMock from '../../../../../test/mocks/rest/api/v1/apps/get-auth.js';
|
||||
|
||||
describe('GET /api/v1/apps/:appKey/auth', () => {
|
||||
let currentUser, token;
|
||||
|
||||
beforeEach(async () => {
|
||||
currentUser = await createUser();
|
||||
token = createAuthTokenByUserId(currentUser.id);
|
||||
});
|
||||
|
||||
it('should return the app auth info', async () => {
|
||||
const exampleApp = await App.findOneByKey('github');
|
||||
|
||||
const response = await request(app)
|
||||
.get(`/api/v1/apps/${exampleApp.key}/auth`)
|
||||
.set('Authorization', token)
|
||||
.expect(200);
|
||||
|
||||
const expectedPayload = getAuthMock(exampleApp.auth);
|
||||
expect(response.body).toEqual(expectedPayload);
|
||||
});
|
||||
|
||||
it('should return not found response for invalid app key', async () => {
|
||||
await request(app)
|
||||
.get('/api/v1/apps/invalid-app-key')
|
||||
.set('Authorization', token)
|
||||
.expect(404);
|
||||
});
|
||||
});
|
@@ -39,6 +39,13 @@ class App {
|
||||
return appInfoConverter(rawAppData);
|
||||
}
|
||||
|
||||
static async findAuthByKey(key, stripFuncs = false) {
|
||||
const rawAppData = await getApp(key, stripFuncs);
|
||||
const appData = appInfoConverter(rawAppData);
|
||||
|
||||
return appData.auth;
|
||||
}
|
||||
|
||||
static async checkAppAndAction(appKey, actionKey) {
|
||||
const app = await this.findOneByKey(appKey);
|
||||
|
||||
|
@@ -2,9 +2,11 @@ import { Router } from 'express';
|
||||
import asyncHandler from 'express-async-handler';
|
||||
import { authenticateUser } from '../../../helpers/authentication.js';
|
||||
import getAppAction from '../../../controllers/api/v1/apps/get-app.js';
|
||||
import getAuthAction from '../../../controllers/api/v1/apps/get-auth.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get('/:appKey', authenticateUser, asyncHandler(getAppAction));
|
||||
router.get('/:appKey/auth', authenticateUser, asyncHandler(getAuthAction));
|
||||
|
||||
export default router;
|
||||
|
@@ -3,7 +3,7 @@ import App from '../models/app';
|
||||
import appSerializer from './app';
|
||||
|
||||
describe('appSerializer', () => {
|
||||
it('should return permission data', async () => {
|
||||
it('should return app data', async () => {
|
||||
const app = await App.findOneByKey('deepl');
|
||||
|
||||
const expectedPayload = {
|
||||
|
9
packages/backend/src/serializers/auth.js
Normal file
9
packages/backend/src/serializers/auth.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const authSerializer = (auth) => {
|
||||
return {
|
||||
fields: auth.fields,
|
||||
authenticationSteps: auth.authenticationSteps,
|
||||
reconnectionSteps: auth.reconnectionSteps,
|
||||
};
|
||||
};
|
||||
|
||||
export default authSerializer;
|
17
packages/backend/src/serializers/auth.test.js
Normal file
17
packages/backend/src/serializers/auth.test.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import App from '../models/app';
|
||||
import authSerializer from './auth';
|
||||
|
||||
describe('authSerializer', () => {
|
||||
it('should return auth data', async () => {
|
||||
const auth = await App.findAuthByKey('deepl');
|
||||
|
||||
const expectedPayload = {
|
||||
fields: auth.fields,
|
||||
authenticationSteps: auth.authenticationSteps,
|
||||
reconnectionSteps: auth.reconnectionSteps,
|
||||
};
|
||||
|
||||
expect(authSerializer(auth)).toEqual(expectedPayload);
|
||||
});
|
||||
});
|
@@ -6,6 +6,7 @@ import appAuthClientSerializer from './app-auth-client.js';
|
||||
import flowSerializer from './flow.js';
|
||||
import stepSerializer from './step.js';
|
||||
import appSerializer from './app.js';
|
||||
import authSerializer from './auth.js';
|
||||
|
||||
const serializers = {
|
||||
User: userSerializer,
|
||||
@@ -16,6 +17,7 @@ const serializers = {
|
||||
Flow: flowSerializer,
|
||||
Step: stepSerializer,
|
||||
App: appSerializer,
|
||||
Auth: authSerializer,
|
||||
};
|
||||
|
||||
export default serializers;
|
||||
|
18
packages/backend/test/mocks/rest/api/v1/apps/get-auth.js
Normal file
18
packages/backend/test/mocks/rest/api/v1/apps/get-auth.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const getAuthMock = (auth) => {
|
||||
return {
|
||||
data: {
|
||||
fields: auth.fields,
|
||||
authenticationSteps: auth.authenticationSteps,
|
||||
reconnectionSteps: auth.reconnectionSteps,
|
||||
},
|
||||
meta: {
|
||||
count: 1,
|
||||
currentPage: null,
|
||||
isArray: false,
|
||||
totalPages: null,
|
||||
type: 'Object',
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export default getAuthMock;
|
Reference in New Issue
Block a user