From 7568e91a7607db39def976f991907049463cb539 Mon Sep 17 00:00:00 2001 From: Brian Beggs Date: Mon, 8 Jul 2024 16:06:16 -0400 Subject: [PATCH] feat: check for session existance before cache access. ENT-9187. --- openedx/features/enterprise_support/api.py | 7 ++++++- openedx/features/enterprise_support/tests/test_api.py | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/openedx/features/enterprise_support/api.py b/openedx/features/enterprise_support/api.py index 5b62815f06fe..32c493d1e35e 100644 --- a/openedx/features/enterprise_support/api.py +++ b/openedx/features/enterprise_support/api.py @@ -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): diff --git a/openedx/features/enterprise_support/tests/test_api.py b/openedx/features/enterprise_support/tests/test_api.py index 8629caae9067..40f2c834e6b0 100644 --- a/openedx/features/enterprise_support/tests/test_api.py +++ b/openedx/features/enterprise_support/tests/test_api.py @@ -36,6 +36,7 @@ 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 @@ def test_enterprise_customer_from_session(self): # 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',