import { SuperTest } from 'supertest' ; import type { Client } from '@elastic/elasticsearch' ; import expect from '@kbn/expect' ; import type { SearchTotalHits } from '@elastic/elasticsearch/lib/api/types' ; import { SAVED_OBJECT_TEST_CASES as CASES } from '../lib/saved_object_test_cases' ; import { SPACES } from '../lib/spaces' ; import { expectResponses , getUrlPrefix , getTestTitle } from '../lib/saved_object_test_utils' ; import { ExpectResponseBody , TestCase , TestDefinition , TestSuite } from '../lib/types' ; export interface DeleteTestDefinition extends TestDefinition { request : { type : string ; id : string ; force ? : boolean ; } ; } export type DeleteTestSuite = TestSuite < DeleteTestDefinition > ; export interface DeleteTestCase extends TestCase { force ? : boolean ; failure ? : 400 | 403 | 404 ; } const ALIAS_DELETE_INCLUSIVE = Object . freeze ( { type : 'resolvetype' , id : 'alias-match-newid' } ) ; const ALIAS_DELETE_EXCLUSIVE = Object . freeze ( { type : 'resolvetype' , id : 'all_spaces' } ) ; const DOES_NOT_EXIST = Object . freeze ( { type : 'dashboard' , id : 'does-not-exist' } ) ; export const TEST_CASES = Object . freeze ( { ... CASES , ALIAS_DELETE_INCLUSIVE , ALIAS_DELETE_EXCLUSIVE , DOES_NOT_EXIST , } ) ; /**
 * Test cases have additional properties that we don't want to send in HTTP Requests
 */ const createRequest = ( { type , id , force } ) => ( { type , id , force } ) ; export function deleteTestSuiteFactory ( es , esArchiver , supertest ) { const expectSavedObjectForbidden = expectResponses . forbiddenTypes ( 'delete' ) ; const expectResponseBody = ( testCase ) : ExpectResponseBody => async ( response ) => { if ( testCase . failure === 403 ) { await expectSavedObjectForbidden ( testCase . type ) ( response ) ; } else { const object = response . body ; if ( testCase . failure ) { await expectResponses . permitted ( object , testCase ) ; } else { expect ( object ) . to . eql ( { } ) ; await es . indices . refresh ( { index : '.kibana' } ) ; const searchResponse = await es . search ( { index : '.kibana' , body : { size : 0 , query : { terms : { type : [ 'legacy-url-alias' ] } } , track_total_hits : true , } , } ) ; const expectAliasWasDeleted = ! ! [ ALIAS_DELETE_INCLUSIVE , ALIAS_DELETE_EXCLUSIVE ] . find ( ( { type , id } ) => testCase . type === type && testCase . id === id ) ; expect ( ( searchResponse . hits . total as SearchTotalHits ) . value ) . to . eql ( expectAliasWasDeleted ? 4 : 5 ) ; } } } ; const createTestDefinitions = ( testCases , forbidden , options ? ) : DeleteTestDefinition [ ] => { let cases = Array . isArray ( testCases ) ? testCases : [ testCases ] ; if ( forbidden ) { cases = cases . map ( ( x ) => ( { ... x , failure : 403 } ) ) ; } return cases . map ( ( x ) => ( { title : getTestTitle ( x ) , responseStatusCode : x . failure ?? 200 , request : createRequest ( x ) , responseBody : options ?. responseBodyOverride || expectResponseBody ( x ) , } ) ) ; } ; const makeDeleteTest = ( describeFn ) => ( description , definition ) => { const { user , spaceId = SPACES . DEFAULT . spaceId , tests } = definition ; describeFn ( description , ( ) => { before ( ( ) => esArchiver . load ( 'x-pack/test/saved_object_api_integration/common/fixtures/es_archiver/saved_objects/spaces' ) ) ; after ( ( ) => esArchiver . unload ( 'x-pack/test/saved_object_api_integration/common/fixtures/es_archiver/saved_objects/spaces' ) ) ; for ( const test of tests ) { it ( `should return ${ test . responseStatusCode } ${ test . title }` , async ( ) => { const { type , id , force } = test . request ; await supertest . delete ( `${ getUrlPrefix ( spaceId ) }/api/saved_objects/${ type }/${ id }` ) . query ( { ... ( force && { force } ) } ) . auth ( user ?. username , user ?. password ) . expect ( test . responseStatusCode ) . then ( test . responseBody ) ; } ) ; } } ) ; } ; const addTests = makeDeleteTest ( describe ) ; addTests . only = makeDeleteTest ( describe . only ) ; return { addTests , createTestDefinitions , } ; }