11"""Integration tests for openedx_authz views."""
22
33
4- from django .test import TestCase
4+ import uuid
5+ from urllib .parse import urlencode
6+
7+ import pytest
8+ from django .test import TestCase , override_settings
59from django .urls import reverse
610from rest_framework import status
711from rest_framework .test import APIClient
812from django .contrib .auth import get_user_model
913
14+ from openedx_authz .api .users import assign_role_to_user_in_scope
1015from openedx_authz .models .core import ExtendedCasbinRule
1116from openedx_authz .tests .integration .test_models import create_test_library
1217
1318
1419User = get_user_model ()
1520
21+
22+ @pytest .mark .integration
23+ @override_settings (ROOT_URLCONF = "openedx_authz.urls" )
1624class TestRoleAssignmentView (TestCase ):
1725 """Tests for the role assignment view."""
1826
1927 def setUp (self ):
2028 """Set up the test client and any required data."""
2129 self .client = APIClient ()
22- self .url = reverse ("openedx_authz:role-assignment " )
30+ self .url = reverse ("openedx_authz:role-user-list " )
2331 self .library_metadata , self .library_key , self .content_library = create_test_library ("TestOrg" )
2432 self .role_key = "library_admin"
25- # Create User
26- self .user_data = {
27- "username" : "test_user" ,
28- 29- }
30- self .user = User .objects .create_user (** self .user_data )
33+
34+ # Create random users to avoid conflicts in persistent database
35+ unique_id = uuid .uuid4 ().hex [:8 ]
36+ self .user = User .objects .create_user (
37+ username = f"test_user_{ unique_id } " ,
38+ email = f"test_{ unique_id } @example.com"
39+ )
40+ self .admin_user = User .objects .create_user (
41+ username = f"admin_user_{ unique_id } " ,
42+ email = f"admin_{ unique_id } @example.com" ,
43+ is_staff = True ,
44+ is_superuser = True
45+ )
46+
47+ assign_role_to_user_in_scope (
48+ user_external_key = self .admin_user .username ,
49+ role_external_key = self .role_key ,
50+ scope_external_key = str (self .library_key )
51+ )
52+ self .client .force_authenticate (user = self .admin_user )
3153
3254 def test_role_assignment_with_extended_model (self ):
3355 """Test role assignment when ExtendedCasbinRule model is in use.
3456
3557 Expected Results:
36- - Role assignment is successful (HTTP 201 Created ).
58+ - Role assignment is successful (HTTP 207 Multi-Status ).
3759 - An ExtendedCasbinRule is created with the correct scope and subject.
3860 """
3961 payload = {
40- "user " : self .user .username ,
62+ "users " : [ self .user .username ] ,
4163 "role" : self .role_key ,
42- "scope" : self .library_key ,
64+ "scope" : str ( self .library_key ) ,
4365 }
4466
45- response = self .client .post (self .url , payload , format = 'json' )
67+ response = self .client .put (self .url , payload , format = 'json' )
4668
47- self .assertEqual (response .status_code , status .HTTP_201_CREATED )
48- self .assertIn ( "role_assignment_id" , response . data )
69+ self .assertEqual (response .status_code , status .HTTP_207_MULTI_STATUS )
70+ self .assertEqual ( len ( response . data [ "completed" ]), 1 )
4971
5072 extended_rule = ExtendedCasbinRule .objects .filter (
51- subject__user = self .user ,
52- scope__content_library = self .content_library ,
73+ subject__usersubject__user = self .user ,
74+ scope__contentlibraryscope__content_library = self .content_library ,
5375 ).first ()
5476 self .assertIsNotNone (extended_rule )
5577 self .assertIn (payload ["role" ], extended_rule .casbin_rule_key )
@@ -58,26 +80,31 @@ def test_role_unassignment_with_extended_model(self):
5880 """Test role unassignment when ExtendedCasbinRule model is in use.
5981
6082 Expected Results:
61- - Role unassignment is successful (HTTP 204 No Content ).
83+ - Role unassignment is successful (HTTP 207 Multi-Status ).
6284 - The associated ExtendedCasbinRule is deleted.
6385 - No orphaned ExtendedCasbinRule remains after unassignment.
6486 """
6587 payload = {
66- "user " : self .user .username ,
88+ "users " : [ self .user .username ] ,
6789 "role" : self .role_key ,
68- "scope" : self .library_key ,
90+ "scope" : str ( self .library_key ) ,
6991 }
70- create_response = self .client .post (self .url , payload , format = 'json' )
71- self .assertEqual (create_response .status_code , status .HTTP_201_CREATED )
72- role_assignment_id = create_response .data ["role_assignment_id" ]
92+ create_response = self .client .put (self .url , payload , format = 'json' )
93+ self .assertEqual (create_response .status_code , status .HTTP_207_MULTI_STATUS )
94+ self . assertEqual ( len ( create_response .data ["completed" ]), 1 )
7395
74- unassign_url = reverse ("openedx_authz:role-unassignment" , args = [role_assignment_id ])
75- unassign_response = self .client .delete (unassign_url )
96+ delete_params = {
97+ "role" : self .role_key ,
98+ "scope" : str (self .library_key ),
99+ "users" : self .user .username ,
100+ }
101+ unassign_response = self .client .delete (f"{ self .url } ?{ urlencode (delete_params )} " )
76102
77- self .assertEqual (unassign_response .status_code , status .HTTP_204_NO_CONTENT )
103+ self .assertEqual (unassign_response .status_code , status .HTTP_207_MULTI_STATUS )
104+ self .assertEqual (len (unassign_response .data ["completed" ]), 1 )
78105
79106 extended_rule = ExtendedCasbinRule .objects .filter (
80- subject__user = self .user ,
81- scope__content_library__id = self .content_library . id ,
107+ subject__usersubject__user = self .user ,
108+ scope__contentlibraryscope__content_library = self .content_library ,
82109 ).first ()
83110 self .assertIsNone (extended_rule )
0 commit comments