|
2 | 2 | Tests of student.roles |
3 | 3 | """ |
4 | 4 |
|
| 5 | +from unittest.mock import patch |
5 | 6 |
|
6 | 7 | import ddt |
7 | | -from unittest.mock import patch |
8 | 8 | from django.contrib.auth.models import Permission |
9 | 9 | from django.test import TestCase |
10 | 10 | from edx_toggles.toggles.testutils import override_waffle_flag |
11 | 11 | from opaque_keys.edx.keys import CourseKey |
12 | 12 | from opaque_keys.edx.locator import LibraryLocator |
13 | | - |
14 | | -from openedx.core.djangoapps.content.course_overviews.tests.factories import CourseOverviewFactory |
15 | | -from openedx_authz.api.data import ContentLibraryData, RoleAssignmentData, RoleData, UserData |
| 13 | +from openedx_authz.api.data import ContentLibraryData, CourseOverviewData, RoleAssignmentData, RoleData, UserData |
| 14 | +from openedx_authz.constants.roles import COURSE_ADMIN, COURSE_STAFF |
16 | 15 | from openedx_authz.engine.enforcer import AuthzEnforcer |
17 | 16 |
|
18 | 17 | from common.djangoapps.student.admin import CourseAccessRoleHistoryAdmin |
19 | 18 | from common.djangoapps.student.models import CourseAccessRoleHistory, User |
| 19 | +from common.djangoapps.student.role_helpers import get_course_roles, has_staff_roles |
20 | 20 | from common.djangoapps.student.roles import ( |
| 21 | + ROLE_CACHE_UNGROUPED_ROLES__KEY, |
21 | 22 | AuthzCompatCourseAccessRole, |
22 | 23 | CourseAccessRole, |
23 | 24 | CourseBetaTesterRole, |
| 25 | + CourseDataResearcherRole, |
| 26 | + CourseFinanceAdminRole, |
24 | 27 | CourseInstructorRole, |
25 | | - CourseRole, |
26 | 28 | CourseLimitedStaffRole, |
27 | | - CourseStaffRole, |
28 | | - CourseFinanceAdminRole, |
| 29 | + CourseRole, |
29 | 30 | CourseSalesAdminRole, |
30 | | - LibraryUserRole, |
31 | | - CourseDataResearcherRole, |
| 31 | + CourseStaffRole, |
32 | 32 | GlobalStaff, |
| 33 | + LibraryUserRole, |
33 | 34 | OrgContentCreatorRole, |
34 | 35 | OrgInstructorRole, |
35 | 36 | OrgStaffRole, |
36 | 37 | RoleCache, |
37 | 38 | get_authz_compat_course_access_roles_for_user, |
38 | 39 | get_role_cache_key_for_course, |
39 | | - ROLE_CACHE_UNGROUPED_ROLES__KEY |
40 | 40 | ) |
41 | | -from common.djangoapps.student.role_helpers import get_course_roles, has_staff_roles |
42 | 41 | from common.djangoapps.student.tests.factories import AnonymousUserFactory, InstructorFactory, StaffFactory, UserFactory |
| 42 | +from openedx.core.djangoapps.content.course_overviews.tests.factories import CourseOverviewFactory |
43 | 43 | from openedx.core.toggles import AUTHZ_COURSE_AUTHORING_FLAG |
44 | 44 |
|
45 | 45 |
|
@@ -239,9 +239,51 @@ def test_get_orgs_for_user(self): |
239 | 239 | role_second_org.add_users(self.student) |
240 | 240 | assert len(role.get_orgs_for_user(self.student)) == 2 |
241 | 241 |
|
| 242 | + @override_waffle_flag(AUTHZ_COURSE_AUTHORING_FLAG, active=True) |
| 243 | + def test_get_orgs_for_user_authz(self): |
| 244 | + """ |
| 245 | + Test get_orgs_for_user using AuthZ compatibility layer |
| 246 | + """ |
| 247 | + role = CourseStaffRole(self.course_key) |
| 248 | + |
| 249 | + other_org = "MIT" |
| 250 | + other_course_key = CourseKey.from_string(f"course-v1:{other_org}+Javascript+2026_T1") |
| 251 | + another_course_key = CourseKey.from_string(f"course-v1:{other_org}+Python+2026_T1") |
| 252 | + |
| 253 | + staff_authz_role = RoleData(external_key=COURSE_STAFF) |
| 254 | + instructor_authz_role = RoleData(external_key=COURSE_ADMIN) |
| 255 | + |
| 256 | + assignments = [ |
| 257 | + RoleAssignmentData( |
| 258 | + subject=UserData(external_key=self.student.username), |
| 259 | + roles=[staff_authz_role], |
| 260 | + scope=CourseOverviewData(external_key=str(self.course_key)), |
| 261 | + ), |
| 262 | + RoleAssignmentData( |
| 263 | + subject=UserData(external_key=self.student.username), |
| 264 | + roles=[staff_authz_role], |
| 265 | + scope=CourseOverviewData(external_key=str(other_course_key)), |
| 266 | + ), |
| 267 | + RoleAssignmentData( |
| 268 | + subject=UserData(external_key=self.student.username), |
| 269 | + roles=[staff_authz_role], |
| 270 | + scope=CourseOverviewData(external_key=str(another_course_key)), |
| 271 | + ), |
| 272 | + # Non-matching role should be ignored |
| 273 | + RoleAssignmentData( |
| 274 | + subject=UserData(external_key=self.student.username), |
| 275 | + roles=[instructor_authz_role], |
| 276 | + scope=CourseOverviewData(external_key=str(self.course_key)), |
| 277 | + ), |
| 278 | + ] |
| 279 | + |
| 280 | + with patch("openedx_authz.api.users.get_user_role_assignments_filtered", return_value=assignments): |
| 281 | + result = role.get_orgs_for_user(self.student) |
| 282 | + self.assertEqual(result, [self.course_key.org, other_org]) |
| 283 | + |
242 | 284 | def test_get_authz_compat_course_access_roles_for_user(self): |
243 | 285 | """ |
244 | | - Thest that get_authz_compat_course_access_roles_for_user doesn't crash when the user |
| 286 | + Test that get_authz_compat_course_access_roles_for_user doesn't crash when the user |
245 | 287 | has Libraries V2 or other non-course roles in their assignments. |
246 | 288 | """ |
247 | 289 | lib_assignment = RoleAssignmentData( |
|
0 commit comments