From 606d4e0ea22cb6511923d36778b264d551636519 Mon Sep 17 00:00:00 2001 From: Jake Jackson Date: Thu, 21 May 2020 16:50:27 -0400 Subject: [PATCH] added change so that groups and host names cannot share the same name in the same inventory. added tests to validate --- awx/api/serializers.py | 4 ++++ awx/main/tests/functional/api/test_inventory.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 9f99cd8ab4..840db64458 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -1719,6 +1719,8 @@ class HostSerializer(BaseSerializerWithVariables): name = force_text(value or '') # Validate here only, update in main validate method. host, port = self._get_host_port_from_name(name) + if Group.objects.filter(name=value).exists(): + raise serializers.ValidationError(_('Invalid group name. Name already exists as a Group.')) return value def validate_inventory(self, value): @@ -1808,6 +1810,8 @@ class GroupSerializer(BaseSerializerWithVariables): def validate_name(self, value): if value in ('all', '_meta'): raise serializers.ValidationError(_('Invalid group name.')) + elif Host.objects.filter(name=value).exists(): + raise serializers.ValidationError(_('Invalid group name. Name already exists as a Host.')) return value def validate_inventory(self, value): diff --git a/awx/main/tests/functional/api/test_inventory.py b/awx/main/tests/functional/api/test_inventory.py index 7a25f7e1cd..ea4435845a 100644 --- a/awx/main/tests/functional/api/test_inventory.py +++ b/awx/main/tests/functional/api/test_inventory.py @@ -59,6 +59,21 @@ def test_inventory_source_unique_together_with_inv(inventory_factory): is2 = InventorySource(name='foo', source='file', inventory=inv2) is2.validate_unique() +@pytest.mark.django_db +def test_inventory_host_name_unique(scm_inventory, post, admin_user): + inv_src = scm_inventory.inventory_sources.first() + group = inv_src.groups.create(name='barfoo', inventory=scm_inventory) + group.save() + host1 = inv_src.hosts.create(name='barfoo', inventory=scm_inventory) + post(reverse('api:inventory_hosts_list', kwargs={'pk': host1.id}), admin_user, expect=400) + +@pytest.mark.django_db +def test_inventory_group_name_unique(scm_inventory, post, admin_user): + inv_src = scm_inventory.inventory_sources.first() + host = inv_src.hosts.create(name='barfoo', inventory=scm_inventory) + host.save() + group = inv_src.groups.create(name='barfoo', inventory=scm_inventory) + post(reverse('api:inventory_groups_list', kwargs={'pk': group.id}), admin_user, expect=400) @pytest.mark.parametrize("role_field,expected_status_code", [ (None, 403),