import expect from '@kbn/expect' ; import { SuperTest } from 'supertest' ; import { getTestScenariosForSpace } from '../lib/space_test_utils' ; import { DescribeFn , TestDefinitionAuthentication } from '../lib/types' ; interface CreateTest { statusCode : number ; response : ( resp : { [ key ] : any ; } ) => void ; } interface CreateTests { newSpace : CreateTest ; alreadyExists : CreateTest ; reservedSpecified : CreateTest ; } interface CreateTestDefinition { user ? : TestDefinitionAuthentication ; spaceId : string ; tests : CreateTests ; } export function createTestSuiteFactory ( esArchiver , supertest ) { const expectConflictResponse = ( resp ) => { expect ( resp . body ) . to . only . have . keys ( [ 'error' , 'message' , 'statusCode' ] ) ; expect ( resp . body . error ) . to . equal ( 'Conflict' ) ; expect ( resp . body . statusCode ) . to . equal ( 409 ) ; expect ( resp . body . message ) . to . match ( new RegExp ( `A space with the identifier .*` ) ) ; } ; const expectNewSpaceResult = ( resp ) => { expect ( resp . body ) . to . eql ( { name : 'marketing' , id : 'marketing' , description : 'a description' , color : '#5c5959' , disabledFeatures : [ ] , } ) ; } ; const expectRbacForbiddenResponse = ( resp ) => { expect ( resp . body ) . to . eql ( { statusCode : 403 , error : 'Forbidden' , message : 'Unauthorized to create spaces' , } ) ; } ; const expectReservedSpecifiedResult = ( resp ) => { expect ( resp . body ) . to . eql ( { name : 'reserved space' , id : 'reserved' , description : 'a description' , color : '#5c5959' , disabledFeatures : [ ] , } ) ; } ; const makeCreateTest = ( describeFn ) => ( description , { user = { } , spaceId , tests } ) => { describeFn ( description , ( ) => { beforeEach ( ( ) => 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 . newSpace . statusCode } ${ scenario }` , async ( ) => { return supertest . post ( `${ urlPrefix }/api/spaces/space` ) . auth ( user . username , user . password ) . send ( { name : 'marketing' , id : 'marketing' , description : 'a description' , color : '#5c5959' , disabledFeatures : [ ] , } ) . expect ( tests . newSpace . statusCode ) . then ( tests . newSpace . response ) ; } ) ; describe ( 'when it already exists' , ( ) => { it ( `should return ${ tests . alreadyExists . statusCode } ${ scenario }` , async ( ) => { return supertest . post ( `${ urlPrefix }/api/spaces/space` ) . auth ( user . username , user . password ) . send ( { name : 'space_1' , id : 'space_1' , color : '#ffffff' , description : 'a description' , disabledFeatures : [ ] , } ) . expect ( tests . alreadyExists . statusCode ) . then ( tests . alreadyExists . response ) ; } ) ; } ) ; describe ( 'when _reserved is specified' , ( ) => { it ( `should return ${ tests . reservedSpecified . statusCode } and ignore _reserved ${ scenario }` , async ( ) => { return supertest . post ( `${ urlPrefix }/api/spaces/space` ) . auth ( user . username , user . password ) . send ( { name : 'reserved space' , id : 'reserved' , description : 'a description' , color : '#5c5959' , _reserved : true , disabledFeatures : [ ] , } ) . expect ( tests . reservedSpecified . statusCode ) . then ( tests . reservedSpecified . response ) ; } ) ; } ) ; } ) ; } ) ; } ; const createTest = makeCreateTest ( describe ) ; createTest . only = makeCreateTest ( describe . only ) ; return { createTest , expectConflictResponse , expectNewSpaceResult , expectRbacForbiddenResponse , expectReservedSpecifiedResult , } ; }