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 { CreateEndpointListSchema , createEndpointListSchema } from '.' ; import { getExceptionListSchemaMock } from '../exception_list_schema/index.mock' ; describe ( 'create_endpoint_list_schema' , ( ) => { test ( 'it should validate a typical endpoint list response' , ( ) => { const payload = getExceptionListSchemaMock ( ) ; const decoded = createEndpointListSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( payload ) ; } ) ; test ( 'it should accept an empty object when an endpoint list already exists' , ( ) => { const payload = { } ; const decoded = createEndpointListSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( payload ) ; } ) ; test ( 'it should NOT accept an undefined for "list_id"' , ( ) => { const payload = getExceptionListSchemaMock ( ) ; delete payload . list_id ; const decoded = createEndpointListSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "_version,created_at,created_by,description,id,immutable,meta,{},name,namespace_type,os_types,["linux"],tags,["user added string for a tag","malware"],tie_breaker_id,type,updated_at,updated_by,version"' , ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it should NOT allow missing fields' , ( ) => { const payload = getExceptionListSchemaMock ( ) ; delete payload . list_id ; const decoded = createEndpointListSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) . length ) . toEqual ( 1 ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it should not allow an extra key to be sent in' , ( ) => { const payload = getExceptionListSchemaMock ( ) ; payload . extraKey = 'some new value' ; const decoded = createEndpointListSchema . 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 ( { } ) ; } ) ; } ) ;