import MockAdapter from 'axios-mock-adapter' ; import { MicrofabClient , MicrofabComponent , isIdentity , isOrderer , isPeer , isGateway , isCertificateAuthority } from '../../src/environments/MicrofabClient' ; import * as chai from 'chai' ; import * as chaiAsPromised from 'chai-as-promised' ; chai . should ( ) ; chai . use ( chaiAsPromised ) ; describe ( 'MicrofabClient' , ( ) => { let client ; let mockAxios ; beforeEach ( ( ) => { client = new MicrofabClient ( 'http://console.microfab.example.org:8080' ) ; mockAxios = new MockAdapter ( client [ 'axios' ] ) ; mockAxios . onGet ( 'http://console.microfab.example.org:8080/ak/api/v1/components' ) . reply ( 200 , [ { id : 'ordereradmin' , display_name : 'Orderer Admin' , type : 'identity' , cert : 'LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUIwekNDQVhxZ0F3SUJBZ0lRTXNjMWxWemUzSzlZQ3IrNktieVJ5akFLQmdncWhrak9QUVFEQWpBVk1STXcKRVFZRFZRUURFd3BQY21SbGNtVnlJRU5CTUI0WERUSXdNRFV4TkRFd05EY3dNRm9YRFRNd01EVXhNakV3TkRjdwpNRm93S0RFT01Bd0dBMVVFQ3hNRllXUnRhVzR4RmpBVUJnTlZCQU1URFU5eVpHVnlaWElnUVdSdGFXNHdXVEFUCkJnY3Foa2pPUFFJQkJnZ3Foa2pPUFFNQkJ3TkNBQVR0dkhyZDF5cWJNZWFDbHR5cXlNeVNESm5Va0FZWTYzalQKMkxicjBqdjR5NXhYdlF6SytOaDMzMnNZYllqYXpSM3JweG1xRVQvdVBoSFQrTk9vNGxSNG80R1lNSUdWTUE0RwpBMVVkRHdFQi93UUVBd0lGb0RBZEJnTlZIU1VFRmpBVUJnZ3JCZ0VGQlFjREFnWUlLd1lCQlFVSEF3RXdEQVlEClZSMFRBUUgvQkFJd0FEQXBCZ05WSFE0RUlnUWdiNkJYRlFKRFRocmc3WmdFVFAyM3RXTlkxZjlOMEdESVlZVGIKZ25obFdFNHdLd1lEVlIwakJDUXdJb0FnNEI1c2Fqc2FWc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UXZCbGlmTUJrb1h5RUp3cmQ0d2ZTM21zVwotLS0tLUVORCBQUklWQVRFIEtFWS0tLS0tCg==' , msp_id : 'Org1MSP' , wallet : 'Org1' } , { id : 'orderer' , display_name : 'Orderer' , type : 'fabric-orderer' , api_url : 'grpc://localhost:8080' , api_options : { 'grpc.default_authority' : 'orderer-api.127-0-0-1.nip.io:8080' , 'grpc.ssl_target_name_override' : 'orderer-api.127-0-0-1.nip.io:8080' } , operations_url : 'http://localhost:8080' , operations_options : { 'grpc.default_authority' : 'orderer-operations.127-0-0-1.nip.io:8080' , 'grpc.ssl_target_name_override' : 'orderer-operations.127-0-0-1.nip.io:8080' } , msp_id : 'OrdererMSP' , wallet : 'Orderer' , identity : 'Orderer Admin' } , { id : 'org1peer' , display_name : 'Org1 Peer' , type : 'fabric-peer' , api_url : 'grpc://localhost:8080' , api_options : { 'grpc.default_authority' : 'org1peer-api.127-0-0-1.nip.io:8080' , 'grpc.ssl_target_name_override' : 'org1peer-api.127-0-0-1.nip.io:8080' } , chaincode_url : 'grpc://localhost:8080' , chaincode_options : { 'grpc.default_authority' : 'org1peer-chaincode.127-0-0-1.nip.io:8080' , 'grpc.ssl_target_name_override' : 'org1peer-chaincode.127-0-0-1.nip.io:8080' } , operations_url : 'http://localhost:8080' , operations_options : { 'grpc.default_authority' : 'org1peer-operations.127-0-0-1.nip.io:8080' , 'grpc.ssl_target_name_override' : 'org1peer-operations.127-0-0-1.nip.io:8080' } , msp_id : 'Org1MSP' , wallet : 'Org1' , identity : 'Org1 Admin' } , { id : 'org1ca' , display_name : 'Org1 CA' , type : 'fabric-ca' , api_url : 'http://localhost:8080' , api_options : { 'grpc.default_authority' : 'org1ca-api.127-0-0-1.nip.io:8080' , 'grpc.ssl_target_name_override' : 'org1ca-api.127-0-0-1.nip.io:8080' } , operations_url : 'http://localhost:8080' , operations_options : { 'grpc.default_authority' : 'org1ca-operations.127-0-0-1.nip.io:8080' , 'grpc.ssl_target_name_override' : 'org1ca-operations.127-0-0-1.nip.io:8080' } , msp_id : 'Org1MSP' , wallet : 'Org1' , identity : 'Org1 CA Admin' } , { client : { connection : { timeout : { orderer : '300' , peer : { endorser : '300' } } } , organization : 'Org1' } , display_name : 'Org1 Gateway' , id : 'org1gateway' , name : 'Org1 Gateway' , organizations : { Org1 : { mspid : 'Org1MSP' , peers : [ 'org1peer-api.127-0-0-1.nip.io:8080' ] } } , peers : { 'org1peer-api.127-0-0-1.nip.io:8080' : { grpcOptions : { 'grpc.default_authority' : 'org1peer-api.127-0-0-1.nip.io:8080' , 'grpc.ssl_target_name_override' : 'org1peer-api.127-0-0-1.nip.io:8080' } , url : 'grpc://localhost:8080' } } , type : 'gateway' , version : '1.0' , wallet : 'Org1' } ] ) ; mockAxios . onGet ( 'http://console.microfab.example.org:8080/ak/api/v1/components/org1peer' ) . reply ( 200 , { id : 'org1peer' , display_name : 'Org1 Peer' , type : 'fabric-peer' , api_url : 'grpc://localhost:8080' , api_options : { 'grpc.default_authority' : 'org1peer-api.127-0-0-1.nip.io:8080' , 'grpc.ssl_target_name_override' : 'org1peer-api.127-0-0-1.nip.io:8080' } , chaincode_url : 'grpc://localhost:8080' , chaincode_options : { 'grpc.default_authority' : 'org1peer-chaincode.127-0-0-1.nip.io:8080' , 'grpc.ssl_target_name_override' : 'org1peer-chaincode.127-0-0-1.nip.io:8080' } , operations_url : 'http://localhost:8080' , operations_options : { 'grpc.default_authority' : 'org1peer-operations.127-0-0-1.nip.io:8080' , 'grpc.ssl_target_name_override' : 'org1peer-operations.127-0-0-1.nip.io:8080' } , msp_id : 'Org1MSP' , wallet : 'Org1' , identity : 'Org1 Admin' } ) ; } ) ; afterEach ( ( ) => { mockAxios . restore ( ) ; } ) ; describe ( '#isAlive' , ( ) => { it ( 'should return true if alive' , async ( ) => { mockAxios . onGet ( 'http://console.microfab.example.org:8080/ak/api/v1/health' ) . reply ( 200 , { } ) ; await client . isAlive ( ) . should . eventually . be . true ; } ) ; it ( 'should return false if not alive (HTTP status code)' , async ( ) => { mockAxios . onGet ( 'http://console.microfab.example.org:8080/ak/api/v1/health' ) . reply ( 500 , { error : 'internal server error' } ) ; await client . isAlive ( ) . should . eventually . be . false ; } ) ; it ( 'should return false if not alive (network error)' , async ( ) => { mockAxios . onGet ( 'http://console.microfab.example.org:8080/ak/api/v1/health' ) . networkError ( ) ; await client . isAlive ( ) . should . eventually . be . false ; } ) ; it ( 'should return false if not alive (timeout)' , async ( ) => { mockAxios . onGet ( 'http://console.microfab.example.org:8080/ak/api/v1/health' ) . reply ( ( ) => { return new Promise ( ( resolve ) => { setTimeout ( ( ) => { resolve ( [ 200 , { } ] ) ; } , 31000 ) ; } ) ; } ) ; await client . isAlive ( ) . should . eventually . be . false ; } ) . timeout ( 35000 ) ; } ) ; describe ( '#getComponents' , ( ) => { it ( 'should get the list of components' , async ( ) => { const components = await client . getComponents ( ) ; components . should . have . lengthOf ( 7 ) ; isIdentity ( components [ 0 ] ) . should . be . true ; components [ 0 ] . display_name . should . equal ( 'Orderer Admin' ) ; isIdentity ( components [ 1 ] ) . should . be . true ; components [ 1 ] . display_name . should . equal ( 'Org1 Admin' ) ; isIdentity ( components [ 2 ] ) . should . be . true ; components [ 2 ] . display_name . should . equal ( 'Org1 CA Admin' ) ; isOrderer ( components [ 3 ] ) . should . be . true ; components [ 3 ] . display_name . should . equal ( 'Orderer' ) ; isPeer ( components [ 4 ] ) . should . be . true ; components [ 4 ] . display_name . should . equal ( 'Org1 Peer' ) ; isCertificateAuthority ( components [ 5 ] ) . should . be . true ; components [ 5 ] . display_name . should . equal ( 'Org1 CA' ) ; isGateway ( components [ 6 ] ) . should . be . true ; components [ 6 ] . display_name . should . equal ( 'Org1 Gateway' ) ; } ) ; } ) ; describe ( '#getComponent' , ( ) => { it ( 'should get the component' , async ( ) => { const component = await client . getComponent ( 'org1peer' ) ; isPeer ( component ) . should . be . true ; component . display_name . should . equal ( 'Org1 Peer' ) ; } ) ; } ) ; } ) ;