From ece4cb2c94faf29066a889374fb0f73740e6d425 Mon Sep 17 00:00:00 2001 From: Eugene Lisitsky Date: Wed, 2 Nov 2016 22:31:10 +0300 Subject: [PATCH 1/2] Accept all types of "true" setting available in json: on, yes, true, y. Set any of to enable DEBUG mode in docker-composer.yml in section web/environment/DEBUG. --- web/docker_django/settings.py | 2 +- web/tests/__init__.py | 0 web/tests/test_env_settings.py | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 web/tests/__init__.py create mode 100644 web/tests/test_env_settings.py diff --git a/web/docker_django/settings.py b/web/docker_django/settings.py index 169ac0f..19ec07f 100644 --- a/web/docker_django/settings.py +++ b/web/docker_django/settings.py @@ -23,7 +23,7 @@ SECRET_KEY = os.environ['SECRET_KEY'] # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True if os.getenv('DEBUG') == 'true' else False +DEBUG = os.getenv('DEBUG', 'NO').lower() in ('on', 'true', 'y', 'yes') ALLOWED_HOSTS = ['*'] diff --git a/web/tests/__init__.py b/web/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/web/tests/test_env_settings.py b/web/tests/test_env_settings.py new file mode 100644 index 0000000..5a8ca12 --- /dev/null +++ b/web/tests/test_env_settings.py @@ -0,0 +1,38 @@ +"""Test Environmental settings are handled properly.""" + + +import os +import importlib +from unittest.mock import patch +# from django.test import TestCase +# from unittest import skip +# we have to use tools outside of django, because when it's initialized +# it's too late to change environment variables +from unittest import TestCase, main + + +class DebugSettingTest(TestCase): + """Test if setting DEBUG is handled properly.""" + + _variants = { + True: ('Yes', 'YES', 'Y', 'TRUE', 'tRUE', 'true', 'On'), + + False: ('No', 'nO', 'N', 'n', 'false', 'False', 'off', 'oFF'), + } + env_var_debug = 'DEBUG' + + def test_debug_setting(self): + """Check if config accepts environment variable DEBUG and sets it.""" + from docker_django import settings + for result, words in self._variants.items(): + for word in words: + # print(word, result) + with patch.dict('os.environ', {self.env_var_debug: word}): + importlib.reload(settings) + assert self.env_var_debug in os.environ + self.assertEqual(settings.DEBUG, result) + assert self.env_var_debug not in os.environ # should be True + + +if __name__ == '__main__': + main() From 7570b5e07e778c3d5e488264e1cedcaaa9574187 Mon Sep 17 00:00:00 2001 From: Eugene Lisitsky Date: Wed, 2 Nov 2016 22:36:14 +0300 Subject: [PATCH 2/2] Add .venv and .cache to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 0c7445d..510d1cb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ venv env __pycache__ +.venv +.cache