diff --git a/awx/main/models/inventory.py b/awx/main/models/inventory.py index cb18d4844c..e3b71ee594 100644 --- a/awx/main/models/inventory.py +++ b/awx/main/models/inventory.py @@ -370,6 +370,8 @@ class Inventory(CommonModelNameNotUnique, ResourceMixin): return self.groups.exclude(parents__pk__in=group_pks).distinct() def clean_insights_credential(self): + if self.kind == 'smart': + raise ValidationError(_("Assignment not allowed for Smart Inventory")) if self.insights_credential and self.insights_credential.credential_type.kind != 'insights': raise ValidationError(_("Credential kind must be 'insights'.")) return self.insights_credential diff --git a/awx/main/tests/unit/models/test_inventory.py b/awx/main/tests/unit/models/test_inventory.py index 900881aa4c..4f0d5eddd8 100644 --- a/awx/main/tests/unit/models/test_inventory.py +++ b/awx/main/tests/unit/models/test_inventory.py @@ -1,9 +1,16 @@ import pytest import mock +import json + +from django.core.exceptions import ValidationError + from awx.main.models import ( UnifiedJob, InventoryUpdate, Job, + Inventory, + Credential, + CredentialType, ) @@ -36,3 +43,33 @@ def test__build_job_explanation(): assert job_explanation == 'Previous Task Canceled: {"job_type": "%s", "job_name": "%s", "job_id": "%s"}' % \ ('inventory_update', 'I_am_an_Inventory_Update', 3) + + +def test_valid_clean_insights_credential(): + cred_type = CredentialType.defaults['insights']() + insights_cred = Credential(credential_type=cred_type) + inv = Inventory(insights_credential=insights_cred) + + inv.clean_insights_credential() + + +def test_invalid_clean_insights_credential(): + cred_type = CredentialType.defaults['scm']() + cred = Credential(credential_type=cred_type) + inv = Inventory(insights_credential=cred) + + with pytest.raises(ValidationError) as e: + inv.clean_insights_credential() + + assert json.dumps(str(e.value)) == json.dumps(str([u"Credential kind must be 'insights'."])) + + +def test_invalid_kind_clean_insights_credential(): + cred_type = CredentialType.defaults['insights']() + insights_cred = Credential(credential_type=cred_type) + inv = Inventory(kind='smart', insights_credential=insights_cred) + + with pytest.raises(ValidationError) as e: + inv.clean_insights_credential() + + assert json.dumps(str(e.value)) == json.dumps(str([u'Assignment not allowed for Smart Inventory']))