import * as sinon from 'sinon' ; import * as chai from 'chai' ; import * as chaiAsPromised from 'chai-as-promised' ; import { FabricNode , FabricNodeType } from '../../src/fabricModel/FabricNode' ; chai . use ( chaiAsPromised ) ; describe ( 'FabricNode' , ( ) => { let mySandBox ; let peerNode ; let caNode ; let ordererNode ; beforeEach ( async ( ) => { mySandBox = sinon . createSandbox ( ) ; peerNode = FabricNode . newPeer ( 'peer0.org1.example.com' , 'peer0.org1.example.com' , 'grpc://localhost:7051' , 'Org1' , 'admin' , 'Org1MSP' ) ; caNode = FabricNode . newCertificateAuthority ( 'ca.org1.example.com' , 'ca.org1.example.com' , 'http://localhost:7054' , 'ca_name' , 'Org1' , 'admin' , 'Org1MSP' , 'admin' , 'adminpw' ) ; ordererNode = FabricNode . newOrderer ( 'orderer.example.com' , 'orderer.example.com' , 'grpc://localhost:7050' , 'Org1' , 'admin' , 'OrdererMSP' , 'myCluster' ) ; } ) ; afterEach ( ( ) => { mySandBox . restore ( ) ; } ) ; it ( 'should create a peer' , ( ) => { peerNode . should . exist ; } ) ; it ( 'should create a secure peer' , ( ) => { const securePeerNode = FabricNode . newSecurePeer ( 'peer0.org1.example.com' , 'peer0.org1.example.com' , 'grpc://localhost:7051' , 'myPem' , 'Org1' , 'admin' , 'Org1MSP' ) ; securePeerNode . should . exist ; } ) ; it ( 'should create a orderer' , ( ) => { ordererNode . should . exist ; } ) ; it ( 'should create a secure orderer' , ( ) => { const secureOrdererNode = FabricNode . newSecureOrderer ( 'orderer.example.com' , 'orderer.example.com' , 'grpc://localhost:7050' , 'myPem' , 'Org1' , 'admin' , 'OrdererMSP' , 'myCluster' ) ; secureOrdererNode . should . exist ; } ) ; it ( 'should create a couchdb' , ( ) => { const couchNode = FabricNode . newCouchDB ( 'couchdb.example.com' , 'couchdb.example.com' , 'grpc://localhost:7064' ) ; couchNode . should . exist ; } ) ; it ( 'should create a ca' , ( ) => { caNode . should . exist ; } ) ; it ( 'should create a secure ca' , ( ) => { const secureCaNode = FabricNode . newSecureCertificateAuthority ( 'ca.org1.example.com' , 'ca.org1.example.com' , 'http://localhost:7054' , 'ca_name' , 'myPem' , 'Org1' , 'admin' , 'Org1MSP' , 'admin' , 'adminpw' ) ; secureCaNode . should . exist ; } ) ; describe ( 'validateNode' , ( ) => { it ( 'should not throw an error if peer node is valid' , ( ) => { try { FabricNode . validateNode ( peerNode ) ; } catch ( error ) { throw new Error ( 'should not get here ' + error . message ) ; } } ) ; it ( 'should not throw an error if orderer node is valid' , ( ) => { try { FabricNode . validateNode ( ordererNode ) ; } catch ( error ) { throw new Error ( 'should not get here ' + error . message ) ; } } ) ; it ( 'should not throw an error if ca node is valid' , ( ) => { try { FabricNode . validateNode ( caNode ) ; } catch ( error ) { throw new Error ( 'should not get here ' + error . message ) ; } } ) ; it ( 'should throw an error if doesn\'t have a name' , ( ) => { try { peerNode . name = undefined ; FabricNode . validateNode ( peerNode ) ; } catch ( error ) { error . message . should . equal ( 'A node should have a name property' ) ; } } ) ; it ( 'should throw an error if doesn\'t have a type' , ( ) => { try { peerNode . type = undefined ; FabricNode . validateNode ( peerNode ) ; } catch ( error ) { error . message . should . equal ( 'A node should have a type property' ) ; } } ) ; it ( 'should throw an error if doesn\'t have a api_url' , ( ) => { try { peerNode . api_url = undefined ; FabricNode . validateNode ( peerNode ) ; } catch ( error ) { error . message . should . equal ( 'A node should have a api_url property' ) ; } } ) ; it ( 'should throw an error if a peer doesn\'t have a msp_id' , ( ) => { try { peerNode . msp_id = undefined ; FabricNode . validateNode ( peerNode ) ; } catch ( error ) { error . message . should . equal ( `A ${ peerNode . type } node should have a msp_id property` ) ; } } ) ; it ( 'should throw an error if a orderer doesn\'t have a msp_id' , ( ) => { try { ordererNode . msp_id = undefined ; FabricNode . validateNode ( ordererNode ) ; } catch ( error ) { error . message . should . equal ( `A ${ ordererNode . type } node should have a msp_id property` ) ; } } ) ; it ( 'should throw an error if a ca doesn\'t have a ca_name' , ( ) => { try { caNode . ca_name = undefined ; FabricNode . validateNode ( caNode ) ; } catch ( error ) { error . message . should . equal ( `A ${ caNode . type } node should have a ca_name property` ) ; } } ) ; } ) ; describe ( 'pruneNode' , ( ) => { it ( 'should set hidden to false when undefined' , ( ) => { try { peerNode . hidden = undefined ; const prunedNode = FabricNode . pruneNode ( peerNode ) ; prunedNode . hidden . should . equal ( false ) ; } catch ( error ) { throw new Error ( 'should not get here ' + error . message ) ; } } ) ; it ( 'should set hidden, and cluster name if defined' , ( ) => { const fabricNode = FabricNode . pruneNode ( { short_name : 'myNode' , name : 'myNode' , type : FabricNodeType . ORDERER , api_url : 'grpc://localhost:1234' , hidden : true , cluster_name : 'myCluster' } ) ; fabricNode . hidden . should . equal ( true ) ; fabricNode . cluster_name . should . equal ( 'myCluster' ) ; } ) ; it ( 'should set ca name if defined' , ( ) => { const fabricNode = FabricNode . pruneNode ( { short_name : 'myNode' , name : 'myNode' , type : FabricNodeType . CERTIFICATE_AUTHORITY , api_url : 'grpc://localhost:1234' , ca_name : 'myCA' } ) ; fabricNode . ca_name . should . equal ( 'myCA' ) ; } ) ; it ( 'should set pem and ssl target name override if set' , ( ) => { const fabricNode = FabricNode . pruneNode ( { short_name : 'myNode' , name : 'myNode' , type : FabricNodeType . PEER , api_url : 'grpc://localhost:1234' , pem : 'myCert' , ssl_target_name_override : 'myOverride' } ) ; fabricNode . pem . should . equal ( 'myCert' ) ; fabricNode . ssl_target_name_override . should . equal ( 'myOverride' ) ; } ) ; } ) ; } ) ;