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

feat: added pattern validation for config name #34

Merged
merged 2 commits into from
Jul 21, 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
1 change: 1 addition & 0 deletions openapi3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ components:
type: string
configName:
type: string
pattern: '^([a-z0-9]+(-[a-z0-9]+)*)$'
maxLength: 50
schemaId:
type: string
Expand Down
40 changes: 23 additions & 17 deletions tests/integration/configs/configs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand All @@ -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,
},
},
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down
12 changes: 6 additions & 6 deletions tests/integration/configs/helpers/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -145,7 +145,7 @@ export const configsMockData: NewConfig[] = [
},
},
{
configName: 'configRef2',
configName: 'config-ref-2',
createdBy: 'user5',
schemaId: 'https://mapcolonies.com/schemaWithRef/v1',
version: 1,
Expand All @@ -156,7 +156,7 @@ export const configsMockData: NewConfig[] = [
},
},
{
configName: 'configRef3',
configName: 'config-ref-3',
createdBy: 'user5',
schemaId: 'https://mapcolonies.com/schemaWithRef/v1',
version: 1,
Expand All @@ -167,7 +167,7 @@ export const configsMockData: NewConfig[] = [
},
},
{
configName: 'primitiveConfig',
configName: 'primitive-config',
createdBy: 'user5',
schemaId: 'https://mapcolonies.com/primitiveSchema/v1',
version: 1,
Expand All @@ -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,
Expand Down
Loading