@@ -858,6 +858,20 @@ def test_put_accepts_valid_full_course_key_scope(self, _mock_exists, _mock_assig
858858class TestOrgsAPIView (ViewTestMixin ):
859859 """Test suite for OrgsAPIView."""
860860
861+ @classmethod
862+ def setUpClass (cls ):
863+ """Assign a course role to regular_9 for COURSES_VIEW_COURSE_TEAM permission tests."""
864+ super ().setUpClass ()
865+ cls ._assign_roles_to_users (
866+ [
867+ {
868+ "subject_name" : "regular_9" ,
869+ "role_name" : roles .COURSE_STAFF .external_key ,
870+ "scope_name" : "course-v1:Org1+COURSE1+2024" ,
871+ },
872+ ]
873+ )
874+
861875 @classmethod
862876 def setUpTestData (cls ):
863877 """Create Organization fixtures."""
@@ -968,6 +982,59 @@ def test_get_orgs_excludes_inactive(self):
968982 result_names = [org ["name" ] for org in response .data ["results" ]]
969983 self .assertNotIn ("Inactive Org" , result_names )
970984
985+ @data (
986+ # Only VIEW_LIBRARY_TEAM (library_user role in a lib scope)
987+ ("regular_1" , status .HTTP_200_OK ),
988+ # Only COURSES_VIEW_COURSE_TEAM (course_staff role in a course scope)
989+ ("regular_9" , status .HTTP_200_OK ),
990+ # No relevant permissions
991+ ("regular_10" , status .HTTP_403_FORBIDDEN ),
992+ # Superuser
993+ ("admin_1" , status .HTTP_200_OK ),
994+ )
995+ @unpack
996+ def test_get_orgs_permissions (self , username : str , expected_status : int ):
997+ """Test access control for OrgsAPIView.
998+
999+ Test cases:
1000+ - User with only VIEW_LIBRARY_TEAM (via library role): allowed
1001+ - User with only COURSES_VIEW_COURSE_TEAM (via course role): allowed
1002+ - User with neither permission: forbidden
1003+ - Superuser/staff: allowed
1004+
1005+ Expected result:
1006+ - Returns appropriate status code based on user permissions
1007+ """
1008+ user = User .objects .get (username = username )
1009+ self .client .force_authenticate (user = user )
1010+
1011+ response = self .client .get (self .url )
1012+
1013+ self .assertEqual (response .status_code , expected_status )
1014+
1015+ def test_get_orgs_user_with_both_permissions_allowed (self ):
1016+ """Test that a user with both VIEW_LIBRARY_TEAM and COURSES_VIEW_COURSE_TEAM can access the endpoint.
1017+
1018+ Expected result:
1019+ - Returns 200 OK status
1020+ """
1021+ # regular_1 has library_user (VIEW_LIBRARY_TEAM); assign a course role too
1022+ self ._assign_roles_to_users (
1023+ [
1024+ {
1025+ "subject_name" : "regular_1" ,
1026+ "role_name" : roles .COURSE_STAFF .external_key ,
1027+ "scope_name" : "course-v1:Org1+COURSE1+2024" ,
1028+ },
1029+ ]
1030+ )
1031+ user = User .objects .get (username = "regular_1" )
1032+ self .client .force_authenticate (user = user )
1033+
1034+ response = self .client .get (self .url )
1035+
1036+ self .assertEqual (response .status_code , status .HTTP_200_OK )
1037+
9711038 def test_get_orgs_unauthenticated (self ):
9721039 """Test that unauthenticated requests are rejected.
9731040
0 commit comments