import expect from '@kbn/expect' ; import { SuperTest } from 'supertest' ; 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 BulkUpdateTestDefinition extends TestDefinition { request : Array < { type : string ; id : string ; } > ; } export type BulkUpdateTestSuite = TestSuite < BulkUpdateTestDefinition > ; export interface BulkUpdateTestCase extends TestCase { namespace ? : string ; failure ? : 404 ; } const NEW_ATTRIBUTE_KEY = 'title' ; const NEW_ATTRIBUTE_VAL = `Updated attribute value ${ Date . now ( ) }` ; const DOES_NOT_EXIST = Object . freeze ( { type : 'dashboard' , id : 'does-not-exist' } ) ; export const TEST_CASES = Object . freeze ( { ... CASES , DOES_NOT_EXIST , } ) ; const createRequest = ( { type , id , namespace } ) => ( { type , id , ... ( namespace && { namespace } ) , } ) ; export function bulkUpdateTestSuiteFactory ( esArchiver , supertest ) { const expectSavedObjectForbidden = expectResponses . forbiddenTypes ( 'bulk_update' ) ; 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 savedObjects = response . body . saved_objects ; expect ( savedObjects ) . length ( testCaseArray . length ) ; for ( let i = 0 ; i < savedObjects . length ; i ++ ) { const object = savedObjects [ i ] ; const testCase = testCaseArray [ i ] ; await expectResponses . permitted ( object , testCase ) ; if ( ! testCase . failure ) { expect ( object . attributes [ NEW_ATTRIBUTE_KEY ] ) . to . eql ( NEW_ATTRIBUTE_VAL ) ; } } } } ; const createTestDefinitions = ( testCases , forbidden , options ? ) : BulkUpdateTestDefinition [ ] => { 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 makeBulkUpdateTest = ( 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' ) ) ; const attrs = { attributes : { [ NEW_ATTRIBUTE_KEY ] : NEW_ATTRIBUTE_VAL } } ; for ( const test of tests ) { it ( `should return ${ test . responseStatusCode } ${ test . title }` , async ( ) => { const requestBody = test . request . map ( ( x ) => ( { ... x , ... attrs } ) ) ; await supertest . put ( `${ getUrlPrefix ( spaceId ) }/api/saved_objects/_bulk_update` ) . auth ( user ?. username , user ?. password ) . send ( requestBody ) . expect ( test . responseStatusCode ) . then ( test . responseBody ) ; } ) ; } } ) ; } ; const addTests = makeBulkUpdateTest ( describe ) ; addTests . only = makeBulkUpdateTest ( describe . only ) ; return { addTests , createTestDefinitions , expectSavedObjectForbidden , } ; }