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',