@@ -1053,6 +1053,111 @@ def test_user_group_check_serializer_counter_does_not_exist(self):
10531053 },
10541054 )
10551055
1056+ def _get_admin_auth_header (self ):
1057+ """Helper method to get admin authentication header"""
1058+ login_payload = {"username" : "admin" , "password" : "tester" }
1059+ login_url = reverse ("radius:user_auth_token" , args = [self .default_org .slug ])
1060+ login_response = self .client .post (login_url , data = login_payload )
1061+ return f"Bearer { login_response .json ()['key' ]} "
1062+
1063+ def test_batch_update_organization_readonly (self ):
1064+ """
1065+ Test that organization field is readonly when updating RadiusBatch objects
1066+ """
1067+ data = self ._radius_batch_prefix_data ()
1068+ response = self ._radius_batch_post_request (data )
1069+ self .assertEqual (response .status_code , 201 )
1070+ batch = RadiusBatch .objects .get ()
1071+ original_org = batch .organization
1072+
1073+ new_org = self ._create_org (** {"name" : "new-org" , "slug" : "new-org" })
1074+
1075+ header = self ._get_admin_auth_header ()
1076+
1077+ url = reverse ("radius:batch_detail" , args = [batch .pk ])
1078+ update_data = {
1079+ "name" : "updated-batch-name" ,
1080+ "organization" : str (new_org .pk ),
1081+ }
1082+ response = self .client .patch (
1083+ url ,
1084+ json .dumps (update_data ),
1085+ HTTP_AUTHORIZATION = header ,
1086+ content_type = "application/json"
1087+ )
1088+ self .assertEqual (response .status_code , 200 )
1089+
1090+ batch .refresh_from_db ()
1091+ self .assertEqual (batch .organization , original_org )
1092+ self .assertEqual (batch .name , "updated-batch-name" )
1093+
1094+ def test_batch_retrieve_and_update_api (self ):
1095+ """
1096+ Test retrieving and updating RadiusBatch objects via API
1097+ """
1098+ data = self ._radius_batch_prefix_data ()
1099+ response = self ._radius_batch_post_request (data )
1100+ self .assertEqual (response .status_code , 201 )
1101+ batch = RadiusBatch .objects .get ()
1102+
1103+ header = self ._get_admin_auth_header ()
1104+
1105+ url = reverse ("radius:batch_detail" , args = [batch .pk ])
1106+ response = self .client .get (url , HTTP_AUTHORIZATION = header )
1107+ self .assertEqual (response .status_code , 200 )
1108+ self .assertEqual (response .data ["name" ], batch .name )
1109+ self .assertEqual (str (response .data ["organization" ]), str (batch .organization .pk ))
1110+
1111+ update_data = {
1112+ "name" : "updated-batch-name" ,
1113+ "strategy" : "prefix" ,
1114+ "prefix" : batch .prefix ,
1115+ "organization_slug" : batch .organization .slug ,
1116+ }
1117+ response = self .client .put (
1118+ url ,
1119+ json .dumps (update_data ),
1120+ HTTP_AUTHORIZATION = header ,
1121+ content_type = "application/json"
1122+ )
1123+ self .assertEqual (response .status_code , 200 )
1124+ batch .refresh_from_db ()
1125+ self .assertEqual (batch .name , "updated-batch-name" )
1126+
1127+ def test_batch_update_permissions (self ):
1128+ """
1129+ Test that proper permissions are required for updating RadiusBatch objects
1130+ """
1131+ data = self ._radius_batch_prefix_data ()
1132+ response = self ._radius_batch_post_request (data )
1133+ self .assertEqual (response .status_code , 201 )
1134+ batch = RadiusBatch .objects .get ()
1135+
1136+ url = reverse ("radius:batch_detail" , args = [batch .pk ])
1137+
1138+ response = self .client .patch (url , {"name" : "new-name" })
1139+ self .assertEqual (response .status_code , 401 )
1140+
1141+ user = self ._get_user ()
1142+ user_token = Token .objects .create (user = user )
1143+ header = f"Bearer { user_token .key } "
1144+ response = self .client .patch (
1145+ url ,
1146+ json .dumps ({"name" : "new-name" }),
1147+ HTTP_AUTHORIZATION = header ,
1148+ content_type = "application/json"
1149+ )
1150+ self .assertEqual (response .status_code , 403 )
1151+
1152+ header = self ._get_admin_auth_header ()
1153+ response = self .client .patch (
1154+ url ,
1155+ json .dumps ({"name" : "new-name" }),
1156+ HTTP_AUTHORIZATION = header ,
1157+ content_type = "application/json"
1158+ )
1159+ self .assertEqual (response .status_code , 200 )
1160+
10561161
10571162class TestTransactionApi (AcctMixin , ApiTokenMixin , BaseTransactionTestCase ):
10581163 def test_user_radius_usage_view (self ):
0 commit comments