diff --git a/common/lib/xmodule/xmodule/seq_module.py b/common/lib/xmodule/xmodule/seq_module.py index b66277a..d07447a 100644 --- a/common/lib/xmodule/xmodule/seq_module.py +++ b/common/lib/xmodule/xmodule/seq_module.py @@ -88,7 +88,13 @@ class SequenceModule(SequenceFields, XModule): def handle_ajax(self, dispatch, data): # TODO: bounds checking ''' get = request.POST instance ''' if dispatch == 'goto_position': - self.position = int(data['position']) + # set position to default value if either 'position' argument not + # found in request or it is a non-positive integer + position = data.get('position', u'1') + if position.isdigit() and int(position) > 0: + self.position = int(position) + else: + self.position = 1 return json.dumps({'success': True}) raise NotFoundError('Unexpected dispatch type') diff --git a/lms/djangoapps/courseware/tests/test_module_render.py b/lms/djangoapps/courseware/tests/test_module_render.py index 0c2bb31..1250717 100644 --- a/lms/djangoapps/courseware/tests/test_module_render.py +++ b/lms/djangoapps/courseware/tests/test_module_render.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """ Test for lms courseware app, module render unit """ @@ -177,6 +178,44 @@ class ModuleRenderTestCase(ModuleStoreTestCase, LoginEnrollmentTestCase): self.assertEquals(403, response.status_code) self.assertEquals('Unauthenticated', response.content) + def test_missing_position_handler(self): + """ + Test that sending POST request without or invalid position argument don't raise server error + """ + self.client.login(username=self.mock_user.username, password="test") + dispatch_url = reverse( + 'xblock_handler', + args=[ + self.course_key.to_deprecated_string(), + quote_slashes(self.course_key.make_usage_key('videosequence', 'Toy_Videos').to_deprecated_string()), + 'xmodule_handler', + 'goto_position' + ] + ) + response = self.client.post(dispatch_url) + self.assertEqual(200, response.status_code) + self.assertEqual(json.loads(response.content), {'success': True}) + + response = self.client.post(dispatch_url, {'position': ''}) + self.assertEqual(200, response.status_code) + self.assertEqual(json.loads(response.content), {'success': True}) + + response = self.client.post(dispatch_url, {'position': '-1'}) + self.assertEqual(200, response.status_code) + self.assertEqual(json.loads(response.content), {'success': True}) + + response = self.client.post(dispatch_url, {'position': "string"}) + self.assertEqual(200, response.status_code) + self.assertEqual(json.loads(response.content), {'success': True}) + + response = self.client.post(dispatch_url, {'position': u"Φυσικά"}) + self.assertEqual(200, response.status_code) + self.assertEqual(json.loads(response.content), {'success': True}) + + response = self.client.post(dispatch_url, {'position': None}) + self.assertEqual(200, response.status_code) + self.assertEqual(json.loads(response.content), {'success': True}) + @ddt.data('pure', 'vertical') @XBlock.register_temp_plugin(PureXBlock, identifier='pure') def test_rebinding_same_user(self, block_type):