[cwconfig] ensure CubicWebNoAppConfiguration returns a typed value (closes #16364459) 3.24
authorDavid Douard <david.douard@logilab.fr>
Wed, 23 Nov 2016 12:24:04 +0100
branch3.24
changeset 11859 2e1a1ebd4730
parent 11849 882bd17344c2
child 11860 bb5904cd284e
[cwconfig] ensure CubicWebNoAppConfiguration returns a typed value (closes #16364459)
cubicweb/cwconfig.py
cubicweb/test/unittest_cwconfig.py
--- a/cubicweb/cwconfig.py	Mon Nov 21 14:18:43 2016 +0100
+++ b/cubicweb/cwconfig.py	Wed Nov 23 12:24:04 2016 +0100
@@ -202,7 +202,8 @@
 from logilab.common.deprecation import deprecated
 from logilab.common.logging_ext import set_log_methods, init_log
 from logilab.common.configuration import (Configuration, Method,
-                                          ConfigurationMixIn, merge_options)
+                                          ConfigurationMixIn, merge_options,
+                                          _validate as lgc_validate)
 
 from cubicweb import (CW_SOFTWARE_ROOT, CW_MIGRATION_MAP,
                       ConfigurationError, Binary, _)
@@ -420,7 +421,11 @@
     def __getitem__(self, key):
         """Get configuration option, by first looking at environmnent."""
         file_value = super(CubicWebNoAppConfiguration, self).__getitem__(key)
-        return option_value_from_env(key, file_value)
+        value = option_value_from_env(key, file_value)
+        if value is not None:
+            option_def = self.get_option_def(key)
+            value = lgc_validate(value, option_def)
+        return value
 
     # static and class methods used to get instance independant resources ##
     @staticmethod
--- a/cubicweb/test/unittest_cwconfig.py	Mon Nov 21 14:18:43 2016 +0100
+++ b/cubicweb/test/unittest_cwconfig.py	Wed Nov 23 12:24:04 2016 +0100
@@ -208,7 +208,7 @@
         from cubes import file
         self.assertEqual(file.__path__, [join(self.custom_cubes_dir, 'file')])
 
-    def test_config_value_from_environment(self):
+    def test_config_value_from_environment_str(self):
         self.assertIsNone(self.config['base-url'])
         os.environ['CW_BASE_URL'] = 'https://www.cubicweb.org'
         try:
@@ -217,6 +217,24 @@
         finally:
             del os.environ['CW_BASE_URL']
 
+    def test_config_value_from_environment_int(self):
+        self.assertEqual(self.config['connections-pool-size'], 4)
+        os.environ['CW_CONNECTIONS_POOL_SIZE'] = '6'
+        try:
+            self.assertEqual(self.config['connections-pool-size'], 6)
+        finally:
+            del os.environ['CW_CONNECTIONS_POOL_SIZE']
+
+    def test_config_value_from_environment_yn(self):
+        self.assertEqual(self.config['allow-email-login'], False)
+        try:
+            for val, result in (('yes', True), ('no', False),
+                                ('y', True), ('n', False),):
+                os.environ['CW_ALLOW_EMAIL_LOGIN'] = val
+            self.assertEqual(self.config['allow-email-login'], result)
+        finally:
+            del os.environ['CW_ALLOW_EMAIL_LOGIN']
+            
 
 class FindPrefixTC(unittest.TestCase):