From 84b21620b265fb0fd9fb17225a949729a3106348 Mon Sep 17 00:00:00 2001 From: Jake McDermott Date: Wed, 1 May 2019 08:56:56 -0400 Subject: [PATCH] raise url string parsing error as validation error --- awx/main/fields.py | 6 +++++- awx/main/tests/functional/api/test_credential.py | 12 +++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/awx/main/fields.py b/awx/main/fields.py index ab97552e81..4914cc8b07 100644 --- a/awx/main/fields.py +++ b/awx/main/fields.py @@ -492,7 +492,11 @@ def format_ssh_private_key(value): @JSONSchemaField.format_checker.checks('url') def format_url(value): - if urllib.parse.urlparse(value).scheme == '': + try: + scheme = urllib.parse.urlparse(value).scheme + except Exception as e: + raise jsonschema.exceptions.FormatError(str(e)) + if scheme == '': raise jsonschema.exceptions.FormatError( 'Invalid URL: Missing url scheme (http, https, etc.)' ) diff --git a/awx/main/tests/functional/api/test_credential.py b/awx/main/tests/functional/api/test_credential.py index abf4cdc682..0e82785eab 100644 --- a/awx/main/tests/functional/api/test_credential.py +++ b/awx/main/tests/functional/api/test_credential.py @@ -1945,7 +1945,7 @@ def test_create_credential_missing_user_team_org_xfail(post, admin, credentialty @pytest.mark.django_db -def test_create_credential_with_missing_url_schema_xfail(post, organization, admin): +def test_create_credential_with_invalid_url_xfail(post, organization, admin): credential_type = CredentialType( kind='test', name='MyTestCredentialType', @@ -1961,7 +1961,7 @@ def test_create_credential_with_missing_url_schema_xfail(post, organization, adm credential_type.save() params = { - 'name': 'Second Best credential ever', + 'name': 'Second Best Credential Ever', 'organization': organization.pk, 'credential_type': credential_type.pk, 'inputs': {'server_url': 'foo.com'} @@ -1969,7 +1969,13 @@ def test_create_credential_with_missing_url_schema_xfail(post, organization, adm endpoint = reverse('api:credential_list', kwargs={'version': 'v2'}) response = post(endpoint, params, admin) assert response.status_code == 400 + assert response.data['inputs']['server_url'] == ['Invalid URL: Missing url scheme (http, https, etc.)'] - params['inputs'] = {'server_url': 'http://foo.com'} + params['inputs']['server_url'] = 'https://[dead:beef' + response = post(endpoint, params, admin) + assert response.status_code == 400 + assert response.data['inputs']['server_url'] == ['Invalid IPv6 URL'] + + params['inputs']['server_url'] = 'http://foo.com' response = post(endpoint, params, admin) assert response.status_code == 201