@@ -107,6 +107,21 @@ def setUpClass(cls):
107107 "role_name" : "library_admin" ,
108108 "scope_name" : "lib:Org3:LIB3" ,
109109 },
110+ {
111+ "subject_name" : "regular_6" ,
112+ "role_name" : "library_author" ,
113+ "scope_name" : "lib:Org3:LIB3" ,
114+ },
115+ {
116+ "subject_name" : "regular_7" ,
117+ "role_name" : "library_collaborator" ,
118+ "scope_name" : "lib:Org3:LIB3" ,
119+ },
120+ {
121+ "subject_name" : "regular_8" ,
122+ "role_name" : "library_user" ,
123+ "scope_name" : "lib:Org3:LIB3" ,
124+ },
110125 ]
111126 cls ._assign_roles_to_users (assignments = assignments )
112127
@@ -127,7 +142,7 @@ def setUpTestData(cls):
127142 """Set up test fixtures once for the entire test class."""
128143 super ().setUpTestData ()
129144 cls .create_admin_users (quantity = 3 )
130- cls .create_regular_users (quantity = 7 )
145+ cls .create_regular_users (quantity = 10 )
131146
132147 def setUp (self ):
133148 """Set up test fixtures."""
@@ -692,3 +707,40 @@ def test_get_roles_pagination(self, query_params: dict, expected_count: int, has
692707 self .assertIsNotNone (response .data ["next" ])
693708 else :
694709 self .assertIsNone (response .data ["next" ])
710+
711+ @data (
712+ # Unauthenticated
713+ (None , status .HTTP_401_UNAUTHORIZED ),
714+ # Admin user
715+ ("admin_1" , status .HTTP_200_OK ),
716+ # Library Admin user
717+ ("regular_5" , status .HTTP_200_OK ),
718+ # Library Author user
719+ # ("regular_6", status.HTTP_200_OK), # TODO: uncomment this when we have the explicit permissions
720+ # Library Collaborator user
721+ # ("regular_7", status.HTTP_200_OK), # TODO: uncomment this when we have the explicit permissions
722+ # Library User user
723+ ("regular_8" , status .HTTP_200_OK ),
724+ # Regular user without permission
725+ ("regular_9" , status .HTTP_403_FORBIDDEN ),
726+ # Non existent user
727+ ("non_existent_user" , status .HTTP_401_UNAUTHORIZED ),
728+ )
729+ @unpack
730+ def test_get_roles_permissions (self , username : str , status_code : int ):
731+ """Test retrieving roles with permissions.
732+
733+ Expected result:
734+ - Returns 401 UNAUTHORIZED status if user is not authenticated
735+ - Returns 403 FORBIDDEN status if user does not have permission
736+ - Returns 200 OK status if user has permission with correct roles with permissions and user counts
737+ """
738+ user = User .objects .filter (username = username ).first ()
739+ self .client .force_authenticate (user = user )
740+
741+ response = self .client .get (self .url , {"scope" : "lib:Org3:LIB3" })
742+
743+ self .assertEqual (response .status_code , status_code )
744+ if status_code == status .HTTP_200_OK :
745+ self .assertIn ("results" , response .data )
746+ self .assertIn ("count" , response .data )
0 commit comments