import * as Rx from 'rxjs' ; 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 { initGetSpaceApi } from './get' ; describe ( 'GET space' , ( ) => { const spacesSavedObjects = createSpaces ( ) ; const spaces = spacesSavedObjects . map ( ( s ) => ( { id : s . id , ... s . attributes } ) ) ; 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 , } ) ; initGetSpaceApi ( { externalRouter : router , getStartServices : async ( ) => [ coreStart , { } , { } ] , log , getSpacesService : ( ) => spacesServiceStart , usageStatsServicePromise , } ) ; return { routeHandler : router . get . mock . calls [ 0 ] [ 1 ] , } ; } ; it ( `returns http/403 when the license is invalid` , async ( ) => { const { routeHandler } = await setup ( ) ; const request = httpServerMock . createKibanaRequest ( { method : 'get' , } ) ; const response = await routeHandler ( mockRouteContextWithInvalidLicense , request , kibanaResponseFactory ) ; expect ( response . status ) . toEqual ( 403 ) ; expect ( response . payload ) . toEqual ( { message : 'License is invalid for spaces' , } ) ; } ) ; it ( `returns the space with that id` , async ( ) => { const { routeHandler } = await setup ( ) ; const request = httpServerMock . createKibanaRequest ( { params : { id : 'default' , } , method : 'get' , } ) ; const response = await routeHandler ( mockRouteContext , request , kibanaResponseFactory ) ; expect ( response . status ) . toEqual ( 200 ) ; expect ( response . payload ) . toEqual ( spaces . find ( ( s ) => s . id === 'default' ) ) ; } ) ; it ( `'GET spaces/{id}' returns 404 when retrieving a non-existent space` , async ( ) => { const { routeHandler } = await setup ( ) ; const request = httpServerMock . createKibanaRequest ( { params : { id : 'not-a-space' , } , method : 'get' , } ) ; const response = await routeHandler ( mockRouteContext , request , kibanaResponseFactory ) ; expect ( response . status ) . toEqual ( 404 ) ; } ) ; } ) ;