Commit 46b1c850 by Usman Khalid

Catch InvalidKeyError in UserTagsEventContextMiddleware.

LMS-11189
parent adaf2eb5
......@@ -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()
......
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