raise url string parsing error as validation error

This commit is contained in:
Jake McDermott 2019-05-01 08:56:56 -04:00
parent 9737ab620c
commit 84b21620b2
No known key found for this signature in database
GPG Key ID: 9A6F084352C3A0B7
2 changed files with 14 additions and 4 deletions

View File

@ -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.)'
)

View File

@ -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