[Security Solution][Exceptions] - Updates enum schema and tests (#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.
This commit is contained in:
Yara Tercero 2020-09-02 20:07:40 -04:00 committed by GitHub
parent ee2ceef9bb
commit aab8d3c7dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 65 deletions

View file

@ -16,6 +16,8 @@ import {
EsDataTypeRangeTerm,
EsDataTypeSingle,
EsDataTypeUnion,
ExceptionListTypeEnum,
OperatorEnum,
Type,
esDataTypeGeoPoint,
esDataTypeGeoPointRange,
@ -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';
@ -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);
});
});
@ -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);
});
});

View file

@ -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',

View file

@ -25,7 +25,6 @@ export {
NamespaceType,
Operator,
OperatorEnum,
OperatorType,
OperatorTypeEnum,
ExceptionListTypeEnum,
comment,

View file

@ -25,7 +25,6 @@ export {
NamespaceType,
Operator,
OperatorEnum,
OperatorType,
OperatorTypeEnum,
ExceptionListTypeEnum,
exceptionListItemSchema,