From 87d135a40cfdc653bb58f24eb0a427a3c9294923 Mon Sep 17 00:00:00 2001 From: Usman Khalid <2200617@gmail.com> Date: Fri, 19 Sep 2014 15:57:00 +0500 Subject: [PATCH 1/2] Added tests for unparsable matlab grader response. TNL-388 --- common/lib/capa/capa/tests/test_inputtypes.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/common/lib/capa/capa/tests/test_inputtypes.py b/common/lib/capa/capa/tests/test_inputtypes.py index 1982a5f926..8d02edd4e8 100644 --- a/common/lib/capa/capa/tests/test_inputtypes.py +++ b/common/lib/capa/capa/tests/test_inputtypes.py @@ -26,6 +26,7 @@ import xml.sax.saxutils as saxutils from . import test_capa_system from capa import inputtypes +from capa.checker import DemoSystem from mock import ANY, patch from pyparsing import ParseException @@ -727,6 +728,34 @@ class MatlabTest(unittest.TestCase): received = fromstring(context['queue_msg']) html_tree_equal(received, expected) + def test_rendering_with_invalid_queue_msg(self): + self.the_input.queue_msg = (u"
" + u"\nans =\n\n\u0002\n\n
") + 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': "Error running code.", + '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): """ Test allowed tags. From ea032c58ca5b35f149542b146d02b91c92fb687e Mon Sep 17 00:00:00 2001 From: Usman Khalid <2200617@gmail.com> Date: Fri, 19 Sep 2014 15:58:29 +0500 Subject: [PATCH 2/2] If matlab grader sent unparsable response, show error message instead of trying to render the response. TNL-388 --- common/lib/capa/capa/inputtypes.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index 0185b5fd7d..2f30f634dd 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -808,6 +808,7 @@ class MatlabInput(CodeInput): Handle matlab-specific parsing """ _ = self.capa_system.i18n.ugettext + submitted_msg = _("Submitted. As soon as a response is returned, " "this message will be replaced by that feedback.") self.submitted_msg = submitted_msg @@ -889,9 +890,23 @@ class MatlabInput(CodeInput): def _extra_context(self): """ 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'
{0}
'.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"{0}".format(_("Error running code.")) + extra_context = { 'queue_len': str(self.queue_len), - 'queue_msg': self.queue_msg, + 'queue_msg': queue_msg, 'button_enabled': self.button_enabled(), 'matlab_editor_js': '{static_url}js/vendor/CodeMirror/octave.js'.format( static_url=self.capa_system.STATIC_URL),