From 4282530fafa4d246b9ce8e36a4aa6c46fe98b074 Mon Sep 17 00:00:00 2001 From: Jeff LaJoie Date: Wed, 29 Jul 2020 13:53:00 -0400 Subject: [PATCH] Fixes session caching for enterprise portal links by only caching for auth'd learners --- .../features/enterprise_support/tests/test_utils.py | 6 ++++-- openedx/features/enterprise_support/utils.py | 12 ++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/openedx/features/enterprise_support/tests/test_utils.py b/openedx/features/enterprise_support/tests/test_utils.py index 097fc0b201..94b2a3a67a 100644 --- a/openedx/features/enterprise_support/tests/test_utils.py +++ b/openedx/features/enterprise_support/tests/test_utils.py @@ -8,10 +8,10 @@ import ddt from django.test import TestCase from django.test.utils import override_settings -from django.urls import reverse from openedx.core.djangolib.testing.utils import skip_unless_lms -from openedx.features.enterprise_support.utils import get_enterprise_learner_portals +from openedx.core.djangoapps.waffle_utils.testutils import override_waffle_flag +from openedx.features.enterprise_support.utils import ENTERPRISE_HEADER_LINKS, get_enterprise_learner_portals from openedx.features.enterprise_support.tests import FEATURES_WITH_ENTERPRISE_ENABLED from openedx.features.enterprise_support.tests.factories import ( EnterpriseCustomerBrandingConfigurationFactory, EnterpriseCustomerUserFactory, @@ -48,6 +48,7 @@ class TestEnterpriseUtils(TestCase): self.client.get(resource) self.assertEqual(mock_customer_request.call_count, expected_calls) + @override_waffle_flag(ENTERPRISE_HEADER_LINKS, True) def test_get_enterprise_learner_portals_uncached(self): """ Test that only enabled enterprise portals are returned @@ -68,6 +69,7 @@ class TestEnterpriseUtils(TestCase): 'logo': enterprise_customer_user.enterprise_customer.branding_configuration.logo.url, }) + @override_waffle_flag(ENTERPRISE_HEADER_LINKS, True) def test_get_enterprise_learner_portals_cached(self): enterprise_customer_data = { 'name': 'Enabled Customer', diff --git a/openedx/features/enterprise_support/utils.py b/openedx/features/enterprise_support/utils.py index 62c95dab14..4320766c91 100644 --- a/openedx/features/enterprise_support/utils.py +++ b/openedx/features/enterprise_support/utils.py @@ -18,8 +18,11 @@ from lms.djangoapps.branding.api import get_privacy_url from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers from openedx.core.djangoapps.user_authn.cookies import standard_cookie_settings from openedx.core.djangolib.markup import HTML, Text +from openedx.core.djangoapps.waffle_utils import WaffleFlag from student.helpers import get_next_url_for_login_page +ENTERPRISE_HEADER_LINKS = WaffleFlag('enterprise', 'enterprise_header_links') + def get_data_consent_share_cache_key(user_id, course_id): """ @@ -300,18 +303,19 @@ def get_enterprise_learner_portals(request): # Prevent a circular import. from openedx.features.enterprise_support.api import enterprise_enabled - if enterprise_enabled(): + user = request.user + # Only cache this if a learner is authenticated (AnonymousUser exists and should not be tracked) + if enterprise_enabled() and ENTERPRISE_HEADER_LINKS.is_enabled() and user and user.id: # If the key exists return that value if 'enterprise_learner_portals' in request.session: return json.loads(request.session['enterprise_learner_portals']) - - user = request.user # Ordering is important, this is consistent with how we decide on which # enterprise_customer is the selected one for an enterprise_customer enterprise_learner_portals = [{ 'name': enterprise_customer_user.enterprise_customer.name, 'slug': enterprise_customer_user.enterprise_customer.slug, - 'logo': enterprise_customer_user.enterprise_customer.branding_configuration.logo.url, + 'logo': enterprise_customer_user.enterprise_customer.branding_configuration.logo.url if + enterprise_customer_user.enterprise_customer.branding_configuration else None, } for enterprise_customer_user in EnterpriseCustomerUser.objects.filter( user_id=user.id, enterprise_customer__enable_learner_portal=True ).prefetch_related(