From 5268588980f8ef9cf54c843b627f38fddace8519 Mon Sep 17 00:00:00 2001 From: Bill Filler Date: Wed, 4 Oct 2017 09:40:59 -0400 Subject: [PATCH] Display banner if user declines data sharing consent Revert changes for getting enterprise customer from enteprise from Enterprise API and update/add test. Lookup the enterprise_customer_uuid based on the site and user if not found through the cookie or request parameter. --- openedx/features/enterprise_support/api.py | 7 +++++++ .../features/enterprise_support/tests/test_api.py | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/openedx/features/enterprise_support/api.py b/openedx/features/enterprise_support/api.py index ce03291252..9a1e4d6aff 100644 --- a/openedx/features/enterprise_support/api.py +++ b/openedx/features/enterprise_support/api.py @@ -364,6 +364,13 @@ def enterprise_customer_for_request(request): settings.ENTERPRISE_CUSTOMER_COOKIE_NAME ) + if not enterprise_customer_uuid and request.user.is_authenticated(): + # If there's no way to get an Enterprise UUID for the request, check to see + # if there's already an Enterprise attached to the requesting user on the backend. + learner_data = get_enterprise_learner_data(request.site, request.user) + if learner_data: + enterprise_customer_uuid = learner_data[0]['enterprise_customer']['uuid'] + if enterprise_customer_uuid: # If we were able to obtain an EnterpriseCustomer UUID, go ahead # and use it to attempt to retrieve EnterpriseCustomer details diff --git a/openedx/features/enterprise_support/tests/test_api.py b/openedx/features/enterprise_support/tests/test_api.py index a1e6700efb..f8c60969e9 100644 --- a/openedx/features/enterprise_support/tests/test_api.py +++ b/openedx/features/enterprise_support/tests/test_api.py @@ -171,6 +171,7 @@ class TestEnterpriseApi(EnterpriseServiceMockMixin, CacheIsolationTestCase): self.assertFalse(consent_needed_for_course(request, user, 'fake-course')) @httpretty.activate + @mock.patch('openedx.features.enterprise_support.api.get_enterprise_learner_data') @mock.patch('openedx.features.enterprise_support.api.EnterpriseCustomer') @mock.patch('openedx.features.enterprise_support.api.get_partial_pipeline') @mock.patch('openedx.features.enterprise_support.api.Registry') @@ -179,6 +180,7 @@ class TestEnterpriseApi(EnterpriseServiceMockMixin, CacheIsolationTestCase): mock_registry, mock_partial, mock_enterprise_customer_model, + mock_get_enterprise_learner_data, ): def mock_get_enterprise_customer(**kwargs): uuid = kwargs.get('enterprise_customer_identity_provider__provider_id') @@ -227,6 +229,16 @@ class TestEnterpriseApi(EnterpriseServiceMockMixin, CacheIsolationTestCase): ) self.assertEqual(enterprise_customer, {'real': 'enterprisecustomer'}) + # Verify that we can still get enterprise customer from enterprise + # learner API even if we are unable to get it from preferred sources, + # e.g. url query parameters, third-party auth pipeline, enterprise + # cookie. + mock_get_enterprise_learner_data.return_value = [{'enterprise_customer': {'uuid': 'real-ent-uuid'}}] + enterprise_customer = enterprise_customer_for_request( + mock.MagicMock(GET={}, COOKIES={}, user=self.user, site=1) + ) + self.assertEqual(enterprise_customer, {'real': 'enterprisecustomer'}) + def check_data_sharing_consent(self, consent_required=False, consent_url=None): """ Used to test the data_sharing_consent_required view decorator.