From c3f2b6f5c0fb8728ef17baa7457724898c48aa5d Mon Sep 17 00:00:00 2001 From: Simone Orsi Date: Wed, 1 Apr 2020 12:15:57 +0200 Subject: [PATCH 1/4] server_environment: running_env default to `test` (fix #44) --- server_environment/readme/CONFIGURE.rst | 2 ++ server_environment/serv_config.py | 21 ++++++++++++------- server_environment/tests/common.py | 6 +++++- .../tests/test_environment_variable.py | 10 +++++++++ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/server_environment/readme/CONFIGURE.rst b/server_environment/readme/CONFIGURE.rst index deca37ca9..6610f4493 100644 --- a/server_environment/readme/CONFIGURE.rst +++ b/server_environment/readme/CONFIGURE.rst @@ -8,6 +8,8 @@ used values are 'dev', 'test', 'production':: Values associated to keys containing 'passw' are only displayed in the 'dev' environment. +If you don't provide any value, `test` is used as a safe default. + You have several possibilities to set configuration values: server_environment_files diff --git a/server_environment/serv_config.py b/server_environment/serv_config.py index a18bd2282..56cbf3d8b 100644 --- a/server_environment/serv_config.py +++ b/server_environment/serv_config.py @@ -45,14 +45,19 @@ _boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True, '0': False, 'no': False, 'false': False, 'off': False} -if not system_base_config.get('running_env', False): - raise Exception( - "The parameter 'running_env' has not be set neither in base config " - "file option -c or in openerprc.\n" - "We strongly recommend against using the rc file but instead use an " - "explicit config file with this content:\n" - "[options]\nrunning_env = dev" - ) + +def _load_running_env(): + if not system_base_config.get("running_env"): + _logger.warning("`running_env` not found. Using default = `test`.") + _logger.warning( + "We strongly recommend against using the rc file but instead use an " + "explicit config file or env variable." + ) + # safe default + system_base_config["running_env"] = "test" + + +_load_running_env() ck_path = None if _dir: diff --git a/server_environment/tests/common.py b/server_environment/tests/common.py index d74a13338..d71703da5 100644 --- a/server_environment/tests/common.py +++ b/server_environment/tests/common.py @@ -15,10 +15,14 @@ class ServerEnvironmentCase(common.SavepointCase): + _test_case_running_env = "testing" + def setUp(self): super().setUp() self._original_running_env = config.get('running_env') - config['running_env'] = 'testing' + 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() diff --git a/server_environment/tests/test_environment_variable.py b/server_environment/tests/test_environment_variable.py index 469bc1800..3ff948ce6 100644 --- a/server_environment/tests/test_environment_variable.py +++ b/server_environment/tests/test_environment_variable.py @@ -2,6 +2,8 @@ # License GPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo.tools.config import config + from odoo.addons.server_environment import server_env from .common import ServerEnvironmentCase @@ -43,3 +45,11 @@ 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') From 547ebaeab89668e617da92008da02ba81e8e5fb8 Mon Sep 17 00:00:00 2001 From: Simone Orsi Date: Wed, 1 Apr 2020 13:24:20 +0200 Subject: [PATCH 2/4] Travis: remove running_env config This is not needed anymore since https://github.com/OCA/server-env/pull/45 --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5e44cdb8c..58dd6b9be 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,8 +31,6 @@ install: - git clone --depth=1 https://github.com/OCA/maintainer-quality-tools.git ${HOME}/maintainer-quality-tools - export PATH=${HOME}/maintainer-quality-tools/travis:${PATH} - travis_install_nightly - # Requirements to test server_environment modules - - printf '[options]\n\nrunning_env = dev\n' > ${HOME}/.openerp_serverrc - ln -s ${TRAVIS_BUILD_DIR}/server_environment_files_sample ${TRAVIS_BUILD_DIR}/server_environment_files script: From c167f546dca7d45e7965668f1baf6e19b55d0592 Mon Sep 17 00:00:00 2001 From: Simone Orsi Date: Fri, 10 Apr 2020 10:04:39 +0200 Subject: [PATCH 3/4] fixup! server_environment: running_env default to `test` (fix #44) --- server_environment/serv_config.py | 7 ++++--- server_environment/tests/common.py | 14 ------------- .../tests/test_environment_variable.py | 19 +++++++++--------- .../tests/test_server_environment.py | 20 ++++++++++++++++--- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/server_environment/serv_config.py b/server_environment/serv_config.py index 56cbf3d8b..aad77987b 100644 --- a/server_environment/serv_config.py +++ b/server_environment/serv_config.py @@ -169,9 +169,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 @@ -180,6 +177,10 @@ def _build_model(cls, pool, cr): def _format_key(cls, section, key): return '%s_I_%s' % (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 d71703da5..7476760d7 100644 --- a/server_environment/tests/common.py +++ b/server_environment/tests/common.py @@ -7,7 +7,6 @@ from odoo.tests import common from odoo.addons.server_environment import server_env -from odoo.tools.config import config import odoo.addons.server_environment.models.server_env_mixin as \ server_env_mixin @@ -15,19 +14,6 @@ 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 3ff948ce6..d3260326e 100644 --- a/server_environment/tests/test_environment_variable.py +++ b/server_environment/tests/test_environment_variable.py @@ -2,12 +2,21 @@ # 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): @@ -45,11 +54,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 ee3379876..46031a3ba 100644 --- a/server_environment/tests/test_server_environment.py +++ b/server_environment/tests/test_server_environment.py @@ -1,5 +1,10 @@ # 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 odoo.addons.server_environment import server_env from . import common @@ -11,7 +16,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([]) @@ -20,11 +25,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() From 25bd5cb47f4851c6327ae50ec66042d60df1cbad Mon Sep 17 00:00:00 2001 From: Alexey Pelykh Date: Wed, 15 Apr 2020 19:36:11 +0200 Subject: [PATCH 4/4] [MIG] server_environment: backport changes from v13 --- server_environment_files_sample/{dev => test}/base.conf | 0 server_environment_files_sample/{dev => test}/mytopic.conf | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename server_environment_files_sample/{dev => test}/base.conf (100%) rename server_environment_files_sample/{dev => test}/mytopic.conf (100%) diff --git a/server_environment_files_sample/dev/base.conf b/server_environment_files_sample/test/base.conf similarity index 100% rename from server_environment_files_sample/dev/base.conf rename to server_environment_files_sample/test/base.conf diff --git a/server_environment_files_sample/dev/mytopic.conf b/server_environment_files_sample/test/mytopic.conf similarity index 100% rename from server_environment_files_sample/dev/mytopic.conf rename to server_environment_files_sample/test/mytopic.conf