Commit 644dc432 by Braden MacDonald

Don't cause a 500 error in the LMS if get_block unexpectedly returns None

parent 86d0611b
...@@ -236,7 +236,9 @@ class MentoringBlock(XBlock, StepParentMixin, StudioEditableXBlockMixin, StudioC ...@@ -236,7 +236,9 @@ class MentoringBlock(XBlock, StepParentMixin, StudioEditableXBlockMixin, StudioC
for child_id in self.children: for child_id in self.children:
child = self.runtime.get_block(child_id) child = self.runtime.get_block(child_id)
if not isinstance(child, MentoringMessageBlock): if child is None: # child should not be None but it can happen due to bugs or permission issues
child_content += u"<p>[{}]</p>".format(self._(u"Error: Unable to load child component."))
elif not isinstance(child, MentoringMessageBlock):
try: try:
if self.is_assessment and isinstance(child, StepMixin): if self.is_assessment and isinstance(child, StepMixin):
child_fragment = child.render('assessment_step_view', context) child_fragment = child.render('assessment_step_view', context)
......
...@@ -31,6 +31,20 @@ class TestMentoringBlock(unittest.TestCase): ...@@ -31,6 +31,20 @@ class TestMentoringBlock(unittest.TestCase):
self.assertFalse(patched_runtime.publish.called) self.assertFalse(patched_runtime.publish.called)
def test_does_not_crash_when_get_child_is_broken(self):
block = MentoringBlock(MagicMock(), DictFieldData({
'children': ['invalid_id'],
}), Mock())
with patch.object(block, 'runtime') as patched_runtime:
patched_runtime.publish = Mock()
patched_runtime.service().ugettext = lambda str: str
patched_runtime.get_block = lambda block_id: None
fragment = block.student_view(context={})
self.assertIn('Unable to load child component', fragment.content)
@ddt.ddt @ddt.ddt
class TestMentoringBlockTheming(unittest.TestCase): class TestMentoringBlockTheming(unittest.TestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment