From 354d2be84588cb01b8fa6caf3456cba7f7049ec0 Mon Sep 17 00:00:00 2001 From: Zaman Afzal Date: Wed, 17 Aug 2022 17:29:21 +0500 Subject: [PATCH] feat: Update the cache when active enterprise is changed. (#30866) --- .../enterprise_support/tests/test_utils.py | 73 ++++++++++++++----- openedx/features/enterprise_support/utils.py | 13 +++- 2 files changed, 64 insertions(+), 22 deletions(-) diff --git a/openedx/features/enterprise_support/tests/test_utils.py b/openedx/features/enterprise_support/tests/test_utils.py index ec271de137..26781552b6 100644 --- a/openedx/features/enterprise_support/tests/test_utils.py +++ b/openedx/features/enterprise_support/tests/test_utils.py @@ -336,29 +336,30 @@ class TestEnterpriseUtils(TestCase): Test that only an enabled enterprise portal is returned, and that it matches the customer UUID provided in the request. """ - enterprise_customer_user = EnterpriseCustomerUserFactory(active=True, user_id=self.user.id) + old_active_enterprise_customer_user = EnterpriseCustomerUserFactory(active=True, user_id=self.user.id) + old_active_enterprise_customer_user.enable_learner_portal = True + old_active_enterprise_customer_user.save() + + current_active_enterprise_customer_user = EnterpriseCustomerUserFactory(active=True, user_id=self.user.id) + current_active_enterprise_customer = current_active_enterprise_customer_user.enterprise_customer EnterpriseCustomerBrandingConfigurationFactory( - enterprise_customer=enterprise_customer_user.enterprise_customer, + enterprise_customer=current_active_enterprise_customer, ) - enterprise_customer_user.enterprise_customer.enable_learner_portal = True - enterprise_customer_user.enterprise_customer.save() + current_active_enterprise_customer.enable_learner_portal = True + current_active_enterprise_customer.save() request = mock.MagicMock(session={}, user=self.user) - # Indicate the "preferred" customer in the request - request.GET = {'enterprise_customer': enterprise_customer_user.enterprise_customer.uuid} - # Create another enterprise customer association for the same user. - # There should be no data returned for this customer's portal, - # because we filter for only the enterprise customer uuid found in the request. - other_enterprise_customer_user = EnterpriseCustomerUserFactory(active=True, user_id=self.user.id) - other_enterprise_customer_user.enable_learner_portal = True - other_enterprise_customer_user.save() - - portal = get_enterprise_learner_portal(request) + # There should be data returned for the latest enterprise association. + with mock.patch( + 'openedx.features.enterprise_support.api.enterprise_customer_uuid_for_request', + return_value=str(current_active_enterprise_customer.uuid), + ): + portal = get_enterprise_learner_portal(request) self.assertDictEqual(portal, { - 'name': enterprise_customer_user.enterprise_customer.name, - 'slug': enterprise_customer_user.enterprise_customer.slug, - 'logo': enterprise_customer_user.enterprise_customer.safe_branding_configuration.safe_logo_url, + 'name': current_active_enterprise_customer.name, + 'slug': current_active_enterprise_customer.slug, + 'logo': current_active_enterprise_customer.safe_branding_configuration.safe_logo_url, }) @override_waffle_flag(ENTERPRISE_HEADER_LINKS, True) @@ -425,11 +426,47 @@ class TestEnterpriseUtils(TestCase): 'logo': 'https://logo.url', } request = mock.MagicMock(session={ - 'enterprise_learner_portal': json.dumps(enterprise_customer_data) + 'enterprise_learner_portal': json.dumps(enterprise_customer_data), + 'enterprise_customer': enterprise_customer_data, }, user=self.user) portal = get_enterprise_learner_portal(request) self.assertDictEqual(portal, enterprise_customer_data) + @override_waffle_flag(ENTERPRISE_HEADER_LINKS, True) + def test_get_enterprise_learner_portal_of_updated_enterprise(self): + """ + Test that when active enterprise changes, only current active enterprise portal is returned. + """ + current_active_enterprise_customer_user = EnterpriseCustomerUserFactory(user_id=self.user.id) + current_active_enterprise_customer = current_active_enterprise_customer_user.enterprise_customer + EnterpriseCustomerBrandingConfigurationFactory( + enterprise_customer=current_active_enterprise_customer, + ) + current_active_enterprise_customer.enable_learner_portal = True + current_active_enterprise_customer.save() + + old_active_enterprise_customer_user = EnterpriseCustomerUserFactory(user_id=self.user.id) + old_active_enterprise_customer_data = { + 'name': old_active_enterprise_customer_user.enterprise_customer.name, + 'slug': old_active_enterprise_customer_user.enterprise_customer.slug, + 'logo': 'https://logo.url', + } + current_active_enterprise_customer_data_updated = { + 'name': current_active_enterprise_customer.name, + 'slug': current_active_enterprise_customer.slug, + 'logo': current_active_enterprise_customer.safe_branding_configuration.safe_logo_url, + } + request = mock.MagicMock(session={ + 'enterprise_learner_portal': json.dumps(old_active_enterprise_customer_data), + 'enterprise_customer': current_active_enterprise_customer_data_updated, + }, user=self.user) + portal = get_enterprise_learner_portal(request) + self.assertDictEqual(portal, { + 'name': current_active_enterprise_customer.name, + 'slug': current_active_enterprise_customer.slug, + 'logo': current_active_enterprise_customer.safe_branding_configuration.safe_logo_url, + }) + @override_waffle_flag(ENTERPRISE_HEADER_LINKS, True) def test_get_enterprise_learner_portal_no_enterprise_user(self): request = mock.MagicMock(session={}, user=self.user) diff --git a/openedx/features/enterprise_support/utils.py b/openedx/features/enterprise_support/utils.py index 4604b5c4fb..12dd1f16d6 100644 --- a/openedx/features/enterprise_support/utils.py +++ b/openedx/features/enterprise_support/utils.py @@ -329,17 +329,22 @@ def get_enterprise_learner_portal(request): # Only cache this if a learner is authenticated (AnonymousUser exists and should not be tracked) learner_portal_session_key = 'enterprise_learner_portal' - + enterprise_customer_session_key = 'enterprise_customer' if enterprise_enabled() and ENTERPRISE_HEADER_LINKS.is_enabled() and user and user.id: # If the key exists return that value - if learner_portal_session_key in request.session: - return json.loads(request.session[learner_portal_session_key]) + if learner_portal_session_key in request.session and enterprise_customer_session_key in request.session: + learner_portal_session = json.loads(request.session[learner_portal_session_key]) + if request.session[enterprise_customer_session_key].get('slug') == learner_portal_session.get('slug'): + return learner_portal_session kwargs = { 'user_id': user.id, 'enterprise_customer__enable_learner_portal': True, } - enterprise_customer_uuid = enterprise_customer_uuid_for_request(request) + if enterprise_customer_session_key in request.session: + enterprise_customer_uuid = request.session[enterprise_customer_session_key].get('uuid') + else: + enterprise_customer_uuid = enterprise_customer_uuid_for_request(request) if enterprise_customer_uuid: kwargs['enterprise_customer__uuid'] = enterprise_customer_uuid