2626from django .contrib .auth import get_user_model
2727from django .test import RequestFactory
2828from rest_framework import status
29- from django .contrib .auth .models import Permission
3029
31- from rest_framework .test import APITestCase , APIClient
30+ from rest_framework .test import APITestCase
3231from geonode .metadata .settings import MODEL_SCHEMA
3332from geonode .metadata .manager import metadata_manager
3433from geonode .metadata .settings import METADATA_HANDLERS
@@ -66,19 +65,6 @@ def setUp(self):
6665 "fake_handler3" : self .handler3 ,
6766 }
6867
69- # Assign necessary permissions to the user
70- permissions = [
71- "base.view_resourcebase" ,
72- "change_resourcebase_metadata" ,
73- ]
74- for perm in permissions :
75- app_label , codename = perm .split ("." )
76- permission = Permission .objects .get (content_type__app_label = app_label , codename = codename )
77- self .test_user_1 .user_permissions .add (permission )
78-
79- self .client = APIClient ()
80- self .client .force_login (self .test_user_1 )
81-
8268 def tearDown (self ):
8369 super ().tearDown ()
8470
@@ -135,7 +121,8 @@ def test_schema_with_lang(self, mock_get_schema):
135121 mock_get_schema .assert_called_once_with ("it" )
136122
137123 @patch ("geonode.metadata.manager.metadata_manager.build_schema_instance" )
138- def test_get_schema_instance_with_default_lang (self , mock_build_schema_instance ):
124+ @patch ("geonode.base.api.permissions.UserHasPerms.has_permission" , return_value = True )
125+ def test_get_schema_instance_with_default_lang (self , mock_has_permission , mock_build_schema_instance ):
139126 """
140127 Test schema_instance endpoint with the default lang parameter
141128 """
@@ -152,7 +139,8 @@ def test_get_schema_instance_with_default_lang(self, mock_build_schema_instance)
152139 mock_build_schema_instance .assert_called ()
153140
154141 @patch ("geonode.metadata.manager.metadata_manager.build_schema_instance" )
155- def test_get_schema_instance_with_lang (self , mock_build_schema_instance ):
142+ @patch ("geonode.base.api.permissions.UserHasPerms.has_permission" , return_value = True )
143+ def test_get_schema_instance_with_lang (self , mock_has_permission , mock_build_schema_instance ):
156144 """
157145 Test schema_instance endpoint with specific lang parameter
158146 """
@@ -169,7 +157,8 @@ def test_get_schema_instance_with_lang(self, mock_build_schema_instance):
169157 mock_build_schema_instance .assert_called_once_with (self .resource , "it" )
170158
171159 @patch ("geonode.metadata.manager.metadata_manager.update_schema_instance" )
172- def test_put_patch_schema_instance_with_no_errors (self , mock_update_schema_instance ):
160+ @patch ("geonode.base.api.permissions.UserHasPerms.has_permission" , return_value = True )
161+ def test_put_patch_schema_instance_with_no_errors (self , mock_has_permission , mock_update_schema_instance ):
173162 """
174163 Test the success case of PATCH and PUT methods of the schema_instance
175164 """
@@ -181,19 +170,16 @@ def test_put_patch_schema_instance_with_no_errors(self, mock_update_schema_insta
181170 errors = {}
182171 mock_update_schema_instance .return_value = errors
183172
184- methods = [self .client .put , self .client .patch ]
185-
186- for method in methods :
187-
188- response = method (url , data = fake_payload , format = "json" )
189- self .assertEqual (response .status_code , status .HTTP_200_OK )
190- self .assertJSONEqual (
191- response .content , {"message" : "The resource was updated successfully" , "extraErrors" : errors }
192- )
193- mock_update_schema_instance .assert_called_with (self .resource , fake_payload )
173+ response = self .client .put (url , data = fake_payload , format = "json" )
174+ self .assertEqual (response .status_code , status .HTTP_200_OK )
175+ self .assertJSONEqual (
176+ response .content , {"message" : "The resource was updated successfully" , "extraErrors" : errors }
177+ )
178+ mock_update_schema_instance .assert_called_with (self .resource , fake_payload )
194179
195180 @patch ("geonode.metadata.manager.metadata_manager.update_schema_instance" )
196- def test_put_patch_schema_instance_with_errors (self , mock_update_schema_instance ):
181+ @patch ("geonode.base.api.permissions.UserHasPerms.has_permission" , return_value = True )
182+ def test_put_patch_schema_instance_with_errors (self , mock_has_permission , mock_update_schema_instance ):
197183 """
198184 Test the PATCH and PUT methods of the schema_instance in case of errors
199185 """
@@ -205,19 +191,16 @@ def test_put_patch_schema_instance_with_errors(self, mock_update_schema_instance
205191 errors = {"fake_error_1" : "Field 'title' is required" , "fake_error_2" : "Invalid value for 'type'" }
206192 mock_update_schema_instance .return_value = errors
207193
208- methods = [self .client .put , self .client .patch ]
209-
210- for method in methods :
211-
212- response = method (url , data = fake_payload , format = "json" )
213- self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
214- self .assertJSONEqual (
215- response .content ,
216- {"message" : "Some errors were found while updating the resource" , "extraErrors" : errors },
217- )
218- mock_update_schema_instance .assert_called_with (self .resource , fake_payload )
194+ response = self .client .put (url , data = fake_payload , format = "json" )
195+ self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
196+ self .assertJSONEqual (
197+ response .content ,
198+ {"message" : "Some errors were found while updating the resource" , "extraErrors" : errors },
199+ )
200+ mock_update_schema_instance .assert_called_with (self .resource , fake_payload )
219201
220- def test_resource_not_found (self ):
202+ @patch ("geonode.base.api.permissions.UserHasPerms.has_permission" , return_value = True )
203+ def test_resource_not_found (self , mock_has_permission ):
221204 """
222205 Test case that the resource does not exist
223206 """
0 commit comments