From b6b793768a271e2e33ea55a5c0e02127e79d6641 Mon Sep 17 00:00:00 2001 From: CptSchnitz <12687466+CptSchnitz@users.noreply.github.com> Date: Tue, 15 Oct 2024 14:43:23 +0300 Subject: [PATCH 1/2] fix: when schema is not found in post config correct http code is returned --- src/configs/controllers/configController.ts | 3 ++- src/serverBuilder.ts | 1 - tests/integration/configs/configs.spec.ts | 20 +++++++++++++++++++- 3 files changed, 21 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..4b06f56 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,23 @@ 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', + }, + }); + + console.log(response.body); + + 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', From 3e1c588087949d37f4af0e6b318ee0c7f17605b5 Mon Sep 17 00:00:00 2001 From: CptSchnitz <12687466+CptSchnitz@users.noreply.github.com> Date: Wed, 16 Oct 2024 08:23:31 +0300 Subject: [PATCH 2/2] test: removed console log --- tests/integration/configs/configs.spec.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/integration/configs/configs.spec.ts b/tests/integration/configs/configs.spec.ts index 4b06f56..5c61771 100644 --- a/tests/integration/configs/configs.spec.ts +++ b/tests/integration/configs/configs.spec.ts @@ -548,8 +548,6 @@ describe('config', function () { }, }); - console.log(response.body); - expect(response.status).toBe(httpStatusCodes.BAD_REQUEST); expect(response).toSatisfyApiSpec(); });