Skip to content
Snippets Groups Projects
Commit 792940d4 authored by Ibrahim's avatar Ibrahim Committed by Douglas Hall
Browse files

Fix PHX-254 where custom_parameters field is empty by adding field validation

parent 19efe721
No related branches found
No related tags found
No related merge requests found
...@@ -64,6 +64,7 @@ from django.utils import timezone ...@@ -64,6 +64,7 @@ from django.utils import timezone
from xblock.core import String, Scope, List, XBlock from xblock.core import String, Scope, List, XBlock
from xblock.fields import Boolean, Float, Integer from xblock.fields import Boolean, Float, Integer
from xblock.fragment import Fragment from xblock.fragment import Fragment
from xblock.validation import ValidationMessage
from xblockutils.resources import ResourceLoader from xblockutils.resources import ResourceLoader
from xblockutils.studio_editable import StudioEditableXBlockMixin from xblockutils.studio_editable import StudioEditableXBlockMixin
...@@ -73,7 +74,6 @@ from .oauth import log_authorization_header ...@@ -73,7 +74,6 @@ from .oauth import log_authorization_header
from .lti import LtiConsumer from .lti import LtiConsumer
from .outcomes import OutcomeService from .outcomes import OutcomeService
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
DOCS_ANCHOR_TAG_OPEN = ( DOCS_ANCHOR_TAG_OPEN = (
...@@ -424,6 +424,11 @@ class LtiConsumerXBlock(StudioEditableXBlockMixin, XBlock): ...@@ -424,6 +424,11 @@ class LtiConsumerXBlock(StudioEditableXBlockMixin, XBlock):
'ask_to_send_username', 'ask_to_send_email' 'ask_to_send_username', 'ask_to_send_email'
) )
def validate_field_data(self, validation, data):
if not isinstance(data.custom_parameters, list):
_ = self.runtime.service(self, "i18n").ugettext
validation.add(ValidationMessage(ValidationMessage.ERROR, unicode(_("Custom Parameters must be a list"))))
@property @property
def descriptor(self): def descriptor(self):
""" """
...@@ -570,21 +575,22 @@ class LtiConsumerXBlock(StudioEditableXBlockMixin, XBlock): ...@@ -570,21 +575,22 @@ class LtiConsumerXBlock(StudioEditableXBlockMixin, XBlock):
# parsing custom parameters to dict # parsing custom parameters to dict
custom_parameters = {} custom_parameters = {}
for custom_parameter in self.custom_parameters: if isinstance(self.custom_parameters, list):
try: for custom_parameter in self.custom_parameters:
param_name, param_value = [p.strip() for p in custom_parameter.split('=', 1)] try:
except ValueError: param_name, param_value = [p.strip() for p in custom_parameter.split('=', 1)]
_ = self.runtime.service(self, "i18n").ugettext except ValueError:
msg = _('Could not parse custom parameter: {custom_parameter}. Should be "x=y" string.').format( _ = self.runtime.service(self, "i18n").ugettext
custom_parameter="{0!r}".format(custom_parameter) msg = _('Could not parse custom parameter: {custom_parameter}. Should be "x=y" string.').format(
) custom_parameter="{0!r}".format(custom_parameter)
raise LtiError(msg) )
raise LtiError(msg)
# LTI specs: 'custom_' should be prepended before each custom parameter, as pointed in link above.
if param_name not in LTI_PARAMETERS: # LTI specs: 'custom_' should be prepended before each custom parameter, as pointed in link above.
param_name = 'custom_' + param_name if param_name not in LTI_PARAMETERS:
param_name = 'custom_' + param_name
custom_parameters[unicode(param_name)] = unicode(param_value)
custom_parameters[unicode(param_name)] = unicode(param_value)
return custom_parameters return custom_parameters
......
...@@ -52,6 +52,14 @@ class TestProperties(TestLtiConsumerXBlock): ...@@ -52,6 +52,14 @@ class TestProperties(TestLtiConsumerXBlock):
""" """
self.assertEqual(self.xblock.context_id, unicode(self.xblock.course_id)) # pylint: disable=no-member self.assertEqual(self.xblock.context_id, unicode(self.xblock.course_id)) # pylint: disable=no-member
def test_validate(self):
"""
Test that if custom_parameters is empty string, a validation error is added
"""
self.xblock.custom_parameters = ''
validation = self.xblock.validate()
self.assertFalse(validation.empty)
def test_role(self): def test_role(self):
""" """
Test `role` returns the correct LTI role string Test `role` returns the correct LTI role string
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment