From f8833e1ffcc7c58c5248f35286ad58dfe0b54319 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Wed, 28 Nov 2018 13:59:49 -0500 Subject: [PATCH] Enable the ContentTypeGatePartition when the ContentTypeGatingConfig.studio_override_enabled is True --- .../content_type_gating/partitions.py | 4 +- .../tests/test_partitions.py | 54 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 openedx/features/content_type_gating/tests/test_partitions.py diff --git a/openedx/features/content_type_gating/partitions.py b/openedx/features/content_type_gating/partitions.py index e8ad3d9df2..3d0383a199 100644 --- a/openedx/features/content_type_gating/partitions.py +++ b/openedx/features/content_type_gating/partitions.py @@ -44,7 +44,9 @@ def create_content_gating_partition(course): Create and return the Content Gating user partition. """ - if not ContentTypeGatingConfig.enabled_for_course(course_key=course.id): + enabled_for_course = ContentTypeGatingConfig.enabled_for_course(course_key=course.id) + studio_override_for_course = ContentTypeGatingConfig.current(course_key=course.id).studio_override_enabled + if not (enabled_for_course or studio_override_for_course): return None try: diff --git a/openedx/features/content_type_gating/tests/test_partitions.py b/openedx/features/content_type_gating/tests/test_partitions.py new file mode 100644 index 0000000000..8f2cf2a626 --- /dev/null +++ b/openedx/features/content_type_gating/tests/test_partitions.py @@ -0,0 +1,54 @@ +from datetime import date +from mock import Mock, patch + +from opaque_keys.edx.keys import CourseKey +from openedx.core.djangolib.testing.utils import CacheIsolationTestCase +from openedx.features.content_type_gating.partitions import create_content_gating_partition, CONTENT_GATING_PARTITION_ID +from openedx.features.content_type_gating.models import ContentTypeGatingConfig +from xmodule.partitions.partitions import UserPartitionError + + +class TestContentTypeGatingPartition(CacheIsolationTestCase): + def setUp(self): + self.course_key = CourseKey.from_string('course-v1:test+course+key') + + def test_create_content_gating_partition_happy_path(self): + + mock_course = Mock(id=self.course_key, user_partitions={}) + ContentTypeGatingConfig.objects.create(enabled=True, enabled_as_of=date(2018, 1, 1)) + + with patch('openedx.features.content_type_gating.partitions.ContentTypeGatingPartitionScheme.create_user_partition') as mock_create: + partition = create_content_gating_partition(mock_course) + self.assertEqual(partition, mock_create.return_value) + + def test_create_content_gating_partition_override_only(self): + mock_course = Mock(id=self.course_key, user_partitions={}) + ContentTypeGatingConfig.objects.create(enabled=False, studio_override_enabled=True) + + partition = create_content_gating_partition(mock_course) + self.assertIsNotNone(partition) + + def test_create_content_gating_partition_disabled(self): + mock_course = Mock(id=self.course_key, user_partitions={}) + ContentTypeGatingConfig.objects.create(enabled=False, studio_override_enabled=False) + + partition = create_content_gating_partition(mock_course) + self.assertIsNone(partition) + + def test_create_content_gating_partition_no_scheme_installed(self): + mock_course = Mock(id=self.course_key, user_partitions={}) + ContentTypeGatingConfig.objects.create(enabled=True, enabled_as_of=date(2018, 1, 1)) + + with patch('openedx.features.content_type_gating.partitions.UserPartition.get_scheme', side_effect=UserPartitionError): + partition = create_content_gating_partition(mock_course) + + self.assertIsNone(partition) + + def test_create_content_gating_partition_partition_id_used(self): + mock_course = Mock(id=self.course_key, user_partitions={Mock(name='partition', id=CONTENT_GATING_PARTITION_ID): object()}) + ContentTypeGatingConfig.objects.create(enabled=True, enabled_as_of=date(2018, 1, 1)) + + with patch('openedx.features.content_type_gating.partitions.LOG') as mock_log: + partition = create_content_gating_partition(mock_course) + mock_log.warning.assert_called() + self.assertIsNone(partition)