From be3a2e7b2d380074e549499fe944ef9bc982ba29 Mon Sep 17 00:00:00 2001 From: nolash Date: Mon, 15 Mar 2021 19:46:37 +0100 Subject: [PATCH] Port new fixtures, registry setup in tests --- apps/cic-eth/tests/conftest.py | 5 +- apps/cic-eth/tests/fixtures_contracts.py | 35 ++++++ apps/cic-eth/tests/task/__init__.py | 0 apps/cic-eth/tests/task/test_account.py | 137 +++++++++++++++++++++++ apps/cic-eth/tests/test_chainlib.py | 49 ++++++++ apps/cic-eth/tests/test_registry.py | 4 + 6 files changed, 228 insertions(+), 2 deletions(-) create mode 100644 apps/cic-eth/tests/fixtures_contracts.py create mode 100644 apps/cic-eth/tests/task/__init__.py create mode 100644 apps/cic-eth/tests/task/test_account.py create mode 100644 apps/cic-eth/tests/test_chainlib.py create mode 100644 apps/cic-eth/tests/test_registry.py diff --git a/apps/cic-eth/tests/conftest.py b/apps/cic-eth/tests/conftest.py index 5c4ed5fd..22eda579 100644 --- a/apps/cic-eth/tests/conftest.py +++ b/apps/cic-eth/tests/conftest.py @@ -10,5 +10,6 @@ sys.path.insert(0, root_dir) from tests.fixtures_config import * from tests.fixtures_celery import * from tests.fixtures_database import * -from tests.fixtures_registry import * -from chainlib.pytest.fixtures_ethtester import * +from tests.fixtures_contracts import * +from chainlib.eth.pytest import * +from cic_registry.pytest import * diff --git a/apps/cic-eth/tests/fixtures_contracts.py b/apps/cic-eth/tests/fixtures_contracts.py new file mode 100644 index 00000000..66e147f2 --- /dev/null +++ b/apps/cic-eth/tests/fixtures_contracts.py @@ -0,0 +1,35 @@ +# standard imports +import json +import hashlib + +# external imports +import pytest +from eth_accounts_index import AccountRegistry +from chainlib.eth.nonce import NodeNonceOracle +from hexathon import add_0x + +# local imports +from cic_registry.registry import Registry +from cic_registry.encoding import to_identifier + +@pytest.fixture(scope='function') +def accounts_registry( + registry, + eth_signer, + eth_rpc, + accounts, + default_chain_spec, + default_chain_config, + ): + + nonce_oracle = NodeNonceOracle(accounts[0], eth_rpc) + c = Registry(signer=eth_rpc.signer, nonce_oracle=nonce_oracle) + + chain_spec_identifier = to_identifier(str(default_chain_spec)) + + h = hashlib.new('sha256') + j = json.dumps(default_chain_config) + h.update(j.encode('utf-8')) + z = h.digest() + chain_config_digest = add_0x(z.hex()) + o = c.set(registry, accounts[0], 'AccountsRegistry', accounts[1], chain_spec_identifier, chain_config_digest) diff --git a/apps/cic-eth/tests/task/__init__.py b/apps/cic-eth/tests/task/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/cic-eth/tests/task/test_account.py b/apps/cic-eth/tests/task/test_account.py new file mode 100644 index 00000000..8a8fda13 --- /dev/null +++ b/apps/cic-eth/tests/task/test_account.py @@ -0,0 +1,137 @@ +# standard imports +import os +import logging +import time + +# third-party imports +import pytest +import celery +from chainlib.eth.connection import RPCConnection +import logging_tree +from eth_keys import KeyAPI + +# local imports +from cic_eth.error import OutOfGasError +from cic_eth.db.models.otx import Otx +from cic_eth.db.models.base import SessionBase +from cic_eth.db.enum import StatusEnum +from cic_eth.db.enum import StatusEnum +from cic_eth.db.models.nonce import Nonce +from cic_eth.db.models.role import AccountRole +from cic_eth.eth.account import AccountTxFactory + +logg = logging.getLogger() + + +def test_create_account( + default_chain_spec, + init_rpc, + init_database, + celery_session_worker, + caplog, + ): + s = celery.signature( + 'cic_eth.eth.account.create', + [ + 'foo', + str(default_chain_spec), + ], + ) + t = s.apply_async() + r = t.get() + + session = SessionBase.create_session() + q = session.query(Nonce).filter(Nonce.address_hex==r) + o = q.first() + session.close() + assert o != None + assert o.nonce == 0 + + s = celery.signature( + 'cic_eth.eth.account.have', + [ + r, + str(default_chain_spec), + ], + ) + t = s.apply_async() + assert r == t.get() + + +def test_register_account( + default_chain_spec, + accounts_registry, + init_database, + accounts, + rpc, + cic_registry, + celery_session_worker, + empty_accounts, + ): + + logg.debug('chainspec {}'.format(str(default_chain_spec))) + + s_nonce = celery.signature( + 'cic_eth.eth.tx.reserve_nonce', + [ + empty_accounts[0], + accounts[0], + ], + queue=None, + ) + s_register = celery.signature( + 'cic_eth.eth.account.register', + [ + str(default_chain_spec), + init_w3.eth.accounts[0], + ], + ) + s_nonce.link(s_register) + t = s_nonce.apply_async() + address = t.get() + for r in t.collect(): + pass + assert t.successful() + + session = SessionBase.create_session() + o = session.query(Otx).first() + tx_signed_hex = o.signed_tx + session.close() + + s_send = celery.signature( + 'cic_eth.eth.tx.send', + [ + [tx_signed_hex], + str(default_chain_spec), + ], + ) + t = s_send.apply_async() + address = t.get() + r = t.collect() + t.successful() + + init_eth_tester.mine_block() + + assert accounts_registry.have(eth_empty_accounts[0]) + + +def test_role_task( + init_database, + celery_session_worker, + default_chain_spec, + ): + + address = '0x' + os.urandom(20).hex() + role = AccountRole.set('foo', address) + init_database.add(role) + init_database.commit() + s = celery.signature( + 'cic_eth.eth.account.role', + [ + address, + str(default_chain_spec), + ], + ) + t = s.apply_async() + r = t.get() + assert r == 'foo' diff --git a/apps/cic-eth/tests/test_chainlib.py b/apps/cic-eth/tests/test_chainlib.py new file mode 100644 index 00000000..5d862049 --- /dev/null +++ b/apps/cic-eth/tests/test_chainlib.py @@ -0,0 +1,49 @@ +# standard imports +import logging + +# external imports +from chainlib.eth.connection import RPCConnection +from chainlib.eth.gas import ( + balance, + price, + ) +from chainlib.eth.tx import ( + count_pending, + count_confirmed, + ) +from chainlib.eth.sign import ( + sign_message, + ) + +logg = logging.getLogger(__name__) + + +def test_init_eth_tester( + accounts, + init_eth_tester, + init_rpc, + ): + + conn = RPCConnection.connect() + o = balance(accounts[0]) + conn.do(o) + + o = price() + conn.do(o) + + o = count_pending(accounts[0]) + conn.do(o) + + o = count_confirmed(accounts[0]) + conn.do(o) + + +def test_signer( + init_eth_tester, + init_rpc, + accounts, + ): + + o = sign_message(accounts[0], '0x2a') + conn = RPCConnection.connect('signer') + r = conn.do(o) diff --git a/apps/cic-eth/tests/test_registry.py b/apps/cic-eth/tests/test_registry.py new file mode 100644 index 00000000..c7c35797 --- /dev/null +++ b/apps/cic-eth/tests/test_registry.py @@ -0,0 +1,4 @@ +def test_registry( + accounts_registry, + ): + pass