From 34418a2667e3dada656a34b5d310fd07a082ee84 Mon Sep 17 00:00:00 2001 From: Long Lin Date: Tue, 19 Oct 2021 17:58:19 -0400 Subject: [PATCH] fix: check enterprise_enabled in is_enterprise_learner --- common/djangoapps/student/tests/test_activate_account.py | 2 +- openedx/features/enterprise_support/context.py | 3 +-- openedx/features/enterprise_support/signals.py | 5 ++--- openedx/features/enterprise_support/tests/test_utils.py | 9 +++++++++ openedx/features/enterprise_support/utils.py | 7 +++++++ 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/common/djangoapps/student/tests/test_activate_account.py b/common/djangoapps/student/tests/test_activate_account.py index 50ce098c21..2605dafc6b 100644 --- a/common/djangoapps/student/tests/test_activate_account.py +++ b/common/djangoapps/student/tests/test_activate_account.py @@ -167,7 +167,7 @@ class TestActivateAccount(TestCase): self.assertContains(response, 'Your account could not be activated') @override_settings(LOGIN_REDIRECT_WHITELIST=['localhost:1991']) - @override_settings(FEATURES=FEATURES_WITH_AUTHN_MFE_ENABLED) + @override_settings(FEATURES={**FEATURES_WITH_AUTHN_MFE_ENABLED, 'ENABLE_ENTERPRISE_INTEGRATION': True}) @override_waffle_flag(REDIRECT_TO_AUTHN_MICROFRONTEND, active=True) def test_authenticated_account_activation_with_valid_next_url(self): """ diff --git a/openedx/features/enterprise_support/context.py b/openedx/features/enterprise_support/context.py index af287b775c..f4f090f8fa 100644 --- a/openedx/features/enterprise_support/context.py +++ b/openedx/features/enterprise_support/context.py @@ -21,10 +21,9 @@ def get_enterprise_event_context(user_id, course_id): dict: A dictionary representing the enterprise uuid. """ # Prevent a circular import. - from openedx.features.enterprise_support.api import enterprise_enabled from openedx.features.enterprise_support.utils import is_enterprise_learner context = {} - if enterprise_enabled() and is_enterprise_learner(user_id): + if is_enterprise_learner(user_id): uuids = EnterpriseCourseEnrollment.get_enterprise_uuids_with_user_and_course(str(user_id), str(course_id)) if uuids: context.update({"enterprise_uuid": str(uuids[0])}) diff --git a/openedx/features/enterprise_support/signals.py b/openedx/features/enterprise_support/signals.py index 454141c100..975058b867 100644 --- a/openedx/features/enterprise_support/signals.py +++ b/openedx/features/enterprise_support/signals.py @@ -18,7 +18,6 @@ from slumber.exceptions import HttpClientError from openedx.core.djangoapps.commerce.utils import ecommerce_api_client from openedx.core.djangoapps.signals.signals import COURSE_GRADE_NOW_PASSED, COURSE_ASSESSMENT_GRADE_CHANGED -from openedx.features.enterprise_support.api import enterprise_enabled from openedx.features.enterprise_support.tasks import clear_enterprise_customer_data_consent_share_cache from openedx.features.enterprise_support.utils import clear_data_consent_share_cache, is_enterprise_learner from common.djangoapps.student.signals import UNENROLL_DONE @@ -62,7 +61,7 @@ def handle_enterprise_learner_passing_grade(sender, user, course_id, **kwargs): """ Listen for a learner passing a course, transmit data to relevant integrated channel """ - if enterprise_enabled() and is_enterprise_learner(user): + if is_enterprise_learner(user): kwargs = { 'username': str(user.username), 'course_run_id': str(course_id) @@ -76,7 +75,7 @@ def handle_enterprise_learner_subsection(sender, user, course_id, subsection_id, """ Listen for an enterprise learner completing a subsection, transmit data to relevant integrated channel. """ - if enterprise_enabled() and is_enterprise_learner(user): + if is_enterprise_learner(user): kwargs = { 'username': str(user.username), 'course_run_id': str(course_id), diff --git a/openedx/features/enterprise_support/tests/test_utils.py b/openedx/features/enterprise_support/tests/test_utils.py index 101c54c5fc..9e28045988 100644 --- a/openedx/features/enterprise_support/tests/test_utils.py +++ b/openedx/features/enterprise_support/tests/test_utils.py @@ -481,6 +481,15 @@ class TestEnterpriseUtils(TestCase): assert not mock_cache_set.called + @mock.patch('django.core.cache.cache.set') + @mock.patch('django.core.cache.cache.get') + @mock.patch('openedx.features.enterprise_support.api.enterprise_enabled', return_value=False) + def test_is_enterprise_learner_enterprise_disabled(self, _, mock_cache_get, mock_cache_set): + assert not is_enterprise_learner(self.user) + assert not is_enterprise_learner(self.user.id) + assert not mock_cache_get.called + assert not mock_cache_set.called + @mock.patch('openedx.features.enterprise_support.utils.reverse') def test_get_enterprise_slug_login_url_no_reverse_match(self, mock_reverse): mock_reverse.side_effect = NoReverseMatch diff --git a/openedx/features/enterprise_support/utils.py b/openedx/features/enterprise_support/utils.py index 11ef96d02a..88eb766c74 100644 --- a/openedx/features/enterprise_support/utils.py +++ b/openedx/features/enterprise_support/utils.py @@ -420,6 +420,12 @@ def is_enterprise_learner(user): Returns: (bool): True if given user is an enterprise learner. """ + # Prevent a circular import. + from openedx.features.enterprise_support.api import enterprise_enabled + + if not enterprise_enabled(): + return False + try: user_id = int(user) except TypeError: @@ -432,6 +438,7 @@ def is_enterprise_learner(user): # Cache the enterprise user for one hour. cache.set(cached_is_enterprise_key, True, 3600) return True + return False