From 2c7ddb1d158b233bcd6b3692d894586a7468e7db Mon Sep 17 00:00:00 2001 From: Felipe Montoya Date: Wed, 10 Feb 2016 11:31:45 -0500 Subject: [PATCH] Delegating the conditional to the has_configuration_Set method, which is overriden by the database backend, so the enable_pre_startup works well for both backends. Adding tests Fix quality issues --- .../microsite_configuration/backends/base.py | 3 +- .../tests/backends/test_base.py | 41 +++++++++++++++++++ .../tests/backends/test_database.py | 25 ++++------- .../tests/backends/test_filebased.py | 9 ++++ .../tests/test_microsites.py | 22 ++++++++++ 5 files changed, 82 insertions(+), 18 deletions(-) diff --git a/common/djangoapps/microsite_configuration/backends/base.py b/common/djangoapps/microsite_configuration/backends/base.py index e95f995196..a8fec36caa 100644 --- a/common/djangoapps/microsite_configuration/backends/base.py +++ b/common/djangoapps/microsite_configuration/backends/base.py @@ -290,9 +290,8 @@ class BaseMicrositeBackend(AbstractBaseMicrositeBackend): in non-mako templates must be loaded before the django startup """ microsites_root = settings.MICROSITE_ROOT_DIR - microsite_config_dict = settings.MICROSITE_CONFIGURATION - if microsite_config_dict: + if self.has_configuration_set(): settings.DEFAULT_TEMPLATE_ENGINE['DIRS'].append(microsites_root) diff --git a/common/djangoapps/microsite_configuration/tests/backends/test_base.py b/common/djangoapps/microsite_configuration/tests/backends/test_base.py index f4c88cb7fd..e8f138bac7 100644 --- a/common/djangoapps/microsite_configuration/tests/backends/test_base.py +++ b/common/djangoapps/microsite_configuration/tests/backends/test_base.py @@ -1,12 +1,20 @@ """ Test Microsite base backends. """ +import logging + +from mock import patch +from django.conf import settings from django.test import TestCase +from microsite_configuration import microsite from microsite_configuration.backends.base import ( AbstractBaseMicrositeBackend, + BaseMicrositeBackend ) +log = logging.getLogger(__name__) + class NullBackend(AbstractBaseMicrositeBackend): """ @@ -130,3 +138,36 @@ class AbstractBaseMicrositeBackendTests(TestCase): with self.assertRaises(NotImplementedError): backend.get_all_orgs() + + +@patch( + 'microsite_configuration.microsite.BACKEND', + microsite.get_backend( + 'microsite_configuration.backends.base.BaseMicrositeBackend', BaseMicrositeBackend + ) +) +class BaseMicrositeBackendTests(TestCase): + """ + Go through and test the BaseMicrositeBackend class for behavior which is not + overriden in subclasses + """ + def test_enable_microsites_pre_startup(self): + """ + Tests microsite.test_enable_microsites_pre_startup works as expected. + """ + # remove microsite root directory paths first + settings.DEFAULT_TEMPLATE_ENGINE['DIRS'] = [ + path for path in settings.DEFAULT_TEMPLATE_ENGINE['DIRS'] + if path != settings.MICROSITE_ROOT_DIR + ] + + with patch('microsite_configuration.backends.base.BaseMicrositeBackend.has_configuration_set', + return_value=False): + microsite.enable_microsites_pre_startup(log) + self.assertNotIn(settings.MICROSITE_ROOT_DIR, + settings.DEFAULT_TEMPLATE_ENGINE['DIRS']) + with patch('microsite_configuration.backends.base.BaseMicrositeBackend.has_configuration_set', + return_value=True): + microsite.enable_microsites_pre_startup(log) + self.assertIn(settings.MICROSITE_ROOT_DIR, + settings.DEFAULT_TEMPLATE_ENGINE['DIRS']) diff --git a/common/djangoapps/microsite_configuration/tests/backends/test_database.py b/common/djangoapps/microsite_configuration/tests/backends/test_database.py index 294da02ece..43a96cf19d 100644 --- a/common/djangoapps/microsite_configuration/tests/backends/test_database.py +++ b/common/djangoapps/microsite_configuration/tests/backends/test_database.py @@ -105,22 +105,6 @@ class DatabaseMicrositeBackendTests(DatabaseMicrositeTestCase): microsite.clear() self.assertIsNone(microsite.get_value('platform_name')) - def test_enable_microsites_pre_startup(self): - """ - Tests microsite.test_enable_microsites_pre_startup works as expected. - """ - # remove microsite root directory paths first - settings.DEFAULT_TEMPLATE_ENGINE['DIRS'] = [ - path for path in settings.DEFAULT_TEMPLATE_ENGINE['DIRS'] - if path != settings.MICROSITE_ROOT_DIR - ] - with patch.dict('django.conf.settings.FEATURES', {'USE_MICROSITES': False}): - microsite.enable_microsites_pre_startup(log) - self.assertNotIn(settings.MICROSITE_ROOT_DIR, settings.DEFAULT_TEMPLATE_ENGINE['DIRS']) - with patch.dict('django.conf.settings.FEATURES', {'USE_MICROSITES': True}): - microsite.enable_microsites_pre_startup(log) - self.assertIn(settings.MICROSITE_ROOT_DIR, settings.DEFAULT_TEMPLATE_ENGINE['DIRS']) - @patch('edxmako.paths.add_lookup') def test_enable_microsites(self, add_lookup): """ @@ -173,6 +157,15 @@ class DatabaseMicrositeBackendTests(DatabaseMicrositeTestCase): with self.assertRaises(Exception): microsite.set_by_domain('test.microsite2.com') + def test_has_configuration_set(self): + """ + Tests microsite.has_configuration_set works as expected on this backend. + """ + self.assertTrue(microsite.BACKEND.has_configuration_set()) + + Microsite.objects.all().delete() + self.assertFalse(microsite.BACKEND.has_configuration_set()) + @patch( 'microsite_configuration.microsite.TEMPLATES_BACKEND', diff --git a/common/djangoapps/microsite_configuration/tests/backends/test_filebased.py b/common/djangoapps/microsite_configuration/tests/backends/test_filebased.py index 3d219ae209..1931efee17 100644 --- a/common/djangoapps/microsite_configuration/tests/backends/test_filebased.py +++ b/common/djangoapps/microsite_configuration/tests/backends/test_filebased.py @@ -122,6 +122,15 @@ class FilebasedMicrositeBackendTests(TestCase): microsite.set_by_domain('unknown') self.assertEqual(microsite.get_value('university'), 'default_university') + def test_has_configuration_set(self): + """ + Tests microsite.has_configuration_set works as expected. + """ + self.assertTrue(microsite.BACKEND.has_configuration_set()) + + with patch('django.conf.settings.MICROSITE_CONFIGURATION', {}): + self.assertFalse(microsite.BACKEND.has_configuration_set()) + @patch( 'microsite_configuration.microsite.TEMPLATES_BACKEND', diff --git a/common/djangoapps/microsite_configuration/tests/test_microsites.py b/common/djangoapps/microsite_configuration/tests/test_microsites.py index e8982b414f..61520814f4 100644 --- a/common/djangoapps/microsite_configuration/tests/test_microsites.py +++ b/common/djangoapps/microsite_configuration/tests/test_microsites.py @@ -2,6 +2,9 @@ """ Tests microsite_configuration templatetags and helper functions. """ +import logging + +from mock import patch from django.test import TestCase from django.conf import settings from microsite_configuration.templatetags import microsite as microsite_tags @@ -9,6 +12,8 @@ from microsite_configuration import microsite from microsite_configuration.backends.base import BaseMicrositeBackend from microsite_configuration.backends.database import DatabaseMicrositeBackend +log = logging.getLogger(__name__) + class MicrositeTests(TestCase): """ @@ -74,3 +79,20 @@ class MicrositeTests(TestCase): ), DatabaseMicrositeBackend ) + + def test_enable_microsites_pre_startup(self): + """ + Tests microsite.test_enable_microsites_pre_startup is not used if the feature is turned off. + """ + # remove microsite root directory paths first + settings.DEFAULT_TEMPLATE_ENGINE['DIRS'] = [ + path for path in settings.DEFAULT_TEMPLATE_ENGINE['DIRS'] + if path != settings.MICROSITE_ROOT_DIR + ] + + with patch.dict('django.conf.settings.FEATURES', {'USE_MICROSITES': False}): + microsite.enable_microsites_pre_startup(log) + self.assertNotIn(settings.MICROSITE_ROOT_DIR, settings.DEFAULT_TEMPLATE_ENGINE['DIRS']) + with patch.dict('django.conf.settings.FEATURES', {'USE_MICROSITES': True}): + microsite.enable_microsites_pre_startup(log) + self.assertIn(settings.MICROSITE_ROOT_DIR, settings.DEFAULT_TEMPLATE_ENGINE['DIRS'])