refactor(create-config): move unique violation error handling to error-handler

This commit is contained in:
Ali BARIN
2024-08-27 16:30:05 +00:00
parent 48b2b006c0
commit 0800642a2a
5 changed files with 55 additions and 30 deletions

View File

@@ -23,7 +23,7 @@ describe('POST /api/v1/admin/apps/:appKey/auth-clients', () => {
it('should return created response for valid app config', async () => {
await createAppConfig({
key: 'gitlab'
key: 'gitlab',
});
const appAuthClient = {
@@ -35,7 +35,7 @@ describe('POST /api/v1/admin/apps/:appKey/auth-clients', () => {
clientSecret: 'sample client secret',
instanceUrl: 'https://gitlab.com',
oAuthRedirectUrl: 'http://localhost:3001/app/gitlab/connection/add',
}
},
};
const response = await request(app)
@@ -58,7 +58,7 @@ describe('POST /api/v1/admin/apps/:appKey/auth-clients', () => {
clientSecret: 'sample client secret',
instanceUrl: 'https://gitlab.com',
oAuthRedirectUrl: 'http://localhost:3001/app/gitlab/connection/add',
}
},
};
await request(app)
@@ -70,7 +70,7 @@ describe('POST /api/v1/admin/apps/:appKey/auth-clients', () => {
it('should return bad request response for missing required fields', async () => {
await createAppConfig({
key: 'gitlab'
key: 'gitlab',
});
const appAuthClient = {
@@ -86,7 +86,9 @@ describe('POST /api/v1/admin/apps/:appKey/auth-clients', () => {
expect(response.body.meta.type).toEqual('ModelValidation');
expect(response.body.errors).toMatchObject({
name: ["must have required property 'name'"],
formattedAuthDefaults: ["must have required property 'formattedAuthDefaults'"]
formattedAuthDefaults: [
"must have required property 'formattedAuthDefaults'",
],
});
});
});

View File

@@ -1,19 +1,10 @@
import { renderError, renderObject } from '../../../../../helpers/renderer.js';
import { renderObject } from '../../../../../helpers/renderer.js';
import AppConfig from '../../../../../models/app-config.js';
export default async (request, response) => {
const appKey = request.params.appKey;
const appConfig = await AppConfig.query()
.findOne({ key: appKey });
if (appConfig) {
return renderError(response, [{ general: ['App config already exists.'] }], 409);
}
const createdAppConfig = await AppConfig
.query()
.insertAndFetch(appConfigParams(request));
const createdAppConfig = await AppConfig.query().insertAndFetch(
appConfigParams(request)
);
renderObject(response, createdAppConfig, { status: 201 });
};

View File

@@ -23,7 +23,6 @@ describe('POST /api/v1/admin/apps/:appKey/config', () => {
it('should return created app config', async () => {
const appConfig = {
key: 'gitlab',
allowCustomConnection: true,
shared: true,
disabled: false,
@@ -35,11 +34,14 @@ describe('POST /api/v1/admin/apps/:appKey/config', () => {
.send(appConfig)
.expect(201);
const expectedPayload = createAppConfigMock(appConfig);
const expectedPayload = createAppConfigMock({
...appConfig,
key: 'gitlab',
});
expect(response.body).toMatchObject(expectedPayload);
});
it('should return HTTP 409 for already existing app config', async () => {
it('should return HTTP 422 for already existing app config', async () => {
const appConfig = {
key: 'gitlab',
allowCustomConnection: true,
@@ -49,10 +51,17 @@ describe('POST /api/v1/admin/apps/:appKey/config', () => {
await createAppConfig(appConfig);
await request(app)
const response = await request(app)
.post('/api/v1/admin/apps/gitlab/config')
.set('Authorization', token)
.send(appConfig)
.expect(409);
.send({
disabled: false,
})
.expect(422);
expect(response.body.meta.type).toEqual('UniqueViolationError');
expect(response.body.errors).toMatchObject({
key: ["'key' must be unique."],
});
});
});