refactor: rewrite create-app-auth-client mutation as REST endpoint

This commit is contained in:
Ali BARIN
2024-08-23 13:51:31 +00:00
parent 164d31dfbc
commit 81c39d7d93
14 changed files with 168 additions and 63 deletions

View File

@@ -0,0 +1,14 @@
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();
const appAuthClient = await appConfig
.$relatedQuery('appAuthClients')
.insert(request.body);
renderObject(response, appAuthClient, { status: 201 });
};

View File

@@ -0,0 +1,86 @@
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 { createRole } from '../../../../../../test/factories/role.js';
import createAppAuthClientMock from '../../../../../../test/mocks/rest/api/v1/admin/apps/create-auth-client.js';
import { createAppConfig } from '../../../../../../test/factories/app-config.js';
import * as license from '../../../../../helpers/license.ee.js';
describe('POST /api/v1/admin/apps/:appKey/auth-clients', () => {
let currentUser, adminRole, token;
beforeEach(async () => {
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
adminRole = await createRole({ key: 'admin' });
currentUser = await createUser({ roleId: adminRole.id });
token = await createAuthTokenByUserId(currentUser.id);
});
it('should return not found response for not existing app config', async () => {
const appAuthClient = {
active: true,
appKey: 'gitlab',
name: 'First auth client',
formattedAuthDefaults: {
clientid: 'sample client ID',
clientSecret: 'sample client secret',
instanceUrl: 'https://gitlab.com',
oAuthRedirectUrl: 'http://localhost:3001/app/gitlab/connection/add',
}
};
await request(app)
.post('/api/v1/admin/apps/gitlab/auth-clients')
.set('Authorization', token)
.send(appAuthClient)
.expect(404);
});
it('should return created response for valid app config', async () => {
await createAppConfig({
key: 'gitlab'
});
const appAuthClient = {
active: true,
appKey: 'gitlab',
name: 'First auth client',
formattedAuthDefaults: {
clientid: 'sample client ID',
clientSecret: 'sample client secret',
instanceUrl: 'https://gitlab.com',
oAuthRedirectUrl: 'http://localhost:3001/app/gitlab/connection/add',
}
};
const response = await request(app)
.post('/api/v1/admin/apps/gitlab/auth-clients')
.set('Authorization', token)
.send(appAuthClient)
.expect(201);
const expectedPayload = createAppAuthClientMock(appAuthClient);
expect(response.body).toMatchObject(expectedPayload);
});
it('should return bad request response for missing required fields', async () => {
await createAppConfig({
key: 'gitlab'
});
const appAuthClient = {
appKey: 'gitlab',
};
await request(app)
.post('/api/v1/admin/apps/gitlab/auth-clients')
.set('Authorization', token)
.send(appAuthClient)
.expect(400);
});
});

View File

@@ -1,4 +1,3 @@
import createAppAuthClient from './mutations/create-app-auth-client.ee.js';
import createAppConfig from './mutations/create-app-config.ee.js';
import createConnection from './mutations/create-connection.js';
import createFlow from './mutations/create-flow.js';
@@ -32,7 +31,6 @@ import deleteStep from './mutations/delete-step.js';
import verifyConnection from './mutations/verify-connection.js';
const mutationResolvers = {
createAppAuthClient,
createAppConfig,
createConnection,
createFlow,

View File

@@ -1,17 +0,0 @@
import AppConfig from '../../models/app-config.js';
const createAppAuthClient = async (_parent, params, context) => {
context.currentUser.can('update', 'App');
const appConfig = await AppConfig.query()
.findById(params.input.appConfigId)
.throwIfNotFound();
const appAuthClient = await appConfig
.$relatedQuery('appAuthClients')
.insert(params.input);
return appAuthClient;
};
export default createAppAuthClient;

View File

@@ -3,7 +3,6 @@ type Query {
}
type Mutation {
createAppConfig(input: CreateAppConfigInput): AppConfig
createAppAuthClient(input: CreateAppAuthClientInput): AppAuthClient
createConnection(input: CreateConnectionInput): Connection
createFlow(input: CreateFlowInput): Flow
createRole(input: CreateRoleInput): Role
@@ -571,13 +570,6 @@ type AppAuthClient {
active: Boolean
}
input CreateAppAuthClientInput {
appConfigId: String
name: String
formattedAuthDefaults: JSONObject
active: Boolean
}
input UpdateAppAuthClientInput {
id: String
name: String

View File

@@ -41,7 +41,9 @@ const renderObject = (response, object, options) => {
},
};
return response.json(computedPayload);
const status = options?.status || 200;
return response.status(status).json(computedPayload);
};
const renderError = (response, errors, status, type) => {

View File

@@ -5,6 +5,7 @@ import { authorizeAdmin } from '../../../../helpers/authorization.js';
import { checkIsEnterprise } from '../../../../helpers/check-is-enterprise.js';
import getAuthClientsAction from '../../../../controllers/api/v1/admin/apps/get-auth-clients.ee.js';
import getAuthClientAction from '../../../../controllers/api/v1/admin/apps/get-auth-client.ee.js';
import createAuthClientAction from '../../../../controllers/api/v1/admin/apps/create-auth-client.ee.js';
const router = Router();
@@ -16,6 +17,14 @@ router.get(
asyncHandler(getAuthClientsAction)
);
router.post(
'/:appKey/auth-clients',
authenticateUser,
authorizeAdmin,
checkIsEnterprise,
asyncHandler(createAuthClientAction)
);
router.get(
'/:appKey/auth-clients/:appAuthClientId',
authenticateUser,