|
| 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