From 6efc461fb67bf006c1d19cae04f0b040b8064f6b Mon Sep 17 00:00:00 2001 From: Michael Frey Date: Mon, 20 Jun 2016 16:54:13 -0400 Subject: [PATCH] mjfrey/micro-settings-merge: Override base dictionary keys with microsite configuration keys * mattdrayer: Add helpers.get_value test * mattdrayer: Change to simpler implementation, per @douglashall * mattdrayer: Address quality violations and test failures --- .../course_modes/tests/test_views.py | 19 +++++++------- common/djangoapps/student/tests/test_email.py | 2 +- common/djangoapps/student/tests/tests.py | 2 +- lms/djangoapps/branding/tests/test_views.py | 2 +- lms/djangoapps/commerce/tests/test_views.py | 2 +- .../tests/test_comprehensive_theming.py | 2 +- .../tests/test_comprehensive_theming.py | 2 +- .../courseware/tests/test_footer.py | 2 +- .../student_account/test/test_views.py | 2 +- .../verify_student/tests/test_views.py | 2 +- openedx/core/djangoapps/theming/helpers.py | 26 ++++++++++++++++--- .../core/djangoapps/theming/tests/__init__.py | 0 .../djangoapps/theming/tests/test_helpers.py | 25 ++++++++++++++++++ .../theming/{ => tests}/test_util.py | 2 +- 14 files changed, 67 insertions(+), 23 deletions(-) create mode 100644 openedx/core/djangoapps/theming/tests/__init__.py create mode 100644 openedx/core/djangoapps/theming/tests/test_helpers.py rename openedx/core/djangoapps/theming/{ => tests}/test_util.py (97%) diff --git a/common/djangoapps/course_modes/tests/test_views.py b/common/djangoapps/course_modes/tests/test_views.py index b8c5161ddf..8fa21e0fde 100644 --- a/common/djangoapps/course_modes/tests/test_views.py +++ b/common/djangoapps/course_modes/tests/test_views.py @@ -9,21 +9,22 @@ import ddt import freezegun from mock import patch from nose.plugins.attrib import attr + from django.conf import settings from django.core.urlresolvers import reverse +from lms.djangoapps.commerce.tests import test_utils as ecomm_test_utils +from openedx.core.djangoapps.theming.tests import test_util as theming_test_utils from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase - -from util.testing import UrlResetMixin -from embargo.test_utils import restrict_course from xmodule.modulestore.django import modulestore from xmodule.modulestore.tests.factories import CourseFactory -from course_modes.tests.factories import CourseModeFactory -from student.tests.factories import CourseEnrollmentFactory, UserFactory -from student.models import CourseEnrollment -import lms.djangoapps.commerce.tests.test_utils as ecomm_test_utils + from course_modes.models import CourseMode, Mode -from openedx.core.djangoapps.theming.test_util import with_is_edx_domain +from course_modes.tests.factories import CourseModeFactory +from embargo.test_utils import restrict_course +from student.models import CourseEnrollment +from student.tests.factories import CourseEnrollmentFactory, UserFactory +from util.testing import UrlResetMixin @attr('shard_3') @@ -373,7 +374,7 @@ class CourseModeViewTest(UrlResetMixin, ModuleStoreTestCase): self.assertEquals(course_modes, expected_modes) @unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms') - @with_is_edx_domain(True) + @theming_test_utils.with_is_edx_domain(True) def test_hide_nav(self): # Create the course modes for mode in ["honor", "verified"]: diff --git a/common/djangoapps/student/tests/test_email.py b/common/djangoapps/student/tests/test_email.py index 3c44111608..2bd303c91d 100644 --- a/common/djangoapps/student/tests/test_email.py +++ b/common/djangoapps/student/tests/test_email.py @@ -20,7 +20,7 @@ from django.conf import settings from edxmako.shortcuts import render_to_string from util.request import safe_get_host from util.testing import EventTestMixin -from openedx.core.djangoapps.theming.test_util import with_is_edx_domain +from openedx.core.djangoapps.theming.tests.test_util import with_is_edx_domain from openedx.core.djangoapps.theming import helpers as theming_helpers diff --git a/common/djangoapps/student/tests/tests.py b/common/djangoapps/student/tests/tests.py index 4094a456ec..018aad8dd8 100644 --- a/common/djangoapps/student/tests/tests.py +++ b/common/djangoapps/student/tests/tests.py @@ -46,7 +46,7 @@ from certificates.tests.factories import GeneratedCertificateFactory # pylint: from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification import shoppingcart # pylint: disable=import-error from openedx.core.djangoapps.programs.tests.mixins import ProgramsApiConfigMixin -from openedx.core.djangoapps.theming.test_util import with_is_edx_domain +from openedx.core.djangoapps.theming.tests.test_util import with_is_edx_domain # Explicitly import the cache from ConfigurationModel so we can reset it after each test from config_models.models import cache diff --git a/lms/djangoapps/branding/tests/test_views.py b/lms/djangoapps/branding/tests/test_views.py index a6cb5d7249..eb62bf2282 100644 --- a/lms/djangoapps/branding/tests/test_views.py +++ b/lms/djangoapps/branding/tests/test_views.py @@ -10,7 +10,7 @@ import mock import ddt from config_models.models import cache from branding.models import BrandingApiConfig -from openedx.core.djangoapps.theming.test_util import with_edx_domain_context +from openedx.core.djangoapps.theming.tests.test_util import with_edx_domain_context @ddt.ddt diff --git a/lms/djangoapps/commerce/tests/test_views.py b/lms/djangoapps/commerce/tests/test_views.py index 703a518c12..b28fd7a84b 100644 --- a/lms/djangoapps/commerce/tests/test_views.py +++ b/lms/djangoapps/commerce/tests/test_views.py @@ -8,7 +8,7 @@ from django.test import TestCase import mock from student.tests.factories import UserFactory -from openedx.core.djangoapps.theming.test_util import with_is_edx_domain +from openedx.core.djangoapps.theming.tests.test_util import with_is_edx_domain class UserMixin(object): diff --git a/lms/djangoapps/course_wiki/tests/test_comprehensive_theming.py b/lms/djangoapps/course_wiki/tests/test_comprehensive_theming.py index 536ccc7f54..e32af4991f 100644 --- a/lms/djangoapps/course_wiki/tests/test_comprehensive_theming.py +++ b/lms/djangoapps/course_wiki/tests/test_comprehensive_theming.py @@ -9,7 +9,7 @@ from wiki.models import URLPath from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory -from openedx.core.djangoapps.theming.test_util import with_comprehensive_theme +from openedx.core.djangoapps.theming.tests.test_util import with_comprehensive_theme from courseware.tests.factories import InstructorFactory from course_wiki.views import get_or_create_root diff --git a/lms/djangoapps/courseware/tests/test_comprehensive_theming.py b/lms/djangoapps/courseware/tests/test_comprehensive_theming.py index 752eb81030..ee28177a15 100644 --- a/lms/djangoapps/courseware/tests/test_comprehensive_theming.py +++ b/lms/djangoapps/courseware/tests/test_comprehensive_theming.py @@ -6,7 +6,7 @@ from django.test import TestCase from path import path # pylint: disable=no-name-in-module from django.contrib import staticfiles -from openedx.core.djangoapps.theming.test_util import with_comprehensive_theme +from openedx.core.djangoapps.theming.tests.test_util import with_comprehensive_theme from openedx.core.lib.tempdir import mkdtemp_clean diff --git a/lms/djangoapps/courseware/tests/test_footer.py b/lms/djangoapps/courseware/tests/test_footer.py index 0a14ee1860..44cc93cbdd 100644 --- a/lms/djangoapps/courseware/tests/test_footer.py +++ b/lms/djangoapps/courseware/tests/test_footer.py @@ -9,7 +9,7 @@ from django.conf import settings from django.test import TestCase from django.test.utils import override_settings -from openedx.core.djangoapps.theming.test_util import with_is_edx_domain +from openedx.core.djangoapps.theming.tests.test_util import with_is_edx_domain @attr('shard_1') diff --git a/lms/djangoapps/student_account/test/test_views.py b/lms/djangoapps/student_account/test/test_views.py index c5f2c7fe66..7815d24fa5 100644 --- a/lms/djangoapps/student_account/test/test_views.py +++ b/lms/djangoapps/student_account/test/test_views.py @@ -33,7 +33,7 @@ from student_account.views import account_settings_context, get_user_orders from third_party_auth.tests.testutil import simulate_running_pipeline, ThirdPartyAuthTestMixin from util.testing import UrlResetMixin from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase -from openedx.core.djangoapps.theming.test_util import with_edx_domain_context +from openedx.core.djangoapps.theming.tests.test_util import with_edx_domain_context @ddt.ddt diff --git a/lms/djangoapps/verify_student/tests/test_views.py b/lms/djangoapps/verify_student/tests/test_views.py index b937699999..d5231e2d48 100644 --- a/lms/djangoapps/verify_student/tests/test_views.py +++ b/lms/djangoapps/verify_student/tests/test_views.py @@ -39,7 +39,7 @@ from commerce.models import CommerceConfiguration from commerce.tests import TEST_PAYMENT_DATA, TEST_API_URL, TEST_API_SIGNING_KEY, TEST_PUBLIC_URL_ROOT from embargo.test_utils import restrict_course from openedx.core.djangoapps.user_api.accounts.api import get_account_settings -from openedx.core.djangoapps.theming.test_util import with_is_edx_domain +from openedx.core.djangoapps.theming.tests.test_util import with_is_edx_domain from shoppingcart.models import Order, CertificateItem from student.tests.factories import UserFactory, CourseEnrollmentFactory from student.models import CourseEnrollment diff --git a/openedx/core/djangoapps/theming/helpers.py b/openedx/core/djangoapps/theming/helpers.py index 28ce710ebe..82434934cb 100644 --- a/openedx/core/djangoapps/theming/helpers.py +++ b/openedx/core/djangoapps/theming/helpers.py @@ -1,10 +1,10 @@ """ - Helpers for accessing comprehensive theming related variables. +Helpers for accessing comprehensive theming related variables. """ -from microsite_configuration import microsite -from microsite_configuration import page_title_breadcrumbs from django.conf import settings +from microsite_configuration import microsite, page_title_breadcrumbs + def get_page_title_breadcrumbs(*args): """ @@ -17,7 +17,25 @@ def get_value(val_name, default=None, **kwargs): """ This is a proxy function to hide microsite_configuration behind comprehensive theming. """ - return microsite.get_value(val_name, default=default, **kwargs) + + # Retrieve the requested field/value from the microsite configuration + microsite_value = microsite.get_value(val_name, default=default, **kwargs) + + # Attempt to perform a dictionary update using the provided default + # This will fail if either the default or the microsite value is not a dictionary + try: + value = dict(default) + value.update(microsite_value) + + # If the dictionary update fails, just use the microsite value + # TypeError: default is not iterable (simple value or None) + # ValueError: default is iterable but not a dict (list, not dict) + # AttributeError: default does not have an 'update' method + except (TypeError, ValueError, AttributeError): + value = microsite_value + + # Return the end result to the caller + return value def get_template_path(relative_path, **kwargs): diff --git a/openedx/core/djangoapps/theming/tests/__init__.py b/openedx/core/djangoapps/theming/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/openedx/core/djangoapps/theming/tests/test_helpers.py b/openedx/core/djangoapps/theming/tests/test_helpers.py new file mode 100644 index 0000000000..536e9cc657 --- /dev/null +++ b/openedx/core/djangoapps/theming/tests/test_helpers.py @@ -0,0 +1,25 @@ +""" +Test helpers for Comprehensive Theming. +""" +from django.test import TestCase +from mock import patch + +from openedx.core.djangoapps.theming import helpers + + +class ThemingHelpersTests(TestCase): + """ + Make sure some of the theming helper functions work + """ + + def test_get_value_returns_override(self): + """ + Tests to make sure the get_value() operation returns a combined dictionary consisting + of the base container with overridden keys from the microsite configuration + """ + with patch('microsite_configuration.microsite.get_value') as mock_get_value: + override_key = 'JWT_ISSUER' + override_value = 'testing' + mock_get_value.return_value = {override_key: override_value} + jwt_auth = helpers.get_value('JWT_AUTH') + self.assertEqual(jwt_auth[override_key], override_value) diff --git a/openedx/core/djangoapps/theming/test_util.py b/openedx/core/djangoapps/theming/tests/test_util.py similarity index 97% rename from openedx/core/djangoapps/theming/test_util.py rename to openedx/core/djangoapps/theming/tests/test_util.py index fbd871c213..6f13b9c187 100644 --- a/openedx/core/djangoapps/theming/test_util.py +++ b/openedx/core/djangoapps/theming/tests/test_util.py @@ -15,7 +15,7 @@ from django.test.utils import override_settings import edxmako -from .core import comprehensive_theme_changes +from openedx.core.djangoapps.theming.core import comprehensive_theme_changes EDX_THEME_DIR = settings.REPO_ROOT / "themes" / "edx.org"