diff --git a/enterprise_catalog/apps/catalog/models.py b/enterprise_catalog/apps/catalog/models.py index aaaf7999..3774b567 100644 --- a/enterprise_catalog/apps/catalog/models.py +++ b/enterprise_catalog/apps/catalog/models.py @@ -50,6 +50,7 @@ from enterprise_catalog.apps.catalog.utils import ( batch, enterprise_proxy_login_url, + get_advertised_course_run, get_content_filter_hash, get_content_key, get_content_type, @@ -555,10 +556,16 @@ def get_content_enrollment_url(self, content_metadata): else: # Catalog param only needed for legacy (non-learner-portal) enrollment URLs params['catalog'] = self.uuid + + course_key = content_key if parent_content_key else None + course_run_key = content_key if not parent_content_key else None + if not parent_content_key: + if advertised_course_run := get_advertised_course_run(content_metadata.json_metadata): + course_run_key = advertised_course_run['key'] url = '{}/enterprise/{}/course/{}/enroll/'.format( settings.LMS_BASE_URL, self.enterprise_uuid, - content_key, + course_run_key or course_key, ) return update_query_parameters(url, params) diff --git a/enterprise_catalog/apps/catalog/tests/factories.py b/enterprise_catalog/apps/catalog/tests/factories.py index 45fab4b4..93ef6394 100644 --- a/enterprise_catalog/apps/catalog/tests/factories.py +++ b/enterprise_catalog/apps/catalog/tests/factories.py @@ -99,7 +99,7 @@ def _json_metadata(self): 'logo_image_url': fake.image_url() + '.jpg', }] course_runs = [{ - 'key': 'course-v1:edX+DemoX', + 'key': 'course-v1:edX+DemoX+2T2024', 'uuid': str(FAKE_ADVERTISED_COURSE_RUN_UUID), 'content_language': 'en-us', 'status': 'published', diff --git a/enterprise_catalog/apps/catalog/tests/test_models.py b/enterprise_catalog/apps/catalog/tests/test_models.py index 03b7367f..47f97e32 100644 --- a/enterprise_catalog/apps/catalog/tests/test_models.py +++ b/enterprise_catalog/apps/catalog/tests/test_models.py @@ -623,6 +623,46 @@ def test_get_content_enrollment_url( else: assert settings.LMS_BASE_URL in content_enrollment_url + @ddt.data( + { + 'content_key': 'course-v1:edX+DemoX+2T2024', + 'parent_content_key': 'edX+DemoX', + }, + { + 'content_key': 'edX+DemoX', + 'parent_content_key': None + }, + ) + @ddt.unpack + def test_get_content_enrollment_url_disabled_learner_portal( + self, + content_key, + parent_content_key, + ): + enterprise_uuid = uuid4() + enterprise_slug = 'sluggy' + + enterprise_catalog = factories.EnterpriseCatalogFactory(enterprise_uuid=enterprise_uuid) + content_metadata = factories.ContentMetadataFactory( + content_key=content_key, + parent_content_key=parent_content_key, + content_type=COURSE, + ) + + enterprise_catalog.catalog_query.contentmetadata_set.add(*[content_metadata]) + + mock_enterprise_customer_return_value = { + 'slug': enterprise_slug, + 'enable_learner_portal': False, + } + + with self._mock_enterprise_customer_cache( + mock_enterprise_customer_return_value, + ): + content_enrollment_url = enterprise_catalog.get_content_enrollment_url(content_metadata) + + assert "/course/course-v1:edX+DemoX+2T2024/enroll/" in content_enrollment_url + @mock.patch('enterprise_catalog.apps.api_client.enterprise_cache.EnterpriseApiClient') @ddt.data( { diff --git a/enterprise_catalog/apps/catalog/utils.py b/enterprise_catalog/apps/catalog/utils.py index 7a046424..444bce1e 100644 --- a/enterprise_catalog/apps/catalog/utils.py +++ b/enterprise_catalog/apps/catalog/utils.py @@ -176,3 +176,20 @@ def get_course_run_by_uuid(course, course_run_uuid): except IndexError: return None return course_run + + +def get_advertised_course_run(course): + """ + Get part of the advertised course_run as per advertised_course_run_uuid + + Argument: + course (dict) + + Returns: + dict: containing key, pacing_type, start, end, and upgrade deadline + for the course_run, or None + """ + full_course_run = get_course_run_by_uuid(course, course.get('advertised_course_run_uuid')) + if full_course_run is None: + return None + return full_course_run