Skip to content

Commit

Permalink
Will ignore connectivity when using non_interactive
Browse files Browse the repository at this point in the history
Updating required python version to 3.9
Adding test_deposit.py
  • Loading branch information
valefar-on-discord committed May 7, 2024
1 parent 75c8497 commit 4080a18
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ The Launchpad offers many language/internationalization options. If you wish to

###### `--non_interactive` flag

**Warning: With this flag, there will be no confirmation step(s) to verify the input value(s). Please use it carefully.**
**Warning: With this flag, there will be no confirmation step(s) to verify the input value(s). This will also ignore the connectivity check. Please use it carefully.**

| Argument | Type | Description |
| -------- | -------- | -------- |
Expand Down
8 changes: 4 additions & 4 deletions staking_deposit/deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def check_python_version() -> None:
'''
Checks that the python version running is sufficient and exits if not.
'''
if sys.version_info < (3, 7):
if sys.version_info < (3, 9):
click.pause(load_text(['err_python_version']))
sys.exit()

Expand All @@ -38,7 +38,7 @@ def check_connectivity() -> None:
socket.setdefaulttimeout(2)
socket.getaddrinfo('icann.org', 80)
click.pause(load_text(['connectivity_warning']))
except: # noqa: E722
except OSError:
return None


Expand All @@ -62,7 +62,7 @@ def check_connectivity() -> None:
is_flag=True,
help=(
'Disables interactive prompts. Warning: With this flag, there will be no confirmation step(s) to verify the '
'input value(s). Please use it carefully.'
'input value(s). This will also ignore the connectivity check. Please use it carefully.'
),
hidden=False,
)
Expand All @@ -77,7 +77,7 @@ def check_connectivity() -> None:
hidden=False,
)
def cli(ctx: click.Context, language: str, non_interactive: bool, ignore_connectivity: bool) -> None:
if not ignore_connectivity:
if not ignore_connectivity and not non_interactive:
check_connectivity()
config.language = language
config.non_interactive = non_interactive # Remove interactive commands
Expand Down
2 changes: 0 additions & 2 deletions tests/test_cli/test_existing_mnemonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ async def test_script() -> None:
run_script_cmd,
'--language', 'english',
'--non_interactive',
'--ignore_connectivity',
'existing-mnemonic',
'--num_validators', '1',
'--mnemonic="abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"',
Expand Down Expand Up @@ -238,7 +237,6 @@ async def test_script_abbreviated_mnemonic() -> None:
run_script_cmd,
'--language', 'english',
'--non_interactive',
'--ignore_connectivity',
'existing-mnemonic',
'--num_validators', '1',
'--mnemonic="aban aban aban aban aban aban aban aban aban aban aban abou"',
Expand Down
2 changes: 0 additions & 2 deletions tests/test_cli/test_generate_bls_to_execution_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def test_existing_mnemonic_bls_withdrawal() -> None:
arguments = [
'--language', 'english',
'--non_interactive',
'--ignore_connectivity',
'generate-bls-to-execution-change',
'--bls_to_execution_changes_folder', my_folder_path,
'--chain', 'mainnet',
Expand Down Expand Up @@ -96,7 +95,6 @@ def test_existing_mnemonic_bls_withdrawal_multiple() -> None:
data = '\n'.join(inputs)
arguments = [
'--language', 'english',
'--ignore_connectivity',
'--non_interactive',
'generate-bls-to-execution-change',
'--bls_to_execution_changes_folder', my_folder_path,
Expand Down
2 changes: 0 additions & 2 deletions tests/test_cli/test_new_mnemonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ async def test_script_bls_withdrawal() -> None:
run_script_cmd,
'--language', 'english',
'--non_interactive',
'--ignore_connectivity',
'new-mnemonic',
'--num_validators', '5',
'--mnemonic_language', 'english',
Expand Down Expand Up @@ -380,7 +379,6 @@ async def test_script_abbreviated_mnemonic() -> None:
run_script_cmd,
'--language', 'english',
'--non_interactive',
'--ignore_connectivity',
'new-mnemonic',
'--num_validators', '5',
'--mnemonic_language', 'english',
Expand Down
221 changes: 221 additions & 0 deletions tests/test_deposit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
import os
import socket
import sys

import click
from click.testing import CliRunner

from staking_deposit.deposit import check_connectivity, check_python_version, cli
from tests.test_cli.helpers import clean_key_folder


def test_should_notify_user_and_exit_if_invalid_python_version(monkeypatch) -> None:
exit_called = False

def _mock_sys_exit():
nonlocal exit_called
exit_called = True

pause_called = False

def _mock_click_pause(text):
nonlocal pause_called
pause_called = True

monkeypatch.setattr(click, 'pause', _mock_click_pause)
monkeypatch.setattr(sys, 'exit', _mock_sys_exit)
monkeypatch.setattr(sys, 'version_info', (3, 8))

check_python_version()

assert exit_called is True
assert pause_called is True


def test_should_not_exit_if_valid_python_version(monkeypatch) -> None:
exit_called = False

def _mock_sys_exit():
nonlocal exit_called
exit_called = True

monkeypatch.setattr(sys, 'exit', _mock_sys_exit)
monkeypatch.setattr(sys, 'version_info', (3, 9))

check_python_version()

assert exit_called is False


def test_should_pause_if_connected(monkeypatch) -> None:
pause_called = False

def _mock_click_pause(text):
nonlocal pause_called
pause_called = True

def _mock_socket_getaddrinfo(url, port):
return True

monkeypatch.setattr(click, 'pause', _mock_click_pause)
monkeypatch.setattr(socket, 'getaddrinfo', _mock_socket_getaddrinfo)

check_connectivity()
assert pause_called is True


def test_should_not_pause_if_not_connected(monkeypatch) -> None:
pause_called = False

def _mock_click_pause(text):
nonlocal pause_called
pause_called = True

def _mock_socket_getaddrinfo(url, port):
raise OSError()

monkeypatch.setattr(click, 'pause', _mock_click_pause)
monkeypatch.setattr(socket, 'getaddrinfo', _mock_socket_getaddrinfo)

check_connectivity()
assert pause_called is False


def test_should_check_connectivity_by_default(monkeypatch) -> None:
connectivity_called = False

def _mock_socket_getaddrinfo(url, port):
nonlocal connectivity_called
connectivity_called = True
raise OSError()

monkeypatch.setattr(socket, 'getaddrinfo', _mock_socket_getaddrinfo)

my_folder_path = os.path.join(os.getcwd(), 'TESTING_TEMP_FOLDER')
clean_key_folder(my_folder_path)
if not os.path.exists(my_folder_path):
os.mkdir(my_folder_path)
runner = CliRunner()
inputs = [
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about',
'0', '0', '1', 'mainnet', 'MyPassword', 'MyPassword']
data = '\n'.join(inputs)
arguments = [
'--language', 'english',
'existing-mnemonic',
'--eth1_withdrawal_address', '',
'--folder', my_folder_path,

]
result = runner.invoke(cli, arguments, input=data)

assert result.exit_code == 0
assert connectivity_called is True

clean_key_folder(my_folder_path)


def test_should_not_check_connectivity_with_ignore_connectivity(monkeypatch) -> None:
connectivity_called = False

def _mock_socket_getaddrinfo(url, port):
nonlocal connectivity_called
connectivity_called = True
raise OSError()

monkeypatch.setattr(socket, 'getaddrinfo', _mock_socket_getaddrinfo)
my_folder_path = os.path.join(os.getcwd(), 'TESTING_TEMP_FOLDER')
clean_key_folder(my_folder_path)
if not os.path.exists(my_folder_path):
os.mkdir(my_folder_path)
runner = CliRunner()
inputs = [
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about',
'0', '0', '1', 'mainnet', 'MyPassword', 'MyPassword']
data = '\n'.join(inputs)
arguments = [
'--language', 'english',
'--ignore_connectivity',
'existing-mnemonic',
'--eth1_withdrawal_address', '',
'--folder', my_folder_path,

]
result = runner.invoke(cli, arguments, input=data)

assert result.exit_code == 0
assert connectivity_called is False

clean_key_folder(my_folder_path)


def test_should_not_check_connectivity_with_non_interactive(monkeypatch) -> None:
connectivity_called = False

def _mock_socket_getaddrinfo(url, port):
nonlocal connectivity_called
connectivity_called = True
raise OSError()

monkeypatch.setattr(socket, 'getaddrinfo', _mock_socket_getaddrinfo)

my_folder_path = os.path.join(os.getcwd(), 'TESTING_TEMP_FOLDER')
clean_key_folder(my_folder_path)
if not os.path.exists(my_folder_path):
os.mkdir(my_folder_path)
runner = CliRunner()
arguments = [
'--language', 'english',
'--non_interactive',
'existing-mnemonic',
'--num_validators', '1',
'--mnemonic', 'aban aban aban aban aban aban aban aban aban aban aban abou',
'--validator_start_index', '0',
'--chain', 'mainnet',
'--keystore_password', 'MyPassword',
'--eth1_withdrawal_address', '',
'--folder', my_folder_path,
]
result = runner.invoke(cli, arguments)

assert result.exit_code == 0
assert connectivity_called is False

clean_key_folder(my_folder_path)


def test_should_not_check_connectivity_with_both_non_interactive_or_ignore_connectivity(monkeypatch) -> None:
connectivity_called = False

def _mock_socket_getaddrinfo(url, port):
nonlocal connectivity_called
connectivity_called = True
raise OSError()

monkeypatch.setattr(socket, 'getaddrinfo', _mock_socket_getaddrinfo)

my_folder_path = os.path.join(os.getcwd(), 'TESTING_TEMP_FOLDER')
clean_key_folder(my_folder_path)
if not os.path.exists(my_folder_path):
os.mkdir(my_folder_path)
runner = CliRunner()
arguments = [
'--language', 'english',
'--non_interactive',
'--ignore_connectivity',
'existing-mnemonic',
'--num_validators', '1',
'--mnemonic', 'aban aban aban aban aban aban aban aban aban aban aban abou',
'--mnemonic_password', 'TREZOR',
'--validator_start_index', '0',
'--chain', 'mainnet',
'--keystore_password', 'MyPassword',
'--eth1_withdrawal_address', '',
'--folder', my_folder_path,
]
result = runner.invoke(cli, arguments)

assert result.exit_code == 0
assert connectivity_called is False

clean_key_folder(my_folder_path)

0 comments on commit 4080a18

Please sign in to comment.