From 2098a2e293427c75ec0f70886a3dc130c97035ef Mon Sep 17 00:00:00 2001 From: Olivier Leger Date: Wed, 14 Oct 2020 11:48:28 -0400 Subject: [PATCH 1/2] removed six import, fixed typo and improved mongo secure test --- tests/test_config.py | 43 +++++++++++++++++++++++++++++++++++++++++++ tests/utils.py | 6 ++---- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/tests/test_config.py b/tests/test_config.py index 4d3e3f2..a43b3c5 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -377,6 +377,10 @@ def test_force_secure_mongo(): new=write_trigger_upsert_db_users) def test_secure_mongo_advanced_options(): config_object = read_config() + config_object._Config__config["advanced"] = Config.TRUE + + # Try when setup is run for the first time. + config_object._Config__first_time = True with patch("helpers.cli.CLI.colored_input") as mock_ci: mock_ci.side_effect = iter([ "root", @@ -387,6 +391,45 @@ def test_secure_mongo_advanced_options(): config_object._Config__questions_mongo() assert not os.path.exists("/tmp/upsert_db_users") + # Try when setup has been already run once + # If it's an upgrade, users should not see: + # ╔══════════════════════════════════════════════════════╗ + # ║ MongoDB root's and/or user's usernames have changed! ║ + # ╚══════════════════════════════════════════════════════╝ + config_object._Config__first_time = False + config_object._Config__config["mongo_secured"] = Config.FALSE + + with patch("helpers.cli.CLI.colored_input") as mock_ci: + mock_ci.side_effect = iter([ + "root", + "root_password", + "mongo_kobo_user", + "mongo_password" + ]) + config_object._Config__questions_mongo() + assert os.path.exists("/tmp/upsert_db_users") + assert os.path.getsize("/tmp/upsert_db_users") == 0 + os.remove("/tmp/upsert_db_users") + + # Try when setup has been already run once + # If it's NOT an upgrade, Users should see: + # ╔══════════════════════════════════════════════════════╗ + # ║ MongoDB root's and/or user's usernames have changed! ║ + # ╚══════════════════════════════════════════════════════╝ + config_object._Config__config["mongo_secured"] = Config.TRUE + with patch("helpers.cli.CLI.colored_input") as mock_ci: + mock_ci.side_effect = iter([ + "root", + "root_passw0rd", + "kobo_user", + "mongo_password", + Config.TRUE + ]) + config_object._Config__questions_mongo() + assert os.path.exists("/tmp/upsert_db_users") + assert os.path.getsize("/tmp/upsert_db_users") != 0 + os.remove("/tmp/upsert_db_users") + @patch('helpers.config.Config._Config__write_upsert_db_users_trigger_file', new=write_trigger_upsert_db_users) diff --git a/tests/utils.py b/tests/utils.py index 1998a6f..7b31c69 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -9,10 +9,8 @@ from mock import patch, mock_open builtin_open = "__builtin__.open" -from six import with_metaclass - from helpers.config import Config -from helpers.singleton import Singleton +from helpers.singleton import Singleton, with_metaclass def read_config(overrides=None): @@ -44,7 +42,7 @@ def run_command(command, cwd=None, polling=False): return mock_docker.compose(command, cwd) -def write_trigger_upsert_db_users_mock(*args): +def write_trigger_upsert_db_users(*args): content = args[1] with open("/tmp/upsert_db_users", "w") as f: f.write(content) From 2b760028847c1848b222306d2dbacef946a70cb7 Mon Sep 17 00:00:00 2001 From: Olivier Leger Date: Thu, 22 Oct 2020 09:52:54 -0400 Subject: [PATCH 2/2] fixed: tests with Python 2 --- helpers/singleton.py | 2 +- tests/test_config.py | 2 +- tests/test_run.py | 14 +++++++------- tests/utils.py | 23 +++++++++++++++-------- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/helpers/singleton.py b/helpers/singleton.py index 3ac501d..2bd691f 100644 --- a/helpers/singleton.py +++ b/helpers/singleton.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from __future__ import print_function, unicode_literals +from __future__ import print_function # Copy this method from `six` library to avoid import diff --git a/tests/test_config.py b/tests/test_config.py index a43b3c5..7713779 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals +from __future__ import unicode_literals, absolute_import import pytest import os diff --git a/tests/test_run.py b/tests/test_run.py index d9d6018..098bf7b 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -12,7 +12,7 @@ from helpers.config import Config from .utils import ( read_config, - run_command, + MockCommand, MockDocker, ) @@ -24,7 +24,7 @@ @patch('helpers.command.Command.info', MagicMock(return_value=True)) @patch('helpers.cli.CLI.run_command', - new=run_command) + new=MockCommand.run_command) def test_toggle_trivial(): config_object = read_config() Command.start() @@ -46,7 +46,7 @@ def test_toggle_trivial(): @patch('helpers.command.Command.info', MagicMock(return_value=True)) @patch('helpers.cli.CLI.run_command', - new=run_command) + new=MockCommand.run_command) def test_toggle_no_letsencrypt(): config_object = read_config() config_object._Config__config['use_letsencrypt'] = Config.FALSE @@ -68,7 +68,7 @@ def test_toggle_no_letsencrypt(): @patch('helpers.command.Command.info', MagicMock(return_value=True)) @patch('helpers.cli.CLI.run_command', - new=run_command) + new=MockCommand.run_command) def test_toggle_frontend(): config_object = read_config() Command.start(frontend_only=True) @@ -89,7 +89,7 @@ def test_toggle_frontend(): @patch('helpers.command.Command.info', MagicMock(return_value=True)) @patch('helpers.cli.CLI.run_command', - new=run_command) + new=MockCommand.run_command) def test_toggle_primary_backend(): config_object = read_config() config_object._Config__config['backend_server_role'] = 'primary' @@ -113,7 +113,7 @@ def test_toggle_primary_backend(): @patch('helpers.command.Command.info', MagicMock(return_value=True)) @patch('helpers.cli.CLI.run_command', - new=run_command) + new=MockCommand.run_command) def test_toggle_secondary_backend(): config_object = read_config() config_object._Config__config['backend_server_role'] = 'secondary' @@ -137,7 +137,7 @@ def test_toggle_secondary_backend(): @patch('helpers.command.Command.info', MagicMock(return_value=True)) @patch('helpers.cli.CLI.run_command', - new=run_command) + new=MockCommand.run_command) def test_toggle_maintenance(): config_object = read_config() mock_docker = MockDocker() diff --git a/tests/utils.py b/tests/utils.py index 7b31c69..51f39ec 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -34,20 +34,27 @@ def reset_config(config_object): config_object.__config = config_dict -def run_command(command, cwd=None, polling=False): - if 'docker-compose' != command[0]: - raise Exception('Command: `{}` is not implemented!'.format(command[0])) - - mock_docker = MockDocker() - return mock_docker.compose(command, cwd) - - def write_trigger_upsert_db_users(*args): content = args[1] with open("/tmp/upsert_db_users", "w") as f: f.write(content) +class MockCommand: + """ + Create a mock class for Python2 retro compatibility. + Python2 does not pass the class as the first argument explicitly when + `run_command` (as a standalone method) is used as a mock. + """ + @classmethod + def run_command(cls, command, cwd=None, polling=False): + if 'docker-compose' != command[0]: + raise Exception('Command: `{}` is not implemented!'.format(command[0])) + + mock_docker = MockDocker() + return mock_docker.compose(command, cwd) + + class MockDocker(with_metaclass(Singleton)): PRIMARY_BACKEND_CONTAINERS = ['primary_postgres', 'mongo', 'redis_main', 'redis_cache']