import expect from '@kbn/expect' ; import { SuperTest } from 'supertest' ; import { TEST_CASES } from './resolve' ; import { SPACES } from '../lib/spaces' ; import { createRequest , expectResponses , getUrlPrefix , getTestTitle , } from '../lib/saved_object_test_utils' ; import { ExpectResponseBody , TestCase , TestDefinition , TestSuite } from '../lib/types' ; export interface BulkResolveTestDefinition extends TestDefinition { request : Array < { type : string ; id : string ; } > ; } export type BulkResolveTestSuite = TestSuite < BulkResolveTestDefinition > ; export interface BulkResolveTestCase extends TestCase { expectedOutcome ? : 'exactMatch' | 'aliasMatch' | 'conflict' ; expectedId ? : string ; expectedAliasTargetId ? : string ; } export { TEST_CASES } ; export function bulkResolveTestSuiteFactory ( esArchiver , supertest ) { const expectSavedObjectForbidden = expectResponses . forbiddenTypes ( 'bulk_get' ) ; const expectResponseBody = ( testCases , statusCode ) : ExpectResponseBody => async ( response ) => { const testCaseArray = Array . isArray ( testCases ) ? testCases : [ testCases ] ; if ( statusCode === 403 ) { const types = testCaseArray . map ( ( x ) => x . type ) ; await expectSavedObjectForbidden ( types ) ( response ) ; } else { const resolvedObjects = response . body . resolved_objects ; expect ( resolvedObjects ) . length ( testCaseArray . length ) ; for ( let i = 0 ; i < resolvedObjects . length ; i ++ ) { const resolvedObject = resolvedObjects [ i ] ; const testCase = testCaseArray [ i ] ; const { expectedId : id , expectedOutcome , expectedAliasTargetId } = testCase ; await expectResponses . permitted ( resolvedObject . saved_object , { ... testCase , ... ( ! testCase . failure && id && { id } ) , } ) ; if ( ! testCase . failure ) { expect ( resolvedObject . outcome ) . to . eql ( expectedOutcome ) ; if ( expectedOutcome === 'conflict' || expectedOutcome === 'aliasMatch' ) { expect ( resolvedObject . alias_target_id ) . to . eql ( expectedAliasTargetId ) ; } else { expect ( resolvedObject . alias_target_id ) . to . eql ( undefined ) ; } } } } } ; const createTestDefinitions = ( testCases , forbidden , options ? ) : BulkResolveTestDefinition [ ] => { const cases = Array . isArray ( testCases ) ? testCases : [ testCases ] ; const responseStatusCode = forbidden ? 403 : 200 ; if ( ! options ?. singleRequest ) { return cases . map ( ( x ) => ( { title : getTestTitle ( x , responseStatusCode ) , request : [ createRequest ( x ) ] , responseStatusCode , responseBody : options ?. responseBodyOverride || expectResponseBody ( x , responseStatusCode ) , } ) ) ; } return [ { title : getTestTitle ( cases , responseStatusCode ) , request : cases . map ( ( x ) => createRequest ( x ) ) , responseStatusCode , responseBody : options ?. responseBodyOverride || expectResponseBody ( cases , responseStatusCode ) , } , ] ; } ; const makeBulkResolveTest = ( 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 ( ) => { await supertest . post ( `${ getUrlPrefix ( spaceId ) }/api/saved_objects/_bulk_resolve` ) . auth ( user ?. username , user ?. password ) . send ( test . request ) . expect ( test . responseStatusCode ) . then ( test . responseBody ) ; } ) ; } } ) ; } ; const addTests = makeBulkResolveTest ( describe ) ; addTests . only = makeBulkResolveTest ( describe . only ) ; return { addTests , createTestDefinitions , } ; }