|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { left } from 'fp-ts/lib/Either'; |
| 8 | +import { pipe } from 'fp-ts/lib/pipeable'; |
| 9 | + |
| 10 | +import { exactCheck, foldLeftRight, getPaths } from '../../siem_common_deps'; |
| 11 | + |
| 12 | +import { getExceptionListSchemaMock } from './exception_list_schema.mock'; |
| 13 | +import { CreateEndpointListSchema, createEndpointListSchema } from './create_endpoint_list_schema'; |
| 14 | + |
| 15 | +describe('create_endpoint_list_schema', () => { |
| 16 | + test('it should validate a typical endpoint list response', () => { |
| 17 | + const payload = getExceptionListSchemaMock(); |
| 18 | + const decoded = createEndpointListSchema.decode(payload); |
| 19 | + const checked = exactCheck(payload, decoded); |
| 20 | + const message = pipe(checked, foldLeftRight); |
| 21 | + |
| 22 | + expect(getPaths(left(message.errors))).toEqual([]); |
| 23 | + expect(message.schema).toEqual(payload); |
| 24 | + }); |
| 25 | + |
| 26 | + test('it should accept an empty object when an endpoint list already exists', () => { |
| 27 | + const payload = {}; |
| 28 | + const decoded = createEndpointListSchema.decode(payload); |
| 29 | + const checked = exactCheck(payload, decoded); |
| 30 | + const message = pipe(checked, foldLeftRight); |
| 31 | + |
| 32 | + expect(getPaths(left(message.errors))).toEqual([]); |
| 33 | + expect(message.schema).toEqual(payload); |
| 34 | + }); |
| 35 | + |
| 36 | + test('it should NOT allow missing fields', () => { |
| 37 | + const payload = getExceptionListSchemaMock(); |
| 38 | + delete payload.list_id; |
| 39 | + const decoded = createEndpointListSchema.decode(payload); |
| 40 | + const checked = exactCheck(payload, decoded); |
| 41 | + const message = pipe(checked, foldLeftRight); |
| 42 | + |
| 43 | + expect(getPaths(left(message.errors)).length).toEqual(1); |
| 44 | + expect(message.schema).toEqual({}); |
| 45 | + }); |
| 46 | + |
| 47 | + test('it should not allow an extra key to be sent in', () => { |
| 48 | + const payload: CreateEndpointListSchema & { |
| 49 | + extraKey?: string; |
| 50 | + } = getExceptionListSchemaMock(); |
| 51 | + payload.extraKey = 'some new value'; |
| 52 | + const decoded = createEndpointListSchema.decode(payload); |
| 53 | + const checked = exactCheck(payload, decoded); |
| 54 | + const message = pipe(checked, foldLeftRight); |
| 55 | + expect(getPaths(left(message.errors))).toEqual(['invalid keys "extraKey"']); |
| 56 | + expect(message.schema).toEqual({}); |
| 57 | + }); |
| 58 | +}); |
0 commit comments