import expect from '@kbn/expect' ; import { SuperTest } from 'supertest' ; import type { Client } from '@elastic/elasticsearch' ; import { getAggregatedSpaceData , getTestScenariosForSpace } from '../lib/space_test_utils' ; import { MULTI_NAMESPACE_SAVED_OBJECT_TEST_CASES as CASES } from '../lib/saved_object_test_cases' ; import { DescribeFn , TestDefinitionAuthentication } from '../lib/types' ; interface DeleteTest { statusCode : number ; response : ( resp : { [ key ] : any ; } ) => void ; } interface DeleteTests { exists : DeleteTest ; reservedSpace : DeleteTest ; doesntExist : DeleteTest ; } interface DeleteTestDefinition { user ? : TestDefinitionAuthentication ; spaceId : string ; tests : DeleteTests ; } export function deleteTestSuiteFactory ( es , esArchiver , supertest ) { const createExpectResult = ( expectedResult ) => ( resp ) => { expect ( resp . body ) . to . eql ( expectedResult ) ; } ; const expectEmptyResult = async ( resp ) => { expect ( resp . body ) . to . eql ( '' ) ; const response = await getAggregatedSpaceData ( es , [ 'visualization' , 'dashboard' , 'space' , 'index-pattern' , 'legacy-url-alias' , ] ) ; const buckets = response . aggregations ?. count . buckets ; const expectedBuckets = [ { key : 'default' , doc_count : 7 , countByType : { doc_count_error_upper_bound : 0 , sum_other_doc_count : 0 , buckets : [ { key : 'visualization' , doc_count : 3 } , { key : 'space' , doc_count : 2 } , { key : 'dashboard' , doc_count : 1 } , { key : 'index-pattern' , doc_count : 1 } , ] , } , } , { doc_count : 7 , key : 'space_1' , countByType : { doc_count_error_upper_bound : 0 , sum_other_doc_count : 0 , buckets : [ { key : 'visualization' , doc_count : 3 } , { key : 'legacy-url-alias' , doc_count : 2 } , { key : 'dashboard' , doc_count : 1 } , { key : 'index-pattern' , doc_count : 1 } , ] , } , } , { doc_count : 2 , key : 'other_space' , countByType : { doc_count_error_upper_bound : 0 , sum_other_doc_count : 0 , buckets : [ { key : 'legacy-url-alias' , doc_count : 2 } ] , } , } , ] ; expect ( buckets ) . to . eql ( expectedBuckets ) ; const multiNamespaceResponse = await es . search < Record < string , any > > ( { index : '.kibana' , size : 20 , body : { query : { terms : { type : [ 'sharedtype' ] } } } , } ) ; const docs = multiNamespaceResponse . hits . hits ; expect ( docs ) . length ( 17 ) ; docs . forEach ( ( doc ) => ( ) => { const containsSpace2 = doc ?. _source ?. namespaces . includes ( 'space_2' ) ; expect ( containsSpace2 ) . to . eql ( false ) ; } ) ; const space2OnlyObjExists = docs . some ( ( x ) => x . _id === CASES . SPACE_2_ONLY . id ) ; expect ( space2OnlyObjExists ) . to . eql ( false ) ; } ; const expectNotFound = ( resp ) => { expect ( resp . body ) . to . eql ( { error : 'Not Found' , statusCode : 404 , message : 'Not Found' , } ) ; } ; const expectRbacForbidden = ( resp ) => { expect ( resp . body ) . to . eql ( { statusCode : 403 , error : 'Forbidden' , message : 'Unauthorized to delete spaces' , } ) ; } ; const expectReservedSpaceResult = ( resp ) => { expect ( resp . body ) . to . eql ( { error : 'Bad Request' , statusCode : 400 , message : `The default space cannot be deleted because it is reserved.` , } ) ; } ; const makeDeleteTest = ( describeFn ) => ( description , { user = { } , spaceId , tests } ) => { describeFn ( description , ( ) => { beforeEach ( async ( ) => { await esArchiver . load ( 'x-pack/test/spaces_api_integration/common/fixtures/es_archiver/saved_objects/spaces' ) ; } ) ; afterEach ( ( ) => esArchiver . unload ( 'x-pack/test/spaces_api_integration/common/fixtures/es_archiver/saved_objects/spaces' ) ) ; getTestScenariosForSpace ( spaceId ) . forEach ( ( { urlPrefix , scenario } ) => { it ( `should return ${ tests . exists . statusCode } ${ scenario }` , async ( ) => { return supertest . delete ( `${ urlPrefix }/api/spaces/space/space_2` ) . auth ( user . username , user . password ) . expect ( tests . exists . statusCode ) . then ( tests . exists . response ) ; } ) ; describe ( `when the space is reserved` , ( ) => { it ( `should return ${ tests . reservedSpace . statusCode } ${ scenario }` , async ( ) => { return supertest . delete ( `${ urlPrefix }/api/spaces/space/default` ) . auth ( user . username , user . password ) . expect ( tests . reservedSpace . statusCode ) . then ( tests . reservedSpace . response ) ; } ) ; } ) ; describe ( `when the space doesn't exist` , ( ) => { it ( `should return ${ tests . doesntExist . statusCode } ${ scenario }` , async ( ) => { return supertest . delete ( `${ urlPrefix }/api/spaces/space/space_3` ) . auth ( user . username , user . password ) . expect ( tests . doesntExist . statusCode ) . then ( tests . doesntExist . response ) ; } ) ; } ) ; } ) ; } ) ; } ; const deleteTest = makeDeleteTest ( describe ) ; deleteTest . only = makeDeleteTest ( describe . only ) ; return { createExpectResult , deleteTest , expectEmptyResult , expectNotFound , expectRbacForbidden , expectReservedSpaceResult , } ; }