Skip to content

Commit

Permalink
Merge pull request #1675 from automatisch/get-app-auth
Browse files Browse the repository at this point in the history
feat: Implement get app auth API endpoint
  • Loading branch information
farukaydin committed Feb 28, 2024
2 parents 7f324ab + 5dca019 commit 7149c76
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/backend/src/controllers/api/v1/apps/get-auth.js
Original file line number Diff line number Diff line change
@@ -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' });
};
35 changes: 35 additions & 0 deletions packages/backend/src/controllers/api/v1/apps/get-auth.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
7 changes: 7 additions & 0 deletions packages/backend/src/models/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/routes/api/v1/apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion packages/backend/src/serializers/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
9 changes: 9 additions & 0 deletions packages/backend/src/serializers/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const authSerializer = (auth) => {
return {
fields: auth.fields,
authenticationSteps: auth.authenticationSteps,
reconnectionSteps: auth.reconnectionSteps,
};
};

export default authSerializer;
17 changes: 17 additions & 0 deletions packages/backend/src/serializers/auth.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 2 additions & 0 deletions packages/backend/src/serializers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -16,6 +17,7 @@ const serializers = {
Flow: flowSerializer,
Step: stepSerializer,
App: appSerializer,
Auth: authSerializer,
};

export default serializers;
18 changes: 18 additions & 0 deletions packages/backend/test/mocks/rest/api/v1/apps/get-auth.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 7149c76

Please sign in to comment.