clearing all relevant cache if enterprise customer change DSC enable flag.

This commit is contained in:
Hammad Ahmad Waqas
2019-05-13 20:30:27 +05:00
parent 82ce621e0f
commit cdc8ddf190
5 changed files with 151 additions and 8 deletions

View File

@@ -6,8 +6,9 @@ from __future__ import absolute_import, unicode_literals
from uuid import UUID
import factory
from enterprise.models import EnterpriseCustomer, EnterpriseCustomerUser
from faker import Factory as FakerFactory
from enterprise.models import EnterpriseCourseEnrollment, EnterpriseCustomer, EnterpriseCustomerUser
from openedx.core.djangoapps.site_configuration.tests.factories import SiteFactory
FAKER = FakerFactory.create()
@@ -54,3 +55,22 @@ class EnterpriseCustomerUserFactory(factory.django.DjangoModelFactory):
enterprise_customer = factory.SubFactory(EnterpriseCustomerFactory)
user_id = factory.LazyAttribute(lambda x: FAKER.pyint()) # pylint: disable=no-member
class EnterpriseCourseEnrollmentFactory(factory.django.DjangoModelFactory):
"""
EnterpriseCourseEnrollment factory.
Creates an instance of EnterpriseCourseEnrollment with minimal boilerplate.
"""
class Meta(object):
"""
Meta for EnterpriseCourseEnrollmentFactory.
"""
model = EnterpriseCourseEnrollment
id = factory.LazyAttribute(lambda x: FAKER.random_int(min=1)) # pylint: disable=no-member
course_id = factory.LazyAttribute(lambda x: FAKER.slug()) # pylint: disable=no-member
enterprise_customer_user = factory.SubFactory(EnterpriseCustomerUserFactory)

View File

@@ -3,12 +3,17 @@ from __future__ import absolute_import
import logging
from mock import patch
import ddt
from django.test import TestCase
from openedx.features.enterprise_support.tests.factories import EnterpriseCustomerFactory, EnterpriseCustomerUserFactory
from mock import patch
from student.tests.factories import UserFactory
from edx_django_utils.cache import TieredCache
from openedx.features.enterprise_support.tests.factories import (
EnterpriseCourseEnrollmentFactory,
EnterpriseCustomerFactory,
EnterpriseCustomerUserFactory
)
from openedx.features.enterprise_support.utils import get_data_consent_share_cache_key
log = logging.getLogger(__name__)
@@ -25,8 +30,20 @@ class EnterpriseSupportSignals(TestCase):
def setUp(self):
self.user = UserFactory.create(username='test', email=TEST_EMAIL)
self.course_id = 'course-v1:edX+DemoX+Demo_Course'
super(EnterpriseSupportSignals, self).setUp()
@staticmethod
def _create_dsc_cache(user_id, course_id):
consent_cache_key = get_data_consent_share_cache_key(user_id, course_id)
TieredCache.set_all_tiers(consent_cache_key, 0)
@staticmethod
def _is_dsc_cache_found(user_id, course_id):
consent_cache_key = get_data_consent_share_cache_key(user_id, course_id)
data_sharing_consent_needed_cache = TieredCache.get_cached_response(consent_cache_key)
return data_sharing_consent_needed_cache.is_found
@patch('openedx.features.enterprise_support.signals.update_user.delay')
def test_register_user(self, mock_update_user):
"""
@@ -44,3 +61,46 @@ class EnterpriseSupportSignals(TestCase):
},
email=self.user.email
)
def test_signal_update_dsc_cache_on_course_enrollment(self):
"""
make sure update_dsc_cache_on_course_enrollment signal clears cache when Enterprise Course Enrollment
takes place
"""
self._create_dsc_cache(self.user.id, self.course_id)
self.assertTrue(self._is_dsc_cache_found(self.user.id, self.course_id))
# Enrolling user to Course
enterprise_customer_user = EnterpriseCustomerUserFactory(user_id=self.user.id)
EnterpriseCourseEnrollmentFactory(
course_id=self.course_id,
enterprise_customer_user=enterprise_customer_user,
)
self.assertFalse(self._is_dsc_cache_found(self.user.id, self.course_id))
def test_signal_update_dsc_cache_on_enterprise_customer_update(self):
"""
make sure update_dsc_cache_on_enterprise_customer_update signal clears data_sharing_consent_needed cache after
enable_data_sharing_consent flag is changed.
"""
# Enrolling user to Course
enterprise_customer = EnterpriseCustomerFactory()
enterprise_customer_user = EnterpriseCustomerUserFactory(
user_id=self.user.id,
enterprise_customer=enterprise_customer
)
EnterpriseCourseEnrollmentFactory(
course_id=self.course_id,
enterprise_customer_user=enterprise_customer_user,
)
self._create_dsc_cache(self.user.id, self.course_id)
self.assertTrue(self._is_dsc_cache_found(self.user.id, self.course_id))
# updating enable_data_sharing_consent flag
enterprise_customer.enable_data_sharing_consent = False
enterprise_customer.save()
self.assertFalse(self._is_dsc_cache_found(self.user.id, self.course_id))