Skip to content

Commit 30ffbe1

Browse files
committed
test: add authz test for get_orgs_for_user
1 parent daf6196 commit 30ffbe1

1 file changed

Lines changed: 54 additions & 12 deletions

File tree

common/djangoapps/student/tests/test_roles.py

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,44 @@
22
Tests of student.roles
33
"""
44

5+
from unittest.mock import patch
56

67
import ddt
7-
from unittest.mock import patch
88
from django.contrib.auth.models import Permission
99
from django.test import TestCase
1010
from edx_toggles.toggles.testutils import override_waffle_flag
1111
from opaque_keys.edx.keys import CourseKey
1212
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
1615
from openedx_authz.engine.enforcer import AuthzEnforcer
1716

1817
from common.djangoapps.student.admin import CourseAccessRoleHistoryAdmin
1918
from common.djangoapps.student.models import CourseAccessRoleHistory, User
19+
from common.djangoapps.student.role_helpers import get_course_roles, has_staff_roles
2020
from common.djangoapps.student.roles import (
21+
ROLE_CACHE_UNGROUPED_ROLES__KEY,
2122
AuthzCompatCourseAccessRole,
2223
CourseAccessRole,
2324
CourseBetaTesterRole,
25+
CourseDataResearcherRole,
26+
CourseFinanceAdminRole,
2427
CourseInstructorRole,
25-
CourseRole,
2628
CourseLimitedStaffRole,
27-
CourseStaffRole,
28-
CourseFinanceAdminRole,
29+
CourseRole,
2930
CourseSalesAdminRole,
30-
LibraryUserRole,
31-
CourseDataResearcherRole,
31+
CourseStaffRole,
3232
GlobalStaff,
33+
LibraryUserRole,
3334
OrgContentCreatorRole,
3435
OrgInstructorRole,
3536
OrgStaffRole,
3637
RoleCache,
3738
get_authz_compat_course_access_roles_for_user,
3839
get_role_cache_key_for_course,
39-
ROLE_CACHE_UNGROUPED_ROLES__KEY
4040
)
41-
from common.djangoapps.student.role_helpers import get_course_roles, has_staff_roles
4241
from common.djangoapps.student.tests.factories import AnonymousUserFactory, InstructorFactory, StaffFactory, UserFactory
42+
from openedx.core.djangoapps.content.course_overviews.tests.factories import CourseOverviewFactory
4343
from openedx.core.toggles import AUTHZ_COURSE_AUTHORING_FLAG
4444

4545

@@ -239,9 +239,51 @@ def test_get_orgs_for_user(self):
239239
role_second_org.add_users(self.student)
240240
assert len(role.get_orgs_for_user(self.student)) == 2
241241

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+
242284
def test_get_authz_compat_course_access_roles_for_user(self):
243285
"""
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
245287
has Libraries V2 or other non-course roles in their assignments.
246288
"""
247289
lib_assignment = RoleAssignmentData(

0 commit comments

Comments
 (0)