From e820888154592b99813f770646e76df25c35df36 Mon Sep 17 00:00:00 2001 From: DawoudSheraz Date: Fri, 25 Jan 2019 10:57:19 +0500 Subject: [PATCH] create access denied fragment when HTTP request is null --- .../content_type_gating/partitions.py | 2 +- .../tests/test_partitions.py | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/openedx/features/content_type_gating/partitions.py b/openedx/features/content_type_gating/partitions.py index 717318994d..7e6b982329 100644 --- a/openedx/features/content_type_gating/partitions.py +++ b/openedx/features/content_type_gating/partitions.py @@ -88,7 +88,7 @@ class ContentTypeGatingPartition(UserPartition): request = crum.get_current_request() frag = Fragment(render_to_string('content_type_gating/access_denied_message.html', { - 'mobile_app': is_request_from_mobile_app(request), + 'mobile_app': request and is_request_from_mobile_app(request), 'ecommerce_checkout_link': ecommerce_checkout_link, 'min_price': str(verified_mode.min_price) })) diff --git a/openedx/features/content_type_gating/tests/test_partitions.py b/openedx/features/content_type_gating/tests/test_partitions.py index 9ab84073ad..7714505527 100644 --- a/openedx/features/content_type_gating/tests/test_partitions.py +++ b/openedx/features/content_type_gating/tests/test_partitions.py @@ -96,3 +96,30 @@ class TestContentTypeGatingPartition(CacheIsolationTestCase): fragment = partition.access_denied_fragment(mock_block, global_staff, 'test_group', 'test_allowed_group') self.assertIsNotNone(fragment) + + def test_acess_denied_fragment_for_null_request(self): + """ + Verifies the access denied fragment is visible when HTTP request is not available. + + Given the HTTP request instance is None + Then set the mobile_app context variable to False + And the fragment should be created successfully + """ + mock_request = None + mock_course = Mock(id=self.course_key, user_partitions={}) + mock_block = Mock(scope_ids=Mock(usage_id=Mock(course_key=mock_course.id))) + CourseModeFactory.create(course_id=mock_course.id, mode_slug='verified') + global_staff = GlobalStaffFactory.create() + ContentTypeGatingConfig.objects.create(enabled=True, enabled_as_of=datetime(2018, 1, 1)) + partition = create_content_gating_partition(mock_course) + + with patch( + 'crum.get_current_request', + return_value=mock_request + ), patch( + 'openedx.features.content_type_gating.partitions.ContentTypeGatingPartition._is_audit_enrollment', + return_value=True + ): + fragment = partition.access_denied_fragment(mock_block, global_staff, 'test_group', 'test_allowed_group') + + self.assertIsNotNone(fragment)