Revert "Remove CourseOverview.get_from_id[s]_if_exists (#22918)" (#22926)

This reverts commit 3ca006214e.
This commit is contained in:
Kyle McCormick
2020-01-23 13:52:11 -05:00
committed by GitHub
parent 9fa49606f8
commit cdb0619846
7 changed files with 72 additions and 130 deletions

View File

@@ -1,66 +0,0 @@
"""
Tests for (some of) the view utilities.
"""
from django.conf.urls import url
from django.test.utils import override_settings
from rest_framework.response import Response
from rest_framework.test import APITestCase
from rest_framework.views import APIView
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory
from ..view_utils import DeveloperErrorViewMixin, verify_course_exists
class MockAPIView(DeveloperErrorViewMixin, APIView):
"""
Mock API view for testing.
"""
@verify_course_exists
def get(self, request, course_id):
"""
Mock GET handler for testing.
"""
return Response("Success {}".format(course_id))
urlpatterns = [
url(r'^mock/(?P<course_id>.*)/$', MockAPIView.as_view()), # Only works with new-style course keys
]
@override_settings(ROOT_URLCONF=__name__)
class VerifyCourseExistsTestCase(SharedModuleStoreTestCase, APITestCase):
"""
Tests for behavior of @verify_course_exists.
"""
def test_bad_key_404(self):
response = self.client.get('/mock/this-is-a-bad-key/')
assert response.status_code == 404
def test_nonexistent_course_404(self):
CourseFactory()
response = self.client.get('/mock/course-v1:Non+Existent+Course/')
assert response.status_code == 404
def test_course_exists_200(self):
course = CourseFactory(
org="This", course="IsA", run="Course",
default_store=ModuleStoreEnum.Type.split,
)
response = self.client.get('/mock/{}/'.format(course.id))
assert response.status_code == 200
def test_course_with_outdated_overview_200(self):
course = CourseFactory(
org="This", course="IsAnother", run="Course",
default_store=ModuleStoreEnum.Type.split,
)
course_overview = CourseOverview.get_from_id(course.id)
course_overview.version = CourseOverview.VERSION - 1
course_overview.save()
response = self.client.get('/mock/{}/'.format(course.id))
assert response.status_code == 200

View File

@@ -417,7 +417,7 @@ def verify_course_exists(view_func):
error_code='invalid_course_key'
)
if not CourseOverview.course_exists(course_key):
if not CourseOverview.get_from_id_if_exists(course_key):
raise self.api_error(
status_code=status.HTTP_404_NOT_FOUND,
developer_message=u"Requested grade for unknown course {course}".format(course=text_type(course_key)),