From 001fa634aabed3f1675cf0a3f13be238def1a26e Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Mon, 19 Mar 2018 14:16:15 -0400 Subject: [PATCH] include related Jobs in group del protection --- awx/main/models/inventory.py | 5 +++- .../tests/functional/models/test_inventory.py | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/awx/main/models/inventory.py b/awx/main/models/inventory.py index 28337e719e..b63e8a48c0 100644 --- a/awx/main/models/inventory.py +++ b/awx/main/models/inventory.py @@ -960,7 +960,10 @@ class Group(CommonModelNameNotUnique, RelatedJobsMixin): RelatedJobsMixin ''' def _get_related_jobs(self): - return InventoryUpdate.objects.filter(inventory_source__in=self.inventory_sources.all()) + return UnifiedJob.objects.non_polymorphic().filter( + Q(Job___inventory=self.inventory) | + Q(InventoryUpdate___inventory_source__groups=self) + ) class InventorySourceOptions(BaseModel): diff --git a/awx/main/tests/functional/models/test_inventory.py b/awx/main/tests/functional/models/test_inventory.py index 63401f46f0..57365b914b 100644 --- a/awx/main/tests/functional/models/test_inventory.py +++ b/awx/main/tests/functional/models/test_inventory.py @@ -12,6 +12,7 @@ from awx.main.models import ( Inventory, InventorySource, InventoryUpdate, + Job ) from awx.main.utils.filters import SmartFilter @@ -99,6 +100,29 @@ class TestSCMUpdateFeatures: assert not mck_update.called +@pytest.mark.django_db +class TestRelatedJobs: + + def test_inventory_related(self, inventory): + job = Job.objects.create( + inventory=inventory + ) + assert job.id in [jerb.id for jerb in inventory._get_related_jobs()] + + def test_related_group_jobs(self, group): + job = Job.objects.create( + inventory=group.inventory + ) + assert job.id in [jerb.id for jerb in group._get_related_jobs()] + + def test_related_group_update(self, group): + src = group.inventory_sources.create(name='foo') + job = InventoryUpdate.objects.create( + inventory_source=src + ) + assert job.id in [jerb.id for jerb in group._get_related_jobs()] + + @pytest.mark.django_db class TestSCMClean: def test_clean_update_on_project_update_multiple(self, inventory):