Skip to content

Commit

Permalink
Spaces - allow for hex color codes that include uppercase characters (e…
Browse files Browse the repository at this point in the history
…lastic#43470)

* allow for mixed case hex codes

* remove stray newline

* lint and style improvements
  • Loading branch information
legrego committed Aug 16, 2019
1 parent 00e4631 commit 633b58e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
74 changes: 74 additions & 0 deletions x-pack/legacy/plugins/spaces/server/lib/space_schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]]`
);
});
});
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/spaces/server/lib/space_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([]),
Expand Down
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/spaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export default function({ loadTestFile }: FtrProviderContext) {
this.tags('ciGroup6');

loadTestFile(require.resolve('./saved_objects'));
loadTestFile(require.resolve('./space_attributes'));
});
}
31 changes: 31 additions & 0 deletions x-pack/test/api_integration/apis/spaces/space_attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 { 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, {
id: 'api-test-space',
name: 'api test space',
disabledFeatures: [],
color: '#aaBB78',
});
});
});
}

0 comments on commit 633b58e

Please sign in to comment.