@@ -72,3 +72,74 @@ def test_organization_permission_remote_object(rando, foo_type, organization):
7272 org_foo_rd .give_permission (rando , organization )
7373
7474 assert rando .has_obj_perm (a_foo , 'foo' )
75+
76+
77+ @pytest .mark .django_db
78+ def test_object_roles_same_type_different_service (rando ):
79+ cts = {}
80+ rds = {}
81+ foos = {}
82+ for service_name in ('barland' , 'fooland' ):
83+ # same-named model in both services
84+ ct = DABContentType .objects .create (service = service_name , model = 'foo' , app_label = 'foo' )
85+ cts [service_name ] = ct
86+ permissions = []
87+ for codename in ('view_foo' , 'change_foo' , 'foo_foo' ):
88+ permissions .append (DABPermission .objects .create (codename = codename , content_type = ct ))
89+ # NOTE: obviously we have to use the full api_slug of permission, as codename would be ambiguous
90+ rd = RoleDefinition .objects .create_from_permissions (
91+ name = f'The foo role for { service_name } service' , permissions = [perm .api_slug for perm in permissions ], content_type = ct
92+ )
93+ rds [service_name ] = rd
94+ foos [service_name ] = RemoteObject (content_type = ct , object_id = 4 )
95+
96+ for service_name in ('barland' , 'fooland' ):
97+ # Nothing assigned yet, rando has no permission to fooland or barland
98+ assert [rando .has_obj_perm (foos [this_service_name ], 'change' ) for this_service_name in ('barland' , 'fooland' )] == [False , False ]
99+
100+ rds [service_name ].give_permission (rando , foos [service_name ])
101+
102+ # Just has permission to either fooland or barland, according to loop
103+ assert [rando .has_obj_perm (foos [this_service_name ], 'change' ) for this_service_name in ('barland' , 'fooland' )] == [
104+ bool (this_service_name == service_name ) for this_service_name in ('barland' , 'fooland' )
105+ ]
106+
107+ rds [service_name ].remove_permission (rando , foos [service_name ])
108+
109+
110+ @pytest .mark .django_db
111+ def test_org_roles_same_type_different_service (rando , organization ):
112+ org_ct = DABContentType .objects .get_for_model (organization )
113+ cts = {}
114+ rds = {}
115+ foos = {}
116+ for service_name in ('barland' , 'fooland' ):
117+ ct = DABContentType .objects .create (service = service_name , model = 'foo' , app_label = 'foo' , parent_content_type = org_ct )
118+ cts [service_name ] = ct
119+ permissions = []
120+ for codename in ('view_foo' , 'change_foo' , 'foo_foo' ):
121+ permissions .append (DABPermission .objects .create (codename = codename , content_type = ct ))
122+
123+ rd = RoleDefinition .objects .create_from_permissions (
124+ name = f'The organization-level foo role for { service_name } service' ,
125+ permissions = [perm .api_slug for perm in permissions ],
126+ content_type = org_ct , # difference from last test
127+ )
128+ obj_rd = RoleDefinition .objects .create_from_permissions (
129+ name = f'Object-level view permission for { service_name } service' , permissions = [f'{ service_name } .view_foo' ], content_type = ct
130+ )
131+ rds [service_name ] = rd
132+ foos [service_name ] = RemoteObject (content_type = ct , object_id = 4 , parent_reference = organization .pk )
133+ obj_rd .give_permission (rando , foos [service_name ])
134+
135+ for service_name in ('barland' , 'fooland' ):
136+ assert [rando .has_obj_perm (foos [this_service_name ], 'change' ) for this_service_name in ('barland' , 'fooland' )] == [False , False ]
137+
138+ rds [service_name ].give_permission (rando , organization )
139+
140+ # Has permission to either fooland or barland stuff via organization
141+ assert [rando .has_obj_perm (foos [this_service_name ], 'change' ) for this_service_name in ('barland' , 'fooland' )] == [
142+ bool (this_service_name == service_name ) for this_service_name in ('barland' , 'fooland' )
143+ ], f'User should have permission to exactly { service_name } resource'
144+
145+ rds [service_name ].remove_permission (rando , organization )
0 commit comments