Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tests #195

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/commands/tests/test_create_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def test_basic(
str(vault_address),
'--data-dir',
str(data_dir),
'--pool-size',
'2',
]
result = runner.invoke(create_keys, args)
assert result.exit_code == 0
Expand Down Expand Up @@ -76,6 +78,8 @@ def test_per_keystore_password(
str(vault_address),
'--data-dir',
str(data_dir),
'--pool-size',
'2',
'--per-keystore-password',
]
result = runner.invoke(create_keys, args)
Expand Down
44 changes: 36 additions & 8 deletions src/commands/tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,62 @@

@patch('src.common.language.get_mnemonic', return_value=mnemonic)
class TestCreateMnemonic:
def test_basic(self, mnemonic_mock, runner: CliRunner):
def test_basic(self, mnemonic_mock, data_dir, runner: CliRunner):
vault = faker.eth_address()
args = ['--language', 'english', '--vault', vault, '--network', 'goerli']
args = [
'--data-dir',
str(data_dir),
'--language',
'english',
'--vault',
vault,
'--network',
'goerli',
]
result = runner.invoke(init, args, input=f'\n{mnemonic}\n')
assert result.exit_code == 0
mnemonic_mock.assert_called_once()
assert mnemonic in result.output.strip()
assert 'Successfully initialized configuration' in result.output.strip()

def test_bad_verify(self, mnemonic_mock, runner: CliRunner):
def test_bad_verify(self, mnemonic_mock, data_dir, runner: CliRunner):
vault = faker.eth_address()
args = ['--language', 'english', '--vault', vault, '--network', 'goerli']
args = [
'--data-dir',
str(data_dir),
'--language',
'english',
'--vault',
vault,
'--network',
'goerli',
]
result = runner.invoke(init, args, input=f'\n{mnemonic} bad\n\n{mnemonic}\n')
assert result.exit_code == 0
mnemonic_mock.assert_called_once()
assert mnemonic in result.output.strip()
assert 'Successfully initialized configuration' in result.output.strip()

def test_no_verify(self, mnemonic_mock, runner: CliRunner):
def test_no_verify(self, mnemonic_mock, data_dir, runner: CliRunner):
vault = faker.eth_address()
args = ['--language', 'english', '--no-verify', '--vault', vault, '--network', 'goerli']
args = [
'--data-dir',
str(data_dir),
'--language',
'english',
'--no-verify',
'--vault',
vault,
'--network',
'goerli',
]
result = runner.invoke(init, args)
assert result.exit_code == 0
mnemonic_mock.assert_called_once()
assert mnemonic == result.output.strip()

def test_bad_language(self, _, runner: CliRunner):
args = ['--language', 'bad', '--no-verify']
def test_bad_language(self, data_dir, runner: CliRunner):
args = ['--data-dir', str(data_dir), '--language', 'bad', '--no-verify']
result = runner.invoke(init, args)
assert result.exit_code == 2
assert "Invalid value for '--language': 'bad' is not one of" in result.output.strip()
8 changes: 5 additions & 3 deletions src/commands/tests/test_remote_signer_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def test_invalid_input(
assert result.exit_code == 2
assert "Error: Missing option '--remote-signer-url'" in result.output

@pytest.mark.usefixtures('_init_vault', '_create_keys', 'mocked_remote_signer')
@pytest.mark.usefixtures(
'_init_vault', '_create_keys', 'mocked_remote_signer', 'mock_scrypt_keystore'
)
async def test_basic(
self,
vault_address: HexAddress,
Expand Down Expand Up @@ -100,7 +102,7 @@ async def test_basic(
pubkeys_remote_signer = set(await resp.json())
assert len(pubkeys_remote_signer) == key_count * oracle_count

@pytest.mark.usefixtures('_init_vault', 'mocked_remote_signer')
@pytest.mark.usefixtures('_init_vault', 'mocked_remote_signer', 'mock_scrypt_keystore')
def test_add_more_keys_later(
self,
vault_address: HexAddress,
Expand Down Expand Up @@ -157,7 +159,7 @@ def test_add_more_keys_later(
assert len(config.pubkeys_to_shares) == key_count_total

@pytest.mark.parametrize(['remove_existing_keys'], [pytest.param(False), pytest.param(True)])
@pytest.mark.usefixtures('_init_vault', '_remote_signer_setup')
@pytest.mark.usefixtures('_init_vault', '_remote_signer_setup', 'mock_scrypt_keystore')
def test_oracle_set_change(
self,
vault_address: HexAddress,
Expand Down
31 changes: 30 additions & 1 deletion src/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import pytest
from _pytest.fixtures import SubRequest
from click.testing import CliRunner
from Crypto.Protocol.KDF import scrypt as raw_scrypt
from eth_typing import HexAddress, HexStr
from sw_utils.tests import faker

from src.commands.create_keys import create_keys
from src.commands.create_wallet import create_wallet
from src.commands.remote_signer_setup import remote_signer_setup
from src.common.credentials import CredentialManager
from src.common.credentials import CredentialManager, ScryptKeystore
from src.common.typings import Oracles
from src.common.vault_config import VaultConfig
from src.config.networks import GOERLI
Expand Down Expand Up @@ -75,12 +76,38 @@ def runner() -> CliRunner:
return CliRunner()


def _scrypt_without_validation(
*, password: str, salt: str, n: int, r: int, p: int, dklen: int
) -> bytes:
"""
Shortened version of `staking_deposit.utils.crypto.scrypt`.
All validations are deleted to allow small number of hash iterations (`n`).
The function is not secure. Use it in tests only.
"""
res = raw_scrypt(password=password, salt=salt, key_len=dklen, N=n, r=r, p=p)
return res if isinstance(res, bytes) else res[0] # PyCryptodome can return Tuple[bytes]


@pytest.fixture
def mock_scrypt_keystore():
"""
Decreases number of iterations of password hashing. Original value is ~200k.
This improves speed of keystore encryption.
Not secure.
"""
with mock.patch.dict(ScryptKeystore.crypto.kdf.params, {'n': 2}), mock.patch(
'staking_deposit.key_handling.keystore.scrypt', new=_scrypt_without_validation
):
yield


@pytest.fixture
def _create_keys(
test_mnemonic: str,
vault_address: HexAddress,
data_dir: Path,
_test_keystore_password_file: Path,
mock_scrypt_keystore,
runner: CliRunner,
) -> None:
count = 3
Expand All @@ -96,6 +123,8 @@ def _create_keys(
str(vault_address),
'--data-dir',
str(data_dir),
'--pool-size',
'1',
],
)
assert result.exit_code == 0
Expand Down
Loading