diff --git a/integration-tests/schemas/wallet_createMultiTransaction/transferTx_error b/integration-tests/schemas/wallet_createMultiTransaction/transferTx_error new file mode 100644 index 000000000..e2e78a1f7 --- /dev/null +++ b/integration-tests/schemas/wallet_createMultiTransaction/transferTx_error @@ -0,0 +1,32 @@ +{ + "$schema": "http://json-schema.org/schema#", + "properties": { + "error": { + "properties": { + "code": { + "type": "integer" + }, + "message": { + "type": "string" + } + }, + "required": [ + "code", + "message" + ], + "type": "object" + }, + "id": { + "type": "integer" + }, + "jsonrpc": { + "type": "string" + } + }, + "required": [ + "error", + "id", + "jsonrpc" + ], + "type": "object" +} \ No newline at end of file diff --git a/integration-tests/schemas/wallet_createMultiTransaction b/integration-tests/schemas/wallet_createMultiTransaction/transferTx_positive similarity index 100% rename from integration-tests/schemas/wallet_createMultiTransaction rename to integration-tests/schemas/wallet_createMultiTransaction/transferTx_positive diff --git a/integration-tests/tests/test_cases.py b/integration-tests/tests/test_cases.py index 222a2c06d..81ebae27b 100644 --- a/integration-tests/tests/test_cases.py +++ b/integration-tests/tests/test_cases.py @@ -12,16 +12,19 @@ class RpcTestCase: def setup_method(self): self.network_id = 31337 + def _try_except_JSONDecodeError_KeyError(self, response, key: str): + try: + return response.json()[key] + except json.JSONDecodeError: + raise AssertionError(f"Invalid JSON in response: {response.content}") + except KeyError: + raise AssertionError(f"Key '{key}' not found in the JSON response.") + def verify_is_valid_json_rpc_response(self, response, _id=None): assert response.status_code == 200 assert response.content + self._try_except_JSONDecodeError_KeyError(response, "result") - try: - response.json()["result"] - except json.JSONDecodeError: - raise AssertionError(f"invalid JSON in {response.content}") - except KeyError: - raise AssertionError(f"no 'result' in {response.json()}") if _id: try: if _id != response.json()["id"]: @@ -32,6 +35,12 @@ class RpcTestCase: raise AssertionError(f"no id in response {response.json()}") return response + def verify_is_json_rpc_error(self, response): + assert response.status_code == 200 + assert response.content + self._try_except_JSONDecodeError_KeyError(response, "error") + + def rpc_request(self, method, params=[], _id=None, client=None, url=None): client = client if client else requests.Session() url = url if url else option.rpc_url @@ -51,11 +60,28 @@ class RpcTestCase: schema=json.load(schema)) + class TransactionTestCase(RpcTestCase): - def wallet_create_multi_transaction(self): + def wallet_create_multi_transaction(self, **kwargs): method = "wallet_createMultiTransaction" + transferTx_data = { + "data": "", + "from": user_1.address, + "gas": "0x5BBF", + "input": "", + "maxFeePerGas": "0xbcc0f04fd", + "maxPriorityFeePerGas": "0xbcc0f04fd", + "to": user_2.address, + "type": "0x02", + "value": "0x5af3107a4000", + } + for key, new_value in kwargs.items(): + if key in transferTx_data: + transferTx_data[key] = new_value + else: + print(f"Warning: The key '{key}' does not exist in the transferTx parameters and will be ignored.") params = [ { "fromAddress": user_1.address, @@ -69,24 +95,12 @@ class TransactionTestCase(RpcTestCase): { "bridgeName": "Transfer", "chainID": 31337, - "transferTx": { - "data": "", - "from": user_1.address, - "gas": "0x5BBF", - "input": "", - "maxFeePerGas": "0xbcc0f04fd", - "maxPriorityFeePerGas": "0x3b9aca00", - "to": user_2.address, - "type": "0x02", - "value": "0x5af3107a4000", - }, + "transferTx": transferTx_data } ], f"{option.password}", ] - response = self.rpc_request(method, params, 13) - self.verify_is_valid_json_rpc_response(response) return response def setup_method(self): diff --git a/integration-tests/tests/test_wallet_rpc.py b/integration-tests/tests/test_wallet_rpc.py index cdeb0c2a7..7d0f4d23e 100644 --- a/integration-tests/tests/test_wallet_rpc.py +++ b/integration-tests/tests/test_wallet_rpc.py @@ -38,14 +38,40 @@ class TestTransactionRpc(TransactionTestCase): def test_create_multi_transaction(self): response = self.wallet_create_multi_transaction() - + self.verify_is_valid_json_rpc_response(response) + # how to create schema: # from schema_builder import CustomSchemaBuilder # CustomSchemaBuilder(method).create_schema(response.json()) - - with open(f"{option.base_dir}/schemas/wallet_createMultiTransaction", "r") as schema: + + with open(f"{option.base_dir}/schemas/wallet_createMultiTransaction/transferTx_positive", "r") as schema: jsonschema.validate(instance=response.json(), schema=json.load(schema)) + @pytest.mark.parametrize( + "method, changed_values, expected_error_code, expected_error_text", + [ + ( + "transferTx_value_not_enough_balance", + {'value': '0x21e438ea8139cd35004'}, -32000, "Insufficient funds for gas", + ), + ( + "transferTx_from_from_invalid_string", + {'from': 'some_invalid_address'}, -32602, "cannot unmarshal hex string without 0x prefix", + ), + ], + ) + def test_create_multi_transaction_validation(self, method, + changed_values, + expected_error_code, expected_error_text): + response = self.wallet_create_multi_transaction(**changed_values) + self.verify_is_json_rpc_error(response) + actual_error_code, actual_error_text = response.json()['error']['code'], response.json()['error']['message'] + assert expected_error_code == actual_error_code, f"got code: {actual_error_code} instead of expected: {expected_error_code}" + assert expected_error_text in actual_error_text, f"got error: {actual_error_text} that does not include: {expected_error_text}" + + with open(f"{option.base_dir}/schemas/wallet_createMultiTransaction/transferTx_error", "r") as schema: + jsonschema.validate(instance=response.json(), schema=json.load(schema)) + @pytest.mark.wallet @pytest.mark.rpc