import expect from '@kbn/expect' ; import { SuperAgent } from 'superagent' ; import { getTestScenariosForSpace } from '../lib/space_test_utils' ; import { DescribeFn , TestDefinitionAuthentication } from '../lib/types' ; interface GetTest { statusCode : number ; response : ( resp : { [ key ] : any ; } ) => void ; } interface GetTests { default : GetTest ; } interface GetTestDefinition { user ? : TestDefinitionAuthentication ; currentSpaceId : string ; spaceId : string ; tests : GetTests ; } const nonExistantSpaceId = 'not-a-space' ; export function getTestSuiteFactory ( esArchiver , supertest ) { const createExpectEmptyResult = ( ) => ( resp ) => { expect ( resp . body ) . to . eql ( '' ) ; } ; const createExpectNotFoundResult = ( ) => ( resp ) => { expect ( resp . body ) . to . eql ( { error : 'Not Found' , message : 'Not Found' , statusCode : 404 , } ) ; } ; const createExpectRbacForbidden = ( spaceId ) => ( resp ) => { expect ( resp . body ) . to . eql ( { statusCode : 403 , error : 'Forbidden' , message : `Unauthorized to get ${ spaceId } space` , } ) ; } ; const createExpectResults = ( spaceId ) => ( resp ) => { const allSpaces = [ { 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 : [ ] , } , ] ; expect ( resp . body ) . to . eql ( allSpaces . find ( ( space ) => space . id === spaceId ) ) ; } ; const makeGetTest = ( describeFn ) => ( description , { user = { } , currentSpaceId , 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 ( currentSpaceId ) . forEach ( ( { urlPrefix , scenario } ) => { it ( `should return ${ tests . default . statusCode } ${ scenario }` , async ( ) => { return supertest . get ( `${ urlPrefix }/api/spaces/space/${ spaceId }` ) . auth ( user . username , user . password ) . expect ( tests . default . statusCode ) . then ( tests . default . response ) ; } ) ; } ) ; } ) ; } ; const getTest = makeGetTest ( describe ) ; getTest . only = makeGetTest ( describe ) ; return { createExpectResults , createExpectRbacForbidden , createExpectEmptyResult , createExpectNotFoundResult , getTest , nonExistantSpaceId , } ; }