diff --git a/lms/djangoapps/instructor/tests/test_api_v2_authz.py b/lms/djangoapps/instructor/tests/test_api_v2_authz.py
index 32d0b552de36..027e49ec6660 100644
--- a/lms/djangoapps/instructor/tests/test_api_v2_authz.py
+++ b/lms/djangoapps/instructor/tests/test_api_v2_authz.py
@@ -4,6 +4,9 @@
They cover the tabs, ``authz_enabled`` and the migrated endpoints.
"""
+import json
+from itertools import product
+from textwrap import dedent
from unittest.mock import patch
import ddt
@@ -16,21 +19,26 @@
COURSE_LIMITED_STAFF,
)
from openedx_authz.applets.learner_administration.constants import COURSES_VIEW_COURSE_INFO
-from openedx_authz.authz import COURSE_ADMIN, COURSE_STAFF
+from openedx_authz.applets.learner_grading.constants import COURSES_MANAGE_LEARNER_GRADING, COURSES_VIEW_GRADEBOOK
+from openedx_authz.authz import COURSE_ADMIN, COURSE_STAFF, COURSES_VIEW_GRADING_SETTINGS
from rest_framework import status
from rest_framework.test import APIClient
-from common.djangoapps.student.roles import CourseDataResearcherRole, CourseLimitedStaffRole
+from common.djangoapps.student.models import CourseEnrollment
+from common.djangoapps.student.roles import CourseBetaTesterRole, CourseDataResearcherRole, CourseLimitedStaffRole
from common.djangoapps.student.tests.factories import (
InstructorFactory,
StaffFactory,
UserFactory,
)
+from lms.djangoapps.courseware.models import StudentModule
from lms.djangoapps.courseware.tabs import get_course_tab_list
from lms.djangoapps.instructor.authz import constants
+from lms.djangoapps.instructor.authz.compat import LEGACY_ROLE_COMPAT_PERMISSIONS
from lms.djangoapps.instructor.authz.tests.mixins import InstructorAuthzTestMixin
+from lms.djangoapps.instructor_task.models import InstructorTask
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
-from xmodule.modulestore.tests.factories import CourseFactory
+from xmodule.modulestore.tests.factories import BlockFactory, CourseFactory
API = "lms.djangoapps.instructor.authz.api"
@@ -43,6 +51,20 @@
]
+def allowed_only(*permissions):
+ """
+ Build an ``is_user_allowed`` side effect that grants only the given permissions.
+ """
+ return lambda _username, permission, _scope: permission in permissions
+
+
+def allowed_all_except(*permissions):
+ """
+ Build an ``is_user_allowed`` side effect that grants every permission but the given ones.
+ """
+ return lambda _username, permission, _scope: permission not in permissions
+
+
@ddt.ddt
class CourseMetadataViewAuthzTest(InstructorAuthzTestMixin, SharedModuleStoreTestCase):
"""
@@ -193,9 +215,6 @@ def test_authz_tab_shown_with_any_of_its_permissions(self, tab_id, tab_permissio
"""
user = UserFactory()
- def allowed_only(*permissions):
- return lambda _username, permission, _scope: permission in permissions
-
with patch(
f"{API}.authz_api.is_user_allowed",
side_effect=allowed_only(COURSES_VIEW_COURSE_INFO.identifier),
@@ -312,3 +331,241 @@ def test_legacy_without_role_hidden(self):
self.set_authz_enabled(False)
assert not self._has_instructor_tab(UserFactory())
+
+
+# Grading tab requests: the AuthZ permission each one requires and the status it returns on success.
+GRADING_ACTIONS = {
+ "view_learner": (COURSES_VIEW_GRADEBOOK.identifier, status.HTTP_200_OK),
+ "view_problem": (COURSES_VIEW_GRADEBOOK.identifier, status.HTTP_200_OK),
+ "view_grading_config": (COURSES_VIEW_GRADING_SETTINGS.identifier, status.HTTP_200_OK),
+ "reset_attempts": (COURSES_MANAGE_LEARNER_GRADING.identifier, status.HTTP_200_OK),
+ "delete_state": (COURSES_MANAGE_LEARNER_GRADING.identifier, status.HTTP_200_OK),
+ "rescore": (COURSES_MANAGE_LEARNER_GRADING.identifier, status.HTTP_202_ACCEPTED),
+ "override_score": (COURSES_MANAGE_LEARNER_GRADING.identifier, status.HTTP_202_ACCEPTED),
+ "reset_attempts_all_learners": (COURSES_MANAGE_LEARNER_GRADING.identifier, status.HTTP_202_ACCEPTED),
+ "rescore_all_learners": (COURSES_MANAGE_LEARNER_GRADING.identifier, status.HTTP_202_ACCEPTED),
+}
+
+# All Learners actions. With the flag off, they also require instructor access.
+ALL_LEARNERS_GRADING_ACTIONS = ("reset_attempts_all_learners", "rescore_all_learners")
+
+# The reads and the Single Learner actions, which legacy course staff can use with the flag off.
+LEGACY_STAFF_GRADING_ACTIONS = tuple(
+ action for action in GRADING_ACTIONS if action not in ALL_LEARNERS_GRADING_ACTIONS
+)
+
+# Legacy roles that openedx-authz maps without dashboard permissions (see ``authz/compat.py``),
+# each with its AuthZ role.
+LEGACY_COMPAT_ROLES = (
+ (COURSE_LIMITED_STAFF.external_key, CourseLimitedStaffRole),
+ (COURSE_DATA_RESEARCHER.external_key, CourseDataResearcherRole),
+ (COURSE_BETA_TESTER.external_key, CourseBetaTesterRole),
+)
+
+
+@ddt.ddt
+class GradingTabAuthzTest(InstructorAuthzTestMixin, SharedModuleStoreTestCase):
+ """
+ The Grading tab endpoints follow the AuthZ flag.
+
+ With the flag on, ``LearnerView`` and ``ProblemView`` require
+ ``courses.view_gradebook``, ``GradingConfigView`` requires
+ ``courses.view_grading_settings``, and the Single Learner and All Learners
+ actions require ``courses.manage_learner_grading``. With the flag off,
+ each view uses its legacy bridgekeeper permission, and the All Learners
+ actions also require instructor access.
+
+ The functional behavior of each endpoint, with a legacy instructor, is
+ covered in ``tests/views/test_api_v2.py``.
+ """
+
+ PROBLEM_XML = dedent("""\
+
`` block
without additional parsing.
"""
- permission_classes = (IsAuthenticated, permissions.InstructorPermission)
+ permission_classes = (IsAuthenticated, InstructorAuthzPermission)
+ authz_permissions = {
+ "GET": (COURSES_VIEW_GRADING_SETTINGS.identifier,),
+ }
permission_name = permissions.VIEW_DASHBOARD
@extend_schema(
@@ -3735,7 +3745,10 @@ class ResetAttemptsView(DeveloperErrorViewMixin, APIView):
**POST** with `learner` query param: resets a single learner's attempts (synchronous).
**POST** without `learner`: queues a background task to reset all learners (asynchronous).
"""
- permission_classes = (IsAuthenticated, permissions.InstructorPermission)
+ permission_classes = (IsAuthenticated, InstructorAuthzPermission)
+ authz_permissions = {
+ "POST": (COURSES_MANAGE_LEARNER_GRADING.identifier,),
+ }
permission_name = permissions.GIVE_STUDENT_EXTENSION
@extend_schema(
@@ -3801,8 +3814,12 @@ def post(self, request, course_id, problem):
return Response(serializer.data, status=status.HTTP_200_OK)
else:
- course = get_course_with_access(request.user, 'staff', course_key, depth=None)
- if not has_access(request.user, 'instructor', course):
+ if not user_has_instructor_permission(
+ request.user,
+ COURSES_MANAGE_LEARNER_GRADING.identifier,
+ course_key,
+ authz_legacy.has_instructor_access,
+ ):
return Response(
{'error': 'Instructor access required for bulk operations'},
status=status.HTTP_403_FORBIDDEN,
@@ -3834,7 +3851,10 @@ class DeleteStateView(DeveloperErrorViewMixin, APIView):
The `learner` query parameter is required. This operation is destructive
and cannot be undone.
"""
- permission_classes = (IsAuthenticated, permissions.InstructorPermission)
+ permission_classes = (IsAuthenticated, InstructorAuthzPermission)
+ authz_permissions = {
+ "DELETE": (COURSES_MANAGE_LEARNER_GRADING.identifier,),
+ }
permission_name = permissions.GIVE_STUDENT_EXTENSION
@extend_schema(
@@ -3914,7 +3934,10 @@ class RescoreView(DeveloperErrorViewMixin, APIView):
Optionally accepts `only_if_higher=true` (query param or request body) to only
update if the new score is higher.
"""
- permission_classes = (IsAuthenticated, permissions.InstructorPermission)
+ permission_classes = (IsAuthenticated, InstructorAuthzPermission)
+ authz_permissions = {
+ "POST": (COURSES_MANAGE_LEARNER_GRADING.identifier,),
+ }
permission_name = permissions.OVERRIDE_GRADES
@extend_schema(
@@ -3984,8 +4007,12 @@ def post(self, request, course_id, problem):
)
else:
- course = get_course_with_access(request.user, 'staff', course_key)
- if not has_access(request.user, 'instructor', course):
+ if not user_has_instructor_permission(
+ request.user,
+ COURSES_MANAGE_LEARNER_GRADING.identifier,
+ course_key,
+ authz_legacy.has_instructor_access,
+ ):
return Response(
{'error': 'Instructor access required for bulk operations'},
status=status.HTTP_403_FORBIDDEN,
@@ -4021,7 +4048,10 @@ class ScoreOverrideView(DeveloperErrorViewMixin, APIView):
The `learner` query parameter is required. Accepts a JSON body with `score` field.
"""
- permission_classes = (IsAuthenticated, permissions.InstructorPermission)
+ permission_classes = (IsAuthenticated, InstructorAuthzPermission)
+ authz_permissions = {
+ "PUT": (COURSES_MANAGE_LEARNER_GRADING.identifier,),
+ }
permission_name = permissions.OVERRIDE_GRADES
@extend_schema(
@@ -4070,19 +4100,13 @@ def put(self, request, course_id, problem):
score = body_serializer.validated_data['score']
try:
- block = modulestore().get_item(usage_key)
+ modulestore().get_item(usage_key)
except ItemNotFoundError:
return Response(
{'error': 'Problem not found'},
status=status.HTTP_404_NOT_FOUND,
)
- if not has_access(request.user, 'staff', block):
- return Response(
- {'error': 'You do not have permission to override scores for this problem'},
- status=status.HTTP_403_FORBIDDEN,
- )
-
try:
instructor_task = task_api.submit_override_score(
request, usage_key, student, score,