import expect from '@kbn/expect' ; import { SuperTest } from 'supertest' ; import { getTestScenariosForSpace } from '../lib/space_test_utils' ; import { DescribeFn , TestDefinitionAuthentication } from '../lib/types' ; interface GetAllTest { statusCode : number ; response : ( resp : { [ key ] : any ; } ) => void ; } interface GetAllTests { exists : GetAllTest ; copySavedObjectsPurpose : GetAllTest ; shareSavedObjectsPurpose : GetAllTest ; includeAuthorizedPurposes : GetAllTest ; } interface GetAllTestDefinition { user ? : TestDefinitionAuthentication ; spaceId : string ; tests : GetAllTests ; } interface AuthorizedPurposes { any : boolean ; copySavedObjectsIntoSpace : boolean ; findSavedObjects : boolean ; shareSavedObjectsIntoSpace : boolean ; } const ALL_SPACE_RESULTS = [ { id : 'default' , name : 'Default Space' , description : 'This is the default space' , _reserved : true , disabledFeatures : [ ] , } , { id : 'space_1' , name : 'Space 1' , description : 'This is the first test space' , disabledFeatures : [ ] , } , { id : 'space_2' , name : 'Space 2' , description : 'This is the second test space' , disabledFeatures : [ ] , } , ] ; export function getAllTestSuiteFactory ( esArchiver , supertest ) { const createExpectResults = ( ... spaceIds ) => ( resp ) => { const expectedBody = ALL_SPACE_RESULTS . filter ( ( entry ) => spaceIds . includes ( entry . id ) ) ; expect ( resp . body ) . to . eql ( expectedBody ) ; } ; const createExpectAllPurposesResults = ( authorizedPurposes , ... spaceIds ) => ( resp ) => { const expectedBody = ALL_SPACE_RESULTS . filter ( ( entry ) => spaceIds . includes ( entry . id ) ) . map ( ( x ) => ( { ... x , authorizedPurposes } ) ) ; expect ( resp . body ) . to . eql ( expectedBody ) ; } ; const expectEmptyResult = ( resp ) => { expect ( resp . body ) . to . eql ( '' ) ; } ; const expectRbacForbidden = ( resp ) => { expect ( resp . body ) . to . eql ( { error : 'Forbidden' , message : 'Forbidden' , statusCode : 403 , } ) ; } ; const makeGetAllTest = ( describeFn ) => ( description , { user = { } , spaceId , tests } ) => { describeFn ( description , ( ) => { before ( ( ) => esArchiver . load ( 'x-pack/test/spaces_api_integration/common/fixtures/es_archiver/saved_objects/spaces' ) ) ; after ( ( ) => esArchiver . unload ( 'x-pack/test/spaces_api_integration/common/fixtures/es_archiver/saved_objects/spaces' ) ) ; getTestScenariosForSpace ( spaceId ) . forEach ( ( { scenario , urlPrefix } ) => { describe ( 'undefined purpose' , ( ) => { it ( `should return ${ tests . exists . statusCode } ${ scenario }` , async ( ) => { return supertest . get ( `${ urlPrefix }/api/spaces/space` ) . auth ( user . username , user . password ) . expect ( tests . exists . statusCode ) . then ( tests . exists . response ) ; } ) ; } ) ; describe ( 'copySavedObjectsIntoSpace purpose' , ( ) => { it ( `should return ${ tests . copySavedObjectsPurpose . statusCode } ${ scenario }` , async ( ) => { return supertest . get ( `${ urlPrefix }/api/spaces/space` ) . query ( { purpose : 'copySavedObjectsIntoSpace' } ) . auth ( user . username , user . password ) . expect ( tests . copySavedObjectsPurpose . statusCode ) . then ( tests . copySavedObjectsPurpose . response ) ; } ) ; } ) ; describe ( 'shareSavedObjectsIntoSpace purpose' , ( ) => { it ( `should return ${ tests . shareSavedObjectsPurpose . statusCode } ${ scenario }` , async ( ) => { return supertest . get ( `${ urlPrefix }/api/spaces/space` ) . query ( { purpose : 'shareSavedObjectsIntoSpace' } ) . auth ( user . username , user . password ) . expect ( tests . copySavedObjectsPurpose . statusCode ) . then ( tests . copySavedObjectsPurpose . response ) ; } ) ; } ) ; describe ( 'include_authorized_purposes=true' , ( ) => { it ( `should return ${ tests . includeAuthorizedPurposes . statusCode } ${ scenario }` , async ( ) => { return supertest . get ( `${ urlPrefix }/api/spaces/space` ) . query ( { include_authorized_purposes : true } ) . auth ( user . username , user . password ) . expect ( tests . includeAuthorizedPurposes . statusCode ) . then ( tests . includeAuthorizedPurposes . response ) ; } ) ; } ) ; } ) ; } ) ; } ; const getAllTest = makeGetAllTest ( describe ) ; getAllTest . only = makeGetAllTest ( describe . only ) ; return { createExpectResults , createExpectAllPurposesResults , expectRbacForbidden , getAllTest , expectEmptyResult , } ; }