From 765dcd33185224f9dad0db87c7b3588592642c13 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Fri, 11 Mar 2016 08:47:33 -0500 Subject: [PATCH 1/6] Added queries that calculate counts for organization resources --- awx/api/serializers.py | 8 ++ awx/api/views.py | 78 +++++++++++++++++++ .../api/test_organization_counts.py | 54 +++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 awx/main/tests/functional/api/test_organization_counts.py diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 9aed65dbf4..743ca1c054 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -798,6 +798,14 @@ class OrganizationSerializer(BaseSerializer): )) return res + def get_summary_fields(self, obj): + 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] + return summary_dict + class ProjectOptionsSerializer(BaseSerializer): diff --git a/awx/api/views.py b/awx/api/views.py index b203015770..09a14f5381 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -596,6 +596,15 @@ class OrganizationList(ListCreateAPIView): model = Organization serializer_class = OrganizationSerializer + # @paginated + # def get(self, *args, **kwargs): + # # self.paginated_params = {'limit': limit, 'offset': offset, 'ordering': ordering} + # limit = kwargs.pop('limit') + # offset = kwargs.pop('offset') + # ordering = kwargs.pop('ordering') + # # qs[offset:offset + limit] + # return (super(OrganizationList, self).get(*args, **kwargs), 5, None) + def create(self, request, *args, **kwargs): """Create a new organzation. @@ -614,6 +623,75 @@ class OrganizationList(ListCreateAPIView): # Okay, create the organization as usual. return super(OrganizationList, self).create(request, *args, **kwargs) + def get_serializer_context(self, *args, **kwargs): + full_context = super(OrganizationList, self).get_serializer_context(*args, **kwargs) + + if self.request is None: + return full_context + + db_results = {} + org_qs = self.request.user.get_queryset(self.model) + org_id_list = org_qs.values('id') + if len(org_id_list) == 0: + return full_context + + # Produce counts of Foreign Key relationships + db_results['inventories'] = self.request.user.get_queryset(Inventory)\ + .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' + db_JT_results = self.request.user.get_queryset(JobTemplate)\ + .values(JT_reference).annotate(Count(JT_reference)).\ + order_by(JT_reference) + + # Produce counts of m2m relationships + project_qs = self.request.user.get_queryset(Project) + db_results['projects'] = Organization.projects.through.objects\ + .filter( + project_id__in=project_qs.values_list('pk', flat=True), + organization_id__in=org_qs.values_list('pk', flat=True))\ + .values('organization')\ + .annotate(Count('organization')).order_by('organization') + + # TODO: When RBAC branch merges, change these to role relation + user_qs = self.request.user.get_queryset(User) + db_results['users'] = Organization.users.through.objects\ + .filter( + user_id__in=user_qs.values_list('pk', flat=True), + organization_id__in=org_qs.values_list('pk', flat=True))\ + .values('organization')\ + .annotate(Count('organization')).order_by('organization') + + db_results['admins'] = Organization.admins.through.objects\ + .filter( + user_id__in=user_qs.values_list('pk', flat=True), + organization_id__in=org_qs.values_list('pk', flat=True))\ + .values('organization')\ + .annotate(Count('organization')).order_by('organization') + + count_context = {} + for org in org_id_list: + org_id = org['id'] + 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]: + org_id = entry['organization'] + count_context[org_id][res] = entry['organization__count'] + + for entry in db_JT_results: + org_id = entry[JT_reference] + count_context[org_id]['job_templates'] = entry['%s__count' % JT_reference] + + full_context['counts'] = count_context + + return full_context + class OrganizationDetail(RetrieveUpdateDestroyAPIView): model = Organization diff --git a/awx/main/tests/functional/api/test_organization_counts.py b/awx/main/tests/functional/api/test_organization_counts.py new file mode 100644 index 0000000000..6a31bf1151 --- /dev/null +++ b/awx/main/tests/functional/api/test_organization_counts.py @@ -0,0 +1,54 @@ +import pytest + +from django.core.urlresolvers import reverse + +@pytest.fixture +def resourced_organization(organization, project, user): + admin_user = user('test-admin', True) + member_user = user('org-member') + + # Associate one resource of every type with the organization + organization.users.add(member_user) + organization.admins.add(admin_user) + organization.projects.add(project) + organization.teams.create(name='org-team') + inventory = organization.inventories.create(name="associated-inv") + inventory.jobtemplates.create(name="test-jt", + description="test-job-template-desc", + project=project, + playbook="test_playbook.yml") + + return organization + +@pytest.mark.django_db +def test_org_counts_admin(resourced_organization, user, get): + # Check that all types of resources are counted by a superuser + external_admin = user('admin', True) + response = get(reverse('api:organization_list', args=[]), external_admin) + counts = response.data['results'][0]['summary_fields']['counts'] + + assert counts == { + 'users': 1, + 'admins': 1, + 'job_templates': 1, + 'projects': 1, + 'inventories': 1, + 'teams': 1 + } + +@pytest.mark.django_db +def test_org_counts_member(resourced_organization, get): + # Check that a non-admin user can only see the full project and + # user count, consistent with the RBAC rules + member_user = resourced_organization.users.get(username='org-member') + response = get(reverse('api:organization_list', args=[]), member_user) + counts = response.data['results'][0]['summary_fields']['counts'] + + assert counts == { + 'users': 1, # User can see themselves + 'admins': 0, + 'job_templates': 0, + 'projects': 1, # Projects are shared with all the organization + 'inventories': 0, + 'teams': 0 + } From 9f25a48936413c072c0b97d6048abd3eb9952b90 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Sun, 13 Mar 2016 11:45:08 -0400 Subject: [PATCH 2/6] fix for POST scenario --- awx/api/views.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/awx/api/views.py b/awx/api/views.py index 09a14f5381..7d7e5b34ad 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -596,15 +596,6 @@ class OrganizationList(ListCreateAPIView): model = Organization serializer_class = OrganizationSerializer - # @paginated - # def get(self, *args, **kwargs): - # # self.paginated_params = {'limit': limit, 'offset': offset, 'ordering': ordering} - # limit = kwargs.pop('limit') - # offset = kwargs.pop('offset') - # ordering = kwargs.pop('ordering') - # # qs[offset:offset + limit] - # return (super(OrganizationList, self).get(*args, **kwargs), 5, None) - def create(self, request, *args, **kwargs): """Create a new organzation. @@ -673,11 +664,16 @@ 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] = {'inventories': 0, 'teams': 0, 'users': 0, - 'job_templates': 0, 'admins': 0, - 'projects': 0} + 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 for res in db_results: for entry in db_results[res]: From 39c956335243832688db842558108bd8f984418b Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Sun, 13 Mar 2016 20:40:21 -0400 Subject: [PATCH 3/6] 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 + } From 6996ea22b00019eba7be4a61c150032b7549377c Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Sun, 13 Mar 2016 22:39:52 -0400 Subject: [PATCH 4/6] style tweaks, add one more assertion --- awx/api/views.py | 4 ++-- awx/main/tests/functional/api/test_organization_counts.py | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/awx/api/views.py b/awx/api/views.py index 9bf2acbf40..ebdb57098d 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -641,8 +641,8 @@ class OrganizationList(ListCreateAPIView): # 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) + .values(JT_reference).annotate(Count(JT_reference))\ + .order_by(JT_reference) # Produce counts of m2m relationships project_qs = self.request.user.get_queryset(Project) diff --git a/awx/main/tests/functional/api/test_organization_counts.py b/awx/main/tests/functional/api/test_organization_counts.py index 694eb24468..56fdb8215e 100644 --- a/awx/main/tests/functional/api/test_organization_counts.py +++ b/awx/main/tests/functional/api/test_organization_counts.py @@ -63,6 +63,7 @@ def test_new_org_zero_counts(user, post): new_org_list = post_response.render().data counts_dict = new_org_list['summary_fields']['counts'] + assert post_response.status_code == 201 assert counts_dict == { 'users': 0, 'admins': 0, @@ -80,8 +81,6 @@ def test_two_organizations(resourced_organization, organizations, user, get): 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'] From da39f1269a4e97f1bf03806d1264ca791ec2f4db Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Tue, 15 Mar 2016 14:26:50 -0400 Subject: [PATCH 5/6] org counts code restructing to better prepare for RBAC merge --- awx/api/serializers.py | 6 +- awx/api/views.py | 41 +++++----- .../api/test_organization_counts.py | 77 +++++++++++++++---- 3 files changed, 86 insertions(+), 38 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 514075bc29..6ca73cf6a0 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -800,14 +800,14 @@ class OrganizationSerializer(BaseSerializer): def get_summary_fields(self, obj): summary_dict = super(OrganizationSerializer, self).get_summary_fields(obj) - counts_dict = self.context.get('counts', None) + counts_dict = self.context.get('related_field_counts', None) if counts_dict is not None and summary_dict is not None: if obj.id not in counts_dict: - summary_dict['counts'] = { + summary_dict['related_field_counts'] = { 'inventories': 0, 'teams': 0, 'users': 0, 'job_templates': 0, 'admins': 0, 'projects': 0} else: - summary_dict['counts'] = counts_dict[obj.id] + summary_dict['related_field_counts'] = counts_dict[obj.id] return summary_dict diff --git a/awx/api/views.py b/awx/api/views.py index ebdb57098d..b50ba0497c 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -625,47 +625,43 @@ class OrganizationList(ListCreateAPIView): org_id_list = org_qs.values('id') if len(org_id_list) == 0: if self.request.method == 'POST': - full_context['counts'] = {} + full_context['related_field_counts'] = {} return full_context - # Produce counts of Foreign Key relationships inv_qs = self.request.user.get_queryset(Inventory) + project_qs = self.request.user.get_queryset(Project) + user_qs = self.request.user.get_queryset(User) + + # Produce counts of Foreign Key relationships 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') + # TODO: When RBAC branch merges, change this to project relationship 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))\ + db_results['job_templates'] = self.request.user.get_queryset(JobTemplate)\ + .filter(inventory__in=inv_qs)\ .values(JT_reference).annotate(Count(JT_reference))\ .order_by(JT_reference) # Produce counts of m2m relationships - project_qs = self.request.user.get_queryset(Project) db_results['projects'] = Organization.projects.through.objects\ - .filter( - project_id__in=project_qs.values_list('pk', flat=True), - organization_id__in=org_qs.values_list('pk', flat=True))\ + .filter(project__in=project_qs, organization__in=org_qs)\ .values('organization')\ .annotate(Count('organization')).order_by('organization') # TODO: When RBAC branch merges, change these to role relation - user_qs = self.request.user.get_queryset(User) db_results['users'] = Organization.users.through.objects\ - .filter( - user_id__in=user_qs.values_list('pk', flat=True), - organization_id__in=org_qs.values_list('pk', flat=True))\ + .filter(user__in=user_qs, organization__in=org_qs)\ .values('organization')\ .annotate(Count('organization')).order_by('organization') db_results['admins'] = Organization.admins.through.objects\ - .filter( - user_id__in=user_qs.values_list('pk', flat=True), - organization_id__in=org_qs.values_list('pk', flat=True))\ + .filter(user__in=user_qs, organization__in=org_qs)\ .values('organization')\ .annotate(Count('organization')).order_by('organization') @@ -677,15 +673,16 @@ class OrganizationList(ListCreateAPIView): 'admins': 0, 'projects': 0} for res in db_results: + if res == 'job_templates': + org_reference = JT_reference + else: + org_reference = 'organization' for entry in db_results[res]: - org_id = entry['organization'] - count_context[org_id][res] = entry['organization__count'] + org_id = entry[org_reference] + if org_id in count_context: + count_context[org_id][res] = entry['%s__count' % org_reference] - for entry in db_JT_results: - org_id = entry[JT_reference] - count_context[org_id]['job_templates'] = entry['%s__count' % JT_reference] - - full_context['counts'] = count_context + full_context['related_field_counts'] = count_context return full_context diff --git a/awx/main/tests/functional/api/test_organization_counts.py b/awx/main/tests/functional/api/test_organization_counts.py index 56fdb8215e..de629dbcf4 100644 --- a/awx/main/tests/functional/api/test_organization_counts.py +++ b/awx/main/tests/functional/api/test_organization_counts.py @@ -3,7 +3,7 @@ import pytest from django.core.urlresolvers import reverse @pytest.fixture -def resourced_organization(organization, project, user): +def resourced_organization(organization, project, team, inventory, user): admin_user = user('test-admin', True) member_user = user('org-member') @@ -11,12 +11,12 @@ def resourced_organization(organization, project, user): organization.users.add(member_user) organization.admins.add(admin_user) organization.projects.add(project) - organization.teams.create(name='org-team') - inventory = organization.inventories.create(name="associated-inv") - inventory.jobtemplates.create(name="test-jt", - description="test-job-template-desc", - project=project, - playbook="test_playbook.yml") + # organization.teams.create(name='org-team') + # inventory = organization.inventories.create(name="associated-inv") + project.jobtemplates.create(name="test-jt", + description="test-job-template-desc", + inventory=inventory, + playbook="test_playbook.yml") return organization @@ -25,8 +25,9 @@ def test_org_counts_admin(resourced_organization, user, get): # Check that all types of resources are counted by a superuser external_admin = user('admin', True) response = get(reverse('api:organization_list', args=[]), external_admin) - counts = response.data['results'][0]['summary_fields']['counts'] + assert response.status_code == 200 + counts = response.data['results'][0]['summary_fields']['related_field_counts'] assert counts == { 'users': 1, 'admins': 1, @@ -42,7 +43,9 @@ def test_org_counts_member(resourced_organization, get): # user count, consistent with the RBAC rules member_user = resourced_organization.users.get(username='org-member') response = get(reverse('api:organization_list', args=[]), member_user) - counts = response.data['results'][0]['summary_fields']['counts'] + assert response.status_code == 200 + + counts = response.data['results'][0]['summary_fields']['related_field_counts'] assert counts == { 'users': 1, # User can see themselves @@ -60,10 +63,10 @@ def test_new_org_zero_counts(user, post): 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 post_response.status_code == 201 + + new_org_list = post_response.render().data + counts_dict = new_org_list['summary_fields']['related_field_counts'] assert counts_dict == { 'users': 0, 'admins': 0, @@ -79,12 +82,14 @@ def test_two_organizations(resourced_organization, organizations, user, get): external_admin = user('admin', True) organization_zero = organizations(1)[0] response = get(reverse('api:organization_list', args=[]), external_admin) + assert response.status_code == 200 + org_id_full = resourced_organization.id org_id_zero = organization_zero.id counts = {} for i in range(2): org_id = response.data['results'][i]['id'] - counts[org_id] = response.data['results'][i]['summary_fields']['counts'] + counts[org_id] = response.data['results'][i]['summary_fields']['related_field_counts'] assert counts[org_id_full] == { 'users': 1, @@ -102,3 +107,49 @@ def test_two_organizations(resourced_organization, organizations, user, get): 'inventories': 0, 'teams': 0 } + +@pytest.mark.django_db +def test_overlapping_project(resourced_organization, organizations, user, get): + # Check correct results for two organizations are returned + external_admin = user('admin', True) + organization2 = organizations(1)[0] + the_project = resourced_organization.projects.all()[0] + organization2.projects.add(the_project) + organization2.projects.create(name="second-project", + description="test-proj-desc", + scm_type="git", + scm_url="https://github.com/jlaska/ansible-playbooks") + inventory = organization2.inventories.create(name="second-inventory") + organization2.projects.get(name="second-project").jobtemplates.create( + name="second-job-template", + inventory=inventory, + playbook="hello.yml" + ) + + response = get(reverse('api:organization_list', args=[]), external_admin) + assert response.status_code == 200 + + org_id_full = resourced_organization.id + org_id2 = organization2.id + counts = {} + for i in range(2): + org_id = response.data['results'][i]['id'] + counts[org_id] = response.data['results'][i]['summary_fields']['related_field_counts'] + + assert counts[org_id_full] == { + 'users': 1, + 'admins': 1, + 'job_templates': 1, + 'projects': 1, + 'inventories': 1, + 'teams': 1 + } + assert counts[org_id2] == { + 'users': 0, + 'admins': 0, + 'job_templates': 2, + 'projects': 2, + 'inventories': 1, + 'teams': 0 + } + assert False From 52cd4f5ef94a303ef8b839300165f233a77c4822 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Tue, 15 Mar 2016 15:06:00 -0400 Subject: [PATCH 6/6] reduce test to only check project inventory connection --- .../api/test_organization_counts.py | 53 ++++++++----------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/awx/main/tests/functional/api/test_organization_counts.py b/awx/main/tests/functional/api/test_organization_counts.py index de629dbcf4..8d881fe8a0 100644 --- a/awx/main/tests/functional/api/test_organization_counts.py +++ b/awx/main/tests/functional/api/test_organization_counts.py @@ -109,47 +109,36 @@ def test_two_organizations(resourced_organization, organizations, user, get): } @pytest.mark.django_db -def test_overlapping_project(resourced_organization, organizations, user, get): - # Check correct results for two organizations are returned +def test_JT_associated_with_project(organizations, project, user, get): + # Check that adding a project to an organization gets the project's JT + # included in the organization's JT count external_admin = user('admin', True) - organization2 = organizations(1)[0] - the_project = resourced_organization.projects.all()[0] - organization2.projects.add(the_project) - organization2.projects.create(name="second-project", - description="test-proj-desc", - scm_type="git", - scm_url="https://github.com/jlaska/ansible-playbooks") - inventory = organization2.inventories.create(name="second-inventory") - organization2.projects.get(name="second-project").jobtemplates.create( - name="second-job-template", - inventory=inventory, - playbook="hello.yml" - ) + two_orgs = organizations(2) + organization = two_orgs[0] + other_org = two_orgs[1] + + unrelated_inv = other_org.inventories.create(name='not-in-organization') + project.jobtemplates.create(name="test-jt", + description="test-job-template-desc", + inventory=unrelated_inv, + playbook="test_playbook.yml") + organization.projects.add(project) response = get(reverse('api:organization_list', args=[]), external_admin) assert response.status_code == 200 - org_id_full = resourced_organization.id - org_id2 = organization2.id + org_id = organization.id counts = {} for i in range(2): - org_id = response.data['results'][i]['id'] - counts[org_id] = response.data['results'][i]['summary_fields']['related_field_counts'] + working_id = response.data['results'][i]['id'] + counts[working_id] = response.data['results'][i]['summary_fields']['related_field_counts'] - assert counts[org_id_full] == { - 'users': 1, - 'admins': 1, - 'job_templates': 1, - 'projects': 1, - 'inventories': 1, - 'teams': 1 - } - assert counts[org_id2] == { + assert counts[org_id] == { 'users': 0, 'admins': 0, - 'job_templates': 2, - 'projects': 2, - 'inventories': 1, + 'job_templates': 1, + 'projects': 1, + 'inventories': 0, 'teams': 0 } - assert False +