Skip to content

Commit eda3c55

Browse files
committed
Add test coverage in the existing format and more fixes
1 parent 2e96f45 commit eda3c55

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

ansible_base/rbac/models/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def _is_cached_object_valid(self, rel_obj, ct_id, pk_val):
111111
return False
112112

113113
ct_match = ct_id == self.get_content_type(obj=rel_obj).id
114-
pk_match = ct_match and rel_obj.pk == pk_val
114+
pk_match = ct_match and str(rel_obj.pk) == str(pk_val)
115115
return pk_match
116116

117117
def _fetch_related_object(self, ct_id, pk_val):

ansible_base/rbac/remote.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ def summary_fields(self):
117117
This placeholder should be cleary identifable by a client or by the RBAC resource server.
118118
Then, the idea, is that it can make a request to the remote server to get the summary data.
119119
"""
120-
return {'<remote_object_placeholder>': True, 'model_name': self._meta.model_name, 'service': self._meta.service, 'pk': self.pk}
120+
pk_val = self.pk
121+
if not isinstance(pk_val, int):
122+
pk_val = str(pk_val)
123+
return {'<remote_object_placeholder>': True, 'model_name': self._meta.model_name, 'service': self._meta.service, 'pk': pk_val}
121124

122125

123126
def get_remote_base_class() -> Type[RemoteObject]:

test_app/tests/rbac/remote/test_public_api_compat.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from ansible_base.rbac import permission_registry
77
from ansible_base.rbac.models import RoleUserAssignment
88
from ansible_base.rbac.remote import RemoteObject
9+
from ansible_base.rbac.service_api.serializers import ServiceRoleUserAssignmentSerializer
910
from test_app.models import Organization
1011

1112
# Role Definitions
@@ -120,9 +121,20 @@ def test_give_permission_to_remote_object_uuid(admin_api_client, rando, foo_type
120121
valid_items = [item for item in response.data['results'] if item['id'] == assignment.id]
121122
assert len(valid_items) == 1
122123
assignment_data = valid_items[0]
123-
assert 'summary_fields' in assignment_data
124-
sf = assignment_data['summary_fields']
125-
assert 'content_object' in sf
126-
assert str(sf['content_object']['pk']) == str(a_foo.object_id)
124+
assert 'content_object' in assignment_data['summary_fields']
125+
assert assignment_data['summary_fields']['content_object']['pk'] == str(a_foo.object_id)
127126

128127
assert rando.has_obj_perm(a_foo, 'foo')
128+
129+
# Test that we can serialize the assignment in a GET to the service-index endpoint
130+
service_url = get_relative_url('serviceuserassignment-list')
131+
response = admin_api_client.get(service_url + f'?user={rando.id}', format="json")
132+
assert response.status_code == 200, response.data
133+
assert response.data['count'] == 1
134+
assignment_data = response.data['results'][0]
135+
assert assignment_data['object_id'] == str(assignment.object_id)
136+
137+
# Direct serialization is used for synchronizing, so test that as well here
138+
serializer = ServiceRoleUserAssignmentSerializer(assignment)
139+
assignment_data = serializer.data
140+
assert assignment_data['object_id'] == str(assignment.object_id)

test_app/tests/rbac/remote/test_remote_assignment.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_give_remote_permission(rando, foo_type, foo_permission, foo_rd):
3636

3737

3838
@pytest.mark.django_db
39-
def test_prefetch_related_objects(foo_type, foo_type_uuid, foo_rd, foo_rd_uuid, inv_rd, inventory):
39+
def test_prefetch_related_objects(django_assert_num_queries, foo_type, foo_type_uuid, foo_rd, foo_rd_uuid, inv_rd, inventory):
4040
users = [User.objects.create(username=f'user{i}') for i in range(10)]
4141

4242
a_foo = RemoteObject(content_type=foo_type, object_id=42)
@@ -47,9 +47,12 @@ def test_prefetch_related_objects(foo_type, foo_type_uuid, foo_rd, foo_rd_uuid,
4747
inv_rd.give_permission(u, inventory)
4848

4949
assert RoleUserAssignment.objects.count() == 10 * 3
50-
assert {assignment.content_object for assignment in RoleUserAssignment.objects.all()} == {a_foo, inventory, a_foo_uuid}
51-
assert {assignment.content_object for assignment in RoleUserAssignment.objects.all()} == {a_foo, inventory, a_foo_uuid}
52-
assert {assignment.content_object for assignment in RoleUserAssignment.objects.prefetch_related('content_object')} == {a_foo, inventory, a_foo_uuid}
50+
for _ in range(2):
51+
with django_assert_num_queries(11):
52+
assert {assignment.content_object for assignment in RoleUserAssignment.objects.all()} == {a_foo, inventory, a_foo_uuid}
53+
for _ in range(2):
54+
with django_assert_num_queries(2):
55+
assert {assignment.content_object for assignment in RoleUserAssignment.objects.prefetch_related('content_object')} == {a_foo, inventory, a_foo_uuid}
5356

5457

5558
@pytest.mark.django_db

0 commit comments

Comments
 (0)