From 59060215af476a5a2ae9d7cacb0af2412e9c0fb8 Mon Sep 17 00:00:00 2001 From: Larry Gregory Date: Fri, 16 Aug 2019 13:05:25 -0400 Subject: [PATCH 1/3] allow for mixed case hex codes --- .../spaces/server/lib/space_schema.test.ts | 74 +++++++++++++++++++ .../plugins/spaces/server/lib/space_schema.ts | 2 +- .../test/api_integration/apis/spaces/index.ts | 1 + .../apis/spaces/space_attributes.ts | 36 +++++++++ 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 x-pack/test/api_integration/apis/spaces/space_attributes.ts diff --git a/x-pack/legacy/plugins/spaces/server/lib/space_schema.test.ts b/x-pack/legacy/plugins/spaces/server/lib/space_schema.test.ts index 6ca37affb84871..719f9a0908a76f 100644 --- a/x-pack/legacy/plugins/spaces/server/lib/space_schema.test.ts +++ b/x-pack/legacy/plugins/spaces/server/lib/space_schema.test.ts @@ -62,3 +62,77 @@ describe('#id', () => { } ); }); + +describe('#color', () => { + test('is optional', () => { + const result = spaceSchema.validate({ + ...defaultProperties, + color: undefined, + }); + expect(result.error).toBeNull(); + }); + + test(`doesn't allow an empty string`, () => { + const result = spaceSchema.validate({ + ...defaultProperties, + color: '', + }); + expect(result.error).toMatchInlineSnapshot( + `[ValidationError: child "color" fails because ["color" is not allowed to be empty]]` + ); + }); + + test(`allows lower case hex color code`, () => { + const result = spaceSchema.validate({ + ...defaultProperties, + color: '#aabbcc', + }); + expect(result.error).toBeNull(); + }); + + test(`allows upper case hex color code`, () => { + const result = spaceSchema.validate({ + ...defaultProperties, + color: '#AABBCC', + }); + expect(result.error).toBeNull(); + }); + + test(`allows numeric hex color code`, () => { + const result = spaceSchema.validate({ + ...defaultProperties, + color: '#123456', + }); + expect(result.error).toBeNull(); + }); + + test(`must start with a hash`, () => { + const result = spaceSchema.validate({ + ...defaultProperties, + color: '123456', + }); + expect(result.error).toMatchInlineSnapshot( + `[ValidationError: child "color" fails because ["color" with value "123456" fails to match the 6 digit hex color, starting with a # pattern]]` + ); + }); + + test(`cannot exceed 6 digits following the hash`, () => { + const result = spaceSchema.validate({ + ...defaultProperties, + color: '1234567', + }); + expect(result.error).toMatchInlineSnapshot( + `[ValidationError: child "color" fails because ["color" with value "1234567" fails to match the 6 digit hex color, starting with a # pattern]]` + ); + }); + + test(`cannot be fewer than 6 digits following the hash`, () => { + const result = spaceSchema.validate({ + ...defaultProperties, + color: '12345', + }); + expect(result.error).toMatchInlineSnapshot( + `[ValidationError: child "color" fails because ["color" with value "12345" fails to match the 6 digit hex color, starting with a # pattern]]` + ); + }); +}); diff --git a/x-pack/legacy/plugins/spaces/server/lib/space_schema.ts b/x-pack/legacy/plugins/spaces/server/lib/space_schema.ts index c2dcd3b2b9ac06..824c5f27d5fdab 100644 --- a/x-pack/legacy/plugins/spaces/server/lib/space_schema.ts +++ b/x-pack/legacy/plugins/spaces/server/lib/space_schema.ts @@ -12,7 +12,7 @@ export const spaceSchema = Joi.object({ name: Joi.string().required(), description: Joi.string().allow(''), initials: Joi.string().max(MAX_SPACE_INITIALS), - color: Joi.string().regex(/^#[a-z0-9]{6}$/, `6 digit hex color, starting with a #`), + color: Joi.string().regex(/^#[a-zA-Z0-9]{6}$/, `6 digit hex color, starting with a #`), disabledFeatures: Joi.array() .items(Joi.string()) .default([]), diff --git a/x-pack/test/api_integration/apis/spaces/index.ts b/x-pack/test/api_integration/apis/spaces/index.ts index 5aa35a10159f01..adcf70d032e0f1 100644 --- a/x-pack/test/api_integration/apis/spaces/index.ts +++ b/x-pack/test/api_integration/apis/spaces/index.ts @@ -10,5 +10,6 @@ export default function({ loadTestFile }: FtrProviderContext) { this.tags('ciGroup6'); loadTestFile(require.resolve('./saved_objects')); + loadTestFile(require.resolve('./space_attributes')); }); } diff --git a/x-pack/test/api_integration/apis/spaces/space_attributes.ts b/x-pack/test/api_integration/apis/spaces/space_attributes.ts new file mode 100644 index 00000000000000..ded452f307657f --- /dev/null +++ b/x-pack/test/api_integration/apis/spaces/space_attributes.ts @@ -0,0 +1,36 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import expect from '@kbn/expect/expect.js'; +import { FtrProviderContext } from '../../ftr_provider_context'; + +export default function ({ getService }: FtrProviderContext) { + const supertest = getService('supertest'); + + describe('space attributes', () => { + + it('should allow a space to be created with a mixed-case hex color code', async () => { + await supertest + .post('/api/spaces/space') + .set('kbn-xsrf', 'xxx') + .send({ + id: 'api-test-space', + name: 'api test space', + disabledFeatures: [], + color: '#aaBB78' + }) + .expect(200) + .then((response: Record) => { + expect(response.body).to.eql({ + id: 'api-test-space', + name: 'api test space', + disabledFeatures: [], + color: '#aaBB78' + }); + }); + }); + }); +} From 8764fd5b37edbe7a480373bec60f9cadefa82b69 Mon Sep 17 00:00:00 2001 From: Larry Gregory Date: Fri, 16 Aug 2019 13:10:23 -0400 Subject: [PATCH 2/3] remove stray newline --- x-pack/test/api_integration/apis/spaces/space_attributes.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/x-pack/test/api_integration/apis/spaces/space_attributes.ts b/x-pack/test/api_integration/apis/spaces/space_attributes.ts index ded452f307657f..72aa7f5f598c28 100644 --- a/x-pack/test/api_integration/apis/spaces/space_attributes.ts +++ b/x-pack/test/api_integration/apis/spaces/space_attributes.ts @@ -11,7 +11,6 @@ export default function ({ getService }: FtrProviderContext) { const supertest = getService('supertest'); describe('space attributes', () => { - it('should allow a space to be created with a mixed-case hex color code', async () => { await supertest .post('/api/spaces/space') From a9e6c5290debc968932eec907e759ff0682a5b2a Mon Sep 17 00:00:00 2001 From: Larry Gregory Date: Fri, 16 Aug 2019 13:35:42 -0400 Subject: [PATCH 3/3] lint and style improvements --- .../apis/spaces/space_attributes.ts | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/x-pack/test/api_integration/apis/spaces/space_attributes.ts b/x-pack/test/api_integration/apis/spaces/space_attributes.ts index 72aa7f5f598c28..1f9c0315f0e145 100644 --- a/x-pack/test/api_integration/apis/spaces/space_attributes.ts +++ b/x-pack/test/api_integration/apis/spaces/space_attributes.ts @@ -4,10 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import expect from '@kbn/expect/expect.js'; import { FtrProviderContext } from '../../ftr_provider_context'; -export default function ({ getService }: FtrProviderContext) { +export default function({ getService }: FtrProviderContext) { const supertest = getService('supertest'); describe('space attributes', () => { @@ -18,18 +17,15 @@ export default function ({ getService }: FtrProviderContext) { .send({ id: 'api-test-space', name: 'api test space', - disabledFeatures: [], - color: '#aaBB78' + disabledFeatures: [], + color: '#aaBB78', }) - .expect(200) - .then((response: Record) => { - expect(response.body).to.eql({ - id: 'api-test-space', - name: 'api test space', - disabledFeatures: [], - color: '#aaBB78' - }); + .expect(200, { + id: 'api-test-space', + name: 'api test space', + disabledFeatures: [], + color: '#aaBB78', }); - }); + }); }); }