Skip to content

Commit

Permalink
[Security Solution][Exceptions] - Updates enum schema and tests (elas…
Browse files Browse the repository at this point in the history
…tic#76544)

## Summary

Mistmatch caught between the io-ts type and the corresponding typescript enum. Currently, io-ts does not have support for enums - as a workaround I had made a matching typescript enum type. Tests were added to try to ensure the two stayed in sync, but didn't do a straight up comparison of the two.

Updated the tests to now error if the keys do not match.
  • Loading branch information
yctercero committed Sep 3, 2020
1 parent a34b0a2 commit 365436e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 65 deletions.
66 changes: 10 additions & 56 deletions x-pack/plugins/lists/common/schemas/common/schemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
EsDataTypeRangeTerm,
EsDataTypeSingle,
EsDataTypeUnion,
ExceptionListTypeEnum,
OperatorEnum,
Type,
esDataTypeGeoPoint,
esDataTypeGeoPointRange,
Expand All @@ -25,60 +27,10 @@ import {
esDataTypeUnion,
exceptionListType,
operator,
operator_type as operatorType,
type,
} from './schemas';

describe('Common schemas', () => {
describe('operatorType', () => {
test('it should validate for "match"', () => {
const payload = 'match';
const decoded = operatorType.decode(payload);
const message = pipe(decoded, foldLeftRight);

expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it should validate for "match_any"', () => {
const payload = 'match_any';
const decoded = operatorType.decode(payload);
const message = pipe(decoded, foldLeftRight);

expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it should validate for "list"', () => {
const payload = 'list';
const decoded = operatorType.decode(payload);
const message = pipe(decoded, foldLeftRight);

expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it should validate for "exists"', () => {
const payload = 'exists';
const decoded = operatorType.decode(payload);
const message = pipe(decoded, foldLeftRight);

expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it should contain 4 keys', () => {
// Might seem like a weird test, but its meant to
// ensure that if operatorType is updated, you
// also update the OperatorTypeEnum, a workaround
// for io-ts not yet supporting enums
// https://github.com/gcanti/io-ts/issues/67
const keys = Object.keys(operatorType.keys);

expect(keys.length).toEqual(4);
});
});

describe('operator', () => {
test('it should validate for "included"', () => {
const payload = 'included';
Expand All @@ -98,15 +50,16 @@ describe('Common schemas', () => {
expect(message.schema).toEqual(payload);
});

test('it should contain 2 keys', () => {
test('it should contain same amount of keys as enum', () => {
// Might seem like a weird test, but its meant to
// ensure that if operator is updated, you
// also update the operatorEnum, a workaround
// for io-ts not yet supporting enums
// https://github.com/gcanti/io-ts/issues/67
const keys = Object.keys(operator.keys);
const keys = Object.keys(operator.keys).sort().join(',').toLowerCase();
const enumKeys = Object.keys(OperatorEnum).sort().join(',').toLowerCase();

expect(keys.length).toEqual(2);
expect(keys).toEqual(enumKeys);
});
});

Expand All @@ -129,15 +82,16 @@ describe('Common schemas', () => {
expect(message.schema).toEqual(payload);
});

test('it should contain 2 keys', () => {
test('it should contain same amount of keys as enum', () => {
// Might seem like a weird test, but its meant to
// ensure that if exceptionListType is updated, you
// also update the ExceptionListTypeEnum, a workaround
// for io-ts not yet supporting enums
// https://github.com/gcanti/io-ts/issues/67
const keys = Object.keys(exceptionListType.keys);
const keys = Object.keys(exceptionListType.keys).sort().join(',').toLowerCase();
const enumKeys = Object.keys(ExceptionListTypeEnum).sort().join(',').toLowerCase();

expect(keys.length).toEqual(2);
expect(keys).toEqual(enumKeys);
});
});

Expand Down
7 changes: 0 additions & 7 deletions x-pack/plugins/lists/common/schemas/common/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,6 @@ export enum OperatorEnum {
EXCLUDED = 'excluded',
}

export const operator_type = t.keyof({
exists: null,
list: null,
match: null,
match_any: null,
});
export type OperatorType = t.TypeOf<typeof operator_type>;
export enum OperatorTypeEnum {
NESTED = 'nested',
MATCH = 'match',
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/lists/common/shared_exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export {
NamespaceType,
Operator,
OperatorEnum,
OperatorType,
OperatorTypeEnum,
ExceptionListTypeEnum,
comment,
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/security_solution/common/shared_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export {
NamespaceType,
Operator,
OperatorEnum,
OperatorType,
OperatorTypeEnum,
ExceptionListTypeEnum,
exceptionListItemSchema,
Expand Down

0 comments on commit 365436e

Please sign in to comment.