2019-05-11 22:11:44 +00:00
|
|
|
import unittest
|
|
|
|
import unittest.mock
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
from tutor.commands import config as tutor_config
|
|
|
|
from tutor import env
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigTests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
# This is necessary to avoid cached mocks
|
|
|
|
env.Renderer.reset()
|
|
|
|
|
2019-05-20 17:09:58 +00:00
|
|
|
def test_version(self):
|
|
|
|
defaults = tutor_config.load_defaults()
|
|
|
|
self.assertNotIn("TUTOR_VERSION", defaults)
|
|
|
|
|
2019-05-11 22:11:44 +00:00
|
|
|
def test_merge(self):
|
2019-05-29 09:14:06 +00:00
|
|
|
config1 = {"x": "y"}
|
|
|
|
config2 = {"x": "z"}
|
|
|
|
tutor_config.merge(config1, config2)
|
|
|
|
self.assertEqual({"x": "y"}, config1)
|
|
|
|
|
|
|
|
def test_merge_render(self):
|
2019-05-11 22:11:44 +00:00
|
|
|
config = {}
|
|
|
|
defaults = tutor_config.load_defaults()
|
|
|
|
with unittest.mock.patch.object(
|
|
|
|
tutor_config.utils, "random_string", return_value="abcd"
|
|
|
|
):
|
|
|
|
tutor_config.merge(config, defaults)
|
|
|
|
|
|
|
|
self.assertEqual("abcd", config["MYSQL_ROOT_PASSWORD"])
|
2019-05-29 09:20:37 +00:00
|
|
|
|
2019-05-11 22:10:14 +00:00
|
|
|
@unittest.mock.patch.object(tutor_config.fmt, "echo")
|
|
|
|
def test_save_twice(self, mock_echo):
|
2019-05-11 22:11:44 +00:00
|
|
|
with tempfile.TemporaryDirectory() as root:
|
|
|
|
tutor_config.save(root, silent=True)
|
|
|
|
config1 = tutor_config.load_user(root)
|
|
|
|
|
|
|
|
tutor_config.save(root, silent=True)
|
|
|
|
config2 = tutor_config.load_user(root)
|
|
|
|
|
|
|
|
self.assertEqual(config1, config2)
|
|
|
|
|
2019-05-11 22:10:14 +00:00
|
|
|
@unittest.mock.patch.object(tutor_config.fmt, "echo")
|
|
|
|
def test_removed_entry_is_added_on_save(self, mock_echo):
|
2019-05-11 22:11:44 +00:00
|
|
|
with tempfile.TemporaryDirectory() as root:
|
|
|
|
with unittest.mock.patch.object(
|
|
|
|
tutor_config.utils, "random_string"
|
|
|
|
) as mock_random_string:
|
|
|
|
mock_random_string.return_value = "abcd"
|
2019-05-20 17:09:58 +00:00
|
|
|
config1, defaults = tutor_config.load_all(root)
|
2019-05-11 22:11:44 +00:00
|
|
|
password1 = config1["MYSQL_ROOT_PASSWORD"]
|
|
|
|
|
|
|
|
config1.pop("MYSQL_ROOT_PASSWORD")
|
|
|
|
tutor_config.save_config(root, config1)
|
|
|
|
|
|
|
|
mock_random_string.return_value = "efgh"
|
2019-05-20 17:09:58 +00:00
|
|
|
config2, defaults = tutor_config.load_all(root)
|
2019-05-11 22:11:44 +00:00
|
|
|
password2 = config2["MYSQL_ROOT_PASSWORD"]
|
|
|
|
|
|
|
|
self.assertEqual("abcd", password1)
|
|
|
|
self.assertEqual("efgh", password2)
|