refactored to use is_valid_relation instead of post

This commit is contained in:
Gabe Muniz
2023-02-14 00:07:49 -05:00
parent 3ff65db2e6
commit d55af032f7
2 changed files with 30 additions and 24 deletions

View File

@@ -14,6 +14,7 @@ from django.utils.translation import gettext_lazy as _
from rest_framework.exceptions import PermissionDenied from rest_framework.exceptions import PermissionDenied
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework import status from rest_framework import status
from rest_framework import serializers
# AWX # AWX
from awx.main.models import ActivityStream, Inventory, JobTemplate, Role, User, InstanceGroup, InventoryUpdateEvent, InventoryUpdate from awx.main.models import ActivityStream, Inventory, JobTemplate, Role, User, InstanceGroup, InventoryUpdateEvent, InventoryUpdate
@@ -115,14 +116,9 @@ class InventoryInputInventoriesList(SubListAttachDetachAPIView):
parent_model = Inventory parent_model = Inventory
relationship = 'input_inventories' relationship = 'input_inventories'
# Specifically overriding the post method on this view to disallow constructed inventories as input inventories def is_valid_relation(self, parent, sub, created=False):
def post(self, request, *args, **kwargs): if sub.kind == 'constructed':
obj = Inventory.objects.get(id=request.data.get('id')) raise serializers.ValidationError({'error': 'You cannot add a constructed inventory to another constructed inventory.'})
if obj.kind == 'constructed':
return Response(
dict(error=_('You cannot add a constructed inventory to another constructed inventory.')), status=status.HTTP_405_METHOD_NOT_ALLOWED
)
return super(InventoryInputInventoriesList, self).post(request, *args, **kwargs)
class InventoryActivityStreamList(SubListAPIView): class InventoryActivityStreamList(SubListAPIView):

View File

@@ -8,26 +8,35 @@ def constructed_inventory(organization):
""" """
creates a new constructed inventory source creates a new constructed inventory source
""" """
return Inventory.objects.create(name='dummy2', kind='constructed', organization=organization)
def factory(name, org=organization):
try:
inv = Inventory.objects.get(name=name, organization=org, kind='constructed')
except Inventory.DoesNotExist:
inv = Inventory.objects.create(name=name, organization=org, kind='constructed')
return inv
return factory
@pytest.mark.django_db @pytest.mark.django_db
def test_constructed_inventory_post(post, organization, admin_user): def test_constructed_inventory_post(post, admin_user, constructed_inventory):
inventory1 = Inventory.objects.create(name='dummy1', kind='constructed', organization=organization) inv1 = constructed_inventory(name='dummy1')
inventory2 = Inventory.objects.create(name='dummy2', kind='constructed', organization=organization) inv2 = constructed_inventory(name='dummy2')
resp = post( resp = post(
url=reverse('api:inventory_input_inventories', kwargs={'pk': inventory1.pk}), url=reverse('api:inventory_input_inventories', kwargs={'pk': inv1.pk}),
data={'id': inventory2.pk}, data={'id': inv2.pk},
user=admin_user, user=admin_user,
expect=405, expect=400,
) )
assert resp.status_code == 405 assert resp.status_code == 400
@pytest.mark.django_db @pytest.mark.django_db
def test_add_constructed_inventory_source(post, admin_user, constructed_inventory): def test_add_constructed_inventory_source(post, admin_user, constructed_inventory):
inv1 = constructed_inventory(name='dummy1')
resp = post( resp = post(
url=reverse('api:inventory_inventory_sources_list', kwargs={'pk': constructed_inventory.pk}), url=reverse('api:inventory_inventory_sources_list', kwargs={'pk': inv1.pk}),
data={'name': 'dummy1', 'source': 'constructed'}, data={'name': 'dummy1', 'source': 'constructed'},
user=admin_user, user=admin_user,
expect=400, expect=400,
@@ -37,8 +46,9 @@ def test_add_constructed_inventory_source(post, admin_user, constructed_inventor
@pytest.mark.django_db @pytest.mark.django_db
def test_add_constructed_inventory_host(post, admin_user, constructed_inventory): def test_add_constructed_inventory_host(post, admin_user, constructed_inventory):
inv1 = constructed_inventory(name='dummy1')
resp = post( resp = post(
url=reverse('api:inventory_hosts_list', kwargs={'pk': constructed_inventory.pk}), url=reverse('api:inventory_hosts_list', kwargs={'pk': inv1.pk}),
data={'name': 'dummy1'}, data={'name': 'dummy1'},
user=admin_user, user=admin_user,
expect=400, expect=400,
@@ -48,8 +58,9 @@ def test_add_constructed_inventory_host(post, admin_user, constructed_inventory)
@pytest.mark.django_db @pytest.mark.django_db
def test_add_constructed_inventory_group(post, admin_user, constructed_inventory): def test_add_constructed_inventory_group(post, admin_user, constructed_inventory):
inv1 = constructed_inventory(name='dummy1')
resp = post( resp = post(
reverse('api:inventory_groups_list', kwargs={'pk': constructed_inventory.pk}), reverse('api:inventory_groups_list', kwargs={'pk': inv1.pk}),
data={'name': 'group-test'}, data={'name': 'group-test'},
user=admin_user, user=admin_user,
expect=400, expect=400,
@@ -58,12 +69,11 @@ def test_add_constructed_inventory_group(post, admin_user, constructed_inventory
@pytest.mark.django_db @pytest.mark.django_db
def test_edit_constructed_inventory_source(patch, admin_user, inventory): def test_edit_constructed_inventory_source(patch, admin_user, inventory_source_factory):
inventory.inventory_sources.create(name="dummysrc", source="constructed") inv_src = inventory_source_factory(name='dummy1', source='constructed')
inv_id = inventory.inventory_sources.get(name='dummysrc').id
resp = patch( resp = patch(
reverse('api:inventory_source_detail', kwargs={'pk': inv_id}), reverse('api:inventory_source_detail', kwargs={'pk': inv_src.id}),
data={'description': inventory.name}, data={'description': inv_src.name},
user=admin_user, user=admin_user,
expect=400, expect=400,
) )