From 935be27118a6469eba89047b1e6c4bd989373e8d Mon Sep 17 00:00:00 2001 From: Renzo Lucioni Date: Tue, 19 Jul 2016 16:39:07 -0400 Subject: [PATCH] Allow program detail pages to function when catalog config is missing --- .../djangoapps/catalog/tests/test_utils.py | 18 +++++++++--- openedx/core/djangoapps/catalog/utils.py | 29 ++++++++++--------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/openedx/core/djangoapps/catalog/tests/test_utils.py b/openedx/core/djangoapps/catalog/tests/test_utils.py index 690c238427..641488d0d4 100644 --- a/openedx/core/djangoapps/catalog/tests/test_utils.py +++ b/openedx/core/djangoapps/catalog/tests/test_utils.py @@ -5,6 +5,7 @@ import mock from opaque_keys.edx.keys import CourseKey from openedx.core.djangoapps.catalog import utils +from openedx.core.djangoapps.catalog.models import CatalogIntegration from openedx.core.djangoapps.catalog.tests import factories, mixins from student.tests.factories import UserFactory @@ -13,6 +14,8 @@ UTILS_MODULE = 'openedx.core.djangoapps.catalog.utils' @mock.patch(UTILS_MODULE + '.get_edx_api_data') +# ConfigurationModels use the cache. Make every cache get a miss. +@mock.patch('config_models.models.cache.get', return_value=None) class TestGetCourseRun(mixins.CatalogIntegrationMixin, TestCase): """Tests covering retrieval of course runs from the catalog service.""" def setUp(self): @@ -34,7 +37,7 @@ class TestGetCourseRun(mixins.CatalogIntegrationMixin, TestCase): return args, kwargs - def test_get_course_run(self, mock_get_catalog_data): + def test_get_course_run(self, _mock_cache, mock_get_catalog_data): course_run = factories.CourseRun() mock_get_catalog_data.return_value = course_run @@ -43,7 +46,7 @@ class TestGetCourseRun(mixins.CatalogIntegrationMixin, TestCase): self.assert_contract(mock_get_catalog_data.call_args) self.assertEqual(data, course_run) - def test_course_run_unavailable(self, mock_get_catalog_data): + def test_course_run_unavailable(self, _mock_cache, mock_get_catalog_data): mock_get_catalog_data.return_value = [] data = utils.get_course_run(self.course_key, self.user) @@ -51,14 +54,14 @@ class TestGetCourseRun(mixins.CatalogIntegrationMixin, TestCase): self.assert_contract(mock_get_catalog_data.call_args) self.assertEqual(data, {}) - def test_cache_disabled(self, mock_get_catalog_data): + def test_cache_disabled(self, _mock_cache, mock_get_catalog_data): utils.get_course_run(self.course_key, self.user) _, kwargs = self.assert_contract(mock_get_catalog_data.call_args) self.assertIsNone(kwargs['cache_key']) - def test_cache_enabled(self, mock_get_catalog_data): + def test_cache_enabled(self, _mock_cache, mock_get_catalog_data): catalog_integration = self.create_catalog_integration(cache_ttl=1) utils.get_course_run(self.course_key, self.user) @@ -67,6 +70,13 @@ class TestGetCourseRun(mixins.CatalogIntegrationMixin, TestCase): self.assertEqual(kwargs['cache_key'], catalog_integration.CACHE_KEY) + def test_config_missing(self, _mock_cache, _mock_get_catalog_data): + """Verify that no errors occur if this method is called when catalog config is missing.""" + CatalogIntegration.objects.all().delete() + + data = utils.get_course_run(self.course_key, self.user) + self.assertEqual(data, {}) + @mock.patch(UTILS_MODULE + '.get_course_run') @mock.patch(UTILS_MODULE + '.strip_querystring') diff --git a/openedx/core/djangoapps/catalog/utils.py b/openedx/core/djangoapps/catalog/utils.py index 1c762f3fae..e9f9560f20 100644 --- a/openedx/core/djangoapps/catalog/utils.py +++ b/openedx/core/djangoapps/catalog/utils.py @@ -21,21 +21,24 @@ def get_course_run(course_key, user): """ catalog_integration = CatalogIntegration.current() - scopes = ['email', 'profile'] - expires_in = settings.OAUTH_ID_TOKEN_EXPIRATION - jwt = JwtBuilder(user).build_token(scopes, expires_in) - api = EdxRestApiClient(catalog_integration.internal_api_url, jwt=jwt) + if catalog_integration.enabled: + scopes = ['email', 'profile'] + expires_in = settings.OAUTH_ID_TOKEN_EXPIRATION + jwt = JwtBuilder(user).build_token(scopes, expires_in) + api = EdxRestApiClient(catalog_integration.internal_api_url, jwt=jwt) - data = get_edx_api_data( - catalog_integration, - user, - 'course_runs', - resource_id=unicode(course_key), - cache_key=catalog_integration.CACHE_KEY if catalog_integration.is_cache_enabled else None, - api=api, - ) + data = get_edx_api_data( + catalog_integration, + user, + 'course_runs', + resource_id=unicode(course_key), + cache_key=catalog_integration.CACHE_KEY if catalog_integration.is_cache_enabled else None, + api=api, + ) - return data if data else {} + return data if data else {} + else: + return {} def get_run_marketing_url(course_key, user):