Commit 51b680af by Waheed Ahmed

Merge pull request #5022 from edx/waheed/lms11141-fix-invalid-key-error-in-course-handler

Fixed InvalidKeyError in course handler in studio.
parents 9ca4c566 a1e911ae
...@@ -1488,6 +1488,12 @@ class ContentStoreTest(ContentStoreTestCase): ...@@ -1488,6 +1488,12 @@ class ContentStoreTest(ContentStoreTestCase):
course_module = self.store.get_course(course_key) course_module = self.store.get_course(course_key)
self.assertEquals(course_module.wiki_slug, 'MITx.111.2013_Spring') self.assertEquals(course_module.wiki_slug, 'MITx.111.2013_Spring')
def test_course_handler_with_invalid_course_key_string(self):
"""Test viewing the course overview page with invalid course id"""
response = self.client.get_html('/course/edX/test')
self.assertEquals(response.status_code, 404)
class MetadataSaveTestCase(ContentStoreTestCase): class MetadataSaveTestCase(ContentStoreTestCase):
"""Test that metadata is correctly cached and decached.""" """Test that metadata is correctly cached and decached."""
......
...@@ -12,7 +12,7 @@ from django.conf import settings ...@@ -12,7 +12,7 @@ from django.conf import settings
from django.views.decorators.http import require_http_methods from django.views.decorators.http import require_http_methods
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.http import HttpResponseBadRequest, HttpResponseNotFound, HttpResponse from django.http import HttpResponseBadRequest, HttpResponseNotFound, HttpResponse, Http404
from util.json_request import JsonResponse, JsonResponseBadRequest from util.json_request import JsonResponse, JsonResponseBadRequest
from util.date_utils import get_default_time_display from util.date_utils import get_default_time_display
from edxmako.shortcuts import render_to_response from edxmako.shortcuts import render_to_response
...@@ -208,28 +208,31 @@ def course_handler(request, course_key_string=None): ...@@ -208,28 +208,31 @@ def course_handler(request, course_key_string=None):
DELETE DELETE
json: delete this branch from this course (leaving off /branch/draft would imply delete the course) json: delete this branch from this course (leaving off /branch/draft would imply delete the course)
""" """
response_format = request.REQUEST.get('format', 'html') try:
if response_format == 'json' or 'application/json' in request.META.get('HTTP_ACCEPT', 'application/json'): response_format = request.REQUEST.get('format', 'html')
if request.method == 'GET': if response_format == 'json' or 'application/json' in request.META.get('HTTP_ACCEPT', 'application/json'):
course_module = _get_course_module(CourseKey.from_string(course_key_string), request.user, depth=None) if request.method == 'GET':
return JsonResponse(_course_outline_json(request, course_module)) course_module = _get_course_module(CourseKey.from_string(course_key_string), request.user, depth=None)
elif request.method == 'POST': # not sure if this is only post. If one will have ids, it goes after access return JsonResponse(_course_outline_json(request, course_module))
return _create_or_rerun_course(request) elif request.method == 'POST': # not sure if this is only post. If one will have ids, it goes after access
elif not has_course_access(request.user, CourseKey.from_string(course_key_string)): return _create_or_rerun_course(request)
raise PermissionDenied() elif not has_course_access(request.user, CourseKey.from_string(course_key_string)):
elif request.method == 'PUT': raise PermissionDenied()
raise NotImplementedError() elif request.method == 'PUT':
elif request.method == 'DELETE': raise NotImplementedError()
raise NotImplementedError() elif request.method == 'DELETE':
else: raise NotImplementedError()
return HttpResponseBadRequest() else:
elif request.method == 'GET': # assume html return HttpResponseBadRequest()
if course_key_string is None: elif request.method == 'GET': # assume html
return course_listing(request) if course_key_string is None:
return course_listing(request)
else:
return course_index(request, CourseKey.from_string(course_key_string))
else: else:
return course_index(request, CourseKey.from_string(course_key_string)) return HttpResponseNotFound()
else: except InvalidKeyError:
return HttpResponseNotFound() raise Http404
@login_required @login_required
......
...@@ -200,24 +200,17 @@ class SSLClientTest(ModuleStoreTestCase): ...@@ -200,24 +200,17 @@ class SSLClientTest(ModuleStoreTestCase):
This tests to make sure when immediate signup is on that This tests to make sure when immediate signup is on that
the user doesn't get presented with the registration page. the user doesn't get presented with the registration page.
""" """
# Expect an InvalidKeyError from course page as we don't have anything else built response = self.client.get(
with self.assertRaisesRegexp( reverse('signup'), follow=True,
InvalidKeyError, SSL_CLIENT_S_DN=self.AUTH_DN.format(self.USER_NAME, self.USER_EMAIL)
"<class 'opaque_keys.edx.keys.CourseKey'>: None" )
): self.assertEqual(response.status_code, 404)
self.client.get(
reverse('signup'), follow=True,
SSL_CLIENT_S_DN=self.AUTH_DN.format(self.USER_NAME, self.USER_EMAIL)
)
# assert that we are logged in # assert that we are logged in
self.assertIn(SESSION_KEY, self.client.session) self.assertIn(SESSION_KEY, self.client.session)
# Now that we are logged in, make sure we don't see the registration page # Now that we are logged in, make sure we don't see the registration page
with self.assertRaisesRegexp( response = self.client.get(reverse('signup'), follow=True)
InvalidKeyError, self.assertEqual(response.status_code, 404)
"<class 'opaque_keys.edx.keys.CourseKey'>: None"
):
self.client.get(reverse('signup'), follow=True)
@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms') @unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
@override_settings(FEATURES=FEATURES_WITH_SSL_AUTH_IMMEDIATE_SIGNUP) @override_settings(FEATURES=FEATURES_WITH_SSL_AUTH_IMMEDIATE_SIGNUP)
......
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