import { left } from 'fp-ts/lib/Either' ; import { pipe } from 'fp-ts/lib/pipeable' ; import { exactCheck , foldLeftRight , getPaths } from '@kbn/securitysolution-io-ts-utils' ; import { getCreateEndpointListItemSchemaMock } from './index.mock' ; import { CreateEndpointListItemSchema , createEndpointListItemSchema } from '.' ; import { getCreateCommentsArrayMock } from '../../common/create_comment/index.mock' ; import { getCommentsMock } from '../../common/comment/index.mock' ; import { CommentsArray } from '../../common/comment' ; describe ( 'create_endpoint_list_item_schema' , ( ) => { test ( 'it should pass validation when supplied a typical list item request not counting the auto generated uuid' , ( ) => { const payload = getCreateEndpointListItemSchemaMock ( ) ; const decoded = createEndpointListItemSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; delete ( message . schema as CreateEndpointListItemSchema ) . item_id ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( payload ) ; } ) ; test ( 'it should fail validation when supplied an undefined for "description"' , ( ) => { const payload = getCreateEndpointListItemSchemaMock ( ) ; delete payload . description ; const decoded = createEndpointListItemSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'Invalid value "undefined" supplied to "description"' , ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it should fail validation when supplied an undefined for "name"' , ( ) => { const payload = getCreateEndpointListItemSchemaMock ( ) ; delete payload . name ; const decoded = createEndpointListItemSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'Invalid value "undefined" supplied to "name"' , ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it should fail validation when supplied an undefined for "type"' , ( ) => { const payload = getCreateEndpointListItemSchemaMock ( ) ; delete payload . type ; const decoded = createEndpointListItemSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'Invalid value "undefined" supplied to "type"' , ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it should fail validation when supplied a "list_id" since it does not required one' , ( ) => { const inputPayload = { ... getCreateEndpointListItemSchemaMock ( ) , list_id : 'list-123' , } ; const decoded = createEndpointListItemSchema . decode ( inputPayload ) ; const checked = exactCheck ( inputPayload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "list_id"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it should fail validation when supplied a "namespace_type" since it does not required one' , ( ) => { const inputPayload = { ... getCreateEndpointListItemSchemaMock ( ) , namespace_type : 'single' , } ; const decoded = createEndpointListItemSchema . decode ( inputPayload ) ; const checked = exactCheck ( inputPayload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "namespace_type"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it should pass validation when supplied an undefined for "meta" but strip it out and generate a correct body not counting the auto generated uuid' , ( ) => { const payload = getCreateEndpointListItemSchemaMock ( ) ; const outputPayload = getCreateEndpointListItemSchemaMock ( ) ; delete payload . meta ; delete outputPayload . meta ; const decoded = createEndpointListItemSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; delete ( message . schema as CreateEndpointListItemSchema ) . item_id ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( outputPayload ) ; } ) ; test ( 'it should pass validation when supplied an undefined for "comments" but return an array and generate a correct body not counting the auto generated uuid' , ( ) => { const inputPayload = getCreateEndpointListItemSchemaMock ( ) ; const outputPayload = getCreateEndpointListItemSchemaMock ( ) ; delete inputPayload . comments ; outputPayload . comments = [ ] ; const decoded = createEndpointListItemSchema . decode ( inputPayload ) ; const checked = exactCheck ( inputPayload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; delete ( message . schema as CreateEndpointListItemSchema ) . item_id ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( outputPayload ) ; } ) ; test ( 'it should pass validation when supplied "comments" array' , ( ) => { const inputPayload = { ... getCreateEndpointListItemSchemaMock ( ) , comments : getCreateCommentsArrayMock ( ) , } ; const decoded = createEndpointListItemSchema . decode ( inputPayload ) ; const checked = exactCheck ( inputPayload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; delete ( message . schema as CreateEndpointListItemSchema ) . item_id ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( inputPayload ) ; } ) ; test ( 'it should fail validation when supplied "comments" with "created_at", "created_by", or "id" values' , ( ) => { const inputPayload = { ... getCreateEndpointListItemSchemaMock ( ) , comments : [ getCommentsMock ( ) ] , } ; const decoded = createEndpointListItemSchema . decode ( inputPayload ) ; const checked = exactCheck ( inputPayload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "created_at,created_by,id"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it should fail validation when supplied an undefined for "entries"' , ( ) => { const inputPayload = getCreateEndpointListItemSchemaMock ( ) ; const outputPayload = getCreateEndpointListItemSchemaMock ( ) ; delete inputPayload . entries ; outputPayload . entries = [ ] ; const decoded = createEndpointListItemSchema . decode ( inputPayload ) ; const checked = exactCheck ( inputPayload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; delete ( message . schema as CreateEndpointListItemSchema ) . item_id ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'Invalid value "undefined" supplied to "entries"' , ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it should pass validation when supplied an undefined for "tags" but return an array and generate a correct body not counting the auto generated uuid' , ( ) => { const inputPayload = getCreateEndpointListItemSchemaMock ( ) ; const outputPayload = getCreateEndpointListItemSchemaMock ( ) ; delete inputPayload . tags ; outputPayload . tags = [ ] ; const decoded = createEndpointListItemSchema . decode ( inputPayload ) ; const checked = exactCheck ( inputPayload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; delete ( message . schema as CreateEndpointListItemSchema ) . item_id ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( outputPayload ) ; } ) ; test ( 'it should pass validation when supplied an undefined for "item_id" and auto generate a uuid' , ( ) => { const inputPayload = getCreateEndpointListItemSchemaMock ( ) ; delete inputPayload . item_id ; const decoded = createEndpointListItemSchema . decode ( inputPayload ) ; const checked = exactCheck ( inputPayload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( ( message . schema as CreateEndpointListItemSchema ) . item_id ) . toMatch ( /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i ) ; } ) ; test ( 'it should pass validation when supplied an undefined for "item_id" and generate a correct body not counting the uuid' , ( ) => { const inputPayload = getCreateEndpointListItemSchemaMock ( ) ; delete inputPayload . item_id ; const decoded = createEndpointListItemSchema . decode ( inputPayload ) ; const checked = exactCheck ( inputPayload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; delete ( message . schema as CreateEndpointListItemSchema ) . item_id ; expect ( message . schema ) . toEqual ( inputPayload ) ; } ) ; test ( 'it should not allow an extra key to be sent in' , ( ) => { const payload = { ... getCreateEndpointListItemSchemaMock ( ) , extraKey : 'some new value' } ; const decoded = createEndpointListItemSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "extraKey"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; } ) ;