Skip to content

Commit

Permalink
Add account import tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmuhs committed Oct 7, 2020
1 parent 17561fc commit 9b0f85a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ clean-test: ## remove test and coverage artifacts
rm -fr .pytest_cache

test: ## run tests quickly with the default Python
pytest --cov-report html --cov-report term --cov=teatime -vv
pytest --cov-report html --cov-report term --cov=teatime

test-all: ## run tests on every Python version with tox
tox
Expand Down
7 changes: 4 additions & 3 deletions teatime/plugins/eth1/account_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class GethAccountImport(Plugin):

INTRUSIVE = True

def __init__(self, test_password: str):
self.test_password = test_password
def __init__(self, keydata: str, password: str):
self.keydata = keydata
self.password = password

def _check(self, context: "Context"):
if context.node_type != NodeType.GETH:
Expand All @@ -27,7 +28,7 @@ def _check(self, context: "Context"):
payload = self.get_rpc_json(
target=context.target,
method="personal_importRawKey",
params=[self.test_password, self.test_password],
params=[self.keydata, self.password],
)
context.report.add_issue(
Issue(
Expand Down
103 changes: 103 additions & 0 deletions tests/plugins/eth1/test_account_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import pytest

from teatime import Context, NodeType, Report, Severity
from teatime.plugins.eth1 import GethAccountImport

from .util import assert_empty_report, assert_report_has_issue, mocked_execute

TARGET = "127.0.0.1:8545"
RPC_METHOD = "personal_importRawKey"
TEST_PK = "0x0"
RPC_RESULT = {"id": 1, "jsonrpc": "2.0", "result": "0xlolthisistotallyvalid"}
TEST_PASSWORD = "pa$$w0rd"
TITLE = "We managed to import an account on your node"
DESCR = (
"A private key can be imported on the node to initialize an "
"account using the personal_importRawKey RPC call."
)
SEVERITY = Severity.MEDIUM


@pytest.mark.parametrize(
"plugin,node_type",
(
pytest.param(
GethAccountImport(keydata=TEST_PK, password=TEST_PASSWORD),
NodeType.GETH,
id="geth",
),
pytest.param(
GethAccountImport(keydata=TEST_PK, password=TEST_PASSWORD),
NodeType.PARITY,
id="parity",
),
),
)
def test_issue_found(plugin, node_type):
skipped = node_type == NodeType.PARITY
context = Context(
target=TARGET, report=Report(target=TARGET, issues=[]), node_type=node_type
)
mocked_execute(
target=TARGET,
rpc_result=RPC_RESULT,
plugin=plugin,
context=context,
rpc_method=RPC_METHOD,
skipped=skipped,
)
if not skipped:
assert_report_has_issue(
report=context.report,
meta_name=plugin.__class__.__name__,
title=TITLE,
description=DESCR,
rpc_raw=RPC_RESULT["result"],
severity=SEVERITY,
)
else:
assert_empty_report(report=context.report, meta_name=plugin.__class__.__name__)


@pytest.mark.parametrize(
"plugin,node_type,rpc_result",
(
pytest.param(
GethAccountImport(keydata=TEST_PK, password=TEST_PASSWORD),
NodeType.GETH,
{},
id="geth missing payload",
),
pytest.param(
GethAccountImport(keydata=TEST_PK, password=TEST_PASSWORD),
NodeType.PARITY,
{},
id="parity missing payload",
),
pytest.param(
GethAccountImport(keydata=TEST_PK, password=TEST_PASSWORD),
NodeType.GETH,
{"id": 1, "jsonrpc": "2.0", "error": {}},
id="geth error present",
),
pytest.param(
GethAccountImport(keydata=TEST_PK, password=TEST_PASSWORD),
NodeType.PARITY,
{"id": 1, "jsonrpc": "2.0", "error": {}},
id="parity error present",
),
),
)
def test_no_issue_found(plugin, node_type, rpc_result):
context = Context(
target=TARGET, report=Report(target=TARGET, issues=[]), node_type=node_type
)
mocked_execute(
target=TARGET,
rpc_result=rpc_result,
plugin=plugin,
context=context,
rpc_method=RPC_METHOD,
skipped=node_type == NodeType.PARITY,
)
assert_empty_report(report=context.report, meta_name=plugin.__class__.__name__)
8 changes: 4 additions & 4 deletions tests/plugins/eth1/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
from teatime import Issue


def mocked_execute(target, rpc_result, plugin, context, rpc_method):
def mocked_execute(target, rpc_result, plugin, context, rpc_method, skipped=False):
with requests_mock.Mocker() as mock:
mock.request(
requests_mock.ANY,
target,
json=rpc_result,
)
plugin.run(context=context)

assert mock.called
assert mock.request_history[0].json()["method"] == rpc_method
if not skipped:
assert mock.called
assert mock.request_history[0].json()["method"] == rpc_method


def assert_report_has_issue(report, meta_name, title, description, rpc_raw, severity):
Expand Down

0 comments on commit 9b0f85a

Please sign in to comment.