26
26
from django .contrib .auth import get_user_model
27
27
from django .test import RequestFactory
28
28
from rest_framework import status
29
- from django .contrib .auth .models import Permission
30
29
31
- from rest_framework .test import APITestCase , APIClient
30
+ from rest_framework .test import APITestCase
32
31
from geonode .metadata .settings import MODEL_SCHEMA
33
32
from geonode .metadata .manager import metadata_manager
34
33
from geonode .metadata .settings import METADATA_HANDLERS
@@ -66,19 +65,6 @@ def setUp(self):
66
65
"fake_handler3" : self .handler3 ,
67
66
}
68
67
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
-
82
68
def tearDown (self ):
83
69
super ().tearDown ()
84
70
@@ -135,7 +121,8 @@ def test_schema_with_lang(self, mock_get_schema):
135
121
mock_get_schema .assert_called_once_with ("it" )
136
122
137
123
@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 ):
139
126
"""
140
127
Test schema_instance endpoint with the default lang parameter
141
128
"""
@@ -152,7 +139,8 @@ def test_get_schema_instance_with_default_lang(self, mock_build_schema_instance)
152
139
mock_build_schema_instance .assert_called ()
153
140
154
141
@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 ):
156
144
"""
157
145
Test schema_instance endpoint with specific lang parameter
158
146
"""
@@ -169,7 +157,8 @@ def test_get_schema_instance_with_lang(self, mock_build_schema_instance):
169
157
mock_build_schema_instance .assert_called_once_with (self .resource , "it" )
170
158
171
159
@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 ):
173
162
"""
174
163
Test the success case of PATCH and PUT methods of the schema_instance
175
164
"""
@@ -181,19 +170,16 @@ def test_put_patch_schema_instance_with_no_errors(self, mock_update_schema_insta
181
170
errors = {}
182
171
mock_update_schema_instance .return_value = errors
183
172
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 )
194
179
195
180
@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 ):
197
183
"""
198
184
Test the PATCH and PUT methods of the schema_instance in case of errors
199
185
"""
@@ -205,19 +191,16 @@ def test_put_patch_schema_instance_with_errors(self, mock_update_schema_instance
205
191
errors = {"fake_error_1" : "Field 'title' is required" , "fake_error_2" : "Invalid value for 'type'" }
206
192
mock_update_schema_instance .return_value = errors
207
193
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 )
219
201
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 ):
221
204
"""
222
205
Test case that the resource does not exist
223
206
"""
0 commit comments