def test_is_course_in_enterprise_catalog_for_unavailable_course_cached( self): """ Verify that the response from the discovery API call made in method "is_course_in_enterprise_catalog" is cached for cases where the course is not available in the enterprise course catalog. We expect 2 calls to set_all_tiers due to: - the site_configuration api setup - the result being cached """ enterprise_catalog_id = 1 self.mock_access_token_response() self.mock_catalog_contains_endpoint( discovery_api_url=self.site_configuration.discovery_api_url, catalog_id=enterprise_catalog_id, course_run_ids=[self.course.id]) test_course = CourseFactory(id='edx/Non_Enterprise_Course/DemoX') with patch.object( TieredCache, 'set_all_tiers', wraps=TieredCache.set_all_tiers) as mocked_set_all_tiers: mocked_set_all_tiers.assert_not_called() is_course_available = is_course_in_enterprise_catalog( self.request.site, test_course.id, enterprise_catalog_id) self.assertEqual(mocked_set_all_tiers.call_count, 2) self.assertFalse(is_course_available) is_course_available = is_course_in_enterprise_catalog( self.request.site, test_course.id, enterprise_catalog_id) self.assertEqual(mocked_set_all_tiers.call_count, 2) self.assertFalse(is_course_available)
def _assert_is_course_in_enterprise_catalog_for_failure( self, expected_requests, log_message): """ Helper method to validate the response from the method "is_course_in_enterprise_catalog" and verify the logged message. """ enterprise_catalog_id = 1 logger_name = 'ecommerce.enterprise.entitlements' with LogCapture(logger_name) as logger: is_course_available = is_course_in_enterprise_catalog( self.request.site, self.course.id, enterprise_catalog_id) self._assert_num_requests(expected_requests) logger.check((logger_name, 'ERROR', log_message)) self.assertFalse(is_course_available)
def test_is_course_in_enterprise_catalog_for_available_course(self): """ Verify that method "is_course_in_enterprise_catalog" returns True if the provided course is available in the enterprise course catalog. """ enterprise_catalog_id = 1 self.mock_course_discovery_api_for_catalog_contains( catalog_id=enterprise_catalog_id, course_run_ids=[self.course.id]) is_course_available = is_course_in_enterprise_catalog( self.request.site, self.course.id, enterprise_catalog_id) # Verify that there only one call for the course discovery API for # checking if course exists in course runs against the course catalog. self._assert_num_requests(1) self.assertTrue(is_course_available)
def test_is_course_in_enterprise_catalog_for_unavailable_course(self): """ Verify that method "is_course_in_enterprise_catalog" returns False if the provided course is not available in the enterprise course catalog. """ enterprise_catalog_id = 1 self.mock_course_discovery_api_for_catalog_contains( catalog_id=enterprise_catalog_id, course_run_ids=[self.course.id]) test_course = CourseFactory(id='edx/Non_Enterprise_Course/DemoX') is_course_available = is_course_in_enterprise_catalog( self.request.site, test_course.id, enterprise_catalog_id) # Verify that there only one call for the course discovery API for # checking if course exists in course runs against the course catalog. self._assert_num_requests(1) self.assertFalse(is_course_available)
def test_is_course_in_enterprise_catalog_for_available_course(self): """ Verify that method "is_course_in_enterprise_catalog" returns True if the provided course is available in the enterprise course catalog. """ enterprise_catalog_id = 1 catalog_query = '*:*' self.mock_course_discovery_api_for_catalog_by_resource_id( catalog_id=enterprise_catalog_id, catalog_query=catalog_query ) self.mock_dynamic_catalog_contains_api(query=catalog_query, course_run_ids=[self.course.id]) is_course_available = is_course_in_enterprise_catalog(self.request.site, self.course.id, enterprise_catalog_id) # Verify that there were two calls for the course discovery API, one # for getting enterprise course catalog and the other for verifying if # course exists in course runs against the course catalog query self._assert_num_requests(2) self.assertTrue(is_course_available)