def get_enterprise_learner_portal(request): """ Gets the formatted portal name and slug that can be used to generate a link for an enabled enterprise Learner Portal. Caches and returns result in/from the user's request session if provided. """ # Prevent a circular import. from openedx.features.enterprise_support.api import enterprise_enabled, enterprise_customer_uuid_for_request user = request.user # Only cache this if a learner is authenticated (AnonymousUser exists and should not be tracked) learner_portal_session_key = 'enterprise_learner_portal' 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]) kwargs = { 'user_id': user.id, 'enterprise_customer__enable_learner_portal': True, } enterprise_customer_uuid = enterprise_customer_uuid_for_request(request) if enterprise_customer_uuid: kwargs['enterprise_customer__uuid'] = enterprise_customer_uuid queryset = EnterpriseCustomerUser.objects.filter(**kwargs).prefetch_related( 'enterprise_customer', 'enterprise_customer__branding_configuration', ) if not enterprise_customer_uuid: # If the request doesn't help us know which Enterprise Customer UUID to select with, # order by the most recently activated/modified customers, # so that when we select the first result of the query as the preferred # customer, it's the most recently active one. queryset = queryset.order_by('-enterprise_customer__active', '-modified') preferred_enterprise_customer_user = queryset.first() if not preferred_enterprise_customer_user: return None enterprise_customer = preferred_enterprise_customer_user.enterprise_customer learner_portal_data = { 'name': enterprise_customer.name, 'slug': enterprise_customer.slug, 'logo': enterprise_branding_configuration(enterprise_customer).get('logo'), } # Cache the result in the user's request session request.session[learner_portal_session_key] = json.dumps(learner_portal_data) return learner_portal_data return None
def test_customer_uuid_for_request_sso_provider_id_customer_non_existent(self, mock_partial_pipeline): mock_request = mock.Mock( GET={'tpa_hint': 'my-third-party-auth'}, COOKIES={}, session={}, ) actual_uuid = enterprise_customer_uuid_for_request(mock_request) self.assertIsNone(actual_uuid) mock_partial_pipeline.assert_called_once_with(mock_request) self.assertNotIn(ENTERPRISE_CUSTOMER_KEY_NAME, mock_request.session)
def test_enterprise_uuid_for_request_from_session(self, mock_partial_pipeline): expected_uuid = 'my-uuid' mock_request = mock.Mock( GET={}, COOKIES={}, session={ENTERPRISE_CUSTOMER_KEY_NAME: {'uuid': expected_uuid}}, ) actual_uuid = enterprise_customer_uuid_for_request(mock_request) assert expected_uuid == actual_uuid mock_partial_pipeline.assert_called_once_with(mock_request) assert {'uuid': expected_uuid} == mock_request.session.get(ENTERPRISE_CUSTOMER_KEY_NAME)
def test_enterprise_uuid_for_request_from_cookies(self, mock_partial_pipeline): expected_uuid = 'my-uuid' mock_request = mock.Mock( GET={}, COOKIES={settings.ENTERPRISE_CUSTOMER_COOKIE_NAME: expected_uuid}, session={}, ) actual_uuid = enterprise_customer_uuid_for_request(mock_request) assert expected_uuid == actual_uuid mock_partial_pipeline.assert_called_once_with(mock_request) assert ENTERPRISE_CUSTOMER_KEY_NAME not in mock_request.session
def test_enterprise_uuid_for_request_from_query_params(self, mock_partial_pipeline): expected_uuid = 'my-uuid' mock_request = mock.Mock( GET={ENTERPRISE_CUSTOMER_KEY_NAME: expected_uuid}, COOKIES={}, session={}, ) actual_uuid = enterprise_customer_uuid_for_request(mock_request) self.assertEqual(expected_uuid, actual_uuid) mock_partial_pipeline.assert_called_once_with(mock_request) self.assertNotIn(ENTERPRISE_CUSTOMER_KEY_NAME, mock_request.session)
def test_enterprise_uuid_for_request_cache_miss_non_existent( self, mock_partial_pipeline, mock_data_from_db): mock_request = mock.Mock( GET={}, COOKIES={}, session={}, ) actual_uuid = enterprise_customer_uuid_for_request(mock_request) self.assertIsNone(actual_uuid) mock_partial_pipeline.assert_called_once_with(mock_request) mock_data_from_db.assert_called_once_with(mock_request.user) self.assertIsNone(mock_request.session[ENTERPRISE_CUSTOMER_KEY_NAME])
def test_customer_uuid_for_request_sso_provider_id_customer_exists(self, mock_partial_pipeline): mock_idp = EnterpriseCustomerIdentityProviderFactory.create() mock_customer = mock_idp.enterprise_customer mock_request = mock.Mock( GET={'tpa_hint': mock_idp.provider_id}, COOKIES={}, session={}, ) actual_uuid = enterprise_customer_uuid_for_request(mock_request) expected_uuid = mock_customer.uuid assert expected_uuid == actual_uuid mock_partial_pipeline.assert_called_once_with(mock_request) assert ENTERPRISE_CUSTOMER_KEY_NAME not in mock_request.session
def test_enterprise_uuid_for_request_cache_miss_but_exists_in_db(self, mock_partial_pipeline, mock_data_from_db): mock_request = mock.Mock( GET={}, COOKIES={}, session={}, ) mock_data_from_db.return_value = [ {'enterprise_customer': {'uuid': 'my-uuid'}}, ] actual_uuid = enterprise_customer_uuid_for_request(mock_request) expected_uuid = 'my-uuid' assert expected_uuid == actual_uuid mock_partial_pipeline.assert_called_once_with(mock_request) mock_data_from_db.assert_called_once_with(mock_request.user) assert {'uuid': 'my-uuid'} == mock_request.session[ENTERPRISE_CUSTOMER_KEY_NAME]
def test_customer_uuid_for_request_sso_provider_id_customer_non_existent_but_exist_in_db( self, mock_partial_pipeline, mock_data_from_db, ): enterprise_customer_uuid = 'adab9a14-f263-42e6-a234-db707026c4a6' mock_request = mock.Mock( GET={'tpa_hint': 'my-third-party-auth'}, COOKIES={}, session={}, ) mock_data_from_db.return_value = [ {'enterprise_customer': {'uuid': enterprise_customer_uuid}}, ] actual_uuid = enterprise_customer_uuid_for_request(mock_request) assert actual_uuid == enterprise_customer_uuid mock_partial_pipeline.assert_called_once_with(mock_request) assert ENTERPRISE_CUSTOMER_KEY_NAME in mock_request.session
def test_enterprise_uuid_for_request_cache_miss_non_existent( self, is_user_authenticated, mock_partial_pipeline, mock_data_from_db): mock_request = mock.Mock( GET={}, COOKIES={}, session={}, ) mock_request.user.is_authenticated = is_user_authenticated actual_uuid = enterprise_customer_uuid_for_request(mock_request) self.assertIsNone(actual_uuid) mock_partial_pipeline.assert_called_once_with(mock_request) if is_user_authenticated: mock_data_from_db.assert_called_once_with(mock_request.user) self.assertIsNone( mock_request.session[ENTERPRISE_CUSTOMER_KEY_NAME]) else: self.assertFalse(mock_data_from_db.called) self.assertNotIn(ENTERPRISE_CUSTOMER_KEY_NAME, mock_request.session)
def test_enterprise_uuid_for_request_cache_miss_non_existent( self, is_user_authenticated, mock_partial_pipeline, mock_data_from_db ): mock_request = mock.Mock( GET={}, COOKIES={}, session={}, ) mock_request.user.is_authenticated = is_user_authenticated actual_uuid = enterprise_customer_uuid_for_request(mock_request) assert actual_uuid is None mock_partial_pipeline.assert_called_once_with(mock_request) if is_user_authenticated: mock_data_from_db.assert_called_once_with(mock_request.user) assert mock_request.session[ENTERPRISE_CUSTOMER_KEY_NAME] is None else: assert not mock_data_from_db.called assert ENTERPRISE_CUSTOMER_KEY_NAME not in mock_request.session