staking-deposit-cli/tests/test_cli.py
2020-05-12 15:35:56 +08:00

69 lines
2.1 KiB
Python

import asyncio
import os
import pytest
from click.testing import CliRunner
from eth2deposit import deposit
from eth2deposit.deposit import main
from eth2deposit.utils.constants import DEFAULT_VALIDATOR_KEYS_FOLDER_NAME
def test_deposit(monkeypatch):
# monkeypatch get_mnemonic
def get_mnemonic(language, words_path, entropy=None):
return "fakephrase"
monkeypatch.setattr(deposit, "get_mnemonic", get_mnemonic)
# Prepare folder
my_folder_path = os.path.join(os.getcwd(), 'my_folder_name')
if not os.path.exists(my_folder_path):
os.mkdir(my_folder_path)
runner = CliRunner()
inputs = ['1', 'english', 'MyPassword', 'MyPassword', 'fakephrase']
data = '\n'.join(inputs)
result = runner.invoke(main, ['--folder', my_folder_path], input=data)
assert result.exit_code == 0
# Check files
validator_keys_folder_path = os.path.join(my_folder_path, DEFAULT_VALIDATOR_KEYS_FOLDER_NAME)
_, _, key_files = next(os.walk(validator_keys_folder_path))
assert len(key_files) == 2
# Clean up
for key_file_name in key_files:
os.remove(os.path.join(validator_keys_folder_path, key_file_name))
os.rmdir(validator_keys_folder_path)
os.rmdir(my_folder_path)
@pytest.mark.asyncio
async def test_script():
cmd = "./deposit.sh --num_validators 1 --mnemonic_language english --password MyPassword"
proc = await asyncio.create_subprocess_shell(
cmd,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
)
seed_phrase = ''
parsing = False
async for out in proc.stdout:
output = out.decode('utf-8').rstrip()
if output.startswith("This is your seed phrase."):
parsing = True
elif output.startswith("Please type your mnemonic"):
parsing = False
elif parsing:
seed_phrase += output
if len(seed_phrase) > 0:
encoded_phrase = seed_phrase.encode()
proc.stdin.write(encoded_phrase)
proc.stdin.write(b'\n')
assert len(seed_phrase) > 0