server_environment: running_env default to `test` (fix #44)

This commit is contained in:
Simone Orsi 2020-04-01 12:15:57 +02:00 committed by Maxime Franco
parent 45ffa60191
commit aab642738a
5 changed files with 47 additions and 23 deletions

View File

@ -8,6 +8,8 @@ used values are 'dev', 'test', 'production'::
Values associated to keys containing 'passw' are only displayed in the 'dev' Values associated to keys containing 'passw' are only displayed in the 'dev'
environment. environment.
If you don't provide any value, `test` is used as a safe default.
You have several possibilities to set configuration values: You have several possibilities to set configuration values:
server_environment_files server_environment_files

View File

@ -58,14 +58,20 @@ _boolean_states = {
"off": False, "off": False,
} }
if not system_base_config.get("running_env", False):
raise Exception( def _load_running_env():
"The parameter 'running_env' has not be set neither in base config " if not system_base_config.get("running_env"):
"file option -c or in openerprc.\n" _logger.warning("`running_env` not found. Using default = `test`.")
"We strongly recommend against using the rc file but instead use an " _logger.warning(
"explicit config file with this content:\n" "We strongly recommend against using the rc file but instead use an "
"[options]\nrunning_env = dev" "explicit config file or env variable."
) )
# safe default
system_base_config["running_env"] = "test"
_load_running_env()
ck_path = None ck_path = None
if _dir: if _dir:
@ -184,9 +190,6 @@ class ServerConfiguration(models.TransientModel):
""" """
ModelClass = super(ServerConfiguration, cls)._build_model(pool, cr) ModelClass = super(ServerConfiguration, cls)._build_model(pool, cr)
ModelClass._add_columns() 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._arch = None
ModelClass._build_osv() ModelClass._build_osv()
return ModelClass return ModelClass
@ -195,6 +198,10 @@ class ServerConfiguration(models.TransientModel):
def _format_key(cls, section, key): def _format_key(cls, section, key):
return "{}_I_{}".format(section, key) return "{}_I_{}".format(section, key)
@property
def show_passwords(self):
return system_base_config["running_env"] in ("dev",)
@classmethod @classmethod
def _format_key_display_name(cls, key_name): def _format_key_display_name(cls, key_name):
return key_name.replace("_I_", " | ") return key_name.replace("_I_", " | ")

View File

@ -6,22 +6,12 @@ from contextlib import contextmanager
from unittest.mock import patch from unittest.mock import patch
from odoo.tests import common from odoo.tests import common
from odoo.tools.config import config
import odoo.addons.server_environment.models.server_env_mixin as server_env_mixin import odoo.addons.server_environment.models.server_env_mixin as server_env_mixin
from odoo.addons.server_environment import server_env from odoo.addons.server_environment import server_env
class ServerEnvironmentCase(common.SavepointCase): class ServerEnvironmentCase(common.SavepointCase):
def setUp(self):
super().setUp()
self._original_running_env = config.get("running_env")
config["running_env"] = "testing"
def tearDown(self):
super().tearDown()
config["running_env"] = self._original_running_env
@contextmanager @contextmanager
def set_config_dir(self, path): def set_config_dir(self, path):
original_dir = server_env._dir original_dir = server_env._dir

View File

@ -2,11 +2,22 @@
# License GPL-3.0 or later (http://www.gnu.org/licenses/agpl). # 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 odoo.addons.server_environment import server_env
from .common import ServerEnvironmentCase 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): class TestEnvironmentVariables(ServerEnvironmentCase):
def test_env_variables(self): def test_env_variables(self):
public = "[section]\n" "foo=bar\n" "bar=baz\n" public = "[section]\n" "foo=bar\n" "bar=baz\n"

View File

@ -1,5 +1,9 @@
# Copyright 2018 Camptocamp (https://www.camptocamp.com). # Copyright 2018 Camptocamp (https://www.camptocamp.com).
# License GPL-3.0 or later (http://www.gnu.org/licenses/agpl). # 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 server_env
from . import common from . import common
@ -10,7 +14,7 @@ class TestEnv(common.ServerEnvironmentCase):
view = model.fields_view_get() view = model.fields_view_get()
self.assertTrue(view) self.assertTrue(view)
def test_default(self): def _test_default(self, hidden_pwd=False):
model = self.env["server.config"] model = self.env["server.config"]
rec = model.create({}) rec = model.create({})
defaults = rec.default_get([]) defaults = rec.default_get([])
@ -19,10 +23,20 @@ class TestEnv(common.ServerEnvironmentCase):
pass_checked = False pass_checked = False
for default in defaults: for default in defaults:
if "passw" in default: if "passw" in default:
self.assertNotEqual(defaults[default], "**********") check = self.assertEqual if hidden_pwd else self.assertNotEqual
check(defaults[default], "**********")
pass_checked = True pass_checked = True
self.assertTrue(pass_checked) 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): def test_value_retrival(self):
with self.set_config_dir("testfiles"): with self.set_config_dir("testfiles"):
parser = server_env._load_config() parser = server_env._load_config()