11import  pytest 
22
33from  ansible_base .lib .utils .response  import  get_relative_url 
4+ from  ansible_base .rbac .remote  import  RemoteObject 
45
56
67@pytest .mark .django_db  
78def  test_role_definition_list_remote_and_local (admin_api_client , inv_rd , foo_rd ):
9+     "Test that the role_definitions endpoint does not choke when remote permissions are listed." 
810    url  =  get_relative_url ('roledefinition-list' )
911    response  =  admin_api_client .get (url )
1012    assert  response .status_code  ==  200 
@@ -18,10 +20,8 @@ def test_role_definition_list_remote_and_local(admin_api_client, inv_rd, foo_rd)
1820
1921
2022@pytest .mark .django_db  
21- def  test_create_remote_role_definition (admin_api_client , foo_type , foo_permission ):
22-     """ 
23-     Test creation of a custom, remote role definition. 
24-     """ 
23+ def  test_create_remote_role_definition_for_remote (admin_api_client , foo_type , foo_permission ):
24+     "Test creation of a custom role definition that gives permission to remote things." 
2525    url  =  get_relative_url ("roledefinition-list" )
2626    data  =  dict (name = 'foo-foo-foo-custom' , description = 'bar' , permissions = [foo_permission .api_slug ], content_type = foo_type .api_slug )
2727    response  =  admin_api_client .post (url , data = data , format = "json" )
@@ -30,26 +30,26 @@ def test_create_remote_role_definition(admin_api_client, foo_type, foo_permissio
3030    assert  response .data ['permissions' ] ==  ['foo.foo_foo' ]
3131
3232
33- # TODO: check that assignment endpoint works 
34- 
35- # @pytest.mark.django_db 
36- # def test_give_remote_permission(rando,  foo_type, foo_permission, foo_rd): 
37- #      assert foo_type.service == 'foo'  # a place, a domain, a server, known as foo 
38- #      assert foo_type.api_slug == 'foo.foo'  # there lives a foo in foo 
39- 
40- #       assert foo_permission.api_slug == 'foo.foo_foo'  # expression of the ability that one may foo a foo 
41- 
42- #      a_foo = RemoteObject(content_type=foo_type, object_id=42) 
43- #      assignment = foo_rd.give_permission(rando, a_foo )
44- 
45- #      assignment = RoleUserAssignment.objects.get(pk=assignment.pk) 
46- #     assert isinstance(assignment.content_object, RemoteObject) 
47- 
48- #      # We can do evaluation querysets, but these can not return objects, just  id values 
49- #      assert set(foo_type.model_class().access_ids_qs(actor=rando, codename='foo')) == {(int(assignment.object_id),)} 
50- 
51- #      # Test that user-attached methods also work 
52- #       assert rando.has_obj_perm(a_foo, 'foo') 
53- #      with pytest.raises(RuntimeError) as exc: 
54- #           assert not rando.has_obj_perm(a_foo, 'bar')  # not a valid permission 
55- #       assert 'The permission bar_foo is not valid for model  foo' in str(exc) 
33+ @ pytest . mark . django_db 
34+ def   test_user_role_assignment_remote_and_local ( admin_api_client ,  rando ,  foo_type ,  foo_rd ): 
35+      "Test that after assigning permission to remote objects the assignment list works." 
36+      a_foo   =   RemoteObject ( content_type = foo_type , object_id = 42 ) 
37+     assignment   =   foo_rd . give_permission ( rando ,  a_foo ) 
38+     assignment . content_object 
39+ 
40+     assert  isinstance ( assignment . content_object ,  RemoteObject ) 
41+ 
42+     # Should show up in the assignments list 
43+     url   =   get_relative_url ( 'roleuserassignment-list' )
44+      response   =   admin_api_client . get ( url ,  format = "json" ) 
45+     assert   response . status_code   ==   200 ,  response . data 
46+ 
47+      data_by_rd   =  { item [ 'role_definition' ]:  item   for   item   in   response . data [ 'results' ]} 
48+     assert   foo_rd . id  in   data_by_rd 
49+     item   =   data_by_rd [ foo_rd . id ] 
50+      assert   item [ 'user' ]  ==   rando . id 
51+     assert   item [ 'object_id' ]  ==   str ( a_foo . object_id ) 
52+     assert  'summary_fields'   in   item 
53+     sf   =   item [ 'summary_fields' ] 
54+     assert  'content_object'   in   sf 
55+     assert  sf [ 'content_object' ]  ==  { '<remote_object_placeholder>' :  True ,  'model_name' :  'foo' ,  'service' :  ' foo',  'pk' :  42 } 
0 commit comments