Skip to content

Commit

Permalink
feat: Implement get app auth API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
farukaydin committed Feb 28, 2024
1 parent 356668a commit 5dca019
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
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);
});
});
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;
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 5dca019

Please sign in to comment.