def test_call_success(self, m): method = RPCMethod("some_rpc_method", RPCHost("user", "password", "host", 18443, False)) m.register_uri( 'POST', 'http://*****:*****@host:18443/', request_headers={"content-type": "application/json"}, additional_matcher=lambda req: req.text == json.dumps( { "method": "some_rpc_method", "params": ["arg1", 2], "jsonrpc": "2.0" }), json=json.loads('{"result": true, "error": null}'), status_code=200, ) self.assertEqual(method("arg1", 2), True) method = RPCMethod("other_rpc_method", RPCHost("user", "password", "host", 18443, False)) m.register_uri( 'POST', 'http://*****:*****@host:18443/', request_headers={"content-type": "application/json"}, additional_matcher=lambda req: req.text == json.dumps( { "method": "other_rpc_method", "params": [[0], "arg2", "arg3"], "jsonrpc": "2.0" }), json=json.loads('{"result": true, "error": null}'), status_code=500, ) self.assertEqual(method([0], "arg2", "arg3"), True)
def test_connect_to_node_test(self): # Copy the NetworkAPI class as to not override it class n(NetworkAPI): pass n.connect_to_node(user="******", password="******", host="host", port=18443, use_https=True, testnet=True) assert (sum( both_rpchosts_equal(call.__self__, RPCHost("usr", "pass", "host", 18443, True)) for call in n.GET_BALANCE_TEST) == 1) assert all( isinstance(call.__self__, RPCHost) for call in n.GET_BALANCE_TEST) assert (sum( both_rpchosts_equal(call.__self__, RPCHost("usr", "pass", "host", 18443, True)) for call in n.GET_TRANSACTIONS_TEST) == 1) assert all( isinstance(call.__self__, RPCHost) for call in n.GET_TRANSACTIONS_TEST) assert (sum( both_rpchosts_equal(call.__self__, RPCHost("usr", "pass", "host", 18443, True)) for call in n.GET_TRANSACTION_BY_ID_TEST) == 1) assert all( isinstance(call.__self__, RPCHost) for call in n.GET_TRANSACTION_BY_ID_TEST) assert (sum( both_rpchosts_equal(call.__self__, RPCHost("usr", "pass", "host", 18443, True)) for call in n.GET_UNSPENT_TEST) == 1) assert all( isinstance(call.__self__, RPCHost) for call in n.GET_UNSPENT_TEST) assert (sum( both_rpchosts_equal(call.__self__, RPCHost("usr", "pass", "host", 18443, True)) for call in n.BROADCAST_TX_TEST) == 1) assert all( isinstance(call.__self__, RPCHost) for call in n.BROADCAST_TX_TEST) assert all(not isinstance(call.__self__, RPCHost) for call in n.GET_BALANCE_MAIN) assert all(not isinstance(call.__self__, RPCHost) for call in n.GET_TRANSACTIONS_MAIN) assert all(not isinstance(call.__self__, RPCHost) for call in n.GET_TRANSACTION_BY_ID_MAIN) assert all(not isinstance(call.__self__, RPCHost) for call in n.GET_UNSPENT_MAIN) assert all(not isinstance(call.__self__, RPCHost) for call in n.BROADCAST_TX_MAIN)
def test_connect_to_node_main(self): # Copy the NetworkAPI class as to not override it class n(NetworkAPI): pass n.connect_to_node(user="******", password="******") assert (sum( both_rpchosts_equal( call.__self__, RPCHost("user", "password", "localhost", 8332, False)) for call in n.GET_BALANCE_MAIN) == 1) assert all( isinstance(call.__self__, RPCHost) for call in n.GET_BALANCE_MAIN) assert (sum( both_rpchosts_equal( call.__self__, RPCHost("user", "password", "localhost", 8332, False)) for call in n.GET_TRANSACTIONS_MAIN) == 1) assert all( isinstance(call.__self__, RPCHost) for call in n.GET_TRANSACTIONS_MAIN) assert (sum( both_rpchosts_equal( call.__self__, RPCHost("user", "password", "localhost", 8332, False)) for call in n.GET_TRANSACTION_BY_ID_MAIN) == 1) assert all( isinstance(call.__self__, RPCHost) for call in n.GET_TRANSACTION_BY_ID_MAIN) assert (sum( both_rpchosts_equal( call.__self__, RPCHost("user", "password", "localhost", 8332, False)) for call in n.GET_UNSPENT_MAIN) == 1) assert all( isinstance(call.__self__, RPCHost) for call in n.GET_UNSPENT_MAIN) assert (sum( both_rpchosts_equal( call.__self__, RPCHost("user", "password", "localhost", 8332, False)) for call in n.BROADCAST_TX_MAIN) == 1) assert all( isinstance(call.__self__, RPCHost) for call in n.BROADCAST_TX_MAIN) assert all(not isinstance(call.__self__, RPCHost) for call in n.GET_BALANCE_TEST) assert all(not isinstance(call.__self__, RPCHost) for call in n.GET_TRANSACTIONS_TEST) assert all(not isinstance(call.__self__, RPCHost) for call in n.GET_TRANSACTION_BY_ID_TEST) assert all(not isinstance(call.__self__, RPCHost) for call in n.GET_UNSPENT_TEST) assert all(not isinstance(call.__self__, RPCHost) for call in n.BROADCAST_TX_TEST)
def test_init(self): node = RPCHost("user", "password", "host", 8333, True) assert node._url == "https://*****:*****@host:8333/" assert isinstance(node._session, requests.Session) assert node._session.verify is True assert node._headers == {"content-type": "application/json"} node = RPCHost("usr", "pass", "other", 18443, False) assert node._url == "http://*****:*****@other:18443/" assert isinstance(node._session, requests.Session) assert node._session.verify is False assert node._headers == {"content-type": "application/json"}
def test_call_fails_status_code(self, m): method = RPCMethod("some_rpc_method", RPCHost("user", "password", "host", 18443, False)) m.register_uri('POST', 'http://*****:*****@host:18443/', status_code=201, reason="testing failing status code") with pytest.raises(bit.exceptions.BitcoinNodeException): method()
def test_call_fails_unsupported_command(self, m): method = RPCMethod("some_rpc_method", RPCHost("user", "password", "host", 18443, False)) m.register_uri('POST', 'http://*****:*****@host:18443/', json=json.loads('{"result": false, "error": true}'), status_code=200, reason="testing failing return value") with pytest.raises(bit.exceptions.BitcoinNodeException): method()
def test_call_fails_connection(self): method = RPCMethod( "some_rpc_method", RPCHost("user", "password", "some_invalid_host", 18443, False)) with pytest.raises(ConnectionError): method()
def test_rpc_method_call(self): assert isinstance(RPCHost.__getattr__(None, "rpc_method"), RPCMethod) assert RPCHost.__getattr__(None, "rpc_method")._rpc_method == "rpc_method" assert RPCHost.__getattr__(None, "rpc_method")._host is None