def __init__(self, **kwargs): id_manager = CourseLocationManager(kwargs['course_id']) kwargs.setdefault('id_reader', id_manager) kwargs.setdefault('id_generator', id_manager) kwargs.setdefault('services', {}).setdefault('field-data', DictFieldData({})) super().__init__(**kwargs)
def __init__(self, **kwargs): # pylint: disable=unused-argument id_manager = CourseLocationManager(kwargs['course_id']) kwargs.setdefault('id_reader', id_manager) kwargs.setdefault('id_generator', id_manager) kwargs.setdefault('services', {}).setdefault('field-data', DictFieldData({})) super(TestModuleSystem, self).__init__(**kwargs)
def test_from_xml_render(self): descriptor = NonStaffErrorBlock.from_xml( self.valid_xml, self.system, CourseLocationManager(self.course_id)) descriptor.xmodule_runtime = self.system context_repr = self.system.render(descriptor, STUDENT_VIEW).content assert self.error_msg not in context_repr assert repr(self.valid_xml) not in context_repr
def test_from_xml_render(self): descriptor = NonStaffErrorDescriptor.from_xml( self.valid_xml, self.system, CourseLocationManager(self.course_id)) descriptor.xmodule_runtime = self.system context_repr = self.system.render(descriptor, STUDENT_VIEW).content self.assertNotIn(self.error_msg, context_repr) self.assertNotIn(repr(self.valid_xml), context_repr)
def test_non_staff_error_block_create(self): descriptor = NonStaffErrorBlock.from_xml( self.valid_xml, self.system, CourseLocationManager(self.course_id) ) self.assertIsInstance(descriptor, NonStaffErrorBlock)
def test_error_module_xml_rendering(self): descriptor = ErrorDescriptor.from_xml( self.valid_xml, self.system, CourseLocationManager(self.course_id), self.error_msg) self.assertIsInstance(descriptor, ErrorDescriptor) descriptor.xmodule_runtime = self.system context_repr = self.system.render(descriptor, STUDENT_VIEW).content self.assertIn(self.error_msg, context_repr) self.assertIn(repr(self.valid_xml), context_repr)
def test_error_block_xml_rendering(self): descriptor = ErrorBlock.from_xml(self.valid_xml, self.system, CourseLocationManager(self.course_id), self.error_msg) assert isinstance(descriptor, ErrorBlock) descriptor.xmodule_runtime = self.system context_repr = self.system.render(descriptor, STUDENT_VIEW).content assert self.error_msg in context_repr assert repr(self.valid_xml) in context_repr
def process_xml(self, xml): # pylint: disable=method-hidden """Parse `xml` as an XBlock, and add it to `self._descriptors`""" descriptor = self.xblock_from_node( etree.fromstring(xml), None, CourseLocationManager(self.course_id), ) self._descriptors[descriptor.location.to_deprecated_string()] = descriptor return descriptor
def process_xml(self, xml): # pylint: disable=method-hidden """Parse `xml` as an XBlock, and add it to `self._descriptors`""" self.get_asides = Mock(return_value=[]) descriptor = self.xblock_from_node( etree.fromstring(xml), None, CourseLocationManager(self.course_id), ) self._descriptors[text_type(descriptor.location)] = descriptor return descriptor
def __init__(self, **kwargs): course_id = kwargs['course_id'] id_manager = CourseLocationManager(course_id) kwargs.setdefault('id_reader', id_manager) kwargs.setdefault('id_generator', id_manager) services = kwargs.get('services', {}) services.setdefault('cache', CacheService(DoNothingCache())) services.setdefault('field-data', DictFieldData({})) services.setdefault('sandbox', SandboxService(contentstore, course_id)) kwargs['services'] = services super().__init__(**kwargs)
def test_course_error(self): """ Ensure the view still returns results even if get_courses() returns an ErrorDescriptor. The ErrorDescriptor should be filtered out. """ error_descriptor = ErrorDescriptor.from_xml( '<course></course>', get_test_system(), CourseLocationManager( CourseLocator(org='org', course='course', run='run')), None) descriptors = [error_descriptor, self.empty_course, self.course] with patch('xmodule.modulestore.mixed.MixedModuleStore.get_courses', Mock(return_value=descriptors)): self.test_get()
def test_has_staff_access_to_preview_mode(self): """ Tests users have right access to content in preview mode. """ course_key = self.course.id usage_key = self.course.scope_ids.usage_id chapter = ItemFactory.create(category="chapter", parent_location=self.course.location) overview = CourseOverview.get_from_id(course_key) test_system = get_test_system() ccx = CcxFactory(course_id=course_key) ccx_locator = CCXLocator.from_course_locator(course_key, ccx.id) error_descriptor = ErrorDescriptor.from_xml( u"<problem>ABC \N{SNOWMAN}</problem>", test_system, CourseLocationManager(course_key), "error msg") # Enroll student to the course CourseEnrollmentFactory(user=self.student, course_id=self.course.id) modules = [ self.course, overview, chapter, ccx_locator, error_descriptor, course_key, usage_key, ] # Course key is not None self.assertTrue( bool( access.has_staff_access_to_preview_mode( self.global_staff, obj=self.course, course_key=course_key))) for user in [ self.global_staff, self.course_staff, self.course_instructor ]: for obj in modules: self.assertTrue( bool(access.has_staff_access_to_preview_mode(user, obj=obj))) self.assertFalse( bool( access.has_staff_access_to_preview_mode(self.student, obj=obj)))
def create(system, source_is_error_module=False): """ return a dict of modules: the conditional with a single source and a single child. Keys are 'cond_module', 'source_module', and 'child_module'. if the source_is_error_module flag is set, create a real ErrorModule for the source. """ descriptor_system = get_test_descriptor_system() # construct source descriptor and module: source_location = Location("edX", "conditional_test", "test_run", "problem", "SampleProblem", None) if source_is_error_module: # Make an error descriptor and module source_descriptor = NonStaffErrorDescriptor.from_xml( 'some random xml data', system, id_generator=CourseLocationManager(source_location.course_key), error_msg='random error message') else: source_descriptor = Mock(name='source_descriptor') source_descriptor.location = source_location source_descriptor.runtime = descriptor_system source_descriptor.render = lambda view, context=None: descriptor_system.render( source_descriptor, view, context) # construct other descriptors: child_descriptor = Mock(name='child_descriptor') child_descriptor._xmodule.student_view.return_value.content = u'<p>This is a secret</p>' child_descriptor.student_view = child_descriptor._xmodule.student_view child_descriptor.displayable_items.return_value = [child_descriptor] child_descriptor.runtime = descriptor_system child_descriptor.xmodule_runtime = get_test_system() child_descriptor.render = lambda view, context=None: descriptor_system.render( child_descriptor, view, context) child_descriptor.location = source_location.replace(category='html', name='child') descriptor_system.load_item = { child_descriptor.location: child_descriptor, source_location: source_descriptor }.get system.descriptor_runtime = descriptor_system # construct conditional module: cond_location = Location("edX", "conditional_test", "test_run", "conditional", "SampleConditional", None) field_data = DictFieldData({ 'data': '<conditional/>', 'xml_attributes': { 'attempted': 'true' }, 'children': [child_descriptor.location], }) cond_descriptor = ConditionalDescriptor( descriptor_system, field_data, ScopeIds(None, None, cond_location, cond_location)) cond_descriptor.xmodule_runtime = system system.get_module = lambda desc: desc cond_descriptor.get_required_module_descriptors = Mock( return_value=[source_descriptor]) # return dict: return { 'cond_module': cond_descriptor, 'source_module': source_descriptor, 'child_module': child_descriptor }
def create(system, source_is_error_module=False, source_visible_to_staff_only=False): """ return a dict of modules: the conditional with a single source and a single child. Keys are 'cond_module', 'source_module', and 'child_module'. if the source_is_error_module flag is set, create a real ErrorModule for the source. """ descriptor_system = get_test_descriptor_system() # construct source descriptor and module: source_location = BlockUsageLocator(CourseLocator("edX", "conditional_test", "test_run", deprecated=True), "problem", "SampleProblem", deprecated=True) if source_is_error_module: # Make an error descriptor and module source_descriptor = NonStaffErrorDescriptor.from_xml( 'some random xml data', system, id_generator=CourseLocationManager(source_location.course_key), error_msg='random error message') else: source_descriptor = Mock(name='source_descriptor') source_descriptor.location = source_location source_descriptor.visible_to_staff_only = source_visible_to_staff_only source_descriptor.runtime = descriptor_system source_descriptor.render = lambda view, context=None: descriptor_system.render( source_descriptor, view, context) # construct other descriptors: child_descriptor = Mock(name='child_descriptor') child_descriptor.visible_to_staff_only = False child_descriptor._xmodule.student_view.return_value = Fragment( content=u'<p>This is a secret</p>') child_descriptor.student_view = child_descriptor._xmodule.student_view child_descriptor.displayable_items.return_value = [child_descriptor] child_descriptor.runtime = descriptor_system child_descriptor.xmodule_runtime = get_test_system() child_descriptor.render = lambda view, context=None: descriptor_system.render( child_descriptor, view, context) child_descriptor.location = source_location.replace(category='html', name='child') def visible_to_nonstaff_users(desc): """ Returns if the object is visible to nonstaff users. """ return not desc.visible_to_staff_only def load_item(usage_id, for_parent=None): # pylint: disable=unused-argument """Test-only implementation of load_item that simply returns static xblocks.""" return { child_descriptor.location: child_descriptor, source_location: source_descriptor }.get(usage_id) descriptor_system.load_item = load_item system.descriptor_runtime = descriptor_system # construct conditional module: cond_location = BlockUsageLocator(CourseLocator("edX", "conditional_test", "test_run", deprecated=True), "conditional", "SampleConditional", deprecated=True) field_data = DictFieldData({ 'data': '<conditional/>', 'conditional_attr': 'attempted', 'conditional_value': 'true', 'xml_attributes': { 'attempted': 'true' }, 'children': [child_descriptor.location], }) cond_descriptor = ConditionalDescriptor( descriptor_system, field_data, ScopeIds(None, None, cond_location, cond_location)) cond_descriptor.xmodule_runtime = system system.get_module = lambda desc: desc if visible_to_nonstaff_users( desc) else None cond_descriptor.get_required_module_descriptors = Mock( return_value=[source_descriptor]) cond_descriptor.required_modules = [ system.get_module(descriptor) for descriptor in cond_descriptor.get_required_module_descriptors() ] # return dict: return { 'cond_module': cond_descriptor, 'source_module': source_descriptor, 'child_module': child_descriptor }