Skip to content

Commit

Permalink
fix: when schema is not found in post config correct http code is ret…
Browse files Browse the repository at this point in the history
…urned (#50)

* fix: when schema is not found in post config correct http code is returned

* test: removed console log
  • Loading branch information
CptSchnitz authored Oct 20, 2024
1 parent 4be6da0 commit cec662a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/configs/controllers/configController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ConfigVersionMismatchError,
SortQueryRepeatError,
} from '../models/errors';
import { SchemaNotFoundError } from '../../schemas/models/errors';

function configMapper(config: Config): components['schemas']['config'] {
return {
Expand Down Expand Up @@ -113,7 +114,7 @@ export class ConfigController {
await this.manager.createConfig(req.body);
return res.status(httpStatus.CREATED).json();
} catch (error) {
if (error instanceof ConfigValidationError || error instanceof ConfigNotFoundError) {
if (error instanceof ConfigValidationError || error instanceof ConfigNotFoundError || error instanceof SchemaNotFoundError) {
(error as HttpError).status = httpStatus.BAD_REQUEST;
} else if (error instanceof ConfigVersionMismatchError || error instanceof ConfigSchemaMismatchError) {
(error as HttpError).status = httpStatus.CONFLICT;
Expand Down
1 change: 0 additions & 1 deletion src/serverBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export class ServerBuilder {

private registerPostRoutesMiddleware(): void {
const isStaticEnabled = this.config.get<boolean>('server.staticAssets.enabled');
console.log('isStaticEnabled', isStaticEnabled);

if (isStaticEnabled) {
const staticPath = this.config.get<string>('server.staticAssets.folder');
Expand Down
18 changes: 17 additions & 1 deletion tests/integration/configs/configs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Drizzle } from '../../../src/db/createConnection';
import { getApp } from '../../../src/app';
import { SERVICES } from '../../../src/common/constants';
import { Config, configs, configsRefs } from '../../../src/configs/models/config';
import { SchemaNotFoundError } from '../../../src/schemas/models/errors';
import { ConfigRequestSender } from './helpers/requestSender';
import { configsMockData, refs, schemaWithRef, simpleSchema, primitiveRefSchema, primitiveSchema } from './helpers/data';

Expand All @@ -28,7 +29,7 @@ async function getSchemaMock(id: string): Promise<JSONSchema> {
case primitiveRefSchema.$id:
return Promise.resolve(primitiveRefSchema);
default:
throw new Error('Schema not found');
throw new SchemaNotFoundError('Schema not found');
}
}

Expand Down Expand Up @@ -536,6 +537,21 @@ describe('config', function () {
expect(response).toSatisfyApiSpec();
});

it('should return 400 if the schemaId of the config does not exist', async function () {
const response = await requestSender.postConfig({
configName: 'config-not-exists',
schemaId: 'https://mapcolonies.com/not-exists/v1',
version: 1,
config: {
manager: 'null',
role: 'unknown',
},
});

expect(response.status).toBe(httpStatusCodes.BAD_REQUEST);
expect(response).toSatisfyApiSpec();
});

it('should return 409 status code when trying to post a new version of a config that does not exists', async function () {
const response = await requestSender.postConfig({
configName: 'not-exists',
Expand Down

0 comments on commit cec662a

Please sign in to comment.