import * as Rx from 'rxjs' ; import type { ObjectType } from '@kbn/config-schema' ; import type { RouteValidatorConfig } from 'src/core/server' ; import { kibanaResponseFactory } from 'src/core/server' ; import { coreMock , httpServerMock , httpServiceMock , loggingSystemMock , } from 'src/core/server/mocks' ; import { spacesConfig } from '../../../lib/__fixtures__' ; import { SpacesClientService } from '../../../spaces_client' ; import { SpacesService } from '../../../spaces_service' ; import { usageStatsServiceMock } from '../../../usage_stats/usage_stats_service.mock' ; import { createMockSavedObjectsRepository , createSpaces , mockRouteContext , mockRouteContextWithInvalidLicense , } from '../__fixtures__' ; import { initPostSpacesApi } from './post' ; describe ( 'Spaces Public API' , ( ) => { const spacesSavedObjects = createSpaces ( ) ; const setup = async ( ) => { const httpService = httpServiceMock . createSetupContract ( ) ; const router = httpService . createRouter ( ) ; const coreStart = coreMock . createStart ( ) ; const savedObjectsRepositoryMock = createMockSavedObjectsRepository ( spacesSavedObjects ) ; const log = loggingSystemMock . create ( ) . get ( 'spaces' ) ; const clientService = new SpacesClientService ( jest . fn ( ) ) ; clientService . setup ( { config$ : Rx . of ( spacesConfig ) } ) . setClientRepositoryFactory ( ( ) => savedObjectsRepositoryMock ) ; const service = new SpacesService ( ) ; service . setup ( { basePath : httpService . basePath , } ) ; const usageStatsServicePromise = Promise . resolve ( usageStatsServiceMock . createSetupContract ( ) ) ; const clientServiceStart = clientService . start ( coreStart ) ; const spacesServiceStart = service . start ( { basePath : coreStart . http . basePath , spacesClientService : clientServiceStart , } ) ; initPostSpacesApi ( { externalRouter : router , getStartServices : async ( ) => [ coreStart , { } , { } ] , log , getSpacesService : ( ) => spacesServiceStart , usageStatsServicePromise , } ) ; const [ routeDefinition , routeHandler ] = router . post . mock . calls [ 0 ] ; return { routeValidation : routeDefinition . validate as RouteValidatorConfig < { } , { } , { } > , routeHandler , savedObjectsRepositoryMock , } ; } ; it ( 'should create a new space with the provided ID' , async ( ) => { const payload = { id : 'my-space-id' , name : 'my new space' , description : 'with a description' , disabledFeatures : [ 'foo' ] , } ; const { routeHandler , savedObjectsRepositoryMock } = await setup ( ) ; const request = httpServerMock . createKibanaRequest ( { body : payload , method : 'post' , } ) ; const response = await routeHandler ( mockRouteContext , request , kibanaResponseFactory ) ; const { status } = response ; expect ( status ) . toEqual ( 200 ) ; expect ( savedObjectsRepositoryMock . create ) . toHaveBeenCalledTimes ( 1 ) ; expect ( savedObjectsRepositoryMock . create ) . toHaveBeenCalledWith ( 'space' , { name : 'my new space' , description : 'with a description' , disabledFeatures : [ 'foo' ] } , { id : 'my-space-id' } ) ; } ) ; it ( `returns http/403 when the license is invalid` , async ( ) => { const { routeHandler } = await setup ( ) ; const request = httpServerMock . createKibanaRequest ( { method : 'post' , } ) ; const response = await routeHandler ( mockRouteContextWithInvalidLicense , request , kibanaResponseFactory ) ; expect ( response . status ) . toEqual ( 403 ) ; expect ( response . payload ) . toEqual ( { message : 'License is invalid for spaces' , } ) ; } ) ; it ( 'should not allow a space to be updated' , async ( ) => { const payload = { id : 'a-space' , name : 'my updated space' , description : 'with a description' , } ; const { routeHandler } = await setup ( ) ; const request = httpServerMock . createKibanaRequest ( { body : payload , method : 'post' , } ) ; const response = await routeHandler ( mockRouteContext , request , kibanaResponseFactory ) ; const { status , payload : responsePayload } = response ; expect ( status ) . toEqual ( 409 ) ; expect ( responsePayload . message ) . toEqual ( 'A space with the identifier a-space already exists.' ) ; } ) ; it ( 'should not require disabledFeatures to be specified' , async ( ) => { const payload = { id : 'my-space-id' , name : 'my new space' , description : 'with a description' , } ; const { routeValidation , routeHandler , savedObjectsRepositoryMock } = await setup ( ) ; const request = httpServerMock . createKibanaRequest ( { body : ( routeValidation . body as ObjectType ) . validate ( payload ) , method : 'post' , } ) ; const response = await routeHandler ( mockRouteContext , request , kibanaResponseFactory ) ; const { status } = response ; expect ( status ) . toEqual ( 200 ) ; expect ( savedObjectsRepositoryMock . create ) . toHaveBeenCalledTimes ( 1 ) ; expect ( savedObjectsRepositoryMock . create ) . toHaveBeenCalledWith ( 'space' , { name : 'my new space' , description : 'with a description' , disabledFeatures : [ ] } , { id : 'my-space-id' } ) ; } ) ; } ) ;