Commit 0df355e536a12a0e04c39e0cc7395431cfd62433
Exists in
master
and in
4 other branches
Merge pull request #104 from colab/test_utils_conf
Test utils/conf
Showing
10 changed files
with
186 additions
and
6 deletions
Show diff stats
colab/utils/conf.py
| ... | ... | @@ -32,7 +32,7 @@ def _load_py_file(py_path, path): |
| 32 | 32 | try: |
| 33 | 33 | py_settings = importlib.import_module(py_path) |
| 34 | 34 | |
| 35 | - except IOError: | |
| 35 | + except ImportError: | |
| 36 | 36 | msg = ('Could not open settings file {}. Please ' |
| 37 | 37 | 'check if the file exists and if user ' |
| 38 | 38 | 'has read rights.').format(py_path) |
| ... | ... | @@ -54,7 +54,7 @@ def _load_py_file(py_path, path): |
| 54 | 54 | return py_setting |
| 55 | 55 | |
| 56 | 56 | |
| 57 | -def load_py_settings(): | |
| 57 | +def load_py_settings(settings_dir='/etc/colab/settings.d'): | |
| 58 | 58 | settings_file = os.getenv('COLAB_SETTINGS', '/etc/colab/settings.py') |
| 59 | 59 | settings_module = settings_file.split('.')[-2].split('/')[-1] |
| 60 | 60 | py_path = "/".join(settings_file.split('/')[:-1]) |
| ... | ... | @@ -67,8 +67,6 @@ def load_py_settings(): |
| 67 | 67 | |
| 68 | 68 | py_settings = _load_py_file(settings_module, py_path) |
| 69 | 69 | |
| 70 | - # Read settings from settings.d | |
| 71 | - settings_dir = '/etc/colab/settings.d' | |
| 72 | 70 | logger.info('Settings directory: %s', settings_dir) |
| 73 | 71 | |
| 74 | 72 | if not os.path.exists(settings_dir): | ... | ... |
| ... | ... | @@ -0,0 +1,56 @@ |
| 1 | + | |
| 2 | +# Set to false in production | |
| 3 | +DEBUG = True | |
| 4 | +TEMPLATE_DEBUG = False | |
| 5 | + | |
| 6 | +# System admins | |
| 7 | +ADMINS = [['John Foo', 'john@example.com'], ['Mary Bar', 'mary@example.com']] | |
| 8 | + | |
| 9 | +MANAGERS = ADMINS | |
| 10 | + | |
| 11 | +COLAB_FROM_ADDRESS = '"Colab" <noreply@example.com>' | |
| 12 | +SERVER_EMAIL = '"Colab" <noreply@example.com>' | |
| 13 | + | |
| 14 | +EMAIL_HOST = 'localhost' | |
| 15 | +EMAIL_PORT = 25 | |
| 16 | +EMAIL_SUBJECT_PREFIX = '[colab]' | |
| 17 | + | |
| 18 | +SECRET_KEY = 'not-a-secret' | |
| 19 | + | |
| 20 | +ALLOWED_HOSTS = [ | |
| 21 | + 'localhost', | |
| 22 | +] | |
| 23 | + | |
| 24 | +# Uncomment to enable social networks fields profile | |
| 25 | +SOCIAL_NETWORK_ENABLED = True | |
| 26 | + | |
| 27 | +# Disable indexing | |
| 28 | +ROBOTS_NOINDEX = True | |
| 29 | + | |
| 30 | +LOGGING = { | |
| 31 | + 'version': 1, | |
| 32 | + | |
| 33 | + 'handlers': { | |
| 34 | + 'null': { | |
| 35 | + 'level': 'DEBUG', | |
| 36 | + 'class': 'logging.NullHandler', | |
| 37 | + }, | |
| 38 | + }, | |
| 39 | + | |
| 40 | + 'loggers': { | |
| 41 | + 'colab.mailman': { | |
| 42 | + 'handlers': ['null'], | |
| 43 | + 'propagate': False, | |
| 44 | + }, | |
| 45 | + 'haystack': { | |
| 46 | + 'handlers': ['null'], | |
| 47 | + 'propagate': False, | |
| 48 | + }, | |
| 49 | + }, | |
| 50 | +} | |
| 51 | + | |
| 52 | +STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage' | |
| 53 | + | |
| 54 | +from colab.settings import INSTALLED_APPS | |
| 55 | + | |
| 56 | +INSTALLED_APPS += ('behave_django', ) | ... | ... |
| ... | ... | @@ -0,0 +1,15 @@ |
| 1 | +from colab.plugins.utils.menu import colab_url_factory | |
| 2 | + | |
| 3 | +name = "gitlab" | |
| 4 | +verbose_name = "Gitlab" | |
| 5 | + | |
| 6 | +upstream = 'https://localhost/gitlab/' | |
| 7 | +private_token = 'AVA8vrohDpoSws41zd1w' | |
| 8 | + | |
| 9 | +urls = { | |
| 10 | + "include": "gitlab.urls", | |
| 11 | + "prefix": 'gitlab/', | |
| 12 | + "namespace": "gitlab" | |
| 13 | +} | |
| 14 | + | |
| 15 | +url = colab_url_factory('gitlab') | ... | ... |
| ... | ... | @@ -0,0 +1,15 @@ |
| 1 | +from colab.plugins.utils.menu import colab_url_factory | |
| 2 | + | |
| 3 | +name = "noosfero" | |
| 4 | +verbose_name = "Noosfero" | |
| 5 | +private_token = "ef9a334177c620b68e75a89844e8a402" | |
| 6 | + | |
| 7 | +upstream = 'http://localhost/social/' | |
| 8 | + | |
| 9 | +urls = { | |
| 10 | + "include": "noosfero.urls", | |
| 11 | + "prefix": '^social/', | |
| 12 | + "namespace": "social" | |
| 13 | +} | |
| 14 | + | |
| 15 | +url = colab_url_factory('social') | ... | ... |
| ... | ... | @@ -0,0 +1 @@ |
| 1 | +) | ... | ... |
colab/utils/tests/test_conf.py
| 1 | +import sys | |
| 1 | 2 | |
| 2 | 3 | from django.test import TestCase, override_settings |
| 3 | 4 | from django.conf import settings |
| 4 | 5 | |
| 5 | -from ..conf import DatabaseUndefined, validate_database | |
| 6 | +from ..conf import (DatabaseUndefined, validate_database, | |
| 7 | + InaccessibleSettings, _load_py_file, load_py_settings, | |
| 8 | + load_colab_apps, load_widgets_settings) | |
| 9 | + | |
| 10 | +from mock import patch | |
| 11 | + | |
| 12 | + | |
| 13 | +test_files_dir = "./colab/utils/tests" | |
| 6 | 14 | |
| 7 | 15 | |
| 8 | 16 | class TestConf(TestCase): |
| ... | ... | @@ -16,3 +24,78 @@ class TestConf(TestCase): |
| 16 | 24 | with self.assertRaises(DatabaseUndefined): |
| 17 | 25 | validate_database(settings.DATABASES, settings.DEFAULT_DATABASE, |
| 18 | 26 | settings.DEBUG) |
| 27 | + | |
| 28 | + def test_load_py_file_with_io_error(self): | |
| 29 | + self.assertRaises(InaccessibleSettings, | |
| 30 | + _load_py_file, 'settings_test', '/etc/colab/') | |
| 31 | + | |
| 32 | + def test_load_py_file_with_syntax_error(self): | |
| 33 | + self.assertRaises(InaccessibleSettings, | |
| 34 | + _load_py_file, 'settings_with_syntax_error', | |
| 35 | + test_files_dir) | |
| 36 | + | |
| 37 | + def test_load_py_file(self): | |
| 38 | + py_settings = _load_py_file('colab_settings', test_files_dir) | |
| 39 | + | |
| 40 | + self.assertIn('SOCIAL_NETWORK_ENABLED', py_settings) | |
| 41 | + self.assertTrue(py_settings['SOCIAL_NETWORK_ENABLED']) | |
| 42 | + | |
| 43 | + self.assertIn('EMAIL_PORT', py_settings) | |
| 44 | + self.assertEquals(py_settings['EMAIL_PORT'], 25) | |
| 45 | + | |
| 46 | + @patch('os.getenv', return_value='/path/fake/settings.py') | |
| 47 | + def test_load_py_settings_with_inaccessible_settings(self, mock): | |
| 48 | + self.assertRaises(InaccessibleSettings, load_py_settings) | |
| 49 | + | |
| 50 | + @patch('os.getenv', return_value=test_files_dir + '/colab_settings.py') | |
| 51 | + def test_load_py_settings_without_settings_d(self, mock): | |
| 52 | + py_settings = load_py_settings('/path/fake/settings.d/test.py') | |
| 53 | + | |
| 54 | + self.assertIn('SOCIAL_NETWORK_ENABLED', py_settings) | |
| 55 | + self.assertTrue(py_settings['SOCIAL_NETWORK_ENABLED']) | |
| 56 | + | |
| 57 | + self.assertIn('EMAIL_PORT', py_settings) | |
| 58 | + self.assertEquals(py_settings['EMAIL_PORT'], 25) | |
| 59 | + | |
| 60 | + @patch('os.listdir', return_value=[test_files_dir + '/settings.d/test.py', | |
| 61 | + 'non_python_file']) | |
| 62 | + @patch('colab.utils.conf._load_py_file', | |
| 63 | + side_effect=[{'SOCIAL_NETWORK_ENABLED': True, 'EMAIL_PORT': 25}, | |
| 64 | + {'TEST': 'test'}]) | |
| 65 | + def test_load_py_settings_with_settings_d(self, mock_py, mock_listdir): | |
| 66 | + py_settings = load_py_settings(test_files_dir + '/settings.d/') | |
| 67 | + | |
| 68 | + self.assertIn('SOCIAL_NETWORK_ENABLED', py_settings) | |
| 69 | + self.assertTrue(py_settings['SOCIAL_NETWORK_ENABLED']) | |
| 70 | + | |
| 71 | + self.assertIn('EMAIL_PORT', py_settings) | |
| 72 | + self.assertEquals(py_settings['EMAIL_PORT'], 25) | |
| 73 | + | |
| 74 | + self.assertIn('TEST', py_settings) | |
| 75 | + self.assertEquals(py_settings['TEST'], 'test') | |
| 76 | + | |
| 77 | + @patch('os.getenv', return_value='/path/fake/plugins.d/') | |
| 78 | + def test_load_colab_apps_without_plugins_d_directory(self, mock): | |
| 79 | + colab_apps = load_colab_apps() | |
| 80 | + self.assertIn('COLAB_APPS', colab_apps) | |
| 81 | + self.assertEquals(colab_apps['COLAB_APPS'], {}) | |
| 82 | + | |
| 83 | + @patch('os.getenv', return_value=test_files_dir + '/plugins.d/') | |
| 84 | + def test_load_colab_apps_with_plugins_d_directory(self, os_getenv): | |
| 85 | + sys.path.insert(0, os_getenv.return_value) | |
| 86 | + colab_apps = load_colab_apps() | |
| 87 | + | |
| 88 | + self.assertIn('gitlab', colab_apps['COLAB_APPS']) | |
| 89 | + self.assertIn('noosfero', colab_apps['COLAB_APPS']) | |
| 90 | + sys.path.remove(os_getenv.return_value) | |
| 91 | + | |
| 92 | + self.assertNotIn(os_getenv.return_value, sys.path) | |
| 93 | + | |
| 94 | + @patch('os.getenv', return_value='/path/fake/widgets_settings.py') | |
| 95 | + def test_load_widgets_settings_without_settings(self, mock): | |
| 96 | + self.assertIsNone(load_widgets_settings()) | |
| 97 | + | |
| 98 | + @patch('os.getenv', side_effect=[test_files_dir + '/colab_settings.py', | |
| 99 | + '/path/fake/widgets_settings.py']) | |
| 100 | + def test_load_widgets_settings_without_settings_d(self, mock): | |
| 101 | + self.assertIsNone(load_widgets_settings()) | ... | ... |
setup.cfg