import { testing , PartialApplicationConfig } from 'lisk-framework' ; import axios from 'axios' ; import { when } from 'jest-when' ; import { HTTPAPIPlugin } from '../../src/http_api_plugin' ; import { getURL , callNetwork } from './utils/application' ; describe ( 'Peers endpoint' , ( ) => { let appEnv ; const label = 'peers_http_functional' ; beforeAll ( async ( ) => { const rootPath = '~/.lisk/http-plugin' ; const config = { rootPath , label , } as PartialApplicationConfig ; appEnv = testing . createDefaultApplicationEnv ( { config , plugins : [ HTTPAPIPlugin ] , } ) ; await appEnv . startApplication ( ) ; } ) ; afterAll ( async ( ) => { jest . spyOn ( process , 'exit' ) . mockImplementation ( ( ( ) => { } ) as never ) ; await appEnv . stopApplication ( ) ; } ) ; describe ( '/api/peers' , ( ) => { describe ( '400 - Invalid query values' , ( ) => { it ( 'should respond with 400 and error message when passed incorrect limit value' , async ( ) => { const { response , status } = await callNetwork ( axios . get ( getURL ( '/api/peers?limit=123xy' ) ) ) ; expect ( status ) . toBe ( 400 ) ; expect ( response ) . toEqual ( { errors : [ { message : 'Lisk validator found 1 error[s]:\nProperty \'.limit\' must match format "uint32"' , } , ] , } ) ; } ) ; it ( 'should respond with 400 and error message when passed incorrect offset value' , async ( ) => { const { response , status } = await callNetwork ( axios . get ( getURL ( '/api/peers?offset=123xy' ) ) ) ; expect ( status ) . toBe ( 400 ) ; expect ( response ) . toEqual ( { errors : [ { message : 'Lisk validator found 1 error[s]:\nProperty \'.offset\' must match format "uint32"' , } , ] , } ) ; } ) ; } ) ; describe ( '500 - Some internal operation fails to process' , ( ) => { it ( 'should throw 500 error when channel.invoke fails' , async ( ) => { appEnv . application [ '_controller' ] [ '_inMemoryPlugins' ] [ 'httpApi' ] [ 'plugin' ] . _channel . invoke = jest . fn ( ) ; when ( appEnv . application [ '_controller' ] [ '_inMemoryPlugins' ] [ 'httpApi' ] [ 'plugin' ] . _channel . invoke ) . calledWith ( 'app:getConnectedPeers' ) . mockRejectedValue ( new Error ( 'test' ) as never ) ; const { response , status } = await callNetwork ( axios . get ( getURL ( '/api/peers' ) ) ) ; expect ( status ) . toBe ( 500 ) ; expect ( response . errors ) . toHaveLength ( 1 ) ; expect ( response . errors [ 0 ] . message ) . toEqual ( 'test' ) ; } ) ; } ) ; } ) ; } ) ;