From 06dbb68e757e38efc6d9c8ff8118dd4c18f782e2 Mon Sep 17 00:00:00 2001 From: Mat Peterson Date: Fri, 13 Jun 2014 17:58:35 +0000 Subject: [PATCH] courseware/views.py index function position value integer error correction and test LMS-2844 --- lms/djangoapps/courseware/tests/test_views.py | 23 ++++++++++++++----- lms/djangoapps/courseware/views.py | 7 ++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lms/djangoapps/courseware/tests/test_views.py b/lms/djangoapps/courseware/tests/test_views.py index d235de4656..778da787b5 100644 --- a/lms/djangoapps/courseware/tests/test_views.py +++ b/lms/djangoapps/courseware/tests/test_views.py @@ -80,13 +80,13 @@ class ViewsTestCase(TestCase): Tests for views.py methods. """ def setUp(self): - course = CourseFactory() - chapter = ItemFactory(category='chapter', parent_location=course.location) # pylint: disable=no-member - section = ItemFactory(category='sequential', parent_location=chapter.location, due=datetime(2013, 9, 18, 11, 30, 00)) - vertical = ItemFactory(category='vertical', parent_location=section.location) - self.component = ItemFactory(category='problem', parent_location=vertical.location) + self.course = CourseFactory() + self.chapter = ItemFactory(category='chapter', parent_location=self.course.location) # pylint: disable=no-member + self.section = ItemFactory(category='sequential', parent_location=self.chapter.location, due=datetime(2013, 9, 18, 11, 30, 00)) + self.vertical = ItemFactory(category='vertical', parent_location=self.section.location) + self.component = ItemFactory(category='problem', parent_location=self.vertical.location) - self.course_key = course.id + self.course_key = self.course.id self.user = User.objects.create(username='dummy', password='123456', email='test@mit.edu') self.date = datetime(2013, 1, 22, tzinfo=UTC) @@ -147,6 +147,17 @@ class ViewsTestCase(TestCase): self.assertRaises(Http404, views.redirect_to_course_position, mock_module, views.CONTENT_DEPTH) + def test_index_invalid_position(self): + request_url = '/'.join([ + '/courses', + self.course.id.to_deprecated_string(), + self.chapter.location.name, + self.section.location.name, + 'f' + ]) + response = self.client.get(request_url) + self.assertEqual(response.status_code, 404) + def test_registered_for_course(self): self.assertFalse(views.registered_for_course('Basketweaving', None)) mock_user = MagicMock() diff --git a/lms/djangoapps/courseware/views.py b/lms/djangoapps/courseware/views.py index 228533f448..208bcf5380 100644 --- a/lms/djangoapps/courseware/views.py +++ b/lms/djangoapps/courseware/views.py @@ -354,6 +354,13 @@ def index(request, course_id, chapter=None, section=None, section_field_data_cache = FieldDataCache.cache_for_descriptor_descendents( course_key, user, section_descriptor, depth=None) + # Verify that position a string is in fact an int + if position is not None: + try: + int(position) + except ValueError: + raise Http404("Position {} is not an integer!".format(position)) + section_module = get_module_for_descriptor( request.user, request,