@@ -36,6 +36,15 @@ def resource_client(system_user, admin_user, live_server, local_authenticator, t
36
36
return ResourceAPIClient (live_server .url , "/api/v1/service-index/" , jwt_user_id = admin_user .resource .ansible_id )
37
37
38
38
39
+ @pytest .fixture
40
+ def inv_rd ():
41
+ return RoleDefinition .objects .create_from_permissions (
42
+ permissions = ['change_inventory' , 'view_inventory' ],
43
+ name = 'change-inv' ,
44
+ content_type = permission_registry .content_type_model .objects .get_for_model (Inventory ),
45
+ )
46
+
47
+
39
48
@pytest .mark .django_db
40
49
def test_service_metadata (resource_client ):
41
50
"""Test that the resource list is working."""
@@ -183,7 +192,7 @@ def test_list_role_permissions_all_pages(resource_client):
183
192
assert resp .json ()["count" ] > 25
184
193
185
194
186
- def _assert_assignment_matches_data (assignment , data , obj , user ):
195
+ def _assert_assignment_matches_data (assignment , data , obj , actor ):
187
196
assert 'created' in data , data
188
197
# assert DateTimeField().to_representation(assignment.created) == data['created'] # TODO
189
198
assert str (assignment .created_by .resource .ansible_id ) == data ['created_by_ansible_id' ]
@@ -196,7 +205,10 @@ def _assert_assignment_matches_data(assignment, data, obj, user):
196
205
else :
197
206
assert 'aap.inventory' == data ['content_type' ]
198
207
assert 'change-inv' == data ['role_definition' ]
199
- assert str (user .resource .ansible_id ) == data ['user_ansible_id' ]
208
+ if 'user_ansible_id' in data :
209
+ assert str (actor .resource .ansible_id ) == data ['user_ansible_id' ]
210
+ elif 'team_ansible_id' in data :
211
+ assert str (actor .resource .ansible_id ) == data ['team_ansible_id' ]
200
212
201
213
202
214
@pytest .mark .django_db
@@ -217,12 +229,7 @@ def test_sync_org_assignment(resource_client, org_admin_rd, user, organization):
217
229
218
230
219
231
@pytest .mark .django_db
220
- def test_sync_obj_assignment (resource_client , user , inventory ):
221
- inv_rd = RoleDefinition .objects .create_from_permissions (
222
- permissions = ['change_inventory' , 'view_inventory' ],
223
- name = 'change-inv' ,
224
- content_type = permission_registry .content_type_model .objects .get_for_model (Inventory ),
225
- )
232
+ def test_sync_obj_assignment (resource_client , user , inventory , inv_rd ):
226
233
assignment = inv_rd .give_permission (user , inventory )
227
234
resp = resource_client .sync_assignment (assignment )
228
235
assert resp .status_code == 200 , resp .text
@@ -307,3 +314,51 @@ def test_validate_local_user(resource_client, admin_user, member_rd):
307
314
308
315
resp = resource_client .validate_local_user (username = admin_user .username , password = "fake password" )
309
316
assert resp .status_code == 401
317
+
318
+
319
+ @pytest .mark .django_db
320
+ def test_list_user_assignments (resource_client , org_admin_rd , user , organization ):
321
+ """Test listing user role assignments."""
322
+ # Create an assignment for the user
323
+ assignment = org_admin_rd .give_permission (user , organization )
324
+
325
+ # Call the list_user_assignments method (doesn't exist yet)
326
+ resp = resource_client .list_user_assignments (user_ansible_id = str (user .resource .ansible_id ))
327
+
328
+ assert resp .status_code == 200
329
+ data = resp .json ()
330
+ assert data ["count" ] >= 1
331
+
332
+ # Find our assignment in the results
333
+ assignment_found = False
334
+ for result in data ["results" ]:
335
+ if result ["user_ansible_id" ] == str (user .resource .ansible_id ):
336
+ _assert_assignment_matches_data (assignment , result , organization , user )
337
+ assignment_found = True
338
+ break
339
+
340
+ assert assignment_found , "User assignment not found in list results"
341
+
342
+
343
+ @pytest .mark .django_db
344
+ def test_list_team_assignments (resource_client , inv_rd , team , inventory ):
345
+ """Test listing team role assignments."""
346
+ # Create an assignment for the team
347
+ assignment = inv_rd .give_permission (team , inventory )
348
+
349
+ # Call the list_team_assignments method (doesn't exist yet)
350
+ resp = resource_client .list_team_assignments (team_ansible_id = str (team .resource .ansible_id ))
351
+
352
+ assert resp .status_code == 200
353
+ data = resp .json ()
354
+ assert data ["count" ] >= 1
355
+
356
+ # Find our assignment in the results
357
+ assignment_found = False
358
+ for result in data ["results" ]:
359
+ if result ["team_ansible_id" ] == str (team .resource .ansible_id ):
360
+ _assert_assignment_matches_data (assignment , result , inventory , team )
361
+ assignment_found = True
362
+ break
363
+
364
+ assert assignment_found , "Team assignment not found in list results"
0 commit comments