diff --git a/openedx/core/djangoapps/catalog/tests/test_utils.py b/openedx/core/djangoapps/catalog/tests/test_utils.py index 1f9c4dfb542a23c22c250f14092d03049511775f..47ce167c8662685ce327934df000b22a482324f1 100644 --- a/openedx/core/djangoapps/catalog/tests/test_utils.py +++ b/openedx/core/djangoapps/catalog/tests/test_utils.py @@ -57,6 +57,7 @@ from openedx.core.djangoapps.content.course_overviews.tests.factories import Cou from openedx.core.djangoapps.site_configuration.tests.factories import SiteFactory from openedx.core.djangolib.testing.utils import CacheIsolationTestCase, skip_unless_lms from student.tests.factories import CourseEnrollmentFactory, UserFactory +from openedx.core.djangoapps.site_configuration.tests.test_util import with_site_configuration_context UTILS_MODULE = 'openedx.core.djangoapps.catalog.utils' User = get_user_model() # pylint: disable=invalid-name @@ -83,11 +84,12 @@ class TestGetPrograms(CacheIsolationTestCase): # When called before UUIDs are cached, the function should return an # empty list and log a warning. - self.assertEqual(get_programs(site=self.site), []) - mock_warning.assert_called_once_with( - u'Failed to get program UUIDs from the cache for site {}.'.format(self.site.domain) - ) - mock_warning.reset_mock() + with with_site_configuration_context(domain=self.site.name, configuration={'COURSE_CATALOG_API_URL': 'foo'}): + self.assertEqual(get_programs(site=self.site), []) + mock_warning.assert_called_once_with( + u'Failed to get program UUIDs from the cache for site {}.'.format(self.site.domain) + ) + mock_warning.reset_mock() # Cache UUIDs for all 3 programs. cache.set( @@ -158,21 +160,22 @@ class TestGetPrograms(CacheIsolationTestCase): mock_cache.get.return_value = [program['uuid'] for program in programs] mock_cache.get_many.side_effect = fake_get_many - actual_programs = get_programs(site=self.site) + with with_site_configuration_context(domain=self.site.name, configuration={'COURSE_CATALOG_API_URL': 'foo'}): + actual_programs = get_programs(site=self.site) # All 3 cached programs should be returned. An info message should be # logged about the one that was initially missing, but the code should # be able to stitch together all the details. - self.assertEqual( - set(program['uuid'] for program in actual_programs), - set(program['uuid'] for program in all_programs.values()) - ) - self.assertFalse(mock_warning.called) - mock_info.assert_called_with('Failed to get details for 1 programs. Retrying.') - - for program in actual_programs: - key = PROGRAM_CACHE_KEY_TPL.format(uuid=program['uuid']) - self.assertEqual(program, all_programs[key]) + self.assertEqual( + set(program['uuid'] for program in actual_programs), + set(program['uuid'] for program in all_programs.values()) + ) + self.assertFalse(mock_warning.called) + mock_info.assert_called_with('Failed to get details for 1 programs. Retrying.') + + for program in actual_programs: + key = PROGRAM_CACHE_KEY_TPL.format(uuid=program['uuid']) + self.assertEqual(program, all_programs[key]) def test_get_one(self, mock_warning, _mock_info): expected_program = ProgramFactory() diff --git a/openedx/core/djangoapps/catalog/utils.py b/openedx/core/djangoapps/catalog/utils.py index c5604de59d4ceed4c8dccf931aa78cb4325edd38..cb3343c4cfba2bbc4575c1385082211cf5ef4c84 100644 --- a/openedx/core/djangoapps/catalog/utils.py +++ b/openedx/core/djangoapps/catalog/utils.py @@ -119,9 +119,14 @@ def get_programs(site=None, uuid=None, uuids=None, course=None, organization=Non # without programs. After this is changed, log any cache misses here. return [] elif site: - uuids = cache.get(SITE_PROGRAM_UUIDS_CACHE_KEY_TPL.format(domain=site.domain), []) - if not uuids: - logger.warning(u'Failed to get program UUIDs from the cache for site {}.'.format(site.domain)) + site_config = getattr(site, 'configuration', None) + catalog_url = site_config.get_value('COURSE_CATALOG_API_URL') if site_config else None + if site_config and catalog_url: + uuids = cache.get(SITE_PROGRAM_UUIDS_CACHE_KEY_TPL.format(domain=site.domain), []) + if not uuids: + logger.warning(u'Failed to get program UUIDs from the cache for site {}.'.format(site.domain)) + else: + uuids = [] elif organization: uuids = get_programs_for_organization(organization) if not uuids: