class ClientBase: """Base class for a Hyperledger Fabric client.""" def __init__(self, profile, channel_name, org_name, peer_name, user_name): self.client = Client(profile) self._channel_name = channel_name self._org_name = org_name self._peer_name = peer_name self._user_name = user_name self._user = self.client.get_user(self._org_name, self._user_name) endpoint = self.client.get_net_info('peers', self._peer_name, 'url') tlscert = self.client.get_net_info('peers', self._peer_name, 'tlsCACerts', 'path') loop = asyncio.get_event_loop() peer = create_peer(endpoint=endpoint, tls_cacerts=tlscert) loop.run_until_complete( self.client.init_with_discovery(self._user, peer, self._channel_name)) self._channel = self.client.new_channel(self._channel_name) @property def channel_name(self): return self._channel_name @property def channel(self): return self._channel @property def org_name(self): return self._org_name @property def peer_name(self): return self._peer_name @property def user_name(self): return self._user_name @property def user(self): return self._user
class ClientTest(unittest.TestCase): def setUp(self): self.client = Client('test/fixtures/network.json') def test_create_client(self): self.client.crypto_suite = 'my_crypto_suite' self.assertEqual(self.client.crypto_suite, 'my_crypto_suite') self.client.tx_context = 'my_tx_context' self.assertEqual(self.client.tx_context, 'my_tx_context') self.client.user_context = 'my_user_context' self.assertEqual(self.client.user_context, 'my_user_context') self.client.state_store = 'my_state_store' self.assertEqual(self.client.state_store, 'my_state_store') def test_new_channel(self): test_channel = self.client.new_channel('test') self.assertEqual(test_channel, self.client.get_channel('test')) def test_get_channel(self): test_channel = self.client.new_channel('test') self.assertEqual(test_channel, self.client.get_channel('test')) no_chain = self.client.get_channel('test1') self.assertIsNone(no_chain) def test_create_channel_missing_signatures(self): request = {} request['config'] = 'config' request['channel_name'] = 'channel_name' request['orderer'] = 'orderer' request['tx_id'] = 'tx_id' request['nonce'] = 'nonce' with self.assertRaises(ValueError): loop.run_until_complete(self.client._create_or_update_channel( request)) def test_create_channel_not_list_of_signatures(self): request = {} request['config'] = 'config' request['signatures'] = 'signatures' request['channel_name'] = 'channel_name' request['orderer'] = 'orderer' request['tx_id'] = 'tx_id' request['nonce'] = 'nonce' with self.assertRaises(ValueError): loop.run_until_complete(self.client._create_or_update_channel( request)) def test_create_channel_missing_tx_id(self): request = {} request['config'] = 'config' request['channel_name'] = 'channel_name' request['orderer'] = 'orderer' request['nonce'] = 'nonce' with self.assertRaises(ValueError): loop.run_until_complete(self.client._create_or_update_channel( request)) def test_create_channel_missing_orderer(self): request = {} request['config'] = 'config' request['channel_name'] = 'channel_name' request['tx_id'] = 'tx_id' request['nonce'] = 'nonce' with self.assertRaises(ValueError): loop.run_until_complete(self.client._create_or_update_channel( request)) def test_create_channel_missing_channel_name(self): request = { 'config': 'config', 'orderer': 'orderer', 'tx_id': 'tx_id', 'nonce': 'nonce', } with self.assertRaises(ValueError): loop.run_until_complete(self.client._create_or_update_channel( request)) def test_export_network_profile(self): network_info = { "organizations": { }, "orderers": { }, "peers": { }, "certificateAuthorities": { }, 'a': { 'b': 'a.b' } } self.client.network_info = network_info self.client.export_net_profile('test-export.json') self.client.network_info = dict() self.client.init_with_net_profile('test-export.json') self.assertEqual(network_info, self.client.network_info) self.assertEqual('a.b', self.client.get_net_info('a', 'b')) def test_init_with_net_profile(self): self.client.init_with_net_profile('test/fixtures/network.json') self.assertEqual(self.client.get_net_info('organizations', 'orderer.example.com', 'mspid'), 'OrdererMSP', "profile not match")