feat: write PATCH /v1/admin/apps/:appKey/auth-clients/:appAuthClientId

This commit is contained in:
Ali BARIN
2024-08-27 16:02:16 +00:00
parent 09b255f99e
commit b2bda8479e
4 changed files with 131 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
import { renderObject } from '../../../../../helpers/renderer.js';
import AppAuthClient from '../../../../../models/app-auth-client.js';
export default async (request, response) => {
const id = request.params.appAuthClientId;
const appAuthClient = await AppAuthClient.query()
.findById(id)
.throwIfNotFound();
await appAuthClient.$query().patchAndFetch(appAuthClientParams(request));
renderObject(response, appAuthClient);
};
const appAuthClientParams = (request) => {
const { active, name, formattedAuthDefaults } = request.body;
return {
active,
name,
formattedAuthDefaults,
};
};

View File

@@ -0,0 +1,80 @@
import { vi, describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import Crypto from 'crypto';
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 updateAppAuthClientMock from '../../../../../../test/mocks/rest/api/v1/admin/apps/update-auth-client.js';
import { createAppConfig } from '../../../../../../test/factories/app-config.js';
import { createAppAuthClient } from '../../../../../../test/factories/app-auth-client.js';
import * as license from '../../../../../helpers/license.ee.js';
describe('PATCH /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);
await createAppConfig({
key: 'gitlab',
});
});
it('should return updated entity for valid app auth client', async () => {
const appAuthClient = {
active: true,
appKey: 'gitlab',
formattedAuthDefaults: {
clientid: 'sample client ID',
clientSecret: 'sample client secret',
instanceUrl: 'https://gitlab.com',
oAuthRedirectUrl: 'http://localhost:3001/app/gitlab/connection/add',
},
};
const existingAppAuthClient = await createAppAuthClient({
appKey: 'gitlab',
name: 'First auth client',
});
const response = await request(app)
.patch(
`/api/v1/admin/apps/gitlab/auth-clients/${existingAppAuthClient.id}`
)
.set('Authorization', token)
.send(appAuthClient)
.expect(200);
const expectedPayload = updateAppAuthClientMock({
...existingAppAuthClient,
...appAuthClient,
});
expect(response.body).toMatchObject(expectedPayload);
});
it('should return not found response for not existing app auth client', async () => {
const notExistingAppAuthClientId = Crypto.randomUUID();
await request(app)
.patch(
`/api/v1/admin/apps/gitlab/auth-clients/${notExistingAppAuthClientId}`
)
.set('Authorization', token)
.expect(404);
});
it('should return bad request response for invalid UUID', async () => {
await request(app)
.patch('/api/v1/admin/apps/gitlab/auth-clients/invalidAuthClientUUID')
.set('Authorization', token)
.expect(400);
});
});

View File

@@ -6,6 +6,7 @@ import createConfigAction from '../../../../controllers/api/v1/admin/apps/create
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';
import updateAuthClientAction from '../../../../controllers/api/v1/admin/apps/update-auth-client.ee.js';
const router = Router();
@@ -42,4 +43,12 @@ router.get(
getAuthClientAction
);
router.patch(
'/:appKey/auth-clients/:appAuthClientId',
authenticateUser,
authorizeAdmin,
checkIsEnterprise,
asyncHandler(updateAuthClientAction)
);
export default router;

View File

@@ -0,0 +1,18 @@
const updateAppAuthClientMock = (appAuthClient) => {
return {
data: {
id: appAuthClient.id,
name: appAuthClient.name,
active: appAuthClient.active,
},
meta: {
count: 1,
currentPage: null,
isArray: false,
totalPages: null,
type: 'AppAuthClient',
},
};
};
export default updateAppAuthClientMock;