def assert_block_is_gated(self, block, is_gated, mock_get_current_request): ''' This functions asserts whether the passed in block is gated by content type gating. This is determined by checking whether the has_access method called the IncorrectPartitionGroupError. This error gets swallowed up and is raised as a 404, which is why we are checking for a 404 being raised. However, the 404 could also be caused by other errors, which is why the actual assertion is checking whether the IncorrectPartitionGroupError was called. ''' fake_request = Mock() fake_request = self.factory.get('') mock_get_current_request.return_value = fake_request with patch.object(IncorrectPartitionGroupError, '__init__', wraps=IncorrectPartitionGroupError.__init__) as mock_access_error: if is_gated: with self.assertRaises(Http404): block = load_single_xblock(fake_request, self.audit_user.id, unicode(self.course.id), unicode(block.scope_ids.usage_id), course=None) # check that has_access raised the IncorrectPartitionGroupError in order to gate the block self.assertTrue(mock_access_error.called) else: block = load_single_xblock(fake_request, self.audit_user.id, unicode(self.course.id), unicode(block.scope_ids.usage_id), course=None) # check that has_access did not raise the IncorrectPartitionGroupError thereby not gating the block self.assertFalse(mock_access_error.called)
def _assert_block_is_gated(self, mock_get_current_request, block, is_gated, user_id, course): """ Asserts that a block in a specific course is gated for a specific user Arguments: block: some sort of xblock descriptor, must implement .scope_ids.usage_id is_gated (bool): if True, this user is expected to be gated from this block user_id (int): id of user course_id (CourseLocator): id of course view_name (str): type of view for the block, if not set will default to 'student_view' """ fake_request = self.factory.get('') mock_get_current_request.return_value = fake_request # Load a block we know will pass access control checks vertical_xblock = load_single_xblock( request=fake_request, user_id=user_id, course_id=unicode(course.id), usage_key_string=unicode(self.blocks_dict['vertical'].scope_ids.usage_id), course=course ) runtime = vertical_xblock.runtime # This method of fetching the block from the descriptor bypassess access checks problem_block = runtime.get_module(block) # Attempt to render the block, this should return different fragments if the content is gated or not. frag = runtime.render(problem_block, 'student_view') if is_gated: assert 'content-paywall' in frag.content else: assert 'content-paywall' not in frag.content
def _get_fragment_from_block(block, user_id, course, request_factory, mock_get_current_request): """ Returns the rendered fragment of a block Arguments: block: some sort of xblock descriptor, must implement .scope_ids.usage_id user_id (int): id of user course_id (CourseLocator): id of course """ fake_request = request_factory.get('') mock_get_current_request.return_value = fake_request # Load a block we know will pass access control checks vertical_xblock = load_single_xblock(request=fake_request, user_id=user_id, course_id=unicode(course.id), usage_key_string=unicode( course.scope_ids.usage_id), course=course) runtime = vertical_xblock.runtime # This method of fetching the block from the descriptor bypassess access checks problem_block = runtime.get_module(block) # Attempt to render the block, this should return different fragments if the content is gated or not. frag = runtime.render(problem_block, 'student_view') return frag
def _get_fragment_from_block(block, user_id, course, request_factory, mock_get_current_request): """ Returns the rendered fragment of a block Arguments: block: some sort of xblock descriptor, must implement .scope_ids.usage_id user_id (int): id of user course_id (CourseLocator): id of course """ fake_request = request_factory.get('') mock_get_current_request.return_value = fake_request # Load a block we know will pass access control checks vertical_xblock = load_single_xblock( request=fake_request, user_id=user_id, course_id=unicode(course.id), usage_key_string=unicode(course.scope_ids.usage_id), course=course ) runtime = vertical_xblock.runtime # This method of fetching the block from the descriptor bypassess access checks problem_block = runtime.get_module(block) # Attempt to render the block, this should return different fragments if the content is gated or not. frag = runtime.render(problem_block, 'student_view') return frag
def get_problem(self): """ Get the problem from the test course """ course = self.course store = modulestore() problem = [ item for item in store.get_items(course.course_id) if item.__class__.__name__ == 'ProblemBlockWithMixins' ][0] problem.bind_for_student(self.runtime, self.instructor) # Workaround handle_ajax binding strangeness request = HttpRequest() request.META['SERVER_NAME'] = 'mit.edu' request.META['SERVER_PORT'] = 1234 return load_single_xblock( request=request, course_id=str(self.course_id), user_id=self.instructor.id, usage_key_string=str(problem.location) )
def _get_content_from_fragment(block, user_id, course, request_factory, mock_get_current_request): """ Returns the content from the rendered fragment of a block Arguments: block: some sort of xblock descriptor, must implement .scope_ids.usage_id user_id (int): id of user course_id (CourseLocator): id of course """ fake_request = request_factory.get('') mock_get_current_request.return_value = fake_request # Load a block we know will pass access control checks block = load_single_xblock( request=fake_request, user_id=user_id, course_id=six.text_type(course.id), usage_key_string=six.text_type(block.scope_ids.usage_id), course=course, will_recheck_access=True, ) # Attempt to render the block, this should return different fragments if the content is gated or not. frag = block.render('student_view') return frag.content
def _get_content_from_fragment(block, user_id, course, request_factory, mock_get_current_request): """ Returns the content from the rendered fragment of a block Arguments: block: some sort of xblock descriptor, must implement .scope_ids.usage_id user_id (int): id of user course_id (CourseLocator): id of course """ fake_request = request_factory.get('') mock_get_current_request.return_value = fake_request # Load a block we know will pass access control checks block = load_single_xblock( request=fake_request, user_id=user_id, course_id=six.text_type(course.id), usage_key_string=six.text_type(block.scope_ids.usage_id), course=course, will_recheck_access=True, ) # Attempt to render the block, this should return different fragments if the content is gated or not. frag = block.render('student_view') return frag.content