66from ansible_base .resource_registry .models import Resource
77
88
9+ @pytest .mark .django_db
910class TestAnsibleIdAliasFilterBackend :
1011
11- @pytest .mark .django_db
12- def test_filter_user_ansible_id (self , admin_api_client , org_inv_rd , inv_rd , inventory , rando , organization ):
12+ def test_filter_user_ansible_id (self , admin_api_client , org_inv_rd , inv_rd , inventory , rando , random_user , organization ):
1313 '''
1414 Test filtering RoleUserAssignment by user_ansible_id and object_ansible_id.
15+ Also test that other filtering works,
1516 '''
16- # user - org assigment
17+ # rando - org assigment
1718 user_resource = Resource .objects .get (object_id = rando .pk , content_type = ContentType .objects .get_for_model (rando ).pk )
1819 organization_resource = Resource .objects .get (object_id = organization .pk , content_type = ContentType .objects .get_for_model (organization ).pk )
1920 url = get_relative_url ('roleuserassignment-list' )
2021 data = dict (role_definition = org_inv_rd .id , content_type = 'shared.organization' , user_ansible_id = user_resource .ansible_id , object_id = organization .id )
2122 response = admin_api_client .post (url , data = data , format = "json" )
2223 assert response .status_code == 201 , response .data
2324
24- # user - inventory assignment (just a random assignment to make total count > 1)
25- data = dict (role_definition = inv_rd .id , content_type = 'shared.inventory' , user_ansible_id = user_resource .ansible_id , object_id = inventory .id )
25+ # random_user - org assigment
26+ data = dict (role_definition = org_inv_rd .id , content_type = 'shared.organization' , user = random_user .id , object_id = organization .id )
27+ response = admin_api_client .post (url , data = data , format = "json" )
28+ assert response .status_code == 201 , response .data
29+
30+ # rando - inventory assignment (an additional assignment to make total count > 1)
31+ data = dict (role_definition = inv_rd .id , content_type = 'aap.inventory' , user_ansible_id = user_resource .ansible_id , object_id = inventory .id )
2632 response = admin_api_client .post (url , data = data , format = "json" )
2733 assert response .status_code == 201 , response .data
2834
@@ -40,15 +46,44 @@ def test_filter_user_ansible_id(self, admin_api_client, org_inv_rd, inv_rd, inve
4046 query_params = {'object_ansible_id' : organization_resource .ansible_id }
4147 response = admin_api_client .get (url + '?' + urlencode (query_params ))
4248 assert response .status_code == 200 , response .data
43- assert response .data ["count" ] == 1 , response .data
49+ assert response .data ["count" ] == 2 , response .data
4450
4551 # filter by both user_ansible_id and object_ansible_id
4652 query_params = {'user_ansible_id' : user_resource .ansible_id , 'object_ansible_id' : organization_resource .ansible_id }
4753 response = admin_api_client .get (url + '?' + urlencode (query_params ))
4854 assert response .status_code == 200 , response .data
4955 assert response .data ["count" ] == 1 , response .data
5056
51- @pytest .mark .django_db
57+ # filter by random user id
58+ query_params = {'user' : random_user .id }
59+ response = admin_api_client .get (url + '?' + urlencode (query_params ))
60+ assert response .status_code == 200 , response .data
61+ assert response .data ["count" ] == 1 , response .data
62+
63+ def test_filter_by_user_id (self , admin_api_client , inv_rd , inventory , random_user ):
64+ '''
65+ Test that filtering with user id still works.
66+ This ensures that the default rest filters are still functional for this
67+ viewset.
68+ '''
69+ user_resource = Resource .objects .get (object_id = random_user .pk , content_type = ContentType .objects .get_for_model (random_user ).pk )
70+ url = get_relative_url ('roleuserassignment-list' )
71+ data = dict (role_definition = inv_rd .id , content_type = 'aap.inventory' , user_ansible_id = user_resource .ansible_id , object_id = inventory .id )
72+ response = admin_api_client .post (url , data = data , format = "json" )
73+ assert response .status_code == 201 , response .data
74+
75+ # filter by user_id
76+ query_params = {'user' : random_user .id }
77+ response = admin_api_client .get (url + '?' + urlencode (query_params ))
78+ assert response .status_code == 200 , response .data
79+ assert response .data ["count" ] == 1 , response .data
80+
81+ # filter by user_ansible_id
82+ query_params = {'user_ansible_id' : user_resource .ansible_id }
83+ response = admin_api_client .get (url + '?' + urlencode (query_params ))
84+ assert response .status_code == 200 , response .data
85+ assert response .data ["count" ] == 1 , response .data
86+
5287 def test_filter_team_ansible_id (self , admin_api_client , team , inv_rd , inventory ):
5388 '''
5489 Test filtering RoleTeamAssignment by team_ansible_id.
@@ -66,7 +101,6 @@ def test_filter_team_ansible_id(self, admin_api_client, team, inv_rd, inventory)
66101 assert response .data ["count" ] == 1 , response .data
67102
68103 @pytest .mark .parametrize ("ansible_id_type" , ["user" , "team" , "object" ])
69- @pytest .mark .django_db
70104 def test_invalid_ansible_id_format (self , admin_api_client , ansible_id_type ):
71105 '''
72106 Test that invalid UUID formats for ansible_id raise a 400 error.
0 commit comments