Commit 7e3e8d04 by Usman Khalid

Merge pull request #4678 from edx/usman/lms11189-user-api-middleware-exception-2

Catch InvalidKeyError in UserTagsEventContextMiddleware and static_tab view.
parents adaf2eb5 9599c31a
......@@ -2,11 +2,14 @@
Middleware for user api.
Adds user's tags to tracking event context.
"""
from track.contexts import COURSE_REGEX
from eventtracking import tracker
from user_api.models import UserCourseTag
from opaque_keys import InvalidKeyError
from opaque_keys.edx.locations import SlashSeparatedCourseKey
from track.contexts import COURSE_REGEX
from user_api.models import UserCourseTag
class UserTagsEventContextMiddleware(object):
"""Middleware that adds a user's tags to tracking event context."""
......@@ -20,7 +23,11 @@ class UserTagsEventContextMiddleware(object):
course_id = None
if match:
course_id = match.group('course_id')
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)
try:
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)
except InvalidKeyError:
course_id = None
course_key = None
context = {}
......
......@@ -92,6 +92,13 @@ class TagsMiddlewareTest(TestCase):
self.assertContextSetTo({})
def test_invalid_course_id(self):
self.request = self.request_factory.get('/courses/edX/101/')
self.request.user = self.user
self.process_request()
self.assertContextSetTo({})
def test_anonymous_user(self):
self.request.user = AnonymousUserFactory()
......
......@@ -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
......
......@@ -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)
......
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment