ENT-2893: Updates header branding and href for Enterprise learners

This commit is contained in:
Jeff LaJoie
2020-06-30 09:53:57 -04:00
parent 0741c383ad
commit bf11b68fda
6 changed files with 127 additions and 9 deletions

View File

@@ -8,7 +8,9 @@ from uuid import UUID
import factory
from faker import Factory as FakerFactory
from enterprise.models import EnterpriseCourseEnrollment, EnterpriseCustomer, EnterpriseCustomerUser
from enterprise.models import (
EnterpriseCourseEnrollment, EnterpriseCustomer, EnterpriseCustomerBrandingConfiguration, EnterpriseCustomerUser,
)
from openedx.core.djangoapps.site_configuration.tests.factories import SiteFactory
FAKER = FakerFactory.create()
@@ -73,3 +75,22 @@ class EnterpriseCourseEnrollmentFactory(factory.django.DjangoModelFactory):
course_id = factory.LazyAttribute(lambda x: FAKER.slug()) # pylint: disable=no-member
enterprise_customer_user = factory.SubFactory(EnterpriseCustomerUserFactory)
class EnterpriseCustomerBrandingConfigurationFactory(factory.django.DjangoModelFactory):
"""
EnterpriseCustomerBrandingConfiguration factory
Creates an instance of EnterpriseCustomerBrandingConfiguration with minimal boilerplate.
"""
class Meta(object):
"""
Meta for EnterpriseCustomerBrandingConfigurationFactory.
"""
model = EnterpriseCustomerBrandingConfiguration
logo = FAKER.image_url()
banner_background_color = FAKER.color()
banner_border_color = FAKER.color()

View File

@@ -2,7 +2,7 @@
Test the enterprise support utils.
"""
import json
import mock
import ddt
@@ -11,7 +11,11 @@ from django.test.utils import override_settings
from django.urls import reverse
from openedx.core.djangolib.testing.utils import skip_unless_lms
from openedx.features.enterprise_support.utils import get_enterprise_learner_portals
from openedx.features.enterprise_support.tests import FEATURES_WITH_ENTERPRISE_ENABLED
from openedx.features.enterprise_support.tests.factories import (
EnterpriseCustomerBrandingConfigurationFactory, EnterpriseCustomerUserFactory,
)
from student.tests.factories import UserFactory
@@ -43,3 +47,36 @@ class TestEnterpriseUtils(TestCase):
) as mock_customer_request:
self.client.get(resource)
self.assertEqual(mock_customer_request.call_count, expected_calls)
def test_get_enterprise_learner_portals_uncached(self):
"""
Test that only enabled enterprise portals are returned
"""
enterprise_customer_user = EnterpriseCustomerUserFactory(active=True, user_id=self.user.id)
EnterpriseCustomerBrandingConfigurationFactory(
enterprise_customer=enterprise_customer_user.enterprise_customer,
)
enterprise_customer_user.enterprise_customer.enable_learner_portal = True
enterprise_customer_user.enterprise_customer.save()
request = mock.MagicMock(session={}, user=self.user)
portals = get_enterprise_learner_portals(request)
self.assertEqual(len(portals), 1)
self.assertDictEqual(portals[0], {
'name': enterprise_customer_user.enterprise_customer.name,
'slug': enterprise_customer_user.enterprise_customer.slug,
'logo': enterprise_customer_user.enterprise_customer.branding_configuration.logo.url,
})
def test_get_enterprise_learner_portals_cached(self):
enterprise_customer_data = {
'name': 'Enabled Customer',
'slug': 'enabled_customer',
'logo': 'https://logo.url',
}
request = mock.MagicMock(session={
'enterprise_learner_portals': json.dumps([enterprise_customer_data])
}, user=self.user)
portals = get_enterprise_learner_portals(request)
self.assertEqual(len(portals), 1)
self.assertDictEqual(portals[0], enterprise_customer_data)

View File

@@ -283,6 +283,41 @@ def _get_sync_learner_profile_data(enterprise_customer):
return False
def get_enterprise_learner_portals(request):
"""
Gets the formatted portal names and slugs that can be used
to generate links for enabled enterprise Learner Portals.
Caches and returns results in/from the user's request session if provided.
"""
# Prevent a circular import.
from openedx.features.enterprise_support.api import enterprise_enabled
if enterprise_enabled():
# If the key exists return that value
if 'enterprise_learner_portals' in request.session:
return json.loads(request.session['enterprise_learner_portals'])
user = request.user
# Ordering is important, this is consistent with how we decide on which
# enterprise_customer is the selected one for an enterprise_customer
enterprise_learner_portals = [{
'name': enterprise_customer_user.enterprise_customer.name,
'slug': enterprise_customer_user.enterprise_customer.slug,
'logo': enterprise_customer_user.enterprise_customer.branding_configuration.logo.url,
} for enterprise_customer_user in EnterpriseCustomerUser.objects.filter(
user_id=user.id, enterprise_customer__enable_learner_portal=True
).prefetch_related(
'enterprise_customer', 'enterprise_customer__branding_configuration'
).order_by('-enterprise_customer__active', '-modified')]
# Cache the result in the user's request session
request.session['enterprise_learner_portals'] = json.dumps(enterprise_learner_portals)
return enterprise_learner_portals
return None
def get_enterprise_learner_generic_name(request):
"""
Get a generic name concatenating the Enterprise Customer name and 'Learner'.