11from django .contrib .auth import get_user_model
22from django .contrib .auth .models import Permission
3- from django .test import TestCase
43from django .urls import reverse
54from swapper import load_model
65
76from openwisp_users .api .throttling import AuthRateThrottle
7+ from openwisp_users .tests .test_api import APITestCase
88
9+ from .. import CreateMixin
910from ..models import Template
10- from .mixins import TestMultitenancyMixin
1111
1212User = get_user_model ()
1313Group = load_model ('openwisp_users' , 'Group' )
1414OrganizationUser = load_model ('openwisp_users' , 'OrganizationUser' )
1515
1616
17- class TestPermissionClasses (TestMultitenancyMixin , TestCase ):
17+ class TestPermissionClasses (CreateMixin , APITestCase ):
1818 def setUp (self ):
1919 AuthRateThrottle .rate = 0
2020 self .template_model = Template
@@ -231,114 +231,39 @@ def test_view_django_model_permission_with_change_perm(self):
231231 )
232232 self .assertEqual (response .status_code , 200 )
233233
234- def _test_access_shared_object (
235- self , token , expected_templates_count = 1 , expected_status_codes = {}
236- ):
237- auth = dict (HTTP_AUTHORIZATION = f'Bearer { token } ' )
238- template = self ._create_template (organization = None )
239-
240- with self .subTest ('Test listing templates' ):
241- response = self .client .get (reverse ('test_template_list' ), ** auth )
242- data = response .data .copy ()
243- # Only check "templates" in response.
244- if isinstance (data , dict ):
245- data .pop ('detail' , None )
246- self .assertEqual (response .status_code , expected_status_codes ['list' ])
247- self .assertEqual (len (data ), expected_templates_count )
248-
249- with self .subTest ('Test creating template' ):
250- response = self .client .post (
251- reverse ('test_template_list' ),
252- data = {'name' : 'Test Template' , 'organization' : None },
253- content_type = 'application/json' ,
254- ** auth ,
255- )
256- self .assertEqual (response .status_code , expected_status_codes ['create' ])
257- if expected_status_codes ['create' ] == 400 :
258- self .assertEqual (
259- str (response .data ['organization' ][0 ]), 'This field may not be null.'
260- )
261-
262- with self .subTest ('Test retreiving template' ):
263- response = self .client .get (
264- reverse ('test_template_detail' , args = [template .id ]), ** auth
265- )
266- self .assertEqual (response .status_code , expected_status_codes ['retrieve' ])
267-
268- with self .subTest ('Test updating template' ):
269- response = self .client .put (
270- reverse ('test_template_detail' , args = [template .id ]),
271- data = {'name' : 'Name changed' },
272- content_type = 'application/json' ,
273- ** auth ,
274- )
275- self .assertEqual (response .status_code , expected_status_codes ['update' ])
276-
277- with self .subTest ('Test deleting template' ):
278- response = self .client .delete (
279- reverse ('test_template_detail' , args = [template .id ]), ** auth
280- )
281- self .assertEqual (response .status_code , expected_status_codes ['delete' ])
282-
283- with self .subTest ('Test HEAD and OPTION methods' ):
284- response = self .client .head (reverse ('test_template_list' ), ** auth )
285- self .assertEqual (response .status_code , expected_status_codes ['head' ])
286-
287- response = self .client .options (reverse ('test_template_list' ), ** auth )
288- self .assertEqual (response .status_code , expected_status_codes ['option' ])
289-
290234 def test_superuser_access_shared_object (self ):
291- superuser = self ._get_admin ()
292- token = self ._obtain_auth_token (username = superuser )
293- self ._test_access_shared_object (
294- token ,
295- expected_status_codes = {
296- 'create' : 201 ,
297- 'list' : 200 ,
298- 'retrieve' : 200 ,
299- 'update' : 200 ,
300- 'delete' : 204 ,
301- 'head' : 200 ,
302- 'option' : 200 ,
303- },
235+ self ._test_superuser_access_shared_object (
236+ token = None ,
237+ listview_name = 'test_template_list' ,
238+ detailview_name = 'test_template_detail' ,
239+ create_payload = {'name' : 'test' , 'organization' : '' },
240+ update_payload = {'name' : 'updated-test' },
241+ expected_count = 1 ,
304242 )
305243
306244 def test_org_manager_access_shared_object (self ):
307- org_manager = self ._create_administrator ()
308- token = self ._obtain_auth_token (username = org_manager )
309- # First user is automatically owner, so created dummy
310- # user to keep operator as manager only.
311- self ._create_org_user (user = self ._get_user (), is_admin = True )
312- self ._create_org_user (user = org_manager , is_admin = True )
313- self ._test_access_shared_object (
314- token ,
315- expected_status_codes = {
316- 'create' : 400 ,
317- 'list' : 200 ,
318- 'retrieve' : 200 ,
319- 'update' : 403 ,
320- 'delete' : 403 ,
321- 'head' : 200 ,
322- 'option' : 200 ,
323- },
245+ template = self ._create_template (organization = None )
246+ self ._test_org_user_access_shared_object (
247+ listview_path = reverse ('test_template_list' ),
248+ detailview_path = reverse ('test_template_detail' , args = [template .pk ]),
249+ create_payload = {'name' : 'test' , 'organization' : '' },
250+ update_payload = {'name' : 'updated-test' },
251+ expected_count = 1 ,
324252 )
325253
326254 def test_org_owner_access_shared_object (self ):
327255 # The first admin of an organization automatically
328256 # becomes organization owner.
329257 org_owner = self ._create_administrator (organizations = [self ._get_org ()])
330258 token = self ._obtain_auth_token (username = org_owner )
331- self ._test_access_shared_object (
332- token ,
333- expected_status_codes = {
334- 'create' : 400 ,
335- 'list' : 200 ,
336- 'retrieve' : 200 ,
337- 'update' : 403 ,
338- 'delete' : 403 ,
339- 'head' : 200 ,
340- 'option' : 200 ,
341- },
259+ template = self ._create_template (organization = None )
260+ self ._test_org_user_access_shared_object (
261+ listview_path = reverse ('test_template_list' ),
262+ detailview_path = reverse ('test_template_detail' , args = [template .pk ]),
263+ create_payload = {'name' : 'test' , 'organization' : '' },
264+ update_payload = {'name' : 'updated-test' },
265+ expected_count = 1 ,
266+ token = token ,
342267 )
343268
344269 def test_org_user_access_shared_object (self ):
@@ -348,9 +273,14 @@ def test_org_user_access_shared_object(self):
348273 user = self ._create_administrator ()
349274 token = self ._obtain_auth_token (username = user )
350275 self ._create_org_user (user = user , is_admin = False )
351- self ._test_access_shared_object (
352- token ,
353- expected_templates_count = 0 ,
276+ template = self ._create_template (organization = None )
277+ self ._test_org_user_access_shared_object (
278+ listview_path = reverse ('test_template_list' ),
279+ detailview_path = reverse ('test_template_detail' , args = [template .pk ]),
280+ create_payload = {'name' : 'test' , 'organization' : '' },
281+ update_payload = {'name' : 'updated-test' },
282+ expected_count = 0 ,
283+ token = token ,
354284 expected_status_codes = {
355285 'create' : 400 ,
356286 'list' : 200 ,
0 commit comments