import { errors } from '@elastic/elasticsearch' ; import { RequestHandler } from 'src/core/server' ; import { elasticsearchServiceMock , httpServerMock , httpServiceMock , coreMock , } from '../../../../../../src/core/server/mocks' ; import { kibanaResponseFactory } from '../../../../../../src/core/server' ; import { licensingMock } from '../../../../../plugins/licensing/server/mocks' ; import { API_BASE_PATH } from '../../../common/constants' ; import { handleEsError } from '../../shared_imports' ; import { register } from './get_route' ; import { ScopedClusterClientMock } from './types' ; const { createApiResponse } = elasticsearchServiceMock ; function createCoreRequestHandlerContextMock ( ) { return { core : coreMock . createRequestHandlerContext ( ) , licensing : licensingMock . createRequestHandlerContext ( ) , } ; } const xpackMocks = { createRequestHandlerContext : createCoreRequestHandlerContextMock , } ; describe ( 'GET remote clusters' , ( ) => { let handler ; let mockRouteDependencies ; let mockContext ; let scopedClusterClientMock ; let remoteInfoMockFn ; let getSettingsMockFn ; const createMockRequest = ( ) => httpServerMock . createKibanaRequest ( { method : 'get' , path : API_BASE_PATH , headers : { authorization : 'foo' } , } ) ; const createMockRouteDependencies = ( ) => ( { router : httpServiceMock . createRouter ( ) , getLicenseStatus : ( ) => ( { valid : true } ) , lib : { handleEsError , } , config : { isCloudEnabled : false , } , } ) ; beforeEach ( ( ) => { mockContext = xpackMocks . createRequestHandlerContext ( ) ; scopedClusterClientMock = mockContext . core . elasticsearch . client ; remoteInfoMockFn = scopedClusterClientMock . asCurrentUser . cluster . remoteInfo ; getSettingsMockFn = scopedClusterClientMock . asCurrentUser . cluster . getSettings ; mockRouteDependencies = createMockRouteDependencies ( ) ; register ( mockRouteDependencies ) ; const [ [ , handlerFn ] ] = mockRouteDependencies . router . get . mock . calls ; handler = handlerFn ; } ) ; describe ( 'success' , ( ) => { test ( 'returns remote clusters' , async ( ) => { getSettingsMockFn . mockResolvedValueOnce ( createApiResponse ( { body : { persistent : { cluster : { remote : { test : { seeds : [ '127.0.0.1:9300' ] , skip_unavailable : false , mode : 'sniff' , } , } , } , } , transient : { } , } , } ) ) ; remoteInfoMockFn . mockResolvedValueOnce ( createApiResponse ( { body : { test : { connected : true , mode : 'sniff' , seeds : [ '127.0.0.1:9300' ] , num_nodes_connected : 1 , max_connections_per_cluster : 3 , initial_connect_timeout : '30s' , skip_unavailable : false , } , } , } ) ) ; const mockRequest = createMockRequest ( ) ; const response = await handler ( mockContext , mockRequest , kibanaResponseFactory ) ; expect ( response . status ) . toBe ( 200 ) ; expect ( response . payload ) . toEqual ( [ { name : 'test' , seeds : [ '127.0.0.1:9300' ] , isConnected : true , connectedNodesCount : 1 , maxConnectionsPerCluster : 3 , initialConnectTimeout : '30s' , skipUnavailable : false , isConfiguredByNode : false , mode : 'sniff' , } , ] ) ; expect ( getSettingsMockFn ) . toHaveBeenCalledWith ( ) ; expect ( remoteInfoMockFn ) . toHaveBeenCalledWith ( ) ; } ) ; test ( 'returns an empty array when ES responds with an empty object' , async ( ) => { getSettingsMockFn . mockResolvedValueOnce ( createApiResponse ( { body : { } as any } ) ) ; remoteInfoMockFn . mockResolvedValueOnce ( createApiResponse ( { body : { } } ) ) ; const mockRequest = createMockRequest ( ) ; const response = await handler ( mockContext , mockRequest , kibanaResponseFactory ) ; expect ( response . status ) . toBe ( 200 ) ; expect ( response . payload ) . toEqual ( [ ] ) ; expect ( getSettingsMockFn ) . toHaveBeenCalledWith ( ) ; expect ( remoteInfoMockFn ) . toHaveBeenCalledWith ( ) ; } ) ; } ) ; describe ( 'failure' , ( ) => { const error = new errors . ResponseError ( { body : { message : 'test error' } , statusCode : 406 , headers : { } , meta : { } as any , warnings : [ ] , } ) ; test ( 'returns an error if failure to get cluster settings' , async ( ) => { getSettingsMockFn . mockRejectedValueOnce ( error ) ; const mockRequest = createMockRequest ( ) ; const response = await handler ( mockContext , mockRequest , kibanaResponseFactory ) ; expect ( response . status ) . toBe ( 406 ) ; expect ( response . payload ) . toEqual ( { attributes : { causes : undefined , error : undefined , } , message : '{"message":"test error"}' , } ) ; expect ( getSettingsMockFn ) . toHaveBeenCalled ( ) ; expect ( remoteInfoMockFn ) . not . toHaveBeenCalled ( ) ; } ) ; test ( 'returns an error if failure to get cluster remote info' , async ( ) => { getSettingsMockFn . mockResolvedValueOnce ( createApiResponse ( { body : { persistent : { cluster : { remote : { test : { seeds : [ '127.0.0.1:9300' ] , skip_unavailable : false , mode : 'sniff' , } , } , } , } , transient : { } , } , } ) ) ; remoteInfoMockFn . mockRejectedValueOnce ( error ) ; const mockRequest = httpServerMock . createKibanaRequest ( { method : 'get' , path : API_BASE_PATH , headers : { authorization : 'foo' } , } ) ; const response = await handler ( mockContext , mockRequest , kibanaResponseFactory ) ; expect ( response . status ) . toBe ( 406 ) ; expect ( getSettingsMockFn ) . toHaveBeenCalledWith ( ) ; expect ( remoteInfoMockFn ) . toHaveBeenCalledWith ( ) ; } ) ; } ) ; } ) ;