feat: Update the cache when active enterprise is changed. (#30866)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user