From 6a00878f59630ff9a6fb66ac4a0fc7a6e19cb7a7 Mon Sep 17 00:00:00 2001 From: David Ormsbee Date: Fri, 5 Mar 2021 10:11:31 -0500 Subject: [PATCH] fix: learning_sequences missing courses now return 404 (not 500) We weren't properly catching the CourseOutlineData.DoesNotExist error before this commit. TNL-7979 --- .../content/learning_sequences/tests/test_views.py | 9 +++++++++ .../djangoapps/content/learning_sequences/views.py | 12 +++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/openedx/core/djangoapps/content/learning_sequences/tests/test_views.py b/openedx/core/djangoapps/content/learning_sequences/tests/test_views.py index 24b95ba927..1da7121a53 100644 --- a/openedx/core/djangoapps/content/learning_sequences/tests/test_views.py +++ b/openedx/core/djangoapps/content/learning_sequences/tests/test_views.py @@ -70,6 +70,15 @@ class CourseOutlineViewTest(CacheIsolationTestCase, APITestCase): # lint-amnest result = self.client.get(self.course_url) assert result.status_code == 403 + def test_non_existent_course_404(self): + """ + We should 404, not 500, when asking for a course that isn't there. + """ + self.client.login(username='staff', password='staff_pass') + fake_course_key = self.course_key.replace(run="not_real") + result = self.client.get(self.url_for(fake_course_key)) + assert result.status_code == 404 + def test_deprecated_course_key(self): """ For now, make sure you need staff access bits to use the API. diff --git a/openedx/core/djangoapps/content/learning_sequences/views.py b/openedx/core/djangoapps/content/learning_sequences/views.py index aab74427bb..04905ddeee 100644 --- a/openedx/core/djangoapps/content/learning_sequences/views.py +++ b/openedx/core/djangoapps/content/learning_sequences/views.py @@ -12,13 +12,15 @@ from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthenticat from edx_rest_framework_extensions.auth.session.authentication import SessionAuthenticationAllowInactiveUser from opaque_keys import InvalidKeyError from opaque_keys.edx.keys import CourseKey -from rest_framework.views import APIView -from rest_framework.response import Response from rest_framework import serializers +from rest_framework.exceptions import NotFound +from rest_framework.response import Response +from rest_framework.views import APIView import attr # lint-amnesty, pylint: disable=unused-import from openedx.core.lib.api.permissions import IsStaff from .api import get_user_course_outline_details +from .data import CourseOutlineData User = get_user_model() log = logging.getLogger(__name__) @@ -165,7 +167,11 @@ class CourseOutlineView(APIView): user = self._determine_user(request) # Grab the user's outline and send our response... - user_course_outline_details = get_user_course_outline_details(course_key, user, at_time) + try: + user_course_outline_details = get_user_course_outline_details(course_key, user, at_time) + except CourseOutlineData.DoesNotExist: + raise NotFound() + serializer = self.UserCourseOutlineDataSerializer(user_course_outline_details) return Response(serializer.data)