refactor: rewrite create-app-auth-client mutation as REST endpoint
This commit is contained in:
@@ -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 });
|
||||
};
|
@@ -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);
|
||||
});
|
||||
});
|
@@ -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,
|
||||
|
@@ -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;
|
@@ -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
|
||||
|
@@ -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) => {
|
||||
|
@@ -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,
|
||||
|
@@ -0,0 +1,17 @@
|
||||
const createAppAuthClientMock = (appAuthClient) => {
|
||||
return {
|
||||
data: {
|
||||
name: appAuthClient.name,
|
||||
active: appAuthClient.active,
|
||||
},
|
||||
meta: {
|
||||
count: 1,
|
||||
currentPage: null,
|
||||
isArray: false,
|
||||
totalPages: null,
|
||||
type: 'AppAuthClient',
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export default createAppAuthClientMock;
|
@@ -4,9 +4,9 @@ import { useMutation } from '@apollo/client';
|
||||
|
||||
import { AppPropType } from 'propTypes/propTypes';
|
||||
import { CREATE_APP_CONFIG } from 'graphql/mutations/create-app-config';
|
||||
import { CREATE_APP_AUTH_CLIENT } from 'graphql/mutations/create-app-auth-client';
|
||||
import useAppConfig from 'hooks/useAppConfig.ee';
|
||||
import useFormatMessage from 'hooks/useFormatMessage';
|
||||
import useAdminCreateAppAuthClient from 'hooks/useAdminCreateAppAuthClient.ee';
|
||||
import AdminApplicationAuthClientDialog from 'components/AdminApplicationAuthClientDialog';
|
||||
import useAppAuth from 'hooks/useAppAuth';
|
||||
|
||||
@@ -26,13 +26,11 @@ function AdminApplicationCreateAuthClient(props) {
|
||||
context: { autoSnackbar: false },
|
||||
});
|
||||
|
||||
const [
|
||||
createAppAuthClient,
|
||||
{ loading: loadingCreateAppAuthClient, error: createAppAuthClientError },
|
||||
] = useMutation(CREATE_APP_AUTH_CLIENT, {
|
||||
refetchQueries: ['GetAppAuthClients'],
|
||||
context: { autoSnackbar: false },
|
||||
});
|
||||
const {
|
||||
mutateAsync: createAppAuthClient,
|
||||
isPending: isCreateAppAuithClientPending,
|
||||
error: createAppAuthClientError,
|
||||
} = useAdminCreateAppAuthClient(appKey);
|
||||
|
||||
const submitHandler = async (values) => {
|
||||
let appConfigId = appConfig?.data?.id;
|
||||
@@ -55,14 +53,10 @@ function AdminApplicationCreateAuthClient(props) {
|
||||
const { name, active, ...formattedAuthDefaults } = values;
|
||||
|
||||
await createAppAuthClient({
|
||||
variables: {
|
||||
input: {
|
||||
appConfigId,
|
||||
name,
|
||||
active,
|
||||
formattedAuthDefaults,
|
||||
},
|
||||
},
|
||||
appKey,
|
||||
name,
|
||||
active,
|
||||
formattedAuthDefaults,
|
||||
});
|
||||
|
||||
onClose();
|
||||
@@ -103,7 +97,7 @@ function AdminApplicationCreateAuthClient(props) {
|
||||
loading={isAppConfigLoading}
|
||||
submitHandler={submitHandler}
|
||||
authFields={auth?.data?.fields}
|
||||
submitting={loadingCreateAppConfig || loadingCreateAppAuthClient}
|
||||
submitting={loadingCreateAppConfig || isCreateAppAuithClientPending}
|
||||
defaultValues={defaultValues}
|
||||
/>
|
||||
);
|
||||
|
@@ -16,7 +16,7 @@ function AdminApplicationUpdateAuthClient(props) {
|
||||
const { clientId } = useParams();
|
||||
|
||||
const { data: adminAppAuthClient, isLoading: isAdminAuthClientLoading } =
|
||||
useAdminAppAuthClient(clientId);
|
||||
useAdminAppAuthClient(application.key, clientId);
|
||||
|
||||
const { data: auth } = useAppAuth(application.key);
|
||||
|
||||
|
@@ -1,11 +0,0 @@
|
||||
import { gql } from '@apollo/client';
|
||||
export const CREATE_APP_AUTH_CLIENT = gql`
|
||||
mutation CreateAppAuthClient($input: CreateAppAuthClientInput) {
|
||||
createAppAuthClient(input: $input) {
|
||||
id
|
||||
appConfigId
|
||||
name
|
||||
active
|
||||
}
|
||||
}
|
||||
`;
|
@@ -2,17 +2,17 @@ import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import api from 'helpers/api';
|
||||
|
||||
export default function useAdminAppAuthClient(id) {
|
||||
export default function useAdminAppAuthClient(appKey, id) {
|
||||
const query = useQuery({
|
||||
queryKey: ['admin', 'appAuthClients', id],
|
||||
queryKey: ['admin', 'apps', appKey, 'authClients', id],
|
||||
queryFn: async ({ signal }) => {
|
||||
const { data } = await api.get(`/v1/admin/app-auth-clients/${id}`, {
|
||||
const { data } = await api.get(`/v1/admin/apps/${appKey}/auth-clients/${id}`, {
|
||||
signal,
|
||||
});
|
||||
|
||||
return data;
|
||||
},
|
||||
enabled: !!id,
|
||||
enabled: !!appKey && !!id,
|
||||
});
|
||||
|
||||
return query;
|
||||
|
@@ -1,9 +1,9 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import api from 'helpers/api';
|
||||
|
||||
export default function useAdminAppAuthClient(appKey) {
|
||||
export default function useAdminAppAuthClients(appKey) {
|
||||
const query = useQuery({
|
||||
queryKey: ['admin', 'apps', appKey, 'auth-clients'],
|
||||
queryKey: ['admin', 'apps', appKey, 'authClients'],
|
||||
queryFn: async ({ signal }) => {
|
||||
const { data } = await api.get(`/v1/admin/apps/${appKey}/auth-clients`, {
|
||||
signal,
|
||||
|
21
packages/web/src/hooks/useAdminCreateAppAuthClient.ee.js
Normal file
21
packages/web/src/hooks/useAdminCreateAppAuthClient.ee.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import api from 'helpers/api';
|
||||
|
||||
export default function useAdminCreateAppAuthClient(appKey) {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const query = useMutation({
|
||||
mutationFn: async (payload) => {
|
||||
const { data } = await api.post(`/v1/admin/apps/${appKey}/auth-clients`, payload);
|
||||
|
||||
return data;
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['admin', 'apps', appKey, 'authClients'],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return query;
|
||||
}
|
Reference in New Issue
Block a user