From 47cdf39266fe55064064895e162c052beff8b614 Mon Sep 17 00:00:00 2001
From: asadiqbal <aiqbal@edx.org>
Date: Thu, 27 Apr 2017 18:39:44 +0500
Subject: [PATCH] ENT-342

---
 common/djangoapps/course_modes/tests/test_views.py |  8 +++++++-
 common/djangoapps/course_modes/views.py            |  1 -
 openedx/features/enterprise_support/api.py         | 14 +++++++++++++-
 3 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/common/djangoapps/course_modes/tests/test_views.py b/common/djangoapps/course_modes/tests/test_views.py
index c46db18..94eb2a7 100644
--- a/common/djangoapps/course_modes/tests/test_views.py
+++ b/common/djangoapps/course_modes/tests/test_views.py
@@ -29,12 +29,13 @@ from util.testing import UrlResetMixin
 from openedx.core.djangoapps.theming.tests.test_util import with_comprehensive_theme
 from util.tests.mixins.discovery import CourseCatalogServiceMockMixin
 from util import organizations_helpers as organizations_api
+from openedx.core.djangoapps.catalog.tests.mixins import CatalogIntegrationMixin
 
 
 @attr(shard=3)
 @ddt.ddt
 @unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
-class CourseModeViewTest(UrlResetMixin, ModuleStoreTestCase, EnterpriseServiceMockMixin, CourseCatalogServiceMockMixin):
+class CourseModeViewTest(CatalogIntegrationMixin, UrlResetMixin, ModuleStoreTestCase, EnterpriseServiceMockMixin, CourseCatalogServiceMockMixin):
     """
     Course Mode View tests
     """
@@ -155,6 +156,9 @@ class CourseModeViewTest(UrlResetMixin, ModuleStoreTestCase, EnterpriseServiceMo
         for mode in ('audit', 'honor', 'verified'):
             CourseModeFactory.create(mode_slug=mode, course_id=self.course.id)
 
+        catalog_integration = self.create_catalog_integration()
+        UserFactory(username=catalog_integration.service_username)
+
         self.mock_enterprise_learner_api()
 
         self.mock_course_discovery_api_for_catalog_contains(
@@ -185,6 +189,8 @@ class CourseModeViewTest(UrlResetMixin, ModuleStoreTestCase, EnterpriseServiceMo
         for mode in ('audit', 'honor', 'verified'):
             CourseModeFactory.create(mode_slug=mode, course_id=self.course.id)
 
+        catalog_integration = self.create_catalog_integration()
+        UserFactory(username=catalog_integration.service_username)
         self.mock_enterprise_learner_api()
         self.mock_course_discovery_api_for_catalog_contains(
             catalog_id=1, course_run_ids=[str(self.course.id)]
diff --git a/common/djangoapps/course_modes/views.py b/common/djangoapps/course_modes/views.py
index ef351e1..9377021 100644
--- a/common/djangoapps/course_modes/views.py
+++ b/common/djangoapps/course_modes/views.py
@@ -159,7 +159,6 @@ class ChooseModeView(View):
             is_course_in_enterprise_catalog = enterprise_api.is_course_in_enterprise_catalog(
                 site=request.site,
                 course_id=course_id,
-                user=request.user,
                 enterprise_catalog_id=enterprise_learner_data[0]['enterprise_customer']['catalog']
             )
 
diff --git a/openedx/features/enterprise_support/api.py b/openedx/features/enterprise_support/api.py
index 7160bab..b8e876f 100644
--- a/openedx/features/enterprise_support/api.py
+++ b/openedx/features/enterprise_support/api.py
@@ -29,6 +29,7 @@ from requests.exceptions import ConnectionError, Timeout
 from openedx.core.djangoapps.api_admin.utils import course_discovery_api_client
 
 from openedx.core.lib.token_utils import JwtBuilder
+from openedx.core.djangoapps.catalog.models import CatalogIntegration
 
 
 CONSENT_FAILED_PARAMETER = 'consent_failed'
@@ -455,7 +456,7 @@ def get_dashboard_consent_notification(request, user, course_enrollments):
     return ''
 
 
-def is_course_in_enterprise_catalog(site, course_id, user, enterprise_catalog_id):
+def is_course_in_enterprise_catalog(site, course_id, enterprise_catalog_id):
     """
     Verify that the provided course id exists in the site base list of course
     run keys from the provided enterprise course catalog.
@@ -477,6 +478,17 @@ def is_course_in_enterprise_catalog(site, course_id, user, enterprise_catalog_id
     )
     response = cache.get(cache_key)
     if not response:
+        catalog_integration = CatalogIntegration.current()
+        if not catalog_integration.enabled:
+            LOGGER.error("Catalog integration is not enabled.")
+            return False
+
+        try:
+            user = User.objects.get(username=catalog_integration.service_username)
+        except User.DoesNotExist:
+            LOGGER.exception("Catalog service user '%s' does not exist.", catalog_integration.service_username)
+            return False
+
         try:
             # GET: /api/v1/catalogs/{catalog_id}/contains?course_run_id={course_run_ids}
             response = course_discovery_api_client(user=user).catalogs(enterprise_catalog_id).contains.get(
--
libgit2 0.26.0