test_: restore account (#5960)

This commit is contained in:
Yevheniia Berdnyk 2024-10-23 22:48:33 +03:00 committed by GitHub
parent 132ea05fc8
commit 0555331252
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 85 additions and 43 deletions

View File

@ -66,7 +66,7 @@ type CreateAccount struct {
// If you want to use non-default network, use NetworkID.
CurrentNetwork string `json:"currentNetwork"`
NetworkID *uint64 `json:"networkId"`
TestOverrideNetworks []params.Network `json:"-"` // This is used for testing purposes only
TestOverrideNetworks []params.Network `json:"networksOverride"` // This is used for testing purposes only
TestNetworksEnabled bool `json:"testNetworksEnabled"`

View File

@ -1,6 +1,8 @@
import json
import logging
import time
from datetime import datetime
from json import JSONDecodeError
import jsonschema
import requests
@ -27,7 +29,7 @@ class RpcClient:
f"Key '{key}' not found in the JSON response: {response.content}")
def verify_is_valid_json_rpc_response(self, response, _id=None):
assert response.status_code == 200
assert response.status_code == 200, f"Got response {response.content}, status code {response.status_code}"
assert response.content
self._check_decode_and_key_errors_in_response(response, "result")
@ -53,7 +55,10 @@ class RpcClient:
data["params"] = params
logging.info(f"Sending POST request to url {url} with data: {json.dumps(data, sort_keys=True, indent=4)}")
response = self.client.post(url, json=data)
try:
logging.info(f"Got response: {json.dumps(response.json(), sort_keys=True, indent=4)}")
except JSONDecodeError:
logging.info(f"Got response: {response.content}")
return response
def rpc_valid_request(self, method, params=[], _id=None, url=None):
@ -69,7 +74,7 @@ class RpcClient:
class StatusBackend(RpcClient, SignalClient):
def __init__(self, await_signals):
def __init__(self, await_signals=list()):
self.api_url = f"{option.rpc_url_status_backend}/statusgo"
self.ws_url = f"{option.ws_url_status_backend}"
@ -81,12 +86,15 @@ class StatusBackend(RpcClient, SignalClient):
def api_request(self, method, data, url=None):
url = url if url else self.api_url
url = f"{url}/{method}"
logging.info(f"Sending POST request to url {url} with data: {json.dumps(data, sort_keys=True, indent=4)}")
response = requests.post(url, json=data)
logging.info(f"Got response: {response.content}")
return response
def verify_is_valid_api_response(self, response):
assert response.status_code == 200
assert response.status_code == 200, f"Got response {response.content}, status code {response.status_code}"
assert response.content
logging.info(f"Got response: {response.content}")
try:
assert not response.json()["error"]
except json.JSONDecodeError:
@ -119,6 +127,46 @@ class StatusBackend(RpcClient, SignalClient):
}
return self.api_valid_request(method, data)
def restore_account_and_login(self, display_name="Mr_Meeseeks", user=user_1):
method = "RestoreAccountAndLogin"
data = {
"rootDataDir": "/",
"displayName": display_name,
"password": user.password,
"mnemonic": user.passphrase,
"customizationColor": "blue",
"testNetworksEnabled": True,
"networkId": 31337,
"networksOverride": [
{
"ChainID": 31337,
"ChainName": "Anvil",
"DefaultRPCURL": "http://anvil:8545",
"RPCURL": "http://anvil:8545",
"ShortName": "eth",
"NativeCurrencyName": "Ether",
"NativeCurrencySymbol": "ETH",
"NativeCurrencyDecimals": 18,
"IsTest": False,
"Layer": 1,
"Enabled": True
}
]
}
return self.api_valid_request(method, data)
def restore_account_and_wait_for_rpc_client_to_start(self, timeout=60):
self.restore_account_and_login()
start_time = time.time()
# ToDo: change this part for waiting for `node.login` signal when websockets are migrated to StatusBackend
while time.time() - start_time <= timeout:
try:
self.rpc_valid_request(method='accounts_getKeypairs')
return
except AssertionError:
time.sleep(3)
raise TimeoutError(f"RPC client was not started after {timeout} seconds")
def start_messenger(self, params=[]):
method = "wakuext_startMessenger"
response = self.rpc_request(method, params)

View File

@ -82,6 +82,6 @@ def init_status_backend():
websocket_thread.start()
backend_client.init_status_backend()
backend_client.create_account_and_login()
backend_client.restore_account_and_wait_for_rpc_client_to_start()
yield backend_client

View File

@ -6,15 +6,18 @@ class Account:
address: str
private_key: str
password: str
passphrase: str
user_1 = Account(
address="0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266",
private_key="0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
password="Strong12345"
password="Strong12345",
passphrase="test test test test test test test test test test test junk"
)
user_2 = Account(
address="0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
private_key="0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d",
password="Strong12345"
password="Strong12345",
passphrase="test test test test test test test test test test nest junk"
)

View File

@ -12,3 +12,5 @@ markers =
accounts
ethclient
init
transaction
create_account

View File

@ -74,7 +74,7 @@
"type": "object"
},
"Test": {
"type": "null"
"type": ["null", "object"]
}
},
"required": [

View File

@ -2,12 +2,12 @@ import random
import pytest
from test_cases import StatusDTestCase
from test_cases import StatusBackendTestCase
@pytest.mark.accounts
@pytest.mark.rpc
class TestAccounts(StatusDTestCase):
class TestAccounts(StatusBackendTestCase):
@pytest.mark.parametrize(
"method, params",

View File

@ -7,7 +7,7 @@ from collections import namedtuple
import pytest
from clients.signals import SignalClient
from clients.status_backend import RpcClient
from clients.status_backend import RpcClient, StatusBackend
from conftest import option
from constants import user_1, user_2
@ -21,14 +21,17 @@ class StatusDTestCase:
)
class WalletTestCase(StatusDTestCase):
class StatusBackendTestCase:
def setup_class(self):
self.rpc_client = StatusBackend()
self.network_id = 31337
def setup_method(self):
super().setup_method()
class WalletTestCase(StatusBackendTestCase):
def wallet_create_multi_transaction(self, **kwargs):
method = "wallet_createMultiTransaction"
transferTx_data = {
transfer_tx_data = {
"data": "",
"from": user_1.address,
"gas": "0x5BBF",
@ -40,8 +43,8 @@ class WalletTestCase(StatusDTestCase):
"value": "0x5af3107a4000",
}
for key, new_value in kwargs.items():
if key in transferTx_data:
transferTx_data[key] = new_value
if key in transfer_tx_data:
transfer_tx_data[key] = new_value
else:
logging.info(
f"Warning: The key '{key}' does not exist in the transferTx parameters and will be ignored.")
@ -58,7 +61,7 @@ class WalletTestCase(StatusDTestCase):
{
"bridgeName": "Transfer",
"chainID": 31337,
"transferTx": transferTx_data
"transferTx": transfer_tx_data
}
],
f"{option.password}",
@ -81,7 +84,6 @@ class WalletTestCase(StatusDTestCase):
class TransactionTestCase(WalletTestCase):
def setup_method(self):
super().setup_method()
self.tx_hash = self.send_valid_multi_transaction()
@ -89,10 +91,6 @@ class EthRpcTestCase(WalletTestCase):
@pytest.fixture(autouse=True, scope='class')
def tx_data(self):
self.rpc_client = RpcClient(
option.rpc_url_statusd
)
tx_hash = self.send_valid_multi_transaction()
self.wait_until_tx_not_pending(tx_hash)
@ -103,8 +101,8 @@ class EthRpcTestCase(WalletTestCase):
except (KeyError, json.JSONDecodeError):
raise Exception(receipt.content)
TxData = namedtuple("TxData", ["tx_hash", "block_number", "block_hash"])
return TxData(tx_hash, block_number, block_hash)
tx_data = namedtuple("TxData", ["tx_hash", "block_number", "block_hash"])
return tx_data(tx_hash, block_number, block_hash)
def get_block_header(self, block_number):
method = "ethclient_headerByNumber"
@ -142,9 +140,3 @@ class SignalTestCase(StatusDTestCase):
websocket_thread = threading.Thread(target=self.signal_client._connect)
websocket_thread.daemon = True
websocket_thread.start()
class StatusBackendTestCase:
def setup_method(self):
pass

View File

@ -1,11 +1,9 @@
import pytest
from test_cases import StatusBackendTestCase
@pytest.mark.create_account
@pytest.mark.rpc
class TestInitialiseApp(StatusBackendTestCase):
class TestInitialiseApp:
@pytest.mark.init
def test_init_app(self, init_status_backend):

View File

@ -6,11 +6,10 @@ from typing import Optional
import pytest
from conftest import option
from test_cases import StatusDTestCase
from test_cases import StatusBackendTestCase
@pytest.mark.skip("to be reworked via status-backend")
class TestRpc(StatusDTestCase):
class TestRpc(StatusBackendTestCase):
@pytest.mark.parametrize(
"method, params",
@ -21,12 +20,12 @@ class TestRpc(StatusDTestCase):
def test_(self, method, params):
_id = str(random.randint(1, 8888))
response = self.rpc_client.rpc_valid_request(method, params, _id, url=option.rpc_url_2)
response = self.rpc_client.rpc_valid_request(method, params, _id)
self.rpc_client.verify_json_schema(response, method)
@pytest.mark.skip("to be reworked via status-backend")
class TestRpcMessaging(StatusDTestCase):
class TestRpcMessaging(StatusBackendTestCase):
@dataclass
class User:
rpc_url: str
@ -99,5 +98,5 @@ class TestRpcMessaging(StatusDTestCase):
self.rpc_client.verify_is_valid_json_rpc_response(response)
response = response.json()
assert response["result"][0]["added"] == True
assert response["result"][0]["added"] is True
assert response["result"][0]["id"] == user[1].chat_public_key

View File

@ -5,7 +5,7 @@ import jsonschema
import pytest
from conftest import option
from test_cases import StatusDTestCase, TransactionTestCase
from test_cases import StatusBackendTestCase, TransactionTestCase
@pytest.mark.wallet
@ -76,7 +76,7 @@ class TestTransactionRpc(TransactionTestCase):
@pytest.mark.wallet
@pytest.mark.rpc
class TestRpc(StatusDTestCase):
class TestRpc(StatusBackendTestCase):
@pytest.mark.parametrize(
"method, params",