Commit 131c8cbf by Usman Khalid

Catch InvalidKeyError in course_id_from_url().

LMS-9663
parent dcee518d
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
import re import re
from django.conf import settings from django.conf import settings
from microsite_configuration import microsite from microsite_configuration import microsite
from opaque_keys import InvalidKeyError
from opaque_keys.edx.locations import SlashSeparatedCourseKey from opaque_keys.edx.locations import SlashSeparatedCourseKey
...@@ -41,4 +43,7 @@ def course_id_from_url(url): ...@@ -41,4 +43,7 @@ def course_id_from_url(url):
if course_id is None: if course_id is None:
return None return None
try:
return SlashSeparatedCourseKey.from_deprecated_string(course_id) return SlashSeparatedCourseKey.from_deprecated_string(course_id)
except InvalidKeyError:
return None
from django.test.client import RequestFactory """Tests for util.request module."""
import unittest
from django.conf import settings from django.conf import settings
from util.request import safe_get_host
from django.core.exceptions import SuspiciousOperation from django.core.exceptions import SuspiciousOperation
import unittest from django.test.client import RequestFactory
from util.request import course_id_from_url, safe_get_host
class ResponseTestCase(unittest.TestCase): class ResponseTestCase(unittest.TestCase):
...@@ -37,3 +41,15 @@ class ResponseTestCase(unittest.TestCase): ...@@ -37,3 +41,15 @@ class ResponseTestCase(unittest.TestCase):
settings.ALLOWED_HOSTS = ["the_valid_website.com"] settings.ALLOWED_HOSTS = ["the_valid_website.com"]
with self.assertRaises(SuspiciousOperation): with self.assertRaises(SuspiciousOperation):
safe_get_host(request) safe_get_host(request)
def test_course_id_from_url(self):
""" Test course_id_from_url(). """
self.assertIsNone(course_id_from_url('/login'))
self.assertIsNone(course_id_from_url('/course/edX/maths/2020'))
self.assertIsNone(course_id_from_url('/courses/edX/maths/'))
course_id = course_id_from_url('/courses/edX/maths/2020')
self.assertEqual(course_id.org, 'edX')
self.assertEqual(course_id.course, 'maths')
self.assertEqual(course_id.run, '2020')
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