def test_content_library(self): """ Test when course has content library section. First test user can't see any content library section, and after that mock response from MySQL db. Check user can see mocked sections in content library. """ raw_block_structure = get_course_blocks( self.user, self.course.location, transformers={} ) self.assertEqual(len(list(raw_block_structure.get_block_keys())), len(self.blocks)) clear_course_from_cache(self.course.id) trans_block_structure = get_course_blocks( self.user, self.course.location, transformers={self.transformer} ) # Should dynamically assign a block to student trans_keys = set(trans_block_structure.get_block_keys()) block_key_set = self.get_block_key_set( self.blocks, 'course', 'chapter1', 'lesson1', 'vertical1', 'library_content1' ) for key in block_key_set: self.assertIn(key, trans_keys) vertical2_selected = self.get_block_key_set(self.blocks, 'vertical2').pop() in trans_keys vertical3_selected = self.get_block_key_set(self.blocks, 'vertical3').pop() in trans_keys self.assertTrue(vertical2_selected or vertical3_selected) # Check course structure again, with mocked selected modules for a user. with mock.patch( 'course_blocks.transformers.library_content.ContentLibraryTransformer._get_student_module', return_value=self.selected_module ): clear_course_from_cache(self.course.id) trans_block_structure = get_course_blocks( self.user, self.course.location, transformers={self.transformer} ) self.assertEqual( set(trans_block_structure.get_block_keys()), self.get_block_key_set( self.blocks, 'course', 'chapter1', 'lesson1', 'vertical1', 'library_content1', 'vertical2', 'html1' ) )
def setUp(self): """ Setup course structure and create user for content library transformer test. """ super(ContentLibraryTransformerTestCase, self).setUp() # Build course. self.course_hierarchy = self.get_course_hierarchy() self.blocks = self.build_course(self.course_hierarchy) self.course = self.blocks['course'] clear_course_from_cache(self.course.id) # Enroll user in course. CourseEnrollmentFactory.create(user=self.user, course_id=self.course.id, is_active=True) self.selected_module = MockedModule('{"selected": [["vertical", "vertical_vertical2"]]}') self.transformer = ContentLibraryTransformer()