Revert "ENT-1849: Removed EnterpriseMiddleware"
This commit is contained in:
@@ -1511,6 +1511,9 @@ MIDDLEWARE_CLASSES = [
|
||||
|
||||
'waffle.middleware.WaffleMiddleware',
|
||||
|
||||
# Inserts Enterprise content.
|
||||
'openedx.features.enterprise_support.middleware.EnterpriseMiddleware',
|
||||
|
||||
# Enables force_django_cache_miss functionality for TieredCache.
|
||||
'edx_django_utils.cache.middleware.TieredCacheMiddleware',
|
||||
|
||||
|
||||
@@ -450,9 +450,7 @@ def enterprise_customer_for_request(request):
|
||||
if 'enterprise_customer' in request.session:
|
||||
return enterprise_customer_from_cache(request=request)
|
||||
else:
|
||||
enterprise_customer = enterprise_customer_from_api(request)
|
||||
request.session['enterprise_customer'] = enterprise_customer
|
||||
return enterprise_customer
|
||||
return enterprise_customer_from_api(request)
|
||||
|
||||
|
||||
@enterprise_is_enabled(otherwise=False)
|
||||
|
||||
30
openedx/features/enterprise_support/middleware.py
Normal file
30
openedx/features/enterprise_support/middleware.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""
|
||||
Middleware for the Enterprise feature.
|
||||
|
||||
The Enterprise feature must be turned on for this middleware to have any effect.
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
from django.core.exceptions import MiddlewareNotUsed
|
||||
from openedx.features.enterprise_support import api
|
||||
|
||||
|
||||
class EnterpriseMiddleware(object):
|
||||
"""
|
||||
Middleware that adds Enterprise-related content to the request.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
We don't need to use this middleware if the Enterprise feature isn't enabled.
|
||||
"""
|
||||
if not api.enterprise_enabled():
|
||||
raise MiddlewareNotUsed()
|
||||
|
||||
def process_request(self, request):
|
||||
"""
|
||||
Fill the request with Enterprise-related content.
|
||||
"""
|
||||
if 'enterprise_customer' not in request.session and request.user.is_authenticated:
|
||||
request.session['enterprise_customer'] = api.enterprise_customer_for_request(request)
|
||||
@@ -253,7 +253,6 @@ class TestEnterpriseApi(EnterpriseServiceMockMixin, CacheIsolationTestCase):
|
||||
|
||||
# Verify that the method `enterprise_customer_for_request` returns no
|
||||
# enterprise customer if the enterprise customer API throws 404.
|
||||
del dummy_request.session['enterprise_customer']
|
||||
self.mock_get_enterprise_customer('real-ent-uuid', {'detail': 'Not found.'}, 404)
|
||||
enterprise_customer = enterprise_customer_for_request(dummy_request)
|
||||
self.assertIsNone(enterprise_customer)
|
||||
@@ -289,36 +288,6 @@ class TestEnterpriseApi(EnterpriseServiceMockMixin, CacheIsolationTestCase):
|
||||
)
|
||||
self.assertEqual(enterprise_customer, {'real': 'enterprisecustomer'})
|
||||
|
||||
def test_enterprise_customer_for_request_with_session(self):
|
||||
"""
|
||||
Verify enterprise_customer_for_request stores and retrieves data from session appropriately
|
||||
"""
|
||||
dummy_request = mock.MagicMock(session={}, user=self.user)
|
||||
enterprise_data = {'name': 'dummy-enterprise-customer', 'uuid': '8dc65e66-27c9-447b-87ff-ede6d66e3a5d'}
|
||||
|
||||
# Verify enterprise customer data fetched from API when it is not available in session
|
||||
with mock.patch(
|
||||
'openedx.features.enterprise_support.api.enterprise_customer_from_api',
|
||||
return_value=enterprise_data
|
||||
):
|
||||
self.assertEqual(dummy_request.session.get('enterprise_customer'), None)
|
||||
enterprise_customer = enterprise_customer_for_request(dummy_request)
|
||||
self.assertEqual(enterprise_customer, enterprise_data)
|
||||
self.assertEqual(dummy_request.session.get('enterprise_customer'), enterprise_data)
|
||||
|
||||
# Verify enterprise customer data fetched from session for subsequent calls
|
||||
with mock.patch(
|
||||
'openedx.features.enterprise_support.api.enterprise_customer_from_api',
|
||||
return_value=enterprise_data
|
||||
) as mock_enterprise_customer_from_api, mock.patch(
|
||||
'openedx.features.enterprise_support.api.enterprise_customer_from_cache',
|
||||
return_value=enterprise_data
|
||||
) as mock_enterprise_customer_from_cache:
|
||||
enterprise_customer = enterprise_customer_for_request(dummy_request)
|
||||
self.assertEqual(enterprise_customer, enterprise_data)
|
||||
self.assertEqual(mock_enterprise_customer_from_api.called, False)
|
||||
self.assertEqual(mock_enterprise_customer_from_cache.called, True)
|
||||
|
||||
def check_data_sharing_consent(self, consent_required=False, consent_url=None):
|
||||
"""
|
||||
Used to test the data_sharing_consent_required view decorator.
|
||||
|
||||
51
openedx/features/enterprise_support/tests/test_middleware.py
Normal file
51
openedx/features/enterprise_support/tests/test_middleware.py
Normal file
@@ -0,0 +1,51 @@
|
||||
"""
|
||||
Tests for Enterprise middleware.
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
import mock
|
||||
|
||||
from django.test import TestCase
|
||||
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.tests import (
|
||||
FAKE_ENTERPRISE_CUSTOMER,
|
||||
FEATURES_WITH_ENTERPRISE_ENABLED,
|
||||
factories
|
||||
)
|
||||
from student.tests.factories import UserFactory
|
||||
|
||||
|
||||
@override_settings(FEATURES=FEATURES_WITH_ENTERPRISE_ENABLED)
|
||||
@skip_unless_lms
|
||||
class EnterpriseMiddlewareTest(TestCase):
|
||||
"""
|
||||
Test for `EnterpriseMiddleware`.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
"""Initiate commonly needed objects."""
|
||||
super(EnterpriseMiddlewareTest, self).setUp()
|
||||
|
||||
# Customer & Learner details.
|
||||
self.user = UserFactory.create(username='username', password='password')
|
||||
self.enterprise_customer = FAKE_ENTERPRISE_CUSTOMER
|
||||
self.enterprise_learner = factories.EnterpriseCustomerUserFactory(user_id=self.user.id)
|
||||
|
||||
# Request details.
|
||||
self.client.login(username='username', password='password')
|
||||
self.dashboard = reverse('dashboard')
|
||||
|
||||
# Mocks.
|
||||
patcher = mock.patch('openedx.features.enterprise_support.api.enterprise_customer_from_api')
|
||||
self.mock_enterprise_customer_from_api = patcher.start()
|
||||
self.mock_enterprise_customer_from_api.return_value = self.enterprise_customer
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
def test_anonymous_user(self):
|
||||
"""The `enterprise_customer` is not set in the session if the user is anonymous."""
|
||||
self.client.logout()
|
||||
self.client.get(self.dashboard)
|
||||
assert self.client.session.get('enterprise_customer') is None
|
||||
Reference in New Issue
Block a user