def test_request_urn(urn_test_request_dicts): jsonrpc_request, urn, url, ttl, timeout = urn_test_request_dicts dummy_request = AttrDict() dummy_request.headers = dict() dummy_request['jussi_request_id'] = '123456789012345' dummy_request.app = AttrDict() dummy_request.app.config = AttrDict() dummy_request.app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False) jussi_request = JussiJSONRPCRequest.from_request(dummy_request, 0, jsonrpc_request) assert jussi_request.batch_index == 0 jussi_request = JussiJSONRPCRequest.from_request(dummy_request, 1, jsonrpc_request) assert jussi_request.batch_index == 1
async def test_cache_group_cache_jsonrpc_response( steemd_requests_and_responses): caches = [ CacheGroupItem(SimpleMaxTTLMemoryCache(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache2(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache3(), True, True, SpeedTier.FAST), CacheGroupItem(SimpleMemoryCache4(), True, True, SpeedTier.FAST) ] for cache_item in caches: await cache_item.cache.clear() cache_group = CacheGroup(caches) req, resp = steemd_requests_and_responses req = JussiJSONRPCRequest.from_request(dummy_request, 0, req) resp['jsonrpc'] = '2.0' key = jsonrpc_cache_key(req) assert await cache_group.get(key) is None await cache_group.set('last_irreversible_block_num', 15_000_000) await cache_group.cache_jsonrpc_response(req, resp) for cache_item in caches: assert await cache_item.cache.get(key ) == resp, f'key:{key} urn:{req.urn}' assert await cache_group.get(key) == resp, f'key:{key} urn:{req.urn}'
async def test_cache_group_get_batch_jsonrpc_responses( steemd_requests_and_responses): caches = [ SimpleMaxTTLMemoryCache(), SimpleMemoryCache2(), SimpleMemoryCache3(), SimpleMemoryCache4() ] for cache in caches: await cache.clear() cache_group = CacheGroup(caches) req, resp = steemd_requests_and_responses req = JussiJSONRPCRequest.from_request(dummy_request, 0, req) resp['jsonrpc'] = '2.0' batch_req = [req, req, req] batch_resp = [resp, resp, resp] key = jsonrpc_cache_key(req) assert await cache_group.get(key) is None await cache_group.set('last_irreversible_block_num', 15_000_000) await cache_group.cache_jsonrpc_response(batch_req, batch_resp) assert await cache_group.get(key) == resp assert await cache_group.get_batch_jsonrpc_responses(batch_req ) == batch_resp
def test_params_param_translation(steemd_jussi_requests_and_dicts): jussi_request, jsonrpc_request = steemd_jussi_requests_and_dicts urn = jussi_request.urn translated_request_dict = JussiJSONRPCRequest.translate_to_appbase( jsonrpc_request, urn) if jussi_request.urn.params is False: assert translated_request_dict['params'][2] == [] else: assert translated_request_dict['params'][2] == jussi_request.urn.params
def test_request_to_json(urn_test_request_dicts): jsonrpc_request, urn, url, ttl, timeout = urn_test_request_dicts dummy_request = AttrDict() dummy_request.headers = dict() dummy_request['jussi_request_id'] = '123456789012345' dummy_request.app = AttrDict() dummy_request.app.config = AttrDict() dummy_request.app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False) jussi_request = JussiJSONRPCRequest.from_request(dummy_request, 0, jsonrpc_request) assert ujson.loads(jussi_request.json()) == jussi_request.to_dict()
def test_request_params(full_urn_test_request_dicts): jsonrpc_request, urn_parsed, urn, url, ttl, timeout = full_urn_test_request_dicts dummy_request = AttrDict() dummy_request.headers = dict() dummy_request['jussi_request_id'] = '123456789012345' dummy_request.app = AttrDict() dummy_request.app.config = AttrDict() dummy_request.app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False) jussi_request = JussiJSONRPCRequest.from_request(dummy_request, 0, jsonrpc_request) assert jussi_request.params == jsonrpc_request.get('params', False)
def test_upstream_headers(urn_test_request_dicts): jsonrpc_request, urn, url, ttl, timeout = urn_test_request_dicts dummy_request = AttrDict() dummy_request.headers = dict() dummy_request['jussi_request_id'] = '123456789012345' dummy_request.app = AttrDict() dummy_request.app.config = AttrDict() dummy_request.app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False) jussi_request = JussiJSONRPCRequest.from_request(dummy_request, 0, jsonrpc_request) assert jussi_request.upstream_headers == { 'x-jussi-request-id': '123456789012345' } dummy_request.headers['x-amzn-trace-id'] = '1' jussi_request = JussiJSONRPCRequest.from_request(dummy_request, 0, jsonrpc_request) assert jussi_request.upstream_headers == { 'x-jussi-request-id': '123456789012345', 'x-amzn-trace-id': '1' }
def test_request_upstream(urn_test_request_dicts): jsonrpc_request, urn, url, ttl, timeout = urn_test_request_dicts dummy_request = AttrDict() dummy_request.headers = dict() dummy_request['jussi_request_id'] = '123456789012345' dummy_request.app = AttrDict() dummy_request.app.config = AttrDict() dummy_request.app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False) jussi_request = JussiJSONRPCRequest.from_request(dummy_request, 0, jsonrpc_request) os.environ['JUSSI_ACCOUNT_TRANSFER_STEEMD_URL'] = 'account_transfer_url' assert jussi_request.upstream.url == url
def upstream_request(urn_test_request_dicts): jsonrpc_request, urn, url, ttl, timeout = urn_test_request_dicts dummy_request = AttrDict() dummy_request.headers = dict() dummy_request['jussi_request_id'] = '123456789012345' dummy_request.app = AttrDict() dummy_request.app.config = AttrDict() dummy_request.app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False) jussi_request = JussiJSONRPCRequest.from_request(dummy_request, 0, jsonrpc_request) cpy = deepcopy(jussi_request) cpy['id'] = 123456789012345 assert jussi_request.to_upstream_request(as_json=False) == cpy assert jussi_request.to_upstream_request() == ujson.dumps( cpy, ensure_ascii=False) cpy = deepcopy(jussi_request) cpy['id'] = 123456789012346 jussi_request = JussiJSONRPCRequest.from_request(dummy_request, 1, cpy) assert jussi_request.to_upstream_request(as_json=False) == cpy assert jussi_request.to_upstream_request() == ujson.dumps( cpy, ensure_ascii=False)
def test_is_valid_jussi_response_using_steemd(steemd_requests_and_responses): req, resp = steemd_requests_and_responses req = JussiJSONRPCRequest.from_request(dummy_request, 0, req) assert is_valid_jussi_response(req, resp) is True
dummy_request.headers = dict() dummy_request['jussi_request_id'] = '123456789012345' dummy_request.app = AttrDict() dummy_request.app.config = AttrDict() dummy_request.app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False) caches_config = { 'default': { 'cache': 'jussi.cache.backends.SimpleLRUMemoryCache' } } jrpc_req_1 = JussiJSONRPCRequest.from_request(dummy_request, 0, {"id": "1", "jsonrpc": "2.0", "method": "get_block", "params": [1000]}) jrpc_resp_1 = { "id": 2, "result": { "previous": "000003e7c4fd3221cf407efcf7c1730e2ca54b05", "timestamp": "2016-03-24T16:55:30", "witness": "initminer", "transaction_merkle_root": "0000000000000000000000000000000000000000", "extensions": [], "witness_signature": "207f15578cac20ac0e8af1ebb8f463106b8849577e21cca9fc60da146d1d95df88072dedc6ffb7f7f44a9185bbf9bf8139a5b4285c9f423843720296a44d428856", "transactions": [], "block_id": "000003e8b922f4906a45af8e99d86b3511acd7a5", "signing_key": "STM8GC13uCZbP44HzMLV6zPZGwVQ8Nt4Kji8PapsPiNq1BK153XTX", "transaction_ids": []}}
from jussi.validators import limit_custom_json_account from jussi.validators import is_broadcast_transaction_request from jussi.upstream import _Upstreams from .conftest import AttrDict dummy_request = AttrDict() dummy_request.headers = dict() dummy_request['jussi_request_id'] = '123456789012345' dummy_request.app = AttrDict() dummy_request.app.config = AttrDict() dummy_request.app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False) request = JussiJSONRPCRequest.from_request(dummy_request, 0, { "id": "1", "jsonrpc": "2.0", "method": "get_block", "params": [1000] }) request2 = JussiJSONRPCRequest.from_request(dummy_request, 1, { "id": "1", "jsonrpc": "2.0", "method": "call", "params": ["database_api", "get_block", [1000]] }) response = { "id": 1, "result": { "previous": "000003e7c4fd3221cf407efcf7c1730e2ca54b05", "timestamp": "2016-03-24T16:55:30", "witness": "initminer", "transaction_merkle_root": "0000000000000000000000000000000000000000", "extensions": [],
def test_invalid_limit_broadcast_transaction_request(invalid_broadcast_transactions): req = JussiJSONRPCRequest.from_request(dummy_request, 0, invalid_broadcast_transactions) with pytest.raises(JsonRpcError): limit_broadcast_transaction_request( req, limits=TEST_UPSTREAM_CONFIG['limits'])
def test_id_translation(steemd_jussi_requests_and_dicts): jussi_request, jsonrpc_request = steemd_jussi_requests_and_dicts urn = jussi_request.urn translated_request_dict = JussiJSONRPCRequest.translate_to_appbase( jsonrpc_request, urn) assert translated_request_dict['id'] == jussi_request.id
def test_cache_group_x_jussi_cache_key(steemd_requests_and_responses): req, resp = steemd_requests_and_responses req = JussiJSONRPCRequest.from_request(dummy_request, 0, req) batch_req = [req, req, req] assert jsonrpc_cache_key(req) == CacheGroup.x_jussi_cache_key(req) assert CacheGroup.x_jussi_cache_key(batch_req) == 'batch'
def test_is_get_block_header_request(req, expected): if not isinstance(req, JussiJSONRPCRequest): req = JussiJSONRPCRequest.from_request(dummy_request, 0, req) assert is_get_block_header_request(req) is expected
def test_is_valid_get_block_response(req, response, expected): if not isinstance(req, JussiJSONRPCRequest): req = JussiJSONRPCRequest.from_request(dummy_request, 0, req) assert is_valid_get_block_response(req, response) is expected
def test_is_broadcast_transaction_false(steemd_requests_and_responses): req, resp = steemd_requests_and_responses req = JussiJSONRPCRequest.from_request(dummy_request, 0, req) assert is_broadcast_transaction_request(req) is False
def test_is_broadcast_transaction_true_invalid(invalid_broadcast_transactions): req = JussiJSONRPCRequest.from_request(dummy_request, 0, invalid_broadcast_transactions) assert is_broadcast_transaction_request(req) is True
super(AttrDict, self).__init__(*args, **kwargs) self.__dict__ = self dummy_request = AttrDict() dummy_request.headers = dict() dummy_request['jussi_request_id'] = '123456789012345' dummy_request.app = AttrDict() dummy_request.app.config = AttrDict() dummy_request.app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False) jrpc_req_1 = JussiJSONRPCRequest.from_request(dummy_request, 0, { "id": "1", "jsonrpc": "2.0", "method": "get_block", "params": [1000] }) jrpc_resp_1 = { "id": 2, "result": { "previous": "000003e7c4fd3221cf407efcf7c1730e2ca54b05", "timestamp": "2016-03-24T16:55:30", "witness": "initminer", "transaction_merkle_root": "0000000000000000000000000000000000000000", "extensions": [], "witness_signature": "207f15578cac20ac0e8af1ebb8f463106b8849577e21cca9fc60da146d1d95df88072dedc6ffb7f7f44a9185bbf9bf8139a5b4285c9f423843720296a44d428856", "transactions": [], "block_id": "000003e8b922f4906a45af8e99d86b3511acd7a5",
def test_limit_broadcast_transaction_request(steemd_requests_and_responses): req, resp = steemd_requests_and_responses req = JussiJSONRPCRequest.from_request(dummy_request, 0, req) limit_broadcast_transaction_request(req)
def test_cache_group_is_complete_response(steemd_requests_and_responses): req, resp = steemd_requests_and_responses req = JussiJSONRPCRequest.from_request(dummy_request, 0, req) assert CacheGroup.is_complete_response(req, resp) is True
def test_valid_limit_broadcast_transaction_request(valid_broadcast_transactions): req = JussiJSONRPCRequest.from_request(dummy_request, 0, valid_broadcast_transactions) limit_broadcast_transaction_request( req, limits=TEST_UPSTREAM_CONFIG['limits'])
from jussi.cache.utils import irreversible_ttl from jussi.request import JussiJSONRPCRequest from jussi.upstream import _Upstreams dummy_request = AttrDict() dummy_request.headers = dict() dummy_request['jussi_request_id'] = '123456789012345' dummy_request.app = AttrDict() dummy_request.app.config = AttrDict() dummy_request.app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False) ttl_rpc_req = JussiJSONRPCRequest.from_request(dummy_request, 0, { "id": "1", "jsonrpc": "2.0", "method": "get_block", "params": [1000] }) rpc_resp = { "id": 1, "result": { "previous": "000003e7c4fd3221cf407efcf7c1730e2ca54b05", "timestamp": "2016-03-24T16:55:30", "witness": "initminer", "transaction_merkle_root": "0000000000000000000000000000000000000000", "extensions": [], "witness_signature": "207f15578cac20ac0e8af1ebb8f463106b8849577e21cca9fc60da146d1d95df88072dedc6ffb7f7f44a9185bbf9bf8139a5b4285c9f423843720296a44d428856", "transactions": [], "block_id": "000003e8b922f4906a45af8e99d86b3511acd7a5", "signing_key": "STM8GC13uCZbP44HzMLV6zPZGwVQ8Nt4Kji8PapsPiNq1BK153XTX",
def test_params_method_translation(steemd_jussi_requests_and_dicts): jussi_request, jsonrpc_request = steemd_jussi_requests_and_dicts urn = jussi_request.urn translated_request_dict = JussiJSONRPCRequest.translate_to_appbase( jsonrpc_request, urn) assert translated_request_dict['params'][1] == jussi_request.urn.method