From 3024745bdec99e27ea6fb26726575d6401105c5a Mon Sep 17 00:00:00 2001 From: Simone Orsi Date: Fri, 10 Apr 2020 10:04:39 +0200 Subject: [PATCH] fixup! server_environment: running_env default to `test` (fix #44) --- server_environment/server_env.py | 7 ++++--- server_environment/tests/common.py | 15 --------------- .../tests/test_environment_variable.py | 19 ++++++++++--------- .../tests/test_server_environment.py | 18 ++++++++++++++++-- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/server_environment/server_env.py b/server_environment/server_env.py index 51dfd7e3..1999d6ce 100644 --- a/server_environment/server_env.py +++ b/server_environment/server_env.py @@ -190,9 +190,6 @@ def _build_model(cls, pool, cr): """ ModelClass = super(ServerConfiguration, cls)._build_model(pool, cr) ModelClass._add_columns() - ModelClass.running_env = system_base_config["running_env"] - # Only show passwords in development - ModelClass.show_passwords = ModelClass.running_env in ("dev",) ModelClass._arch = None ModelClass._build_osv() return ModelClass @@ -201,6 +198,10 @@ def _build_model(cls, pool, cr): def _format_key(cls, section, key): return "{}_I_{}".format(section, key) + @property + def show_passwords(self): + return system_base_config["running_env"] in ("dev",) + @classmethod def _format_key_display_name(cls, key_name): return key_name.replace("_I_", " | ") diff --git a/server_environment/tests/common.py b/server_environment/tests/common.py index 43682a46..519706f9 100644 --- a/server_environment/tests/common.py +++ b/server_environment/tests/common.py @@ -6,27 +6,12 @@ from unittest.mock import patch from odoo.tests import common -from odoo.tools.config import config import odoo.addons.server_environment.models.server_env_mixin as server_env_mixin from odoo.addons.server_environment import server_env class ServerEnvironmentCase(common.SavepointCase): - - _test_case_running_env = "testing" - - def setUp(self): - super().setUp() - self._original_running_env = config.get("running_env") - if self._test_case_running_env is not None: - config["running_env"] = self._test_case_running_env - server_env._load_running_env() - - def tearDown(self): - super().tearDown() - config["running_env"] = self._original_running_env - @contextmanager def set_config_dir(self, path): original_dir = server_env._dir diff --git a/server_environment/tests/test_environment_variable.py b/server_environment/tests/test_environment_variable.py index 8ea3b51f..5c21677a 100644 --- a/server_environment/tests/test_environment_variable.py +++ b/server_environment/tests/test_environment_variable.py @@ -2,13 +2,22 @@ # License GPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo.tools.config import config +from unittest.mock import patch + +from odoo.tools.config import config as odoo_config from odoo.addons.server_environment import server_env from .common import ServerEnvironmentCase +class TestRunningEnvDefault(ServerEnvironmentCase): + def test_running_env_default(self): + """When var is not provided it defaults to `test`.""" + self.assertEqual(odoo_config["running_env"], "test") + + +@patch.dict(odoo_config.options, {"running_env": "testing"}) class TestEnvironmentVariables(ServerEnvironmentCase): def test_env_variables(self): public = "[section]\n" "foo=bar\n" "bar=baz\n" @@ -27,11 +36,3 @@ def test_env_variables_override(self): parser = server_env._load_config() val = parser.get("external_service.ftp", "user") self.assertEqual(val, "foo") - - -class TestRunningEnv(ServerEnvironmentCase): - - _test_case_running_env = "" # empty -> no env set - - def test_running_env_default(self): - self.assertEqual(config["running_env"], "test") diff --git a/server_environment/tests/test_server_environment.py b/server_environment/tests/test_server_environment.py index 48df0cdc..892e94ab 100644 --- a/server_environment/tests/test_server_environment.py +++ b/server_environment/tests/test_server_environment.py @@ -1,5 +1,9 @@ # Copyright 2018 Camptocamp (https://www.camptocamp.com). # License GPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from unittest.mock import patch + +from odoo.tools.config import config as odoo_config + from .. import server_env from . import common @@ -10,7 +14,7 @@ def test_view(self): view = model.fields_view_get() self.assertTrue(view) - def test_default(self): + def _test_default(self, hidden_pwd=False): model = self.env["server.config"] rec = model.create({}) defaults = rec.default_get([]) @@ -19,10 +23,20 @@ def test_default(self): pass_checked = False for default in defaults: if "passw" in default: - self.assertNotEqual(defaults[default], "**********") + check = self.assertEqual if hidden_pwd else self.assertNotEqual + check(defaults[default], "**********") pass_checked = True self.assertTrue(pass_checked) + @patch.dict(odoo_config.options, {"running_env": "dev"}) + def test_default_dev(self): + self._test_default() + + @patch.dict(odoo_config.options, {"running_env": "whatever"}) + def test_default_non_dev_env(self): + self._test_default(hidden_pwd=True) + + @patch.dict(odoo_config.options, {"running_env": "testing"}) def test_value_retrival(self): with self.set_config_dir("testfiles"): parser = server_env._load_config()