|
2 | 2 | *SPDX-License-Identifier: Apache-2.0
|
3 | 3 | */
|
4 | 4 |
|
5 |
| -/* tslint:disable:no-unused-expression */ |
6 |
| -import { expect, use } from 'chai'; |
7 |
| -import chaiAsPromised from 'chai-as-promised'; |
8 |
| -import sinonChai from 'sinon-chai'; |
| 5 | +import { expect } from './expect'; |
9 | 6 | import { FabricConfig } from '../platform/fabric/FabricConfig';
|
10 | 7 |
|
11 |
| -use(chaiAsPromised); |
12 |
| -use(sinonChai); |
13 |
| - |
14 |
| -// export const expect = chai.expect |
15 | 8 | import proxyquire from 'proxyquire';
|
16 | 9 | import sinon from 'sinon';
|
17 |
| -import { assert } from 'console'; |
18 |
| -// import { expect } from './expect' |
| 10 | +import { FabricGateway } from '../platform/fabric/gateway/FabricGateway'; |
19 | 11 |
|
20 |
| -describe('setupDiscoveryRequest', () => { |
21 |
| - it('should return without error', async () => { |
22 |
| - const stubSign = sinon.stub(); |
23 |
| - const { FabricGateway } = proxyquire |
24 |
| - .noCallThru() |
25 |
| - .load('../platform/fabric/gateway/FabricGateway', { |
26 |
| - 'fabric-common': { |
27 |
| - DiscoveryService: function() { |
| 12 | +// DiscoveryService (this.ds) |
| 13 | +const stubSign = sinon.stub(); |
| 14 | +const stubGetDiscoveryResults = sinon.stub(); |
| 15 | +const stubClose = sinon.stub(); |
| 16 | + |
| 17 | +// Discoverer |
| 18 | +const stubConnect = sinon.stub(); |
| 19 | + |
| 20 | +// logger |
| 21 | +const stubError = sinon.stub(); |
| 22 | +const stubInfo = sinon.stub(); |
| 23 | + |
| 24 | +// Client |
| 25 | +const stubSetTlsClientCertAndKey = sinon.stub(); |
| 26 | +const stubNewEndpoint = sinon.stub(); |
| 27 | + |
| 28 | +function getFabricGatewayInstance() { |
| 29 | + const { FabricGateway } = proxyquire |
| 30 | + .noCallThru() |
| 31 | + .load('../platform/fabric/gateway/FabricGateway', { |
| 32 | + 'fabric-common': { |
| 33 | + DiscoveryService: function() { |
| 34 | + return { |
| 35 | + build: sinon.stub(), |
| 36 | + sign: stubSign, |
| 37 | + send: sinon.stub(), |
| 38 | + getDiscoveryResults: stubGetDiscoveryResults, |
| 39 | + close: stubClose |
| 40 | + }; |
| 41 | + }, |
| 42 | + Client: function() { |
| 43 | + return { |
| 44 | + setTlsClientCertAndKey: stubSetTlsClientCertAndKey, |
| 45 | + newEndpoint: stubNewEndpoint |
| 46 | + }; |
| 47 | + }, |
| 48 | + Discoverer: function() { |
| 49 | + return { |
| 50 | + connect: stubConnect |
| 51 | + }; |
| 52 | + } |
| 53 | + }, |
| 54 | + '../../../common/helper': { |
| 55 | + helper: { |
| 56 | + getLogger: function() { |
28 | 57 | return {
|
29 |
| - build: sinon.stub(), |
30 |
| - sign: stubSign |
| 58 | + error: stubError, |
| 59 | + info: stubInfo |
31 | 60 | };
|
32 | 61 | }
|
33 |
| - }, |
34 |
| - '../../../common/helper': { |
35 |
| - helper: { |
36 |
| - getLogger: function() {} |
37 |
| - } |
38 | 62 | }
|
39 |
| - }); |
40 |
| - |
41 |
| - const config = new FabricConfig(); |
42 |
| - config.initialize('first-network', { |
43 |
| - name: 'My first network', |
44 |
| - profile: './connection-profile/first-network.json' |
| 63 | + } |
45 | 64 | });
|
| 65 | + const config = new FabricConfig(); |
| 66 | + config.initialize('first-network', { |
| 67 | + name: 'My first network', |
| 68 | + profile: './connection-profile/first-network.json' |
| 69 | + }); |
| 70 | + sinon.stub(config, 'getPeerTlsCACertsPem'); |
| 71 | + |
| 72 | + const gw = new FabricGateway(config); |
| 73 | + gw.clientTlsIdentity = { |
| 74 | + credentials: { |
| 75 | + certificate: 'abc', |
| 76 | + privateKey: 'def' |
| 77 | + }, |
| 78 | + mspId: 'org1', |
| 79 | + type: 'X.509' |
| 80 | + }; |
| 81 | + |
| 82 | + const stubGetNetwork = sinon.stub(gw.gateway, 'getNetwork'); |
| 83 | + const stubGetChannel = sinon.stub(); |
| 84 | + stubGetChannel.returns({}); |
| 85 | + stubGetNetwork.returns(Promise.resolve({ getChannel: stubGetChannel })); |
| 86 | + |
| 87 | + return gw; |
| 88 | +} |
46 | 89 |
|
47 |
| - const gw = new FabricGateway(config); |
48 |
| - const stubGetNetwork = sinon.stub(gw.gateway, 'getNetwork'); |
49 |
| - const stubGetChannel = sinon.stub(); |
50 |
| - stubGetChannel.returns({}); |
51 |
| - stubGetNetwork.returns(Promise.resolve({ getChannel: stubGetChannel })); |
| 90 | +function resetAllStubs() { |
| 91 | + // DiscoveryService (this.ds) |
| 92 | + stubSign.reset(); |
| 93 | + stubGetDiscoveryResults.reset(); |
| 94 | + stubClose.reset(); |
| 95 | + |
| 96 | + // Discoverer |
| 97 | + stubConnect.reset(); |
| 98 | + |
| 99 | + // logger |
| 100 | + stubError.reset(); |
| 101 | + stubInfo.reset(); |
| 102 | + |
| 103 | + // Client |
| 104 | + stubSetTlsClientCertAndKey.reset(); |
| 105 | + stubNewEndpoint.reset(); |
| 106 | +} |
| 107 | + |
| 108 | +describe('setupDiscoveryRequest', () => { |
| 109 | + let gw: FabricGateway; |
| 110 | + |
| 111 | + before(() => { |
| 112 | + gw = getFabricGatewayInstance(); |
| 113 | + }); |
| 114 | + |
| 115 | + beforeEach(() => { |
| 116 | + resetAllStubs(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should return without error', async () => { |
52 | 120 | await gw.setupDiscoveryRequest('testChannel');
|
53 | 121 | expect(stubSign.calledOnce).to.be.equal(true);
|
54 | 122 | });
|
| 123 | + |
| 124 | + it('should throw error if fail to sign', async () => { |
| 125 | + stubSign.throws(); |
| 126 | + await gw.setupDiscoveryRequest('testChannel'); |
| 127 | + expect(stubError.calledOnce).to.be.equal(true); |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +describe('getDiscoveryServiceTarget', () => { |
| 132 | + let gw: FabricGateway; |
| 133 | + |
| 134 | + before(() => { |
| 135 | + gw = getFabricGatewayInstance(); |
| 136 | + }); |
| 137 | + |
| 138 | + beforeEach(() => { |
| 139 | + resetAllStubs(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should return without error', async () => { |
| 143 | + const targetArray = await gw.getDiscoveryServiceTarget(); |
| 144 | + expect(stubSetTlsClientCertAndKey.calledOnceWith('abc', 'def')).to.be.equal( |
| 145 | + true |
| 146 | + ); |
| 147 | + expect(stubNewEndpoint.calledOnce).to.be.equal(true); |
| 148 | + expect(stubConnect.calledOnce).to.be.equal(true); |
| 149 | + expect(targetArray.length).to.be.equal(1); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should return without error when not assigned client TLS config', async () => { |
| 153 | + gw.clientTlsIdentity = undefined; |
| 154 | + const targetArray = await gw.getDiscoveryServiceTarget(); |
| 155 | + expect(stubSetTlsClientCertAndKey.calledOnceWith()).to.be.equal(true); |
| 156 | + expect(stubNewEndpoint.calledOnce).to.be.equal(true); |
| 157 | + expect(stubConnect.calledOnce).to.be.equal(true); |
| 158 | + expect(targetArray.length).to.be.equal(1); |
| 159 | + }); |
| 160 | +}); |
| 161 | + |
| 162 | +describe('sendDiscoveryRequest', () => { |
| 163 | + let gw: FabricGateway; |
| 164 | + |
| 165 | + before(async () => { |
| 166 | + gw = getFabricGatewayInstance(); |
| 167 | + await gw.setupDiscoveryRequest('testChannel'); |
| 168 | + }); |
| 169 | + |
| 170 | + beforeEach(() => { |
| 171 | + resetAllStubs(); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should return without error', async () => { |
| 175 | + stubGetDiscoveryResults.returns(Promise.resolve()); |
| 176 | + await gw.sendDiscoveryRequest(); |
| 177 | + expect(stubError.called).be.equal(false); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should throw error when failed to call getDiscoveryResults()', async () => { |
| 181 | + stubGetDiscoveryResults.throws(); |
| 182 | + await gw.sendDiscoveryRequest(); |
| 183 | + expect(stubError.called).be.equal(true); |
| 184 | + expect(stubClose.calledOnce).be.equal(true); |
| 185 | + }); |
| 186 | +}); |
| 187 | + |
| 188 | +describe('getDiscoveryResult', () => { |
| 189 | + let gw: FabricGateway; |
| 190 | + let stubSetupDiscoveryRequest: sinon.SinonStub; |
| 191 | + |
| 192 | + before(async () => { |
| 193 | + gw = getFabricGatewayInstance(); |
| 194 | + await gw.setupDiscoveryRequest('testChannel'); |
| 195 | + stubSetupDiscoveryRequest = sinon.stub(gw, 'setupDiscoveryRequest'); |
| 196 | + }); |
| 197 | + |
| 198 | + beforeEach(() => { |
| 199 | + resetAllStubs(); |
| 200 | + stubSetupDiscoveryRequest.reset(); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should return without error', async () => { |
| 204 | + await gw.getDiscoveryResult('testChannel'); |
| 205 | + expect(stubSetupDiscoveryRequest.calledOnce).be.equal(false); |
| 206 | + }); |
| 207 | + |
| 208 | + it('should return without error if discover service has not been allocated yet', async () => { |
| 209 | + gw.ds = null; |
| 210 | + await gw.getDiscoveryResult('testChannel'); |
| 211 | + expect(stubSetupDiscoveryRequest.calledOnce).be.equal(true); |
| 212 | + }); |
55 | 213 | });
|
0 commit comments