2018-01-20 13:57:25 +01:00
|
|
|
from django_redis import get_redis_connection
|
|
|
|
|
2016-05-29 08:29:14 +02:00
|
|
|
from openslides.core.config import ConfigVariable, config
|
2015-06-29 12:08:15 +02:00
|
|
|
from openslides.core.exceptions import ConfigError, ConfigNotFound
|
2013-09-25 10:01:01 +02:00
|
|
|
from openslides.utils.test import TestCase
|
2013-03-01 17:13:12 +01:00
|
|
|
|
|
|
|
|
2016-06-02 12:47:01 +02:00
|
|
|
class TestConfigException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
class HandleConfigTest(TestCase):
|
2016-06-02 12:47:01 +02:00
|
|
|
def setUp(self):
|
2016-05-29 08:29:14 +02:00
|
|
|
# Save the old value of the config object and add the test values
|
|
|
|
# TODO: Can be changed to setUpClass when Django 1.8 is no longer supported
|
|
|
|
self._config_values = config.config_variables.copy()
|
2016-06-08 22:18:35 +02:00
|
|
|
config.update_config_variables(set_grouped_config_view())
|
|
|
|
config.update_config_variables(set_simple_config_view())
|
|
|
|
config.update_config_variables(set_simple_config_view_multiple_vars())
|
|
|
|
config.update_config_variables(set_simple_config_collection_disabled_view())
|
|
|
|
config.update_config_variables(set_simple_config_collection_with_callback())
|
2017-08-22 14:17:20 +02:00
|
|
|
config.save_default_values()
|
2016-06-02 12:47:01 +02:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# Reset the config variables
|
2016-05-29 08:29:14 +02:00
|
|
|
config.config_variables = self._config_values
|
2017-08-22 14:17:20 +02:00
|
|
|
super().tearDown()
|
2013-03-01 17:13:12 +01:00
|
|
|
|
|
|
|
def get_config_var(self, key):
|
2013-08-19 12:41:29 +02:00
|
|
|
return config[key]
|
2013-03-01 17:13:12 +01:00
|
|
|
|
2013-09-08 10:26:51 +02:00
|
|
|
def set_config_var(self, key, value):
|
|
|
|
config[key] = value
|
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
def test_get_config_default_value(self):
|
|
|
|
self.assertEqual(config['string_var'], 'default_string_rien4ooCZieng6ah')
|
|
|
|
self.assertTrue(config['bool_var'])
|
|
|
|
self.assertEqual(config['integer_var'], 3)
|
2014-05-28 00:20:13 +02:00
|
|
|
self.assertEqual(config['choices_var'], '1')
|
2013-03-01 17:13:12 +01:00
|
|
|
self.assertEqual(config['none_config_var'], None)
|
2016-01-09 09:58:22 +01:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ConfigNotFound,
|
|
|
|
'The config variable unknown_config_var was not found.'):
|
|
|
|
self.get_config_var('unknown_config_var')
|
2013-03-01 17:13:12 +01:00
|
|
|
|
|
|
|
def test_get_multiple_config_var_error(self):
|
2016-01-09 09:58:22 +01:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ConfigError,
|
2017-08-22 14:17:20 +02:00
|
|
|
'Too many values for config variables {\'multiple_config_var\'} found.'):
|
2016-06-08 22:18:35 +02:00
|
|
|
config.update_config_variables(set_simple_config_view_multiple_vars())
|
2013-03-01 17:13:12 +01:00
|
|
|
|
|
|
|
def test_setup_config_var(self):
|
|
|
|
self.assertRaises(TypeError, ConfigVariable)
|
|
|
|
self.assertRaises(TypeError, ConfigVariable, name='foo')
|
|
|
|
self.assertRaises(TypeError, ConfigVariable, default_value='foo')
|
|
|
|
|
|
|
|
def test_change_config_value(self):
|
|
|
|
self.assertEqual(config['string_var'], 'default_string_rien4ooCZieng6ah')
|
|
|
|
config['string_var'] = 'other_special_unique_string dauTex9eAiy7jeen'
|
2018-01-20 13:57:25 +01:00
|
|
|
get_redis_connection('default').flushall()
|
2013-03-01 17:13:12 +01:00
|
|
|
self.assertEqual(config['string_var'], 'other_special_unique_string dauTex9eAiy7jeen')
|
|
|
|
|
2013-04-03 17:55:30 +02:00
|
|
|
def test_missing_cache_(self):
|
|
|
|
self.assertEqual(config['string_var'], 'default_string_rien4ooCZieng6ah')
|
|
|
|
|
2017-08-22 14:17:20 +02:00
|
|
|
def test_config_exists(self):
|
|
|
|
self.assertTrue(config.exists('string_var'))
|
|
|
|
self.assertFalse(config.exists('unknown_config_var'))
|
2013-04-03 17:55:30 +02:00
|
|
|
|
2013-08-11 10:05:47 +02:00
|
|
|
def test_set_value_before_getting_it(self):
|
|
|
|
"""
|
2015-06-16 14:03:42 +02:00
|
|
|
Try to call __setitem__ before __getitem__.
|
2013-08-11 10:05:47 +02:00
|
|
|
"""
|
2015-06-16 14:03:42 +02:00
|
|
|
config['additional_config_var'] = 'value'
|
2013-08-11 10:05:47 +02:00
|
|
|
|
2013-09-08 10:26:51 +02:00
|
|
|
def test_on_change(self):
|
|
|
|
"""
|
|
|
|
Tests that the special callback is called and raises a special
|
|
|
|
message.
|
|
|
|
"""
|
2016-01-09 09:58:22 +01:00
|
|
|
with self.assertRaisesMessage(
|
2016-06-02 12:47:01 +02:00
|
|
|
TestConfigException,
|
2016-01-09 09:58:22 +01:00
|
|
|
'Change callback dhcnfg34dlg06kdg successfully called.'):
|
|
|
|
self.set_config_var(
|
|
|
|
key='var_with_callback_ghvnfjd5768gdfkwg0hm2',
|
|
|
|
value='new_string_kbmbnfhdgibkdjshg452bc')
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
config['var_with_callback_ghvnfjd5768gdfkwg0hm2'],
|
|
|
|
'new_string_kbmbnfhdgibkdjshg452bc')
|
2013-09-08 10:26:51 +02:00
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
|
2016-06-02 12:47:01 +02:00
|
|
|
def set_grouped_config_view():
|
2013-03-01 17:13:12 +01:00
|
|
|
"""
|
2015-06-17 18:32:05 +02:00
|
|
|
Sets a grouped config collection. There are some variables, one variable
|
2013-03-01 17:13:12 +01:00
|
|
|
with a string as default value, one with a boolean as default value,
|
2015-06-17 18:32:05 +02:00
|
|
|
one with an integer as default value, one with choices and one hidden
|
|
|
|
variable. These variables are grouped in two subgroups.
|
2013-03-01 17:13:12 +01:00
|
|
|
"""
|
2015-06-17 18:32:05 +02:00
|
|
|
yield ConfigVariable(
|
2013-03-01 17:13:12 +01:00
|
|
|
name='string_var',
|
|
|
|
default_value='default_string_rien4ooCZieng6ah',
|
2015-06-17 18:32:05 +02:00
|
|
|
group='Config vars for testing 1',
|
|
|
|
subgroup='Group 1 aiYeix2mCieQuae3')
|
|
|
|
yield ConfigVariable(
|
2013-03-01 17:13:12 +01:00
|
|
|
name='bool_var',
|
|
|
|
default_value=True,
|
2015-06-17 18:32:05 +02:00
|
|
|
input_type='boolean',
|
|
|
|
group='Config vars for testing 1',
|
|
|
|
subgroup='Group 1 aiYeix2mCieQuae3')
|
|
|
|
yield ConfigVariable(
|
2013-03-01 17:13:12 +01:00
|
|
|
name='integer_var',
|
|
|
|
default_value=3,
|
2015-06-17 18:32:05 +02:00
|
|
|
input_type='integer',
|
|
|
|
group='Config vars for testing 1',
|
|
|
|
subgroup='Group 1 aiYeix2mCieQuae3')
|
2013-03-01 17:13:12 +01:00
|
|
|
|
2015-06-17 18:32:05 +02:00
|
|
|
yield ConfigVariable(
|
2013-03-01 17:13:12 +01:00
|
|
|
name='hidden_var',
|
2015-06-17 18:32:05 +02:00
|
|
|
default_value='hidden_value',
|
|
|
|
group='Config vars for testing 1',
|
|
|
|
subgroup='Group 2 Toongai7ahyahy7B')
|
|
|
|
yield ConfigVariable(
|
2013-03-01 17:13:12 +01:00
|
|
|
name='choices_var',
|
2014-05-28 00:20:13 +02:00
|
|
|
default_value='1',
|
2015-06-17 18:32:05 +02:00
|
|
|
input_type='choice',
|
|
|
|
choices=(
|
|
|
|
{'value': '1', 'display_name': 'Choice One Ughoch4ocoche6Ee'},
|
|
|
|
{'value': '2', 'display_name': 'Choice Two Vahnoh5yalohv5Eb'}),
|
|
|
|
group='Config vars for testing 1',
|
|
|
|
subgroup='Group 2 Toongai7ahyahy7B')
|
2013-03-01 17:13:12 +01:00
|
|
|
|
|
|
|
|
2016-06-02 12:47:01 +02:00
|
|
|
def set_simple_config_view():
|
2013-03-01 17:13:12 +01:00
|
|
|
"""
|
2013-12-02 19:33:43 +01:00
|
|
|
Sets a simple config view with some config variables but without
|
2013-03-01 17:13:12 +01:00
|
|
|
grouping.
|
|
|
|
"""
|
2015-06-17 18:32:05 +02:00
|
|
|
yield ConfigVariable(name='additional_config_var', default_value='BaeB0ahcMae3feem')
|
|
|
|
yield ConfigVariable(name='additional_config_var_2', default_value='')
|
|
|
|
yield ConfigVariable(name='none_config_var', default_value=None)
|
2013-03-01 17:13:12 +01:00
|
|
|
|
|
|
|
|
2016-06-02 12:47:01 +02:00
|
|
|
def set_simple_config_view_multiple_vars():
|
2013-03-01 17:13:12 +01:00
|
|
|
"""
|
2013-12-02 19:33:43 +01:00
|
|
|
Sets a bad config view with some multiple config vars.
|
2013-03-01 17:13:12 +01:00
|
|
|
"""
|
2015-06-17 18:32:05 +02:00
|
|
|
yield ConfigVariable(name='multiple_config_var', default_value='foobar1')
|
|
|
|
yield ConfigVariable(name='multiple_config_var', default_value='foobar2')
|
2013-03-01 17:13:12 +01:00
|
|
|
|
|
|
|
|
2016-06-02 12:47:01 +02:00
|
|
|
def set_simple_config_collection_disabled_view():
|
2015-06-17 18:32:05 +02:00
|
|
|
yield ConfigVariable(name='hidden_config_var_2', default_value='')
|
2013-09-08 10:26:51 +02:00
|
|
|
|
|
|
|
|
2016-06-02 12:47:01 +02:00
|
|
|
def set_simple_config_collection_with_callback():
|
2013-09-08 10:26:51 +02:00
|
|
|
def callback():
|
2016-06-02 12:47:01 +02:00
|
|
|
raise TestConfigException('Change callback dhcnfg34dlg06kdg successfully called.')
|
2015-06-17 18:32:05 +02:00
|
|
|
yield ConfigVariable(
|
|
|
|
name='var_with_callback_ghvnfjd5768gdfkwg0hm2',
|
|
|
|
default_value='',
|
|
|
|
on_change=callback)
|