From f0a109a14519d09518a32796b728c2108b7316a2 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 7 Jun 2021 12:53:32 -0400 Subject: [PATCH] island: Add tests for ssl_certificate_key_file --- .../cc/setup/test_island_config_options.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py b/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py index 7219ba0fd..193cc7350 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py +++ b/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py @@ -3,6 +3,7 @@ import os from monkey_island.cc.server_utils.consts import ( DEFAULT_CRT_PATH, DEFAULT_DATA_DIR, + DEFAULT_KEY_PATH, DEFAULT_LOG_LEVEL, DEFAULT_START_MONGO_DB, ) @@ -110,6 +111,43 @@ def assert_ssl_certificate_file_equals(config_file_contents, expected_ssl_certif ) +def test_key_path_uses_default(): + assert_ssl_certificate_key_file_equals(TEST_CONFIG_FILE_CONTENTS_UNSPECIFIED, DEFAULT_KEY_PATH) + + +def test_key_path_specified(): + assert_ssl_certificate_key_file_equals( + TEST_CONFIG_FILE_CONTENTS_SPECIFIED, + TEST_CONFIG_FILE_CONTENTS_SPECIFIED["ssl_certificate"]["ssl_certificate_key_file"], + ) + + +def test_key_path_expanduser(monkeypatch, tmpdir): + set_home_env(monkeypatch, tmpdir) + FILE_NAME = "test.key" + + assert_ssl_certificate_key_file_equals( + {"ssl_certificate": {"ssl_certificate_key_file": os.path.join("~", FILE_NAME)}}, + os.path.join(tmpdir, FILE_NAME), + ) + + +def test_key_path_expandvars(monkeypatch, tmpdir): + set_home_env(monkeypatch, tmpdir) + FILE_NAME = "test.key" + + assert_ssl_certificate_key_file_equals( + {"ssl_certificate": {"ssl_certificate_key_file": os.path.join("$HOME", FILE_NAME)}}, + os.path.join(tmpdir, FILE_NAME), + ) + + +def assert_ssl_certificate_key_file_equals(config_file_contents, expected_ssl_certificate_file): + assert_island_config_option_equals( + config_file_contents, "key_path", expected_ssl_certificate_file + ) + + def assert_island_config_option_equals(config_file_contents, option_name, expected_value): options = IslandConfigOptions(config_file_contents) assert getattr(options, option_name) == expected_value