VAN-311: Add multiple enterprise support for Authn MFE (#26526)
This commit is contained in:
@@ -28,6 +28,7 @@ from openedx.features.enterprise_support.utils import get_data_consent_share_cac
|
||||
from common.djangoapps.third_party_auth.pipeline import get as get_partial_pipeline
|
||||
from common.djangoapps.third_party_auth.provider import Registry
|
||||
|
||||
|
||||
try:
|
||||
from enterprise.models import (
|
||||
EnterpriseCustomer,
|
||||
@@ -35,7 +36,9 @@ try:
|
||||
EnterpriseCustomerUser,
|
||||
PendingEnterpriseCustomerUser
|
||||
)
|
||||
from enterprise.api.v1.serializers import EnterpriseCustomerUserReadOnlySerializer
|
||||
from enterprise.api.v1.serializers import (
|
||||
EnterpriseCustomerUserReadOnlySerializer, EnterpriseCustomerUserWriteSerializer
|
||||
)
|
||||
from consent.models import DataSharingConsent, DataSharingConsentTextOverrides
|
||||
except ImportError: # pragma: no cover
|
||||
pass
|
||||
@@ -301,6 +304,32 @@ class EnterpriseApiServiceClient(EnterpriseServiceClientMixin, EnterpriseApiClie
|
||||
return enterprise_customer
|
||||
|
||||
|
||||
def activate_learner_enterprise(request, user, enterprise_customer):
|
||||
"""
|
||||
Allow an enterprise learner to activate one of learner's linked enterprises.
|
||||
"""
|
||||
serializer = EnterpriseCustomerUserWriteSerializer(data={
|
||||
'enterprise_customer': enterprise_customer,
|
||||
'username': user.username,
|
||||
'active': True
|
||||
})
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
enterprise_customer_user = EnterpriseCustomerUser.objects.get(
|
||||
user_id=user.id,
|
||||
enterprise_customer=enterprise_customer
|
||||
)
|
||||
enterprise_customer_user.update_session(request)
|
||||
LOGGER.info(
|
||||
'[Enterprise Selection Page] Learner activated an enterprise. User: %s, EnterpriseCustomer: %s',
|
||||
user.username,
|
||||
enterprise_customer,
|
||||
)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def data_sharing_consent_required(view_func):
|
||||
"""
|
||||
Decorator which makes a view method redirect to the Data Sharing Consent form if:
|
||||
|
||||
@@ -20,6 +20,7 @@ from slumber.exceptions import HttpClientError
|
||||
from openedx.core.djangoapps.site_configuration.tests.factories import SiteFactory
|
||||
from openedx.core.djangolib.testing.utils import CacheIsolationTestCase, skip_unless_lms
|
||||
from openedx.features.enterprise_support.api import (
|
||||
activate_learner_enterprise,
|
||||
_CACHE_MISS,
|
||||
ENTERPRISE_CUSTOMER_KEY_NAME,
|
||||
EnterpriseApiException,
|
||||
@@ -382,6 +383,17 @@ class TestEnterpriseApi(EnterpriseServiceMockMixin, CacheIsolationTestCase):
|
||||
mock_api_client_class.assert_called_once_with(user=user)
|
||||
mock_client.fetch_enterprise_learner_data.assert_called_once_with(user)
|
||||
|
||||
def test_activate_learner_enterprise(self):
|
||||
"""
|
||||
Test enterprise is activated successfully for user
|
||||
"""
|
||||
request_mock = mock.MagicMock(session={}, user=self.user)
|
||||
enterprise_customer_user = EnterpriseCustomerUserFactory(user_id=self.user.id)
|
||||
enterprise_customer_uuid = enterprise_customer_user.enterprise_customer.uuid
|
||||
|
||||
activate_learner_enterprise(request_mock, self.user, enterprise_customer_uuid)
|
||||
assert request_mock.session['enterprise_customer']['uuid'] == str(enterprise_customer_uuid)
|
||||
|
||||
def test_get_enterprise_learner_data_from_db_no_data(self):
|
||||
assert [] == get_enterprise_learner_data_from_db(self.user)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user