From 58ee10aa029c2793f72a98a12b869a5466c787ba Mon Sep 17 00:00:00 2001 From: Akita Noek Date: Tue, 5 Apr 2016 20:58:09 -0400 Subject: [PATCH] Added organization to Credential summary and related fields #1400 --- awx/api/serializers.py | 18 ++++++++++++++++++ .../tests/functional/api/test_credential.py | 16 ++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 164a5b8477..4227163166 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -1526,8 +1526,26 @@ class CredentialSerializer(BaseSerializer): activity_stream = reverse('api:credential_activity_stream_list', args=(obj.pk,)), access_list = reverse('api:credential_access_list', args=(obj.pk,)), )) + + qs = Organization.objects.filter(admin_role__children=obj.owner_role) + if qs.count() > 0: + res.update(dict(organization=qs[0].get_absolute_url())) return res + def get_summary_fields(self, obj): + summary_dict = super(CredentialSerializer, self).get_summary_fields(obj) + + qs = Organization.objects.filter(admin_role__children=obj.owner_role) + if qs.count() > 0: + org = qs[0] + summary_dict['organization'] = { + 'id': org.id, + 'name': org.name, + 'description': org.description, + } + + return summary_dict + class JobOptionsSerializer(BaseSerializer): diff --git a/awx/main/tests/functional/api/test_credential.py b/awx/main/tests/functional/api/test_credential.py index 726742bd31..24b653b863 100644 --- a/awx/main/tests/functional/api/test_credential.py +++ b/awx/main/tests/functional/api/test_credential.py @@ -123,6 +123,22 @@ def test_create_org_credential_as_admin(post, organization, org_admin): }, org_admin) assert response.status_code == 201 +@pytest.mark.django_db +def test_credential_detail(post, get, organization, org_admin): + response = post(reverse('api:credential_list'), { + 'name': 'Some name', + 'username': 'someusername', + 'organization': organization.id, + }, org_admin) + assert response.status_code == 201 + response = get(reverse('api:credential_detail', args=(response.data['id'],)), org_admin) + assert response.status_code == 200 + summary_fields = response.data['summary_fields'] + assert 'organization' in summary_fields + assert summary_fields['organization']['id'] == organization.id + related_fields = response.data['related'] + assert 'organization' in related_fields + @pytest.mark.django_db def test_list_created_org_credentials(post, get, organization, org_admin, org_member): response = post(reverse('api:credential_list'), {