Skip to content

Commit

Permalink
require a valid netloc for Credential Type inputs w/ format=url
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanpetrello committed May 2, 2019
1 parent de56e20 commit e560dcc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
8 changes: 6 additions & 2 deletions awx/main/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,13 +493,17 @@ def format_ssh_private_key(value):
@JSONSchemaField.format_checker.checks('url')
def format_url(value):
try:
scheme = urllib.parse.urlparse(value).scheme
parsed = urllib.parse.urlparse(value)
except Exception as e:
raise jsonschema.exceptions.FormatError(str(e))
if scheme == '':
if parsed.scheme == '':
raise jsonschema.exceptions.FormatError(
'Invalid URL: Missing url scheme (http, https, etc.)'
)
if parsed.netloc == '':
raise jsonschema.exceptions.FormatError(
'Invalid URL: {}'.format(value)
)
return True


Expand Down
25 changes: 12 additions & 13 deletions awx/main/tests/functional/api/test_credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -1944,8 +1944,15 @@ def test_create_credential_missing_user_team_org_xfail(post, admin, credentialty
assert response.status_code == 400


@pytest.mark.parametrize('url, status, msg', [
('foo.com', 400, 'Invalid URL: Missing url scheme (http, https, etc.)'),
('https://[dead:beef', 400, 'Invalid IPv6 URL'),
('http:domain:8080', 400, 'Invalid URL: http:domain:8080'),
('http:/domain:8080', 400, 'Invalid URL: http:/domain:8080'),
('http://foo.com', 201, None)
])
@pytest.mark.django_db
def test_create_credential_with_invalid_url_xfail(post, organization, admin):
def test_create_credential_with_invalid_url_xfail(post, organization, admin, url, status, msg):
credential_type = CredentialType(
kind='test',
name='MyTestCredentialType',
Expand All @@ -1964,18 +1971,10 @@ def test_create_credential_with_invalid_url_xfail(post, organization, admin):
'name': 'Second Best Credential Ever',
'organization': organization.pk,
'credential_type': credential_type.pk,
'inputs': {'server_url': 'foo.com'}
'inputs': {'server_url': url}
}
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'] = '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
assert response.status_code == status
if status != 201:
assert response.data['inputs']['server_url'] == [msg]

0 comments on commit e560dcc

Please sign in to comment.