def test_success_response(self): """ Simulates sending a command to the node and getting a success response. """ adapter = HttpAdapter('http://localhost:14265') payload = {'command': 'helloWorld'} expected_result = {'message': 'Hello, IOTA!'} mocked_response = create_http_response(json.dumps(expected_result)) mocked_sender = mock.Mock(return_value=mocked_response) # noinspection PyUnresolvedReferences with mock.patch.object(adapter, '_send_http_request', mocked_sender): result = adapter.send_request(payload) self.assertEqual(result, expected_result) # https://github.com/iotaledger/iota.lib.py/issues/84 mocked_sender.assert_called_once_with( headers = { 'Content-type': 'application/json', 'X-IOTA-API-Version': API_VERSION, }, payload = json.dumps(payload), url = adapter.node_url, )
def test_exception_response(self): """ Simulates sending a command to the node and getting an exception response. """ adapter = HttpAdapter('http://localhost:14265') error_message = 'java.lang.ArrayIndexOutOfBoundsException: 4' mocked_response = create_http_response( status = 500, content = json.dumps({ 'exception': error_message, 'duration': 16, }), ) mocked_sender = mock.Mock(return_value=mocked_response) # noinspection PyUnresolvedReferences with mock.patch.object(adapter, '_send_http_request', mocked_sender): with self.assertRaises(BadApiResponse) as context: adapter.send_request({'command': 'helloWorld'}) self.assertEqual( text_type(context.exception), '500 response from node: {error}'.format(error=error_message), )
def test_non_200_status(self): """ The node sends back a non-200 response that we don't know how to handle. """ adapter = HttpAdapter('http://localhost') decoded_response = {'message': 'Request limit exceeded.'} mocked_response = create_http_response( status = 429, content = json.dumps(decoded_response), ) mocked_sender = mock.Mock(return_value=mocked_response) # noinspection PyUnresolvedReferences with mock.patch.object(adapter, '_send_http_request', mocked_sender): with self.assertRaises(BadApiResponse) as context: adapter.send_request({'command': 'helloWorld'}) self.assertEqual( text_type(context.exception), '429 response from node: {decoded}'.format(decoded=decoded_response), )
def test_empty_response(self): """ The response is empty. """ adapter = HttpAdapter('http://localhost:14265') mocked_response = create_http_response('') mocked_sender = mock.Mock(return_value=mocked_response) # noinspection PyUnresolvedReferences with mock.patch.object(adapter, '_send_http_request', mocked_sender): with self.assertRaises(BadApiResponse) as context: adapter.send_request({'command': 'helloWorld'}) self.assertEqual( text_type(context.exception), 'Empty 200 response from node.', )
def test_non_json_response(self): """ The response is not JSON. """ adapter = HttpAdapter('http://localhost:14265') invalid_response = 'EHLO iotatoken.com' # Erm... mocked_response = create_http_response(invalid_response) mocked_sender = mock.Mock(return_value=mocked_response) # noinspection PyUnresolvedReferences with mock.patch.object(adapter, '_send_http_request', mocked_sender): with self.assertRaises(BadApiResponse) as context: adapter.send_request({'command': 'helloWorld'}) self.assertEqual( text_type(context.exception), 'Non-JSON 200 response from node: ' + invalid_response, )
def test_trytes_in_request(): """ Sending a request that includes trytes. """ adapter = HttpAdapter('http://localhost:14265') # Response is not important for this test; we just need to make # sure that the request is converted correctly. mocked_sender = mock.Mock(return_value=create_http_response('{}')) # noinspection PyUnresolvedReferences with mock.patch.object(adapter, '_send_http_request', mocked_sender): adapter.send_request({ 'command': 'helloWorld', 'trytes': [ TryteString(b'RBTC9D9DCDQAEASBYBCCKBFA'), TryteString( b'CCPCBDVC9DTCEAKDXC9D9DEARCWCPCBDVCTCEAHDWCTCEAKDCDFD9DSCSA', ), ], }) mocked_sender.assert_called_once_with( url = adapter.node_url, payload = json.dumps({ 'command': 'helloWorld', # Tryte sequences are converted to strings for transport. 'trytes': [ 'RBTC9D9DCDQAEASBYBCCKBFA', 'CCPCBDVC9DTCEAKDXC9D9DEARCWCPCBDVCTCEAHDWCTCEAKDCDFD9DSCSA', ], }), headers = { 'Content-type': 'application/json', 'X-IOTA-API-Version': API_VERSION, }, )
def test_non_object_response(self): """ The response is valid JSON, but it's not an object. """ adapter = HttpAdapter('http://localhost:14265') invalid_response = ['message', 'Hello, IOTA!'] mocked_response = create_http_response(json.dumps(invalid_response)) mocked_sender = mock.Mock(return_value=mocked_response) # noinspection PyUnresolvedReferences with mock.patch.object(adapter, '_send_http_request', mocked_sender): with self.assertRaises(BadApiResponse) as context: adapter.send_request({'command': 'helloWorld'}) self.assertEqual( text_type(context.exception), 'Malformed 200 response from node: {response!r}'.format( response = invalid_response, ), )
def test_configure_error_udp(self): """ UDP is not a valid protocol for ``HttpAdapter``. """ with self.assertRaises(InvalidUri): HttpAdapter.configure('udp://localhost:14265')
def test_configure_error_non_numeric_port(self): """ Attempting to configure HttpAdapter with non-numeric port. """ with self.assertRaises(InvalidUri): HttpAdapter.configure('http://localhost:iota/')
def test_configure_error_empty_host(self): """ Attempting to configure HttpAdapter with empty host. """ with self.assertRaises(InvalidUri): HttpAdapter.configure('http://:14265')
def test_configure_error_invalid_protocol(self): """ Attempting to configure HttpAdapter with unsupported protocol. """ with self.assertRaises(InvalidUri): HttpAdapter.configure('ftp://localhost:14265/')
def test_configure_error_missing_protocol(self): """ Forgetting to add the protocol to the URI. """ with self.assertRaises(InvalidUri): HttpAdapter.configure('localhost:14265')
def test_router_aliasing(self): """ The router will try to re-use existing adapter instances. """ wrapper1 = RoutingWrapper('http://localhost:14265') print_var_type_n_val(var001 = wrapper1, pointer = "#2345trezGH-(")#2345trezGH-( # Value: # # <iota.adapter.wrappers.RoutingWrapper object at 0x0000024C271B17B8> # Type: <class 'iota.adapter.wrappers.RoutingWrapper'> adapter_default = wrapper1.adapter print_var_type_n_val(var001 = adapter_default, pointer = "#SDFGnbvc1234098QSDnbe")#SDFGnbvc1234098QSDnbe # Value: # # <iota.adapter.HttpAdapter object at 0x0000024C25E7AB70> # Type: <class 'iota.adapter.HttpAdapter'> # The router will try to minimize the number of adapter instances # that it creates from URIs. wrapper1\ .add_route('alpha', 'http://localhost:14265')\ .add_route('bravo', 'http://localhost:14265')\ print_var_type_n_val(var001 = wrapper1, pointer = "#2345trezGH2345YTRE")#2345trezGH2345YTRE # Value: # # <iota.adapter.wrappers.RoutingWrapper object at 0x000001601AD6FD68> # Type: <class 'iota.adapter.wrappers.RoutingWrapper'> # Two routes with the same URI => same adapter instance. self.assertIs( wrapper1.get_adapter('bravo'), wrapper1.get_adapter('alpha'), ) # "127.0.0.1" != "localhost", so separate adapters created. wrapper1.add_route('charlie', 'http://127.0.0.1:14265') self.assertIsNot( wrapper1.get_adapter('charlie'), wrapper1.get_adapter('alpha'), ) # Providing an adapter instance bypasses the whole setup. wrapper1.add_route('delta', HttpAdapter('http://localhost:14265')) self.assertIsNot( wrapper1.get_adapter('delta'), wrapper1.get_adapter('alpha'), ) # The default adapter is always kept separate, even if it URI # matches a routing adapter. self.assertIsNot( wrapper1.get_adapter('foo'), wrapper1.get_adapter('alpha'), ) # Aliased adapters are not shared between routers. wrapper2 = RoutingWrapper(adapter_default) wrapper2.add_route('echo', 'http://localhost:14265') self.assertIsNot( wrapper2.get_adapter('echo'), wrapper1.get_adapter('alpha'), )
# iota_test_msgs.py # using pyota to read messages # https://iota.stackexchange.com/questions/1708/how-to-find-all-the-messages-of-the-txs-of-an-address/1788 # Imports the PyOTA library from iota import Iota from iota import Address, Transaction from iota.commands.extended.utils import find_transaction_objects from iota.adapter import HttpAdapter #trying this api = Iota(HttpAdapter('https://field.carriota.com:443')) ''' help(find_transaction_ojbects) find_transaction_objects(adapter, **kwargs) Finds transactions matching the specified criteria, fetches the corresponding trytes and converts them into Transaction objects. ''' ''' iotaNode = "https://field.carriota.com:443" # Create an IOTA object # api = Iota(iotaNode, "") api = Iota(iotaNode) # Thalia Receive address: #address = [Address(b'RNSVVCTUYTCMZVTUAOUZUZSXKE9XZGUNAG9XNDLEKXFUDE9MSLAEQIJRFIFUCRFIZFCZNZAYFDJFQFELZMFOWWJNTD')] ''' # Thalia Receive address list_add = [ Address( b'RNSVVCTUYTCMZVTUAOUZUZSXKE9XZGUNAG9XNDLEKXFUDE9MSLAEQIJRFIFUCRFIZFCZNZAYFDJFQFELZMFOWWJNTD' )