Skip to content

Commit d762a84

Browse files
test: add tests for users and data modules
1 parent 947d1cc commit d762a84

2 files changed

Lines changed: 247 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""Test data for the authorization API."""
2+
3+
from ddt import data, ddt, unpack
4+
from django.test import TestCase
5+
6+
from openedx_authz.api.data import (
7+
ActionData,
8+
ContentLibraryData,
9+
RoleData,
10+
ScopeData,
11+
UserData,
12+
)
13+
14+
15+
@ddt
16+
class TestNamespacedData(TestCase):
17+
"""Test data for the authorization API."""
18+
19+
@data(
20+
("instructor", "role@instructor"),
21+
("admin", "role@admin"),
22+
)
23+
@unpack
24+
def test_role_data_namespace(self, input, expected):
25+
"""Test that RoleData correctly namespaces role names.
26+
27+
Expected Result:
28+
- If input is 'instructor', expected is 'role@instructor'
29+
- If input is 'admin', expected is 'role@admin'
30+
"""
31+
role = RoleData(name=input)
32+
self.assertEqual(role.role_id, expected)
33+
34+
@data(
35+
("john_doe", "user@john_doe"),
36+
("jane_smith", "user@jane_smith"),
37+
)
38+
@unpack
39+
def test_user_data_namespace(self, username, expected):
40+
"""Test that UserData correctly namespaces user IDs.
41+
42+
Expected Result:
43+
- If input is 'john_doe', expected is 'user@john_doe'
44+
- If input is 'jane_smith', expected is 'user@jane_smith'
45+
"""
46+
user = UserData(username=username)
47+
self.assertEqual(user.subject_id, expected)
48+
49+
@data(
50+
("read", "act@read"),
51+
("write", "act@write"),
52+
)
53+
@unpack
54+
def test_action_data_namespace(self, action_name, expected):
55+
"""Test that ActionData correctly namespaces action IDs.
56+
57+
Expected Result:
58+
- If input is 'read', expected is 'act@read'
59+
- If input is 'write', expected is 'act@write'
60+
"""
61+
action = ActionData(name=action_name)
62+
self.assertEqual(action.action_id, expected)
63+
64+
@data(
65+
("lib:DemoX:CSPROB", "lib@lib:demox:csprob"),
66+
)
67+
@unpack
68+
def test_scope_content_lib_data_namespace(self, library_id, expected):
69+
"""Test that ScopeData correctly namespaces scope IDs.
70+
71+
Expected Result:
72+
- If input is 'lib:DemoX:CSPROB', expected is 'lib@lib:DemoX:CSPROB'
73+
"""
74+
scope = ContentLibraryData(library_id=library_id)
75+
self.assertEqual(scope.scope_id, expected)
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
"""Test suite for user-role assignment API functions."""
2+
3+
from ddt import data, ddt, unpack
4+
5+
from openedx_authz.api.data import RoleData, ScopeData, UserData
6+
from openedx_authz.api.users import (
7+
assign_role_to_user_in_scope,
8+
batch_assign_role_to_users,
9+
batch_unassign_role_from_users,
10+
get_user_role_assignments,
11+
get_user_role_assignments_for_role_in_scope,
12+
get_user_role_assignments_in_scope,
13+
unassign_role_from_user,
14+
)
15+
from openedx_authz.tests.api.test_roles import RolesTestSetupMixin
16+
17+
18+
@ddt
19+
class TestUserRoleAssignments(RolesTestSetupMixin):
20+
"""Test suite for user-role assignment API functions."""
21+
22+
@classmethod
23+
def _assign_roles_to_users(
24+
cls,
25+
assignments: list[dict] | None = None,
26+
):
27+
"""Helper method to assign roles to multiple users.
28+
29+
This method can be used to assign a role to a single user or multiple users
30+
in a specific scope. It can also handle batch assignments.
31+
32+
Args:
33+
assignments (list of dict): List of assignment dictionaries, each containing:
34+
- subject (str): ID of the user namespaced (e.g., 'user:john_doe').
35+
- role_id (str): Name of the role to assign.
36+
- scope (str): Scope in which to assign the role.
37+
"""
38+
if assignments:
39+
for assignment in assignments:
40+
assign_role_to_user_in_scope(
41+
assignment["subject_name"],
42+
assignment["role_name"],
43+
assignment["scope_name"],
44+
)
45+
46+
@data(
47+
("john", "library_admin", "math_101", False),
48+
("jane", "library_user", "english_101", False),
49+
(["mary", "charlie"], "library_collaborator", "science_301", True),
50+
(["david", "sarah"], "library_author", "history_201", True),
51+
)
52+
@unpack
53+
def test_assign_role_to_user_in_scope(self, username, role, scope_name, batch):
54+
"""Test assigning a role to a user in a specific scope.
55+
56+
Expected result:
57+
- The role is successfully assigned to the user in the specified scope.
58+
"""
59+
if batch:
60+
batch_assign_role_to_users(
61+
users=username, role_name=role, scope=scope_name
62+
)
63+
for user in username:
64+
user_roles = get_user_role_assignments_in_scope(
65+
username=user, scope=scope_name
66+
)
67+
role_names = {assignment.role.name for assignment in user_roles}
68+
self.assertIn(role, role_names)
69+
else:
70+
assign_role_to_user_in_scope(
71+
username=username, role_name=role, scope=scope_name
72+
)
73+
user_roles = get_user_role_assignments_in_scope(
74+
username=username, scope=scope_name
75+
)
76+
role_names = {assignment.role.name for assignment in user_roles}
77+
self.assertIn(role, role_names)
78+
79+
@data(
80+
(["Grace", "Henry"], "library_collaborator", "math_advanced", True),
81+
(["Liam", "Maya"], "library_author", "art_101", True),
82+
("Alice", "library_admin", "math_101", False),
83+
("Bob", "library_author", "history_201", False),
84+
)
85+
@unpack
86+
def test_unassign_role_from_user(self, username, role, scope_name, batch):
87+
"""Test unassigning a role from a user in a specific scope.
88+
89+
Expected result:
90+
- The role is successfully unassigned from the user in the specified scope.
91+
- The user no longer has the role in the specified scope.
92+
"""
93+
if batch:
94+
batch_unassign_role_from_users(
95+
users=username, role_name=role, scope=scope_name
96+
)
97+
for user in username:
98+
user_roles = get_user_role_assignments_in_scope(
99+
username=user, scope=scope_name
100+
)
101+
role_names = {assignment.role.name for assignment in user_roles}
102+
self.assertNotIn(role, role_names)
103+
else:
104+
unassign_role_from_user(
105+
user=username, role_name=role, scope=scope_name
106+
)
107+
user_roles = get_user_role_assignments_in_scope(
108+
username=username, scope=scope_name
109+
)
110+
role_names = {assignment.role.name for assignment in user_roles}
111+
self.assertNotIn(role, role_names)
112+
113+
@data(
114+
("Eve", {"library_admin", "library_author", "library_user"}),
115+
("Alice", {"library_admin"}),
116+
("Liam", {"library_author"}),
117+
)
118+
@unpack
119+
def test_get_user_role_assignments(self, username, expected_roles):
120+
"""Test retrieving all role assignments for a user across all scopes.
121+
122+
Expected result:
123+
- All roles assigned to the user across all scopes are correctly retrieved.
124+
- Each assigned role is present in the returned role assignments.
125+
"""
126+
role_assignments = get_user_role_assignments(username=username)
127+
print(role_assignments)
128+
129+
assigned_role_names = {assignment.role.name for assignment in role_assignments}
130+
self.assertEqual(assigned_role_names, expected_roles)
131+
132+
@data(
133+
("Alice", "math_101", {"library_admin"}),
134+
("Bob", "history_201", {"library_author"}),
135+
("Eve", "physics_401", {"library_admin"}),
136+
("Grace", "math_advanced", {"library_collaborator"}),
137+
)
138+
@unpack
139+
def test_get_user_role_assignments_in_scope(self, username, scope_name, expected_roles):
140+
"""Test retrieving role assignments for a user within a specific scope.
141+
142+
Expected result:
143+
- The role assigned to the user in the specified scope is correctly retrieved.
144+
- The returned role assignments contain the assigned role.
145+
"""
146+
user_roles = get_user_role_assignments_in_scope(
147+
username=username, scope=scope_name
148+
)
149+
150+
role_names = {assignment.role.name for assignment in user_roles}
151+
self.assertEqual(role_names, expected_roles)
152+
153+
154+
@data(
155+
("library_admin", "math_101", {"Alice", "Eve"}),
156+
("library_author", "history_201", {"Bob"}),
157+
("library_collaborator", "math_advanced", {"Grace"}),
158+
)
159+
@unpack
160+
def test_get_user_role_assignments_for_role_in_scope(self, role_name, scope_name, expected_users):
161+
"""Test retrieving all users assigned to a specific role within a specific scope.
162+
163+
Expected result:
164+
- All users assigned to the role in the specified scope are correctly retrieved.
165+
- Each assigned user is present in the returned user assignments.
166+
"""
167+
user_assignments = get_user_role_assignments_for_role_in_scope(
168+
role_name=role_name, scope=scope_name
169+
)
170+
171+
assigned_usernames = {assignment.subject.username for assignment in user_assignments}
172+
self.assertEqual(assigned_usernames, expected_users)

0 commit comments

Comments
 (0)