Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_save_api_key(self, key_option):
"""Save API key to configuration file."""
runner = CliRunner()
api_key = ""
expected_config = {
"api_key": api_key,
"api_server": DEFAULT_CONFIG["api_server"],
"timeout": DEFAULT_CONFIG["timeout"],
}
expected_output = "Configuration saved to {!r}\n".format(CONFIG_FILE)
with patch("greynoise.cli.subcommand.save_config") as save_config:
result = runner.invoke(subcommand.setup, [key_option, api_key])
assert result.exit_code == 0
assert result.output == expected_output
save_config.assert_called_with(expected_config)
def test_save_api_key_and_timeout(self, key_option, server_option, timeout_option):
"""Save API key and timeout to configuration file."""
runner = CliRunner()
api_key = ""
api_server = ""
timeout = 123456
expected_config = {
"api_key": api_key,
"api_server": api_server,
"timeout": timeout,
}
expected_output = "Configuration saved to {!r}\n".format(CONFIG_FILE)
with patch("greynoise.cli.subcommand.save_config") as save_config:
result = runner.invoke(
subcommand.setup,
[
key_option,
api_key,
server_option,
api_server,
timeout_option,
timeout,
],
)
assert result.exit_code == 0
assert result.output == expected_output
save_config.assert_called_with(expected_config)
def test_save_config_dir_created(self):
"""Configuration directory created if missing."""
config = {
"api_key": "",
"api_server": "",
"timeout": 123456,
}
with patch("greynoise.util.os") as os, patch("greynoise.util.open") as open_:
os.path.isdir.return_value = False
config_file = StringIO()
open_().__enter__.return_value = config_file
save_config(config)
os.makedirs.assert_called_with(os.path.dirname(CONFIG_FILE))
def setup(api_key, timeout, api_server):
"""Configure API key."""
config = {"api_key": api_key}
if timeout is None:
config["timeout"] = DEFAULT_CONFIG["timeout"]
else:
config["timeout"] = timeout
if api_server is None:
config["api_server"] = DEFAULT_CONFIG["api_server"]
else:
config["api_server"] = api_server
save_config(config)
click.echo("Configuration saved to {!r}".format(CONFIG_FILE))