Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading