mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 09:27:36 -02:30
Merge pull request #441 from ryanpetrello/fix-7607
allow the credential type to be changed for unused credentials
This commit is contained in:
@@ -2162,10 +2162,22 @@ class CredentialSerializer(BaseSerializer):
|
|||||||
|
|
||||||
def validate_credential_type(self, credential_type):
|
def validate_credential_type(self, credential_type):
|
||||||
if self.instance and credential_type.pk != self.instance.credential_type.pk:
|
if self.instance and credential_type.pk != self.instance.credential_type.pk:
|
||||||
raise ValidationError(
|
for rel in (
|
||||||
_('You cannot change the credential type of the credential, as it may break the functionality'
|
'ad_hoc_commands',
|
||||||
' of the resources using it.'),
|
'insights_inventories',
|
||||||
)
|
'inventorysources',
|
||||||
|
'inventoryupdates',
|
||||||
|
'jobs',
|
||||||
|
'jobtemplates',
|
||||||
|
'projects',
|
||||||
|
'projectupdates',
|
||||||
|
'workflowjobnodes'
|
||||||
|
):
|
||||||
|
if getattr(self.instance, rel).count() > 0:
|
||||||
|
raise ValidationError(
|
||||||
|
_('You cannot change the credential type of the credential, as it may break the functionality'
|
||||||
|
' of the resources using it.'),
|
||||||
|
)
|
||||||
return credential_type
|
return credential_type
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ import re
|
|||||||
import mock # noqa
|
import mock # noqa
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from awx.main.models.credential import Credential, CredentialType
|
from awx.main.models import (AdHocCommand, Credential, CredentialType, Job, JobTemplate,
|
||||||
|
Inventory, InventorySource, Project,
|
||||||
|
WorkflowJobNode)
|
||||||
from awx.main.utils import decrypt_field
|
from awx.main.utils import decrypt_field
|
||||||
from awx.api.versioning import reverse
|
from awx.api.versioning import reverse
|
||||||
|
|
||||||
@@ -1410,7 +1412,17 @@ def test_field_removal(put, organization, admin, credentialtype_ssh, version, pa
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_credential_type_immutable_in_v2(patch, organization, admin, credentialtype_ssh, credentialtype_aws):
|
@pytest.mark.parametrize('relation, related_obj', [
|
||||||
|
['ad_hoc_commands', AdHocCommand()],
|
||||||
|
['insights_inventories', Inventory()],
|
||||||
|
['inventorysources', InventorySource()],
|
||||||
|
['jobs', Job()],
|
||||||
|
['jobtemplates', JobTemplate()],
|
||||||
|
['projects', Project()],
|
||||||
|
['workflowjobnodes', WorkflowJobNode()],
|
||||||
|
])
|
||||||
|
def test_credential_type_mutability(patch, organization, admin, credentialtype_ssh,
|
||||||
|
credentialtype_aws, relation, related_obj):
|
||||||
cred = Credential(
|
cred = Credential(
|
||||||
credential_type=credentialtype_ssh,
|
credential_type=credentialtype_ssh,
|
||||||
name='Best credential ever',
|
name='Best credential ever',
|
||||||
@@ -1422,19 +1434,31 @@ def test_credential_type_immutable_in_v2(patch, organization, admin, credentialt
|
|||||||
)
|
)
|
||||||
cred.save()
|
cred.save()
|
||||||
|
|
||||||
response = patch(
|
related_obj.save()
|
||||||
reverse('api:credential_detail', kwargs={'version': 'v2', 'pk': cred.pk}),
|
getattr(cred, relation).add(related_obj)
|
||||||
{
|
|
||||||
'credential_type': credentialtype_aws.pk,
|
def _change_credential_type():
|
||||||
'inputs': {
|
return patch(
|
||||||
'username': u'jim',
|
reverse('api:credential_detail', kwargs={'version': 'v2', 'pk': cred.pk}),
|
||||||
'password': u'pass'
|
{
|
||||||
}
|
'credential_type': credentialtype_aws.pk,
|
||||||
},
|
'inputs': {
|
||||||
admin
|
'username': u'jim',
|
||||||
)
|
'password': u'pass'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
admin
|
||||||
|
)
|
||||||
|
|
||||||
|
response = _change_credential_type()
|
||||||
assert response.status_code == 400
|
assert response.status_code == 400
|
||||||
assert 'credential_type' in response.data
|
expected = ['You cannot change the credential type of the credential, '
|
||||||
|
'as it may break the functionality of the resources using it.']
|
||||||
|
assert response.data['credential_type'] == expected
|
||||||
|
|
||||||
|
related_obj.delete()
|
||||||
|
response = _change_credential_type()
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
|
|||||||
Reference in New Issue
Block a user