github.com/status-im/status-go@v1.1.0/integration-tests/tests/test_cases.py (about) 1 import json 2 import websocket 3 import threading 4 import logging 5 import jsonschema 6 import requests 7 from conftest import option, user_1, user_2 8 9 10 class RpcTestCase: 11 12 def setup_method(self): 13 self.network_id = 31337 14 15 def verify_is_valid_json_rpc_response(self, response, _id=None): 16 assert response.status_code == 200 17 assert response.content 18 19 try: 20 response.json()["result"] 21 except json.JSONDecodeError: 22 raise AssertionError(f"invalid JSON in {response.content}") 23 except KeyError: 24 raise AssertionError(f"no 'result' in {response.json()}") 25 if _id: 26 try: 27 if _id != response.json()["id"]: 28 raise AssertionError( 29 f"got id: {response.json()['id']} instead of expected id: {_id}" 30 ) 31 except KeyError: 32 raise AssertionError(f"no id in response {response.json()}") 33 return response 34 35 def rpc_request(self, method, params=[], _id=None, client=None, url=None): 36 client = client if client else requests.Session() 37 url = url if url else option.rpc_url 38 39 data = {"jsonrpc": "2.0", "method": method} 40 if params: 41 data["params"] = params 42 data["id"] = _id if _id else 13 43 44 response = client.post(url, json=data) 45 46 return response 47 48 def verify_json_schema(self, response, method): 49 with open(f"{option.base_dir}/schemas/{method}", "r") as schema: 50 jsonschema.validate(instance=response.json(), 51 schema=json.load(schema)) 52 53 54 class TransactionTestCase(RpcTestCase): 55 56 def wallet_create_multi_transaction(self): 57 58 method = "wallet_createMultiTransaction" 59 params = [ 60 { 61 "fromAddress": user_1.address, 62 "fromAmount": "0x5af3107a4000", 63 "fromAsset": "ETH", 64 "multiTxType": "MultiTransactionSend", 65 "toAddress": user_2.address, 66 "toAsset": "ETH", 67 }, 68 [ 69 { 70 "bridgeName": "Transfer", 71 "chainID": 31337, 72 "transferTx": { 73 "data": "", 74 "from": user_1.address, 75 "gas": "0x5BBF", 76 "input": "", 77 "maxFeePerGas": "0xbcc0f04fd", 78 "maxPriorityFeePerGas": "0x3b9aca00", 79 "to": user_2.address, 80 "type": "0x02", 81 "value": "0x5af3107a4000", 82 }, 83 } 84 ], 85 f"{option.password}", 86 ] 87 88 response = self.rpc_request(method, params, 13) 89 self.verify_is_valid_json_rpc_response(response) 90 return response 91 92 def setup_method(self): 93 super().setup_method() 94 95 response = self.wallet_create_multi_transaction() 96 try: 97 self.tx_hash = response.json( 98 )["result"]["hashes"][str(self.network_id)][0] 99 except (KeyError, json.JSONDecodeError): 100 raise Exception(response.content) 101 102 103 class SignalTestCase(RpcTestCase): 104 105 received_signals = [] 106 107 def _on_message(self, ws, signal): 108 self.received_signals.append(signal) 109 110 def _on_error(self, ws, error): 111 logging.info(f"Error: {error}") 112 113 def _on_close(self, ws, close_status_code, close_msg): 114 logging.info(f"Connection closed: {close_status_code}, {close_msg}") 115 116 def _on_open(self, ws): 117 logging.info("Connection opened") 118 119 def _connect(self): 120 self.url = f"{option.ws_url}/signals" 121 122 ws = websocket.WebSocketApp(self.url, 123 on_message=self._on_message, 124 on_error=self._on_error, 125 on_close=self._on_close) 126 127 ws.on_open = self._on_open 128 129 ws.run_forever() 130 131 def setup_method(self): 132 super().setup_method() 133 134 websocket_thread = threading.Thread(target=self._connect) 135 websocket_thread.daemon = True 136 websocket_thread.start()