From cec662a9f0081178b745415265b46c2b9cf35725 Mon Sep 17 00:00:00 2001 From: Ofer <12687466+CptSchnitz@users.noreply.github.com> Date: Sun, 20 Oct 2024 10:54:18 +0300 Subject: [PATCH] fix: when schema is not found in post config correct http code is returned (#50) * fix: when schema is not found in post config correct http code is returned * test: removed console log --- src/configs/controllers/configController.ts | 3 ++- src/serverBuilder.ts | 1 - tests/integration/configs/configs.spec.ts | 18 +++++++++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/configs/controllers/configController.ts b/src/configs/controllers/configController.ts index 9374ef3..f56438f 100644 --- a/src/configs/controllers/configController.ts +++ b/src/configs/controllers/configController.ts @@ -15,6 +15,7 @@ import { ConfigVersionMismatchError, SortQueryRepeatError, } from '../models/errors'; +import { SchemaNotFoundError } from '../../schemas/models/errors'; function configMapper(config: Config): components['schemas']['config'] { return { @@ -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; diff --git a/src/serverBuilder.ts b/src/serverBuilder.ts index e250dcf..564f005 100644 --- a/src/serverBuilder.ts +++ b/src/serverBuilder.ts @@ -77,7 +77,6 @@ export class ServerBuilder { private registerPostRoutesMiddleware(): void { const isStaticEnabled = this.config.get('server.staticAssets.enabled'); - console.log('isStaticEnabled', isStaticEnabled); if (isStaticEnabled) { const staticPath = this.config.get('server.staticAssets.folder'); diff --git a/tests/integration/configs/configs.spec.ts b/tests/integration/configs/configs.spec.ts index 6fd63d4..5c61771 100644 --- a/tests/integration/configs/configs.spec.ts +++ b/tests/integration/configs/configs.spec.ts @@ -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'; @@ -28,7 +29,7 @@ async function getSchemaMock(id: string): Promise { case primitiveRefSchema.$id: return Promise.resolve(primitiveRefSchema); default: - throw new Error('Schema not found'); + throw new SchemaNotFoundError('Schema not found'); } } @@ -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',