From 4231f316db86eb19ec07a63b15b22a94f8d3d653 Mon Sep 17 00:00:00 2001
From: Mike Salvatore <mike.s.salvatore@gmail.com>
Date: Mon, 7 Jun 2021 12:41:11 -0400
Subject: [PATCH] island: Add tests for ssl_certificate_file

---
 .../cc/setup/test_island_config_options.py    | 42 +++++++++++++++++++
 1 file changed, 42 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 79cd15f25..7219ba0fd 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
@@ -1,6 +1,7 @@
 import os
 
 from monkey_island.cc.server_utils.consts import (
+    DEFAULT_CRT_PATH,
     DEFAULT_DATA_DIR,
     DEFAULT_LOG_LEVEL,
     DEFAULT_START_MONGO_DB,
@@ -11,6 +12,10 @@ TEST_CONFIG_FILE_CONTENTS_SPECIFIED = {
     "data_dir": "/tmp",
     "log_level": "test",
     "mongodb": {"start_mongodb": False},
+    "ssl_certificate": {
+        "ssl_certificate_file": "/tmp/test.crt",
+        "ssl_certificate_key_file": "/tmp/test.key",
+    },
 }
 
 TEST_CONFIG_FILE_CONTENTS_UNSPECIFIED = {}
@@ -68,6 +73,43 @@ def test_mongodb():
     assert options.start_mongodb == DEFAULT_START_MONGO_DB
 
 
+def test_crt_path_uses_default():
+    assert_ssl_certificate_file_equals(TEST_CONFIG_FILE_CONTENTS_UNSPECIFIED, DEFAULT_CRT_PATH)
+
+
+def test_crt_path_specified():
+    assert_ssl_certificate_file_equals(
+        TEST_CONFIG_FILE_CONTENTS_SPECIFIED,
+        TEST_CONFIG_FILE_CONTENTS_SPECIFIED["ssl_certificate"]["ssl_certificate_file"],
+    )
+
+
+def test_crt_path_expanduser(monkeypatch, tmpdir):
+    set_home_env(monkeypatch, tmpdir)
+    FILE_NAME = "test.crt"
+
+    assert_ssl_certificate_file_equals(
+        {"ssl_certificate": {"ssl_certificate_file": os.path.join("~", FILE_NAME)}},
+        os.path.join(tmpdir, FILE_NAME),
+    )
+
+
+def test_crt_path_expandvars(monkeypatch, tmpdir):
+    set_home_env(monkeypatch, tmpdir)
+    FILE_NAME = "test.crt"
+
+    assert_ssl_certificate_file_equals(
+        {"ssl_certificate": {"ssl_certificate_file": os.path.join("$HOME", FILE_NAME)}},
+        os.path.join(tmpdir, FILE_NAME),
+    )
+
+
+def assert_ssl_certificate_file_equals(config_file_contents, expected_ssl_certificate_file):
+    assert_island_config_option_equals(
+        config_file_contents, "crt_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