From 3ff65db2e6a944c91122cbf36c8c193bca4f8af7 Mon Sep 17 00:00:00 2001 From: Gabe Muniz Date: Sat, 11 Feb 2023 14:43:26 -0500 Subject: [PATCH] block updates to constructed source type --- awx/api/serializers.py | 2 ++ .../test_inventory_input_constructed.py | 28 ++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 7a9721b4e7..7e77b04af0 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -2244,6 +2244,8 @@ class InventorySourceSerializer(UnifiedJobTemplateSerializer, InventorySourceOpt obj = super(InventorySourceSerializer, self).update(obj, validated_data) if deprecated_fields: self._update_deprecated_fields(deprecated_fields, obj) + if obj.source == 'constructed': + raise serializers.ValidationError({'error': _("Cannot edit source of type constructed.")}) return obj # TODO: remove when old 'credential' fields are removed diff --git a/awx/main/tests/functional/test_inventory_input_constructed.py b/awx/main/tests/functional/test_inventory_input_constructed.py index db56521b9a..e677d46f46 100644 --- a/awx/main/tests/functional/test_inventory_input_constructed.py +++ b/awx/main/tests/functional/test_inventory_input_constructed.py @@ -4,10 +4,11 @@ from awx.api.versioning import reverse @pytest.fixture -def constructed_inventory(organization, inventory): - inv_source = Inventory.objects.create(name='dummy2', kind='constructed', organization=organization) - - return inv_source +def constructed_inventory(organization): + """ + creates a new constructed inventory source + """ + return Inventory.objects.create(name='dummy2', kind='constructed', organization=organization) @pytest.mark.django_db @@ -15,8 +16,8 @@ def test_constructed_inventory_post(post, organization, admin_user): inventory1 = Inventory.objects.create(name='dummy1', kind='constructed', organization=organization) inventory2 = Inventory.objects.create(name='dummy2', kind='constructed', organization=organization) resp = post( - url=reverse('api:inventory_input_inventories', kwargs={'pk': inventory1.id}), - data={'id': inventory2.id}, + url=reverse('api:inventory_input_inventories', kwargs={'pk': inventory1.pk}), + data={'id': inventory2.pk}, user=admin_user, expect=405, ) @@ -26,7 +27,7 @@ def test_constructed_inventory_post(post, organization, admin_user): @pytest.mark.django_db def test_add_constructed_inventory_source(post, admin_user, constructed_inventory): resp = post( - url=reverse('api:inventory_inventory_sources_list', kwargs={'pk': constructed_inventory.id}), + url=reverse('api:inventory_inventory_sources_list', kwargs={'pk': constructed_inventory.pk}), data={'name': 'dummy1', 'source': 'constructed'}, user=admin_user, expect=400, @@ -54,3 +55,16 @@ def test_add_constructed_inventory_group(post, admin_user, constructed_inventory expect=400, ) assert resp.status_code == 400 + + +@pytest.mark.django_db +def test_edit_constructed_inventory_source(patch, admin_user, inventory): + inventory.inventory_sources.create(name="dummysrc", source="constructed") + inv_id = inventory.inventory_sources.get(name='dummysrc').id + resp = patch( + reverse('api:inventory_source_detail', kwargs={'pk': inv_id}), + data={'description': inventory.name}, + user=admin_user, + expect=400, + ) + assert resp.status_code == 400