|
6 | 6 |
|
7 | 7 | import ddt |
8 | 8 | from django.urls import reverse |
| 9 | +from openedx_authz.constants.roles import COURSE_DATA_RESEARCHER, COURSE_STAFF |
9 | 10 | from rest_framework import status |
| 11 | +from rest_framework.test import APIClient |
10 | 12 |
|
| 13 | +from cms.djangoapps.contentstore.api.tests.base import BaseCourseViewTest |
11 | 14 | from cms.djangoapps.contentstore.tests.utils import CourseTestCase |
12 | 15 | from cms.djangoapps.contentstore.utils import get_proctored_exam_settings_url |
13 | 16 | from cms.djangoapps.models.settings.course_grading import CourseGradingModel |
| 17 | +from common.djangoapps.student.tests.factories import UserFactory |
| 18 | +from openedx.core.djangoapps.authz.tests.mixins import CourseAuthzTestMixin |
14 | 19 | from openedx.core.djangoapps.credit.tests.factories import CreditCourseFactory |
15 | 20 |
|
16 | 21 | from ...mixins import PermissionAccessMixin |
@@ -117,3 +122,149 @@ def test_post_course_grading(self, mock_update_credit_course_requirements): |
117 | 122 | ) |
118 | 123 | self.assertEqual(response.status_code, status.HTTP_200_OK) |
119 | 124 | mock_update_credit_course_requirements.assert_called_once() |
| 125 | + |
| 126 | + |
| 127 | +class CourseGradingViewAuthzTest(CourseAuthzTestMixin, BaseCourseViewTest): |
| 128 | + """ |
| 129 | + Tests Course Grading Configuration API authorization using openedx-authz. |
| 130 | + The endpoint uses COURSES_VIEW_GRADING_SETTINGS and COURSES_EDIT_GRADING_SETTINGS permissions. |
| 131 | + """ |
| 132 | + |
| 133 | + view_name = "cms.djangoapps.contentstore:v1:course_grading" |
| 134 | + authz_roles_to_assign = [COURSE_STAFF.external_key] |
| 135 | + post_data = json.dumps({ |
| 136 | + "graders": [{ |
| 137 | + "type": "Homework", |
| 138 | + "min_count": 1, |
| 139 | + "drop_count": 0, |
| 140 | + "short_label": "", |
| 141 | + "weight": 100, |
| 142 | + "id": 0 |
| 143 | + }], |
| 144 | + "grade_cutoffs": {"A": 0.75, "B": 0.63, "C": 0.57, "D": 0.5}, |
| 145 | + "grace_period": {"hours": 12, "minutes": 0}, |
| 146 | + "minimum_grade_credit": 0.7, |
| 147 | + "is_credit_course": False, |
| 148 | + }) |
| 149 | + |
| 150 | + def test_authorized_user_can_access_get(self): |
| 151 | + """User with COURSE_STAFF role can access.""" |
| 152 | + resp = self.authorized_client.get(self.get_url(self.course_key)) |
| 153 | + self.assertEqual(resp.status_code, status.HTTP_200_OK) |
| 154 | + |
| 155 | + def test_unauthorized_user_cannot_access_get(self): |
| 156 | + """User without role cannot access.""" |
| 157 | + resp = self.unauthorized_client.get(self.get_url(self.course_key)) |
| 158 | + self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN) |
| 159 | + |
| 160 | + def test_role_scoped_to_course_get(self): |
| 161 | + """Authorization should only apply to the assigned course.""" |
| 162 | + other_course = self.store.create_course("OtherOrg", "OtherCourse", "Run", self.staff.id) |
| 163 | + |
| 164 | + resp = self.authorized_client.get(self.get_url(other_course.id)) |
| 165 | + self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN) |
| 166 | + |
| 167 | + def test_staff_user_allowed_via_legacy_get(self): |
| 168 | + """ |
| 169 | + Staff users should still pass through legacy fallback. |
| 170 | + """ |
| 171 | + self.client.login(username=self.staff.username, password=self.password) |
| 172 | + |
| 173 | + resp = self.client.get(self.get_url(self.course_key)) |
| 174 | + self.assertEqual(resp.status_code, status.HTTP_200_OK) |
| 175 | + |
| 176 | + def test_superuser_allowed_get(self): |
| 177 | + """Superusers should always be allowed.""" |
| 178 | + superuser = UserFactory(is_superuser=True) |
| 179 | + |
| 180 | + client = APIClient() |
| 181 | + client.force_authenticate(user=superuser) |
| 182 | + |
| 183 | + resp = client.get(self.get_url(self.course_key)) |
| 184 | + self.assertEqual(resp.status_code, status.HTTP_200_OK) |
| 185 | + |
| 186 | + def test_non_staff_user_cannot_access_get(self): |
| 187 | + """ |
| 188 | + User without required permissions should be denied. |
| 189 | + This case validates that a non-staff user doesn't get access. |
| 190 | + """ |
| 191 | + non_staff_user = UserFactory() |
| 192 | + non_staff_client = APIClient() |
| 193 | + self.add_user_to_role(non_staff_user, COURSE_DATA_RESEARCHER.external_key) |
| 194 | + non_staff_client.force_authenticate(user=non_staff_user) |
| 195 | + |
| 196 | + resp = non_staff_client.get(self.get_url(self.course_key)) |
| 197 | + self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN) |
| 198 | + |
| 199 | + def test_authorized_user_can_access_post(self): |
| 200 | + """User with COURSE_STAFF role can access.""" |
| 201 | + resp = self.authorized_client.post( |
| 202 | + self.get_url(self.course_key), |
| 203 | + data=self.post_data, |
| 204 | + content_type="application/json" |
| 205 | + ) |
| 206 | + self.assertEqual(resp.status_code, status.HTTP_200_OK) |
| 207 | + |
| 208 | + def test_unauthorized_user_cannot_access_post(self): |
| 209 | + """User without role cannot access.""" |
| 210 | + resp = self.unauthorized_client.post( |
| 211 | + self.get_url(self.course_key), |
| 212 | + data=self.post_data, |
| 213 | + content_type="application/json" |
| 214 | + ) |
| 215 | + self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN) |
| 216 | + |
| 217 | + def test_role_scoped_to_course_post(self): |
| 218 | + """Authorization should only apply to the assigned course.""" |
| 219 | + other_course = self.store.create_course("OtherOrg", "OtherCourse", "Run", self.staff.id) |
| 220 | + |
| 221 | + resp = self.authorized_client.post( |
| 222 | + self.get_url(other_course.id), |
| 223 | + data=self.post_data, |
| 224 | + content_type="application/json" |
| 225 | + ) |
| 226 | + self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN) |
| 227 | + |
| 228 | + def test_staff_user_allowed_via_legacy_post(self): |
| 229 | + """ |
| 230 | + Staff users should still pass through legacy fallback. |
| 231 | + """ |
| 232 | + self.client.login(username=self.staff.username, password=self.password) |
| 233 | + |
| 234 | + resp = self.client.post( |
| 235 | + self.get_url(self.course_key), |
| 236 | + data=self.post_data, |
| 237 | + content_type="application/json" |
| 238 | + ) |
| 239 | + self.assertEqual(resp.status_code, status.HTTP_200_OK) |
| 240 | + |
| 241 | + def test_superuser_allowed_post(self): |
| 242 | + """Superusers should always be allowed.""" |
| 243 | + superuser = UserFactory(is_superuser=True) |
| 244 | + |
| 245 | + client = APIClient() |
| 246 | + client.force_authenticate(user=superuser) |
| 247 | + |
| 248 | + resp = client.post( |
| 249 | + self.get_url(self.course_key), |
| 250 | + data=self.post_data, |
| 251 | + content_type="application/json" |
| 252 | + ) |
| 253 | + self.assertEqual(resp.status_code, status.HTTP_200_OK) |
| 254 | + |
| 255 | + def test_non_staff_user_cannot_access_post(self): |
| 256 | + """ |
| 257 | + User without required permissions should be denied. |
| 258 | + This case validates that a non-staff user doesn't get access. |
| 259 | + """ |
| 260 | + non_staff_user = UserFactory() |
| 261 | + non_staff_client = APIClient() |
| 262 | + self.add_user_to_role(non_staff_user, COURSE_DATA_RESEARCHER.external_key) |
| 263 | + non_staff_client.force_authenticate(user=non_staff_user) |
| 264 | + |
| 265 | + resp = non_staff_client.post( |
| 266 | + self.get_url(self.course_key), |
| 267 | + data=self.post_data, |
| 268 | + content_type="application/json" |
| 269 | + ) |
| 270 | + self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN) |
0 commit comments