From 39c956335243832688db842558108bd8f984418b Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Sun, 13 Mar 2016 20:40:21 -0400 Subject: [PATCH] test and fix for POST to empty list scenaro and JT count fix --- awx/api/serializers.py | 8 ++- awx/api/views.py | 19 +++---- .../api/test_organization_counts.py | 51 +++++++++++++++++++ 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 743ca1c054..514075bc29 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -802,8 +802,12 @@ class OrganizationSerializer(BaseSerializer): summary_dict = super(OrganizationSerializer, self).get_summary_fields(obj) counts_dict = self.context.get('counts', None) if counts_dict is not None and summary_dict is not None: - print 'counts_dict: ' + str(counts_dict) - summary_dict['counts'] = counts_dict[obj.id] + if obj.id not in counts_dict: + summary_dict['counts'] = { + 'inventories': 0, 'teams': 0, 'users': 0, + 'job_templates': 0, 'admins': 0, 'projects': 0} + else: + summary_dict['counts'] = counts_dict[obj.id] return summary_dict diff --git a/awx/api/views.py b/awx/api/views.py index 7d7e5b34ad..9bf2acbf40 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -624,17 +624,23 @@ class OrganizationList(ListCreateAPIView): org_qs = self.request.user.get_queryset(self.model) org_id_list = org_qs.values('id') if len(org_id_list) == 0: + if self.request.method == 'POST': + full_context['counts'] = {} return full_context # Produce counts of Foreign Key relationships - db_results['inventories'] = self.request.user.get_queryset(Inventory)\ + inv_qs = self.request.user.get_queryset(Inventory) + db_results['inventories'] = inv_qs\ .values('organization').annotate(Count('organization')).order_by('organization') db_results['teams'] = self.request.user.get_queryset(Team)\ .values('organization').annotate(Count('organization')).order_by('organization') JT_reference = 'inventory__organization' + # Extra filter is applied on the inventory, because this catches + # the case of deleted (and purged) inventory db_JT_results = self.request.user.get_queryset(JobTemplate)\ + .filter(inventory_id__in=inv_qs.values_list('pk', flat=True))\ .values(JT_reference).annotate(Count(JT_reference)).\ order_by(JT_reference) @@ -664,16 +670,11 @@ class OrganizationList(ListCreateAPIView): .annotate(Count('organization')).order_by('organization') count_context = {} - zeroed_dict = {'inventories': 0, 'teams': 0, 'users': 0, - 'job_templates': 0, 'admins': 0, 'projects': 0} for org in org_id_list: org_id = org['id'] - count_context[org_id] = zeroed_dict.copy() - if self.request.method == 'POST': - org_id = max([int(k) for k in count_context.keys()]) + 1 - # org_id = instance = self.get_object().id - # self.request.data['id'] - count_context[org_id] = zeroed_dict + count_context[org_id] = { + 'inventories': 0, 'teams': 0, 'users': 0, 'job_templates': 0, + 'admins': 0, 'projects': 0} for res in db_results: for entry in db_results[res]: diff --git a/awx/main/tests/functional/api/test_organization_counts.py b/awx/main/tests/functional/api/test_organization_counts.py index 6a31bf1151..694eb24468 100644 --- a/awx/main/tests/functional/api/test_organization_counts.py +++ b/awx/main/tests/functional/api/test_organization_counts.py @@ -52,3 +52,54 @@ def test_org_counts_member(resourced_organization, get): 'inventories': 0, 'teams': 0 } + +@pytest.mark.django_db +def test_new_org_zero_counts(user, post): + # Check that a POST to the organization list endpoint returns + # correct counts, including the new record + org_list_url = reverse('api:organization_list', args=[]) + post_response = post(url=org_list_url, data={'name': 'test organization', + 'description': ''}, user=user('admin', True)) + new_org_list = post_response.render().data + counts_dict = new_org_list['summary_fields']['counts'] + + assert counts_dict == { + 'users': 0, + 'admins': 0, + 'job_templates': 0, + 'projects': 0, + 'inventories': 0, + 'teams': 0 + } + +@pytest.mark.django_db +def test_two_organizations(resourced_organization, organizations, user, get): + # Check correct results for two organizations are returned + external_admin = user('admin', True) + organization_zero = organizations(1)[0] + response = get(reverse('api:organization_list', args=[]), external_admin) + org_id_full = resourced_organization.id + org_id_zero = organization_zero.id + print ' ids: ' + str(org_id_full) + " : " + str(org_id_zero) + print ' counts_dict: ' + str(response.data['results']) + counts = {} + for i in range(2): + org_id = response.data['results'][i]['id'] + counts[org_id] = response.data['results'][i]['summary_fields']['counts'] + + assert counts[org_id_full] == { + 'users': 1, + 'admins': 1, + 'job_templates': 1, + 'projects': 1, + 'inventories': 1, + 'teams': 1 + } + assert counts[org_id_zero] == { + 'users': 0, + 'admins': 0, + 'job_templates': 0, + 'projects': 0, + 'inventories': 0, + 'teams': 0 + }