From d979f089b128a1174266b93d5c4767f4c170081e Mon Sep 17 00:00:00 2001 From: CptSchnitz <12687466+CptSchnitz@users.noreply.github.com> Date: Tue, 16 Jul 2024 10:17:34 +0300 Subject: [PATCH 1/2] feat: added pattern validation for config name --- openapi3.yaml | 1 + tests/integration/configs/configs.spec.ts | 40 +++++++++++++---------- tests/integration/configs/helpers/data.ts | 12 +++---- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/openapi3.yaml b/openapi3.yaml index 83a1bf1..1e92c16 100644 --- a/openapi3.yaml +++ b/openapi3.yaml @@ -290,6 +290,7 @@ components: type: string configName: type: string + pattern: '^[a-z0-9\-]+$' maxLength: 50 schemaId: type: string diff --git a/tests/integration/configs/configs.spec.ts b/tests/integration/configs/configs.spec.ts index 162120e..c1f53d2 100644 --- a/tests/integration/configs/configs.spec.ts +++ b/tests/integration/configs/configs.spec.ts @@ -127,7 +127,7 @@ describe('config', function () { it('should return 200 status code and empty array if no results have returned', async function () { // eslint-disable-next-line @typescript-eslint/naming-convention - const response = await requestSender.getConfigs({ config_name: 'not_exists' }); + const response = await requestSender.getConfigs({ config_name: 'not-exists' }); expect(response.status).toBe(httpStatusCodes.OK); expect(response).toSatisfyApiSpec(); @@ -197,7 +197,7 @@ describe('config', function () { }); it('should return 200 status code and the dereferenced config', async function () { - const response = await requestSender.getConfigByName('configRef2', { shouldDereference: true }); + const response = await requestSender.getConfigByName('config-ref-2', { shouldDereference: true }); expect(response.status).toBe(httpStatusCodes.OK); expect(response).toSatisfyApiSpec(); @@ -212,15 +212,21 @@ describe('config', function () { }); describe('Bad Path', function () { + it('should return 400 status code when using invalid config name', async function () { + const response = await requestSender.getConfigByName('Invalid_name'); + + expect(response.status).toBe(httpStatusCodes.BAD_REQUEST); + expect(response).toSatisfyApiSpec(); + }); it('should return 404 status code when the config not exists', async function () { - const response = await requestSender.getConfigByName('not_exists'); + const response = await requestSender.getConfigByName('not-exists'); expect(response.status).toBe(httpStatusCodes.NOT_FOUND); expect(response).toSatisfyApiSpec(); }); it('should return 404 status code when the config not exists in a dereferenced request', async function () { - const response = await requestSender.getConfigByName('not_exists', { shouldDereference: true }); + const response = await requestSender.getConfigByName('not-exists', { shouldDereference: true }); expect(response.status).toBe(httpStatusCodes.NOT_FOUND); expect(response).toSatisfyApiSpec(); @@ -261,7 +267,7 @@ describe('config', function () { }); it('should return 200 status code and the dereferenced config', async function () { - const response = await requestSender.getConfigByVersion('configRef2', 1, { shouldDereference: true }); + const response = await requestSender.getConfigByVersion('config-ref-2', 1, { shouldDereference: true }); expect(response.status).toBe(httpStatusCodes.OK); expect(response).toSatisfyApiSpec(); @@ -275,7 +281,7 @@ describe('config', function () { }); it('should return 200 status code and the dereferenced config without any refs inside', async function () { - const response = await requestSender.getConfigByVersion('configRef3', 1, { shouldDereference: true }); + const response = await requestSender.getConfigByVersion('config-ref-3', 1, { shouldDereference: true }); expect(response.status).toBe(httpStatusCodes.OK); expect(response).toSatisfyApiSpec(); @@ -317,7 +323,7 @@ describe('config', function () { describe('Happy Path', function () { it('should return 201 and create the config', async function () { const response = await requestSender.postConfig({ - configName: 'newConfig1', + configName: 'new-config1', schemaId: 'https://mapcolonies.com/simpleSchema/v1', version: 1, config: { @@ -347,7 +353,7 @@ describe('config', function () { it('should return 201 and create the config with refs', async function () { const response = await requestSender.postConfig({ - configName: 'configWithRef', + configName: 'config-with-ref', schemaId: 'https://mapcolonies.com/schemaWithRef/v1', version: 1, config: { @@ -367,13 +373,13 @@ describe('config', function () { it('should return 201 and create the config with refs with primitives', async function () { const response = await requestSender.postConfig({ - configName: 'configWithPrimitiveRef', + configName: 'config-with-primitive-ref', schemaId: 'https://mapcolonies.com/primitiveRefSchema/v1', version: 1, config: { primitive: { $ref: { - configName: 'primitiveConfig', + configName: 'primitive-config', version: 1, }, }, @@ -388,7 +394,7 @@ describe('config', function () { describe('Bad Path', function () { it('should return 400 status code when using invalid version', async function () { const response = await requestSender.postConfig({ - configName: 'newConfig2', + configName: 'new-config2', schemaId: 'https://mapcolonies.com/simpleSchema/v1', version: 'invalid' as unknown as number, config: { @@ -403,7 +409,7 @@ describe('config', function () { it('should return 400 status code when using invalid schema id', async function () { const response = await requestSender.postConfig({ - configName: 'newConfig2', + configName: 'new-config2', schemaId: 'invalid', version: 1, config: { @@ -418,7 +424,7 @@ describe('config', function () { it('should return 400 status code when using invalid config', async function () { const response = await requestSender.postConfig({ - configName: 'newConfig2', + configName: 'new-config2', schemaId: 'https://mapcolonies.com/simpleSchema/v1', version: 1, config: { @@ -433,7 +439,7 @@ describe('config', function () { it('should return 400 if a ref is does not exist in the database', async function () { const response = await requestSender.postConfig({ - configName: 'configWithRef', + configName: 'config-with-ref', schemaId: 'https://mapcolonies.com/schemaWithRef/v1', version: 1, config: { @@ -453,7 +459,7 @@ describe('config', function () { it('should return 400 if a ref is not valid', async function () { const response = await requestSender.postConfig({ - configName: 'configWithRef', + configName: 'config-with-ref', schemaId: 'https://mapcolonies.com/schemaWithRef/v1', version: 1, config: { @@ -473,7 +479,7 @@ describe('config', function () { 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', + configName: 'not-exists', schemaId: 'https://mapcolonies.com/simpleSchema/v1', version: 2, config: { @@ -508,7 +514,7 @@ describe('config', function () { jest.spyOn(configRepo, 'createConfig').mockRejectedValueOnce(new Error('Database is down')); const response = await requestSender.postConfig({ - configName: 'newConfig3', + configName: 'new-config3', schemaId: 'https://mapcolonies.com/simpleSchema/v1', version: 1, config: { diff --git a/tests/integration/configs/helpers/data.ts b/tests/integration/configs/helpers/data.ts index 58e82d2..d24b2cb 100644 --- a/tests/integration/configs/helpers/data.ts +++ b/tests/integration/configs/helpers/data.ts @@ -133,7 +133,7 @@ export const configsMockData: NewConfig[] = [ createdBy: 'user3', }, { - configName: 'configRef1', + configName: 'config-ref-1', createdBy: 'user4', schemaId: 'https://mapcolonies.com/schemaWithRef/v1', version: 1, @@ -145,7 +145,7 @@ export const configsMockData: NewConfig[] = [ }, }, { - configName: 'configRef2', + configName: 'config-ref-2', createdBy: 'user5', schemaId: 'https://mapcolonies.com/schemaWithRef/v1', version: 1, @@ -156,7 +156,7 @@ export const configsMockData: NewConfig[] = [ }, }, { - configName: 'configRef3', + configName: 'config-ref-3', createdBy: 'user5', schemaId: 'https://mapcolonies.com/schemaWithRef/v1', version: 1, @@ -167,7 +167,7 @@ export const configsMockData: NewConfig[] = [ }, }, { - configName: 'primitiveConfig', + configName: 'primitive-config', createdBy: 'user5', schemaId: 'https://mapcolonies.com/primitiveSchema/v1', version: 1, @@ -177,13 +177,13 @@ export const configsMockData: NewConfig[] = [ export const refs: ConfigRef[] = [ { - configName: 'configRef2', + configName: 'config-ref-2', version: 1, refConfigName: 'config3', refVersion: 1, }, { - configName: 'configRef3', + configName: 'config-ref-3', version: 1, refConfigName: 'config3', refVersion: null, From 1f4bdcfa5c360c63fafe3e16683450f8577dbbc6 Mon Sep 17 00:00:00 2001 From: CptSchnitz <12687466+CptSchnitz@users.noreply.github.com> Date: Sun, 21 Jul 2024 09:31:13 +0300 Subject: [PATCH 2/2] fix: changed configname pattern so sequential dashes are prohibited --- openapi3.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi3.yaml b/openapi3.yaml index 1e92c16..3d1d4b0 100644 --- a/openapi3.yaml +++ b/openapi3.yaml @@ -290,7 +290,7 @@ components: type: string configName: type: string - pattern: '^[a-z0-9\-]+$' + pattern: '^([a-z0-9]+(-[a-z0-9]+)*)$' maxLength: 50 schemaId: type: string