Commit 44f952da by Usman Khalid

Merge pull request #5293 from edx/usman/tnl388-invalid-xml-in-code-response

If matlab grader sent unparsable response, show error message.
parents 96ac506f ea032c58
...@@ -808,6 +808,7 @@ class MatlabInput(CodeInput): ...@@ -808,6 +808,7 @@ class MatlabInput(CodeInput):
Handle matlab-specific parsing Handle matlab-specific parsing
""" """
_ = self.capa_system.i18n.ugettext _ = self.capa_system.i18n.ugettext
submitted_msg = _("Submitted. As soon as a response is returned, " submitted_msg = _("Submitted. As soon as a response is returned, "
"this message will be replaced by that feedback.") "this message will be replaced by that feedback.")
self.submitted_msg = submitted_msg self.submitted_msg = submitted_msg
...@@ -889,9 +890,23 @@ class MatlabInput(CodeInput): ...@@ -889,9 +890,23 @@ class MatlabInput(CodeInput):
def _extra_context(self): def _extra_context(self):
""" Set up additional context variables""" """ Set up additional context variables"""
_ = self.capa_system.i18n.ugettext
queue_msg = self.queue_msg
if len(self.queue_msg) > 0: # An empty string cannot be parsed as XML but is okay to include in the template.
try:
etree.XML(u'<div>{0}</div>'.format(self.queue_msg))
except etree.XMLSyntaxError:
try:
html5lib.parseFragment(self.queue_msg, treebuilder='lxml', namespaceHTMLElements=False)[0]
except (IndexError, ValueError):
# If neither can parse queue_msg, it contains invalid xml.
queue_msg = u"<span>{0}</span>".format(_("Error running code."))
extra_context = { extra_context = {
'queue_len': str(self.queue_len), 'queue_len': str(self.queue_len),
'queue_msg': self.queue_msg, 'queue_msg': queue_msg,
'button_enabled': self.button_enabled(), 'button_enabled': self.button_enabled(),
'matlab_editor_js': '{static_url}js/vendor/CodeMirror/octave.js'.format( 'matlab_editor_js': '{static_url}js/vendor/CodeMirror/octave.js'.format(
static_url=self.capa_system.STATIC_URL), static_url=self.capa_system.STATIC_URL),
......
...@@ -26,6 +26,7 @@ import xml.sax.saxutils as saxutils ...@@ -26,6 +26,7 @@ import xml.sax.saxutils as saxutils
from . import test_capa_system from . import test_capa_system
from capa import inputtypes from capa import inputtypes
from capa.checker import DemoSystem
from mock import ANY, patch from mock import ANY, patch
from pyparsing import ParseException from pyparsing import ParseException
...@@ -727,6 +728,34 @@ class MatlabTest(unittest.TestCase): ...@@ -727,6 +728,34 @@ class MatlabTest(unittest.TestCase):
received = fromstring(context['queue_msg']) received = fromstring(context['queue_msg'])
html_tree_equal(received, expected) html_tree_equal(received, expected)
def test_rendering_with_invalid_queue_msg(self):
self.the_input.queue_msg = (u"<div class='matlabResponse'><div style='white-space:pre' class='commandWindowOutput'>"
u"\nans =\n\n\u0002\n\n</div><ul></ul></div>")
context = self.the_input._get_render_context() # pylint: disable=protected-access
self.maxDiff = None
expected = {
'STATIC_URL': '/dummy-static/',
'id': 'prob_1_2',
'value': 'print "good evening"',
'status': inputtypes.Status('queued'),
'msg': self.the_input.submitted_msg,
'mode': self.mode,
'rows': self.rows,
'cols': self.cols,
'queue_msg': "<span>Error running code.</span>",
'linenumbers': 'true',
'hidden': '',
'tabsize': int(self.tabsize),
'button_enabled': True,
'queue_len': '3',
'matlab_editor_js': '/dummy-static/js/vendor/CodeMirror/octave.js',
}
self.assertEqual(context, expected)
self.the_input.capa_system.render_template = DemoSystem().render_template
self.the_input.get_html() # Should not raise an exception
def test_matlab_queue_message_allowed_tags(self): def test_matlab_queue_message_allowed_tags(self):
""" """
Test allowed tags. Test allowed tags.
......
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