Commit f0644abe by Renzo Lucioni Committed by GitHub

Merge pull request #13033 from edx/renzo/handle-missing-catalog-config

Allow program detail pages to function when catalog config is missing
parents 51bb4d4e 935be271
...@@ -5,6 +5,7 @@ import mock ...@@ -5,6 +5,7 @@ import mock
from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.keys import CourseKey
from openedx.core.djangoapps.catalog import utils 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 openedx.core.djangoapps.catalog.tests import factories, mixins
from student.tests.factories import UserFactory from student.tests.factories import UserFactory
...@@ -13,6 +14,8 @@ UTILS_MODULE = 'openedx.core.djangoapps.catalog.utils' ...@@ -13,6 +14,8 @@ UTILS_MODULE = 'openedx.core.djangoapps.catalog.utils'
@mock.patch(UTILS_MODULE + '.get_edx_api_data') @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): class TestGetCourseRun(mixins.CatalogIntegrationMixin, TestCase):
"""Tests covering retrieval of course runs from the catalog service.""" """Tests covering retrieval of course runs from the catalog service."""
def setUp(self): def setUp(self):
...@@ -34,7 +37,7 @@ class TestGetCourseRun(mixins.CatalogIntegrationMixin, TestCase): ...@@ -34,7 +37,7 @@ class TestGetCourseRun(mixins.CatalogIntegrationMixin, TestCase):
return args, kwargs 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() course_run = factories.CourseRun()
mock_get_catalog_data.return_value = course_run mock_get_catalog_data.return_value = course_run
...@@ -43,7 +46,7 @@ class TestGetCourseRun(mixins.CatalogIntegrationMixin, TestCase): ...@@ -43,7 +46,7 @@ class TestGetCourseRun(mixins.CatalogIntegrationMixin, TestCase):
self.assert_contract(mock_get_catalog_data.call_args) self.assert_contract(mock_get_catalog_data.call_args)
self.assertEqual(data, course_run) 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 = [] mock_get_catalog_data.return_value = []
data = utils.get_course_run(self.course_key, self.user) data = utils.get_course_run(self.course_key, self.user)
...@@ -51,14 +54,14 @@ class TestGetCourseRun(mixins.CatalogIntegrationMixin, TestCase): ...@@ -51,14 +54,14 @@ class TestGetCourseRun(mixins.CatalogIntegrationMixin, TestCase):
self.assert_contract(mock_get_catalog_data.call_args) self.assert_contract(mock_get_catalog_data.call_args)
self.assertEqual(data, {}) 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) utils.get_course_run(self.course_key, self.user)
_, kwargs = self.assert_contract(mock_get_catalog_data.call_args) _, kwargs = self.assert_contract(mock_get_catalog_data.call_args)
self.assertIsNone(kwargs['cache_key']) 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) catalog_integration = self.create_catalog_integration(cache_ttl=1)
utils.get_course_run(self.course_key, self.user) utils.get_course_run(self.course_key, self.user)
...@@ -67,6 +70,13 @@ class TestGetCourseRun(mixins.CatalogIntegrationMixin, TestCase): ...@@ -67,6 +70,13 @@ class TestGetCourseRun(mixins.CatalogIntegrationMixin, TestCase):
self.assertEqual(kwargs['cache_key'], catalog_integration.CACHE_KEY) 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 + '.get_course_run')
@mock.patch(UTILS_MODULE + '.strip_querystring') @mock.patch(UTILS_MODULE + '.strip_querystring')
......
...@@ -21,21 +21,24 @@ def get_course_run(course_key, user): ...@@ -21,21 +21,24 @@ def get_course_run(course_key, user):
""" """
catalog_integration = CatalogIntegration.current() catalog_integration = CatalogIntegration.current()
scopes = ['email', 'profile'] if catalog_integration.enabled:
expires_in = settings.OAUTH_ID_TOKEN_EXPIRATION scopes = ['email', 'profile']
jwt = JwtBuilder(user).build_token(scopes, expires_in) expires_in = settings.OAUTH_ID_TOKEN_EXPIRATION
api = EdxRestApiClient(catalog_integration.internal_api_url, jwt=jwt) jwt = JwtBuilder(user).build_token(scopes, expires_in)
api = EdxRestApiClient(catalog_integration.internal_api_url, jwt=jwt)
data = get_edx_api_data(
catalog_integration, data = get_edx_api_data(
user, catalog_integration,
'course_runs', user,
resource_id=unicode(course_key), 'course_runs',
cache_key=catalog_integration.CACHE_KEY if catalog_integration.is_cache_enabled else None, resource_id=unicode(course_key),
api=api, 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): def get_run_marketing_url(course_key, user):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment