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
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') @JSONSchemaField.format_checker.checks('url')
def format_url(value): 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( raise jsonschema.exceptions.FormatError(
'Invalid URL: Missing url scheme (http, https, etc.)' '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 @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( credential_type = CredentialType(
kind='test', kind='test',
name='MyTestCredentialType', name='MyTestCredentialType',
@@ -1961,7 +1961,7 @@ def test_create_credential_with_missing_url_schema_xfail(post, organization, adm
credential_type.save() credential_type.save()
params = { params = {
'name': 'Second Best credential ever', 'name': 'Second Best Credential Ever',
'organization': organization.pk, 'organization': organization.pk,
'credential_type': credential_type.pk, 'credential_type': credential_type.pk,
'inputs': {'server_url': 'foo.com'} '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'}) endpoint = reverse('api:credential_list', kwargs={'version': 'v2'})
response = post(endpoint, params, admin) response = post(endpoint, params, admin)
assert response.status_code == 400 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) response = post(endpoint, params, admin)
assert response.status_code == 201 assert response.status_code == 201