[ENT-3315] Reduce calls to enterprise-learner endpoint by looking up data from db
This commit is contained in:
@@ -8,7 +8,6 @@ from django.urls import reverse
|
||||
from django.utils.translation import ugettext as _
|
||||
from lms.djangoapps.ccx.overrides import get_current_ccx
|
||||
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
|
||||
from openedx.features.enterprise_support.api import get_enterprise_learner_data
|
||||
from openedx.features.enterprise_support.utils import get_enterprise_learner_generic_name, get_enterprise_learner_portals
|
||||
|
||||
# App that handles subdomain specific branding
|
||||
|
||||
@@ -35,6 +35,7 @@ try:
|
||||
EnterpriseCustomerUser,
|
||||
PendingEnterpriseCustomerUser
|
||||
)
|
||||
from enterprise.api.v1.serializers import EnterpriseCustomerUserReadOnlySerializer
|
||||
from consent.models import DataSharingConsent, DataSharingConsentTextOverrides
|
||||
except ImportError:
|
||||
pass
|
||||
@@ -438,7 +439,7 @@ def enterprise_customer_uuid_for_request(request):
|
||||
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.user)
|
||||
learner_data = get_enterprise_learner_data_from_db(request.user)
|
||||
if learner_data:
|
||||
enterprise_customer_uuid = learner_data[0]['enterprise_customer']['uuid']
|
||||
|
||||
@@ -470,7 +471,7 @@ def consent_needed_for_course(request, user, course_id, enrollment_exists=False)
|
||||
if data_sharing_consent_needed_cache.is_found and data_sharing_consent_needed_cache.value == 0:
|
||||
return False
|
||||
|
||||
enterprise_learner_details = get_enterprise_learner_data(user)
|
||||
enterprise_learner_details = get_enterprise_learner_data_from_db(user)
|
||||
if not enterprise_learner_details:
|
||||
consent_needed = False
|
||||
else:
|
||||
@@ -560,7 +561,7 @@ def get_enterprise_consent_url(request, course_id, user=None, return_to=None, en
|
||||
|
||||
|
||||
@enterprise_is_enabled()
|
||||
def get_enterprise_learner_data(user):
|
||||
def get_enterprise_learner_data_from_api(user):
|
||||
"""
|
||||
Client API operation adapter/wrapper
|
||||
"""
|
||||
@@ -570,6 +571,17 @@ def get_enterprise_learner_data(user):
|
||||
return enterprise_learner_data['results']
|
||||
|
||||
|
||||
@enterprise_is_enabled()
|
||||
def get_enterprise_learner_data_from_db(user):
|
||||
"""
|
||||
Query the database directly and use the same serializer that the api call would use to return the same results.
|
||||
"""
|
||||
if user.is_authenticated:
|
||||
queryset = EnterpriseCustomerUser.objects.filter(user_id=user.id)
|
||||
serializer = EnterpriseCustomerUserReadOnlySerializer(queryset, many=True)
|
||||
return serializer.data
|
||||
|
||||
|
||||
@enterprise_is_enabled()
|
||||
def get_enterprise_learner_portal_enabled_message(request):
|
||||
"""
|
||||
@@ -585,7 +597,7 @@ def get_enterprise_learner_portal_enabled_message(request):
|
||||
if 'enterprise_customer' in request.session and request.session['enterprise_customer']:
|
||||
enterprise_customer = request.session['enterprise_customer']
|
||||
else:
|
||||
learner_data = get_enterprise_learner_data(request.user)
|
||||
learner_data = get_enterprise_learner_data_from_db(request.user)
|
||||
if learner_data:
|
||||
enterprise_customer = learner_data[0]['enterprise_customer']
|
||||
else:
|
||||
|
||||
@@ -142,6 +142,66 @@ class EnterpriseServiceMockMixin(object):
|
||||
required=False,
|
||||
)
|
||||
|
||||
def get_mock_enterprise_learner_results(
|
||||
self,
|
||||
entitlement_id=1,
|
||||
learner_id=1,
|
||||
enterprise_customer_uuid='cf246b88-d5f6-4908-a522-fc307e0b0c59',
|
||||
enable_audit_enrollment=False,
|
||||
):
|
||||
"""
|
||||
Helper function to format enterprise learner API response.
|
||||
"""
|
||||
mock_results = [
|
||||
{
|
||||
'id': learner_id,
|
||||
'enterprise_customer': {
|
||||
'uuid': enterprise_customer_uuid,
|
||||
'name': 'TestShib',
|
||||
'active': True,
|
||||
'site': {
|
||||
'domain': 'example.com',
|
||||
'name': 'example.com'
|
||||
},
|
||||
'enable_data_sharing_consent': True,
|
||||
'enforce_data_sharing_consent': 'at_login',
|
||||
'enable_audit_enrollment': enable_audit_enrollment,
|
||||
'branding_configuration': {
|
||||
'enterprise_customer': enterprise_customer_uuid,
|
||||
'logo': 'https://open.edx.org/sites/all/themes/edx_open/logo.png'
|
||||
},
|
||||
'enterprise_customer_entitlements': [
|
||||
{
|
||||
'enterprise_customer': enterprise_customer_uuid,
|
||||
'entitlement_id': entitlement_id
|
||||
}
|
||||
],
|
||||
'replace_sensitive_sso_username': True,
|
||||
},
|
||||
'user_id': 5,
|
||||
'user': {
|
||||
'username': 'verified',
|
||||
'first_name': '',
|
||||
'last_name': '',
|
||||
'email': 'verified@example.com',
|
||||
'is_staff': True,
|
||||
'is_active': True,
|
||||
'date_joined': '2016-09-01T19:18:26.026495Z'
|
||||
},
|
||||
'data_sharing_consent': [
|
||||
{
|
||||
"username": "verified",
|
||||
"enterprise_customer_uuid": enterprise_customer_uuid,
|
||||
"exists": True,
|
||||
"course_id": "course-v1:edX DemoX Demo_Course",
|
||||
"consent_provided": True,
|
||||
"consent_required": False
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
return mock_results
|
||||
|
||||
def mock_enterprise_learner_api(
|
||||
self,
|
||||
entitlement_id=1,
|
||||
@@ -152,58 +212,14 @@ class EnterpriseServiceMockMixin(object):
|
||||
"""
|
||||
Helper function to register enterprise learner API endpoint.
|
||||
"""
|
||||
results = self.get_mock_enterprise_learner_results(
|
||||
entitlement_id, learner_id, enterprise_customer_uuid, enable_audit_enrollment
|
||||
)
|
||||
enterprise_learner_api_response = {
|
||||
'count': 1,
|
||||
'num_pages': 1,
|
||||
'current_page': 1,
|
||||
'results': [
|
||||
{
|
||||
'id': learner_id,
|
||||
'enterprise_customer': {
|
||||
'uuid': enterprise_customer_uuid,
|
||||
'name': 'TestShib',
|
||||
'active': True,
|
||||
'site': {
|
||||
'domain': 'example.com',
|
||||
'name': 'example.com'
|
||||
},
|
||||
'enable_data_sharing_consent': True,
|
||||
'enforce_data_sharing_consent': 'at_login',
|
||||
'enable_audit_enrollment': enable_audit_enrollment,
|
||||
'branding_configuration': {
|
||||
'enterprise_customer': enterprise_customer_uuid,
|
||||
'logo': 'https://open.edx.org/sites/all/themes/edx_open/logo.png'
|
||||
},
|
||||
'enterprise_customer_entitlements': [
|
||||
{
|
||||
'enterprise_customer': enterprise_customer_uuid,
|
||||
'entitlement_id': entitlement_id
|
||||
}
|
||||
],
|
||||
'replace_sensitive_sso_username': True,
|
||||
},
|
||||
'user_id': 5,
|
||||
'user': {
|
||||
'username': 'verified',
|
||||
'first_name': '',
|
||||
'last_name': '',
|
||||
'email': 'verified@example.com',
|
||||
'is_staff': True,
|
||||
'is_active': True,
|
||||
'date_joined': '2016-09-01T19:18:26.026495Z'
|
||||
},
|
||||
'data_sharing_consent': [
|
||||
{
|
||||
"username": "verified",
|
||||
"enterprise_customer_uuid": enterprise_customer_uuid,
|
||||
"exists": True,
|
||||
"course_id": "course-v1:edX DemoX Demo_Course",
|
||||
"consent_provided": True,
|
||||
"consent_required": False
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
'results': results,
|
||||
'next': None,
|
||||
'start': 0,
|
||||
'previous': None
|
||||
|
||||
@@ -169,11 +169,13 @@ class TestEnterpriseApi(EnterpriseServiceMockMixin, CacheIsolationTestCase):
|
||||
self._assert_api_client_with_user(ConsentApiClient, mock_jwt_builder)
|
||||
|
||||
@httpretty.activate
|
||||
def test_consent_needed_for_course(self):
|
||||
@mock.patch('openedx.features.enterprise_support.api.get_enterprise_learner_data_from_db')
|
||||
def test_consent_needed_for_course(self, mock_get_enterprise_learner_data):
|
||||
user = UserFactory(username='janedoe')
|
||||
request = mock.MagicMock(session={}, user=user, site=SiteFactory(domain="example.com"))
|
||||
ec_uuid = 'cf246b88-d5f6-4908-a522-fc307e0b0c59'
|
||||
course_id = 'fake-course'
|
||||
mock_get_enterprise_learner_data.return_value = self.get_mock_enterprise_learner_results()
|
||||
self.mock_enterprise_learner_api()
|
||||
|
||||
# test not required consent for example non enterprise customer
|
||||
@@ -219,7 +221,7 @@ class TestEnterpriseApi(EnterpriseServiceMockMixin, CacheIsolationTestCase):
|
||||
self.assertFalse(course_id in consent_required)
|
||||
|
||||
@httpretty.activate
|
||||
@mock.patch('openedx.features.enterprise_support.api.get_enterprise_learner_data')
|
||||
@mock.patch('openedx.features.enterprise_support.api.get_enterprise_learner_data_from_db')
|
||||
@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')
|
||||
|
||||
Reference in New Issue
Block a user