Merge pull request #35097 from openedx/bbeggs/ENT-9187

feat: check for session existance before cache access. ENT-9187.
This commit is contained in:
Brian Beggs
2024-07-09 08:05:35 -04:00
committed by GitHub
2 changed files with 11 additions and 1 deletions

View File

@@ -422,8 +422,13 @@ def enterprise_customer_from_session(request):
"""
Retrieve enterprise_customer data from the request's session,
returning a ``__CACHE_MISS__`` if absent.
Now checks for session existence before attempting to access it.
"""
return request.session.get(ENTERPRISE_CUSTOMER_KEY_NAME, _CACHE_MISS)
if not request or not hasattr(request, 'session'):
return _CACHE_MISS
else:
return request.session.get(ENTERPRISE_CUSTOMER_KEY_NAME, _CACHE_MISS)
def enterprise_customer_uuid_from_session(request):

View File

@@ -36,6 +36,7 @@ from openedx.features.enterprise_support.api import (
data_sharing_consent_required,
enterprise_customer_for_request,
enterprise_customer_from_api,
enterprise_customer_from_session,
enterprise_customer_from_session_or_learner_data,
enterprise_customer_uuid_for_request,
enterprise_enabled,
@@ -1331,6 +1332,10 @@ class TestEnterpriseApi(EnterpriseServiceMockMixin, CacheIsolationTestCase):
# verify that existing session value should not be updated for un-authenticate user
assert mock_request.session[ENTERPRISE_CUSTOMER_KEY_NAME] == enterprise_customer
@ddt.data(None, object())
def test_enterprise_customer_from_session_no_session_CACHE_MISS(self, request):
assert enterprise_customer_from_session(request) == _CACHE_MISS
def test_get_consent_notification_data_no_overrides(self):
enterprise_customer = {
'name': 'abc',