Skip to content

Commit

Permalink
server_environment: running_env default to test (fix OCA#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
simahawk authored and thienvh332 committed Sep 24, 2024
1 parent 186b98b commit bbfca6c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 23 deletions.
2 changes: 2 additions & 0 deletions server_environment/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,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
Expand Down
2 changes: 2 additions & 0 deletions server_environment/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 18 additions & 11 deletions server_environment/server_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,20 @@
"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:
Expand Down Expand Up @@ -184,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
Expand All @@ -195,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_", " | ")
Expand Down
1 change: 1 addition & 0 deletions server_environment/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ <h1><a class="toc-backref" href="#id2">Configuration</a></h1>
</pre>
<p>Values associated to keys containing ‘passw’ are only displayed in the ‘dev’
environment.</p>
<p>If you don’t provide any value, <cite>test</cite> is used as a safe default.</p>
<p>You have several possibilities to set configuration values:</p>
<div class="section" id="server-environment-files">
<h2><a class="toc-backref" href="#id3">server_environment_files</a></h2>
Expand Down
10 changes: 0 additions & 10 deletions server_environment/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +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):
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
def set_config_dir(self, path):
original_dir = server_env._dir
Expand Down
11 changes: 11 additions & 0 deletions server_environment/tests/test_environment_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@
# 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 .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"
Expand Down
18 changes: 16 additions & 2 deletions server_environment/tests/test_server_environment.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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([])
Expand All @@ -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()
Expand Down

0 comments on commit bbfca6c

Please sign in to comment.