Skip to content

Commit

Permalink
Add test for spacecmd function cryptokey_update
Browse files Browse the repository at this point in the history
  • Loading branch information
blu-base committed Jan 29, 2023
1 parent a79995b commit 58cb700
Showing 1 changed file with 158 additions and 0 deletions.
158 changes: 158 additions & 0 deletions spacecmd/tests/test_cryptokey.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,164 @@ def test_cryptokey_create_SSL_key(self, shell):
assert args == (shell.session, "description", "SSL", "contents")
assert not kw

def test_cryptokey_update_no_keytype(self, shell):
"""
Test do_cryptokey_update without correct key type.
:param shell:
:return:
"""
shell.help_cryptokey_update = MagicMock()
shell.client.kickstart.keys.update = MagicMock()
shell.user_confirm = MagicMock(return_value=True)
read_file = MagicMock(return_value="contents")
prompt_user = MagicMock(side_effect=["", "interactive descr", "/tmp/file.txt"])
editor = MagicMock()
logger = MagicMock()

with patch("spacecmd.cryptokey.prompt_user", prompt_user) as pmu, \
patch("spacecmd.cryptokey.read_file", read_file) as rfl, \
patch("spacecmd.cryptokey.editor", editor) as edt, \
patch("spacecmd.cryptokey.logging", logger) as lgr:
spacecmd.cryptokey.do_cryptokey_update(shell, "")

assert not shell.help_cryptokey_update.called
assert not shell.client.kickstart.keys.update.called
assert not editor.called
assert read_file.called
assert prompt_user.called
assert logger.error.called

assert_expect(logger.error.call_args_list, "Invalid key type")

def test_cryptokey_update_interactive_no_contents(self, shell):
"""
Test do_cryptokey_update without arguments (interactive, no contents given).
:param shell:
:return:
"""
shell.help_cryptokey_update = MagicMock()
shell.client.kickstart.keys.update = MagicMock()
shell.user_confirm = MagicMock(return_value=True)
read_file = MagicMock()
prompt_user = MagicMock(side_effect=["g", "interactive descr", ""])
editor = MagicMock()
logger = MagicMock()

with patch("spacecmd.cryptokey.prompt_user", prompt_user) as pmu, \
patch("spacecmd.cryptokey.read_file", read_file) as rfl, \
patch("spacecmd.cryptokey.editor", editor) as edt, \
patch("spacecmd.cryptokey.logging", logger) as lgr:
spacecmd.cryptokey.do_cryptokey_update(shell, "")

assert not shell.help_cryptokey_update.called
assert not shell.client.kickstart.keys.update.called
assert not read_file.called
assert not editor.called
assert prompt_user.called
assert logger.error.called

assert_expect(logger.error.call_args_list, "No contents of the file")

def test_cryptokey_update_interactive_wrong_key_type(self, shell):
"""
Test do_cryptokey_update without arguments (interactive, wrong key type).
:param shell:
:return:
"""
shell.help_cryptokey_update = MagicMock()
shell.client.kickstart.keys.update = MagicMock()
shell.user_confirm = MagicMock(return_value=True)
read_file = MagicMock(return_value="contents")
prompt_user = MagicMock(side_effect=["x", "interactive descr", "/tmp/file.txt"])
editor = MagicMock()
logger = MagicMock()

with patch("spacecmd.cryptokey.prompt_user", prompt_user) as pmu, \
patch("spacecmd.cryptokey.read_file", read_file) as rfl, \
patch("spacecmd.cryptokey.editor", editor) as edt, \
patch("spacecmd.cryptokey.logging", logger) as lgr:
spacecmd.cryptokey.do_cryptokey_update(shell, "")

assert not shell.help_cryptokey_update.called
assert not shell.client.kickstart.keys.update.called
assert not editor.called
assert read_file.called
assert prompt_user.called
assert logger.error.called

assert_expect(logger.error.call_args_list, "Invalid key type")

def test_cryptokey_update_GPG_key(self, shell):
"""
Test do_cryptokey_update with parameters, calling GPG key type.
:param shell:
:return:
"""
shell.help_cryptokey_update = MagicMock()
shell.client.kickstart.keys.update = MagicMock()
shell.user_confirm = MagicMock(return_value=True)
read_file = MagicMock(return_value="contents")
prompt_user = MagicMock(side_effect=[])
editor = MagicMock()
logger = MagicMock()

with patch("spacecmd.cryptokey.prompt_user", prompt_user) as pmu, \
patch("spacecmd.cryptokey.read_file", read_file) as rfl, \
patch("spacecmd.cryptokey.editor", editor) as edt, \
patch("spacecmd.cryptokey.logging", logger) as lgr:
spacecmd.cryptokey.do_cryptokey_update(shell, "-t g -d description -f /tmp/file.txt")

assert not editor.called
assert not shell.help_cryptokey_update.called
assert not prompt_user.called
assert not logger.error.called

assert shell.client.kickstart.keys.update.called
assert read_file.called

for call in shell.client.kickstart.keys.update.call_args_list:
args, kw = call
assert args == (shell.session, "description", "GPG", "contents")
assert not kw

def test_cryptokey_update_SSL_key(self, shell):
"""
Test do_cryptokey_update with parameters, calling SSL key type.
:param shell:
:return:
"""
shell.help_cryptokey_update = MagicMock()
shell.client.kickstart.keys.update = MagicMock()
shell.user_confirm = MagicMock(return_value=True)
read_file = MagicMock(return_value="contents")
prompt_user = MagicMock(side_effect=[])
editor = MagicMock()
logger = MagicMock()

with patch("spacecmd.cryptokey.prompt_user", prompt_user) as pmu, \
patch("spacecmd.cryptokey.read_file", read_file) as rfl, \
patch("spacecmd.cryptokey.editor", editor) as edt, \
patch("spacecmd.cryptokey.logging", logger) as lgr:
spacecmd.cryptokey.do_cryptokey_update(shell, "-t s -d description -f /tmp/file.txt")

assert not editor.called
assert not shell.help_cryptokey_update.called
assert not prompt_user.called
assert not logger.error.called

assert shell.client.kickstart.keys.update.called
assert read_file.called

for call in shell.client.kickstart.keys.update.call_args_list:
args, kw = call
assert args == (shell.session, "description", "SSL", "contents")
assert not kw

def test_cryptokey_delete_noargs(self, shell):
"""
Test do_cryptokey_delete without parameters, so help should be displayed.
Expand Down

0 comments on commit 58cb700

Please sign in to comment.