remove ability to add constructed inventories to constructed inventories

This commit is contained in:
Gabe Muniz 2023-02-10 12:46:11 -05:00
parent c98f86a355
commit 2d9f2d36a1
2 changed files with 26 additions and 0 deletions

View File

@ -115,6 +115,15 @@ class InventoryInputInventoriesList(SubListAttachDetachAPIView):
parent_model = Inventory
relationship = 'input_inventories'
# Specifically overriding the post method on this view to disallow constructed inventories as input inventories
def post(self, request, *args, **kwargs):
obj = Inventory.objects.get(id=request.data.get('id'))
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):
model = ActivityStream

View File

@ -0,0 +1,17 @@
import pytest
from awx.main.models import Inventory
from awx.api.versioning import reverse
@pytest.mark.django_db
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},
user=admin_user,
expect=405,
)
print(resp)
assert resp.status_code == 405