6
6
from ansible_base .resource_registry .models import Resource
7
7
8
8
9
+ @pytest .mark .django_db
9
10
class TestAnsibleIdAliasFilterBackend :
10
11
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 ):
13
13
'''
14
14
Test filtering RoleUserAssignment by user_ansible_id and object_ansible_id.
15
+ Also test that other filtering works,
15
16
'''
16
- # user - org assigment
17
+ # rando - org assigment
17
18
user_resource = Resource .objects .get (object_id = rando .pk , content_type = ContentType .objects .get_for_model (rando ).pk )
18
19
organization_resource = Resource .objects .get (object_id = organization .pk , content_type = ContentType .objects .get_for_model (organization ).pk )
19
20
url = get_relative_url ('roleuserassignment-list' )
20
21
data = dict (role_definition = org_inv_rd .id , content_type = 'shared.organization' , user_ansible_id = user_resource .ansible_id , object_id = organization .id )
21
22
response = admin_api_client .post (url , data = data , format = "json" )
22
23
assert response .status_code == 201 , response .data
23
24
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 )
26
32
response = admin_api_client .post (url , data = data , format = "json" )
27
33
assert response .status_code == 201 , response .data
28
34
@@ -40,15 +46,44 @@ def test_filter_user_ansible_id(self, admin_api_client, org_inv_rd, inv_rd, inve
40
46
query_params = {'object_ansible_id' : organization_resource .ansible_id }
41
47
response = admin_api_client .get (url + '?' + urlencode (query_params ))
42
48
assert response .status_code == 200 , response .data
43
- assert response .data ["count" ] == 1 , response .data
49
+ assert response .data ["count" ] == 2 , response .data
44
50
45
51
# filter by both user_ansible_id and object_ansible_id
46
52
query_params = {'user_ansible_id' : user_resource .ansible_id , 'object_ansible_id' : organization_resource .ansible_id }
47
53
response = admin_api_client .get (url + '?' + urlencode (query_params ))
48
54
assert response .status_code == 200 , response .data
49
55
assert response .data ["count" ] == 1 , response .data
50
56
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
+
52
87
def test_filter_team_ansible_id (self , admin_api_client , team , inv_rd , inventory ):
53
88
'''
54
89
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)
66
101
assert response .data ["count" ] == 1 , response .data
67
102
68
103
@pytest .mark .parametrize ("ansible_id_type" , ["user" , "team" , "object" ])
69
- @pytest .mark .django_db
70
104
def test_invalid_ansible_id_format (self , admin_api_client , ansible_id_type ):
71
105
'''
72
106
Test that invalid UUID formats for ansible_id raise a 400 error.
0 commit comments