import expect from '@kbn/expect' ; import type { UpdateExceptionListItemSchema , ExceptionListItemSchema , } from '@kbn/securitysolution-io-ts-list-types' ; import { EXCEPTION_LIST_URL , EXCEPTION_LIST_ITEM_URL } from '@kbn/securitysolution-list-constants' ; import { getExceptionListItemResponseMockWithoutAutoGeneratedValues } from '../../../../plugins/lists/common/schemas/response/exception_list_item_schema.mock' ; import { getCreateExceptionListItemMinimalSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_exception_list_item_schema.mock' ; import { getCreateExceptionListMinimalSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_exception_list_schema.mock' ; import { FtrProviderContext } from '../../common/ftr_provider_context' ; import { deleteAllExceptions , removeExceptionListServerGeneratedProperties } from '../../utils' ; import { getUpdateMinimalExceptionListItemSchemaMock } from '../../../../plugins/lists/common/schemas/request/update_exception_list_item_schema.mock' ; export default ( { getService } ) => { const supertest = getService ( 'supertest' ) ; const log = getService ( 'log' ) ; describe ( 'update_exception_list_items' , ( ) => { describe ( 'update exception list items' , ( ) => { afterEach ( async ( ) => { await deleteAllExceptions ( supertest , log ) ; } ) ; it ( 'should update a single exception list item property of name using an id' , async ( ) => { await supertest . post ( EXCEPTION_LIST_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( getCreateExceptionListMinimalSchemaMock ( ) ) . expect ( 200 ) ; await supertest . post ( EXCEPTION_LIST_ITEM_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( getCreateExceptionListItemMinimalSchemaMock ( ) ) . expect ( 200 ) ; const updatedList = { ... getUpdateMinimalExceptionListItemSchemaMock ( ) , name : 'some other name' , } ; const { body } = await supertest . put ( EXCEPTION_LIST_ITEM_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( updatedList ) . expect ( 200 ) ; const outputList = { ... getExceptionListItemResponseMockWithoutAutoGeneratedValues ( ) , name : 'some other name' , } ; const bodyToCompare = removeExceptionListServerGeneratedProperties ( body ) ; expect ( bodyToCompare ) . to . eql ( outputList ) ; } ) ; it ( 'should update a single exception list item property of name using an auto-generated item_id' , async ( ) => { await supertest . post ( EXCEPTION_LIST_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( getCreateExceptionListMinimalSchemaMock ( ) ) . expect ( 200 ) ; const { item_id , ... itemNoId } = getCreateExceptionListItemMinimalSchemaMock ( ) ; const { body : createListBody } = await supertest . post ( EXCEPTION_LIST_ITEM_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( itemNoId ) . expect ( 200 ) ; const updatedList = { ... getUpdateMinimalExceptionListItemSchemaMock ( ) , item_id : createListBody . item_id , name : 'some other name' , } ; const { body } = await supertest . put ( EXCEPTION_LIST_ITEM_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( updatedList ) . expect ( 200 ) ; const outputList = { ... getExceptionListItemResponseMockWithoutAutoGeneratedValues ( ) , name : 'some other name' , item_id : body . item_id , } ; const bodyToCompare = removeExceptionListServerGeneratedProperties ( body ) ; expect ( bodyToCompare ) . to . eql ( outputList ) ; } ) ; it ( 'should give a 404 if it is given a fake exception list item id' , async ( ) => { const updatedList = { ... getUpdateMinimalExceptionListItemSchemaMock ( ) , id : '5096dec6-b6b9-4d8d-8f93-6c2602079d9d' , } ; delete updatedList . item_id ; const { body } = await supertest . put ( EXCEPTION_LIST_ITEM_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( updatedList ) . expect ( 404 ) ; expect ( body ) . to . eql ( { status_code : 404 , message : 'exception list item id: "5096dec6-b6b9-4d8d-8f93-6c2602079d9d" does not exist' , } ) ; } ) ; it ( 'should give a 404 if it is given a fake item_id' , async ( ) => { const updatedList = { ... getUpdateMinimalExceptionListItemSchemaMock ( ) , item_id : '5096dec6-b6b9-4d8d-8f93-6c2602079d9d' , } ; const { body } = await supertest . put ( EXCEPTION_LIST_ITEM_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( updatedList ) . expect ( 404 ) ; expect ( body ) . to . eql ( { status_code : 404 , message : 'exception list item item_id: "5096dec6-b6b9-4d8d-8f93-6c2602079d9d" does not exist' , } ) ; } ) ; it ( 'should give a 404 if both id and list_id is null' , async ( ) => { const { item_id , ... listNoId } = getUpdateMinimalExceptionListItemSchemaMock ( ) ; const { body } = await supertest . put ( EXCEPTION_LIST_ITEM_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( listNoId ) . expect ( 404 ) ; expect ( body ) . to . eql ( { status_code : 404 , message : 'either id or item_id need to be defined' , } ) ; } ) ; } ) ; } ) ; } ;