From b203e8a13b6797f90c3ec272bb820cba229a38e9 Mon Sep 17 00:00:00 2001 From: Nimisha Asthagiri <nasthagiri@edx.org> Date: Thu, 28 Jun 2018 18:11:56 -0400 Subject: [PATCH] Enable OAuth Scopes for Grades API --- .../grades/api/v1/tests/test_views.py | 4 +-- lms/djangoapps/grades/api/v1/views.py | 27 ++++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lms/djangoapps/grades/api/v1/tests/test_views.py b/lms/djangoapps/grades/api/v1/tests/test_views.py index 7ffb728376f..5fae2e94af0 100644 --- a/lms/djangoapps/grades/api/v1/tests/test_views.py +++ b/lms/djangoapps/grades/api/v1/tests/test_views.py @@ -185,7 +185,7 @@ class SingleUserGradesTests(GradeViewTestMixin, APITestCase): self.client.logout() self.client.login(username=self.other_student.username, password=self.password) resp = self.client.get(self.get_url(self.student.username)) - self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) + self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN) def test_self_get_grade_not_enrolled(self): """ @@ -337,7 +337,7 @@ class CourseGradesViewTest(GradeViewTestMixin, APITestCase): def test_student(self): resp = self.client.get(self.get_url()) - self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) + self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN) def test_course_does_not_exist(self): self.client.logout() diff --git a/lms/djangoapps/grades/api/v1/views.py b/lms/djangoapps/grades/api/v1/views.py index 36c63396a24..87aa3c12fd9 100644 --- a/lms/djangoapps/grades/api/v1/views.py +++ b/lms/djangoapps/grades/api/v1/views.py @@ -2,25 +2,30 @@ import logging from django.contrib.auth import get_user_model -from opaque_keys import InvalidKeyError -from opaque_keys.edx.keys import CourseKey from rest_framework import status from rest_framework.exceptions import AuthenticationFailed from rest_framework.generics import GenericAPIView from rest_framework.response import Response +from edx_rest_framework_extensions import permissions +from edx_rest_framework_extensions.authentication import JwtAuthentication from enrollment import data as enrollment_data -from student.models import CourseEnrollment from lms.djangoapps.grades.course_grade_factory import CourseGradeFactory +from opaque_keys import InvalidKeyError +from opaque_keys.edx.keys import CourseKey from openedx.core.djangoapps.content.course_overviews.models import CourseOverview -from openedx.core.lib.api.permissions import IsUserInUrlOrStaff -from openedx.core.lib.api.view_utils import DeveloperErrorViewMixin, view_auth_classes +from openedx.core.lib.api.authentication import ( + OAuth2AuthenticationAllowInactiveUser, + SessionAuthenticationAllowInactiveUser +) +from openedx.core.lib.api.view_utils import DeveloperErrorViewMixin +from student.models import CourseEnrollment + log = logging.getLogger(__name__) USER_MODEL = get_user_model() -@view_auth_classes() class GradeViewMixin(DeveloperErrorViewMixin): """ Mixin class for Grades related views. @@ -147,7 +152,15 @@ class CourseGradesView(GradeViewMixin, GenericAPIView): "letter_grade": null, }] """ - permission_classes = (IsUserInUrlOrStaff,) + authentication_classes = ( + JwtAuthentication, + OAuth2AuthenticationAllowInactiveUser, + SessionAuthenticationAllowInactiveUser, + ) + + permission_classes = (permissions.JWT_RESTRICTED_APPLICATION_OR_USER_ACCESS,) + + required_scopes = ['grades:read'] def get(self, request, course_id=None): """ -- GitLab