import expect from '@kbn/expect' ; import type { UpdateListItemSchema , CreateListItemSchema , ListItemSchema , } from '@kbn/securitysolution-io-ts-list-types' ; import { LIST_URL , LIST_ITEM_URL } from '@kbn/securitysolution-list-constants' ; import { getListItemResponseMockWithoutAutoGeneratedValues } from '../../../../plugins/lists/common/schemas/response/list_item_schema.mock' ; import { getCreateMinimalListItemSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_list_item_schema.mock' ; import { FtrProviderContext } from '../../common/ftr_provider_context' ; import { getCreateMinimalListSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_list_schema.mock' ; import { createListsIndex , deleteListsIndex , removeListItemServerGeneratedProperties , } from '../../utils' ; import { getUpdateMinimalListItemSchemaMock } from '../../../../plugins/lists/common/schemas/request/update_list_item_schema.mock' ; export default ( { getService } ) => { const supertest = getService ( 'supertest' ) ; const log = getService ( 'log' ) ; describe ( 'update_list_items' , ( ) => { describe ( 'update list items' , ( ) => { beforeEach ( async ( ) => { await createListsIndex ( supertest , log ) ; } ) ; afterEach ( async ( ) => { await deleteListsIndex ( supertest , log ) ; } ) ; it ( 'should update a single list item property of value using an id' , async ( ) => { await supertest . post ( LIST_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( getCreateMinimalListSchemaMock ( ) ) . expect ( 200 ) ; await supertest . post ( LIST_ITEM_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( getCreateMinimalListItemSchemaMock ( ) ) . expect ( 200 ) ; const updatedListItem = { ... getUpdateMinimalListItemSchemaMock ( ) , value : '192.168.0.2' , } ; const { body } = await supertest . put ( LIST_ITEM_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( updatedListItem ) . expect ( 200 ) ; const outputListItem = { ... getListItemResponseMockWithoutAutoGeneratedValues ( ) , value : '192.168.0.2' , } ; const bodyToCompare = removeListItemServerGeneratedProperties ( body ) ; expect ( bodyToCompare ) . to . eql ( outputListItem ) ; } ) ; it ( 'should update a single list item of value using an auto-generated id of both list and list item' , async ( ) => { const { id , ... listNoId } = getCreateMinimalListSchemaMock ( ) ; const { body : createListBody } = await supertest . post ( LIST_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( listNoId ) . expect ( 200 ) ; const listItem = { ... getCreateMinimalListItemSchemaMock ( ) , list_id : createListBody . id , } ; const { body : createListItemBody } = await supertest . post ( LIST_ITEM_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( listItem ) . expect ( 200 ) ; const updatedList = { ... getUpdateMinimalListItemSchemaMock ( ) , id : createListItemBody . id , value : '192.168.0.2' , } ; const { body } = await supertest . put ( LIST_ITEM_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( updatedList ) . expect ( 200 ) ; const outputListItem = { ... getListItemResponseMockWithoutAutoGeneratedValues ( ) , value : '192.168.0.2' , } ; const bodyToCompare = { ... removeListItemServerGeneratedProperties ( body ) , list_id : outputListItem . list_id , } ; expect ( bodyToCompare ) . to . eql ( outputListItem ) ; } ) ; it ( 'should give a 404 if it is given a fake id' , async ( ) => { await supertest . post ( LIST_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( getCreateMinimalListSchemaMock ( ) ) . expect ( 200 ) ; await supertest . post ( LIST_ITEM_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( getCreateMinimalListItemSchemaMock ( ) ) . expect ( 200 ) ; const updatedListItem = { ... getUpdateMinimalListItemSchemaMock ( ) , id : 'some-other-id' , value : '192.168.0.2' , } ; const { body } = await supertest . put ( LIST_ITEM_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( updatedListItem ) . expect ( 404 ) ; expect ( body ) . to . eql ( { status_code : 404 , message : 'list item id: "some-other-id" not found' , } ) ; } ) ; } ) ; } ) ; } ;