From 9599c31aa068646b92f719662281ab55117d36cd Mon Sep 17 00:00:00 2001 From: Usman Khalid <2200617@gmail.com> Date: Mon, 4 Aug 2014 21:13:36 +0500 Subject: [PATCH] Catch InvalidKeyError in static_tab view. LMS-11189 --- lms/djangoapps/courseware/tests/helpers.py | 2 ++ lms/djangoapps/courseware/tests/test_tabs.py | 8 +++++++- lms/djangoapps/courseware/views.py | 6 +++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lms/djangoapps/courseware/tests/helpers.py b/lms/djangoapps/courseware/tests/helpers.py index 8c76b6844f..1cb548e8e8 100644 --- a/lms/djangoapps/courseware/tests/helpers.py +++ b/lms/djangoapps/courseware/tests/helpers.py @@ -49,9 +49,11 @@ def get_request_for_user(user): request = RequestFactory() request.user = user + request.COOKIES = {} request.META = {} request.is_secure = lambda: True request.get_host = lambda: "edx.org" + request.method = 'GET' return request diff --git a/lms/djangoapps/courseware/tests/test_tabs.py b/lms/djangoapps/courseware/tests/test_tabs.py index f215936072..3b36599d3f 100644 --- a/lms/djangoapps/courseware/tests/test_tabs.py +++ b/lms/djangoapps/courseware/tests/test_tabs.py @@ -4,8 +4,9 @@ Test cases for tabs. from mock import MagicMock, Mock, patch from courseware.courses import get_course_by_id -from courseware.views import get_static_tab_contents +from courseware.views import get_static_tab_contents, static_tab +from django.http import Http404 from django.test.utils import override_settings from django.core.urlresolvers import reverse @@ -43,6 +44,11 @@ class StaticTabDateTestCase(LoginEnrollmentTestCase, ModuleStoreTestCase): self.assertEqual(resp.status_code, 200) self.assertIn("OOGIE BLOOGIE", resp.content) + def test_invalid_course_key(self): + request = get_request_for_user(UserFactory.create()) + with self.assertRaises(Http404): + static_tab(request, 'edX/toy', 'new_tab') + @override_settings(MODULESTORE=TEST_DATA_MIXED_MODULESTORE) def test_get_static_tab_contents(self): course = get_course_by_id(self.toy_course_key) diff --git a/lms/djangoapps/courseware/views.py b/lms/djangoapps/courseware/views.py index 3c501f63ab..8c23a0512b 100644 --- a/lms/djangoapps/courseware/views.py +++ b/lms/djangoapps/courseware/views.py @@ -549,7 +549,11 @@ def static_tab(request, course_id, tab_slug): Assumes the course_id is in a valid format. """ - course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) + try: + course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) + except InvalidKeyError: + raise Http404 + course = get_course_with_access(request.user, 'load', course_key) tab = CourseTabList.get_tab_by_slug(course.tabs, tab_slug)