Merge pull request #1701 from automatisch/rest-get-app-config

feat: Implement get app config API endpoint
This commit is contained in:
Ömer Faruk Aydın
2024-03-07 14:26:46 +01:00
committed by GitHub
9 changed files with 144 additions and 6 deletions

View File

@@ -0,0 +1,12 @@
import { renderObject } from '../../../../helpers/renderer.js';
import AppConfig from '../../../../models/app-config.js';
export default async (request, response) => {
const appConfig = await AppConfig.query()
.findOne({
key: request.params.appKey,
})
.throwIfNotFound();
renderObject(response, appConfig);
};

View File

@@ -0,0 +1,44 @@
import { vi, 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.js';
import { createUser } from '../../../../../test/factories/user.js';
import getAppConfigMock from '../../../../../test/mocks/rest/api/v1/app-configs/get-app-config.js';
import { createAppConfig } from '../../../../../test/factories/app-config.js';
import * as license from '../../../../helpers/license.ee.js';
describe('GET /api/v1/app-configs/:appKey', () => {
let currentUser, appConfig, token;
beforeEach(async () => {
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
currentUser = await createUser();
appConfig = await createAppConfig({
key: 'deepl',
allowCustomConnection: true,
shared: true,
disabled: false,
});
token = createAuthTokenByUserId(currentUser.id);
});
it('should return specified app config info', async () => {
const response = await request(app)
.get(`/api/v1/app-configs/${appConfig.key}`)
.set('Authorization', token)
.expect(200);
const expectedPayload = getAppConfigMock(appConfig);
expect(response.body).toEqual(expectedPayload);
});
it('should return not found response for not existing app key', async () => {
await request(app)
.get('/api/v1/app-configs/not-existing-app-key')
.set('Authorization', token)
.expect(404);
});
});

View File

@@ -0,0 +1,16 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { authenticateUser } from '../../../helpers/authentication.js';
import { checkIsEnterprise } from '../../../helpers/check-is-enterprise.js';
import getAppConfigAction from '../../../controllers/api/v1/app-configs/get-app-config.ee.js';
const router = Router();
router.get(
'/:appKey',
authenticateUser,
checkIsEnterprise,
asyncHandler(getAppConfigAction)
);
export default router;

View File

@@ -7,6 +7,7 @@ import automatischRouter from './api/v1/automatisch.js';
import usersRouter from './api/v1/users.js';
import paymentRouter from './api/v1/payment.ee.js';
import appAuthClientsRouter from './api/v1/app-auth-clients.js';
import appConfigsRouter from './api/v1/app-configs.ee.js';
import flowsRouter from './api/v1/flows.js';
import appsRouter from './api/v1/apps.js';
import executionsRouter from './api/v1/executions.js';
@@ -26,6 +27,7 @@ router.use('/api/v1/automatisch', automatischRouter);
router.use('/api/v1/users', usersRouter);
router.use('/api/v1/payment', paymentRouter);
router.use('/api/v1/app-auth-clients', appAuthClientsRouter);
router.use('/api/v1/app-configs', appConfigsRouter);
router.use('/api/v1/flows', flowsRouter);
router.use('/api/v1/apps', appsRouter);
router.use('/api/v1/executions', executionsRouter);

View File

@@ -0,0 +1,15 @@
const appConfigSerializer = (appConfig) => {
return {
id: appConfig.id,
key: appConfig.key,
allowCustomConnection: appConfig.allowCustomConnection,
shared: appConfig.shared,
disabled: appConfig.disabled,
canConnect: appConfig.canConnect,
canCustomConnect: appConfig.canCustomConnect,
createdAt: appConfig.createdAt.getTime(),
updatedAt: appConfig.updatedAt.getTime(),
};
};
export default appConfigSerializer;

View File

@@ -0,0 +1,27 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { createAppConfig } from '../../test/factories/app-config';
import appConfigSerializer from './app-config';
describe('appConfig serializer', () => {
let appConfig;
beforeEach(async () => {
appConfig = await createAppConfig();
});
it('should return app config data', async () => {
const expectedPayload = {
id: appConfig.id,
key: appConfig.key,
allowCustomConnection: appConfig.allowCustomConnection,
shared: appConfig.shared,
disabled: appConfig.disabled,
canConnect: appConfig.canConnect,
canCustomConnect: appConfig.canCustomConnect,
createdAt: appConfig.createdAt.getTime(),
updatedAt: appConfig.updatedAt.getTime(),
};
expect(appConfigSerializer(appConfig)).toEqual(expectedPayload);
});
});

View File

@@ -3,6 +3,7 @@ import roleSerializer from './role.js';
import permissionSerializer from './permission.js';
import samlAuthProviderSerializer from './saml-auth-provider.ee.js';
import appAuthClientSerializer from './app-auth-client.js';
import appConfigSerializer from './app-config.js';
import flowSerializer from './flow.js';
import stepSerializer from './step.js';
import appSerializer from './app.js';
@@ -18,6 +19,7 @@ const serializers = {
Permission: permissionSerializer,
SamlAuthProvider: samlAuthProviderSerializer,
AppAuthClient: appAuthClientSerializer,
AppConfig: appConfigSerializer,
Flow: flowSerializer,
Step: stepSerializer,
App: appSerializer,

View File

@@ -1,13 +1,9 @@
import AppConfig from '../../src/models/app-config.js';
export const createAppConfig = async (params = {}) => {
const appConfigData = {
key: params?.key || 'gitlab',
};
params.key = params?.key || 'gitlab';
const appConfig = await AppConfig.query()
.insert(appConfigData)
.returning('*');
const appConfig = await AppConfig.query().insert(params).returning('*');
return appConfig;
};

View File

@@ -0,0 +1,24 @@
const getAppConfigMock = (appConfig) => {
return {
data: {
id: appConfig.id,
key: appConfig.key,
allowCustomConnection: appConfig.allowCustomConnection,
shared: appConfig.shared,
disabled: appConfig.disabled,
canConnect: appConfig.canConnect,
canCustomConnect: appConfig.canCustomConnect,
createdAt: appConfig.createdAt.getTime(),
updatedAt: appConfig.updatedAt.getTime(),
},
meta: {
count: 1,
currentPage: null,
isArray: false,
totalPages: null,
type: 'AppConfig',
},
};
};
export default getAppConfigMock;