Add more tests around existing validation

This commit is contained in:
Ian Cordasco 2017-03-08 19:11:18 -06:00
parent 259207214f
commit 46ecb84683
No known key found for this signature in database
GPG Key ID: 656D3395E4A9791A
2 changed files with 51 additions and 2 deletions

View File

@ -139,7 +139,7 @@ class Validator(object):
raise ValueError(
'"{}" is not a valid component'.format(component)
)
self.require_presence_of({
self.require_presence_of.update({
component: True for component in components
})
return self
@ -190,7 +190,7 @@ def ensure_required_components_exist(uri, required_components):
if getattr(uri, component) is None
])
if missing_components:
raise exceptions.MissingComponentError(uri)
raise exceptions.MissingComponentError(uri, *missing_components)
def is_valid(value, matcher, require):

View File

@ -46,6 +46,12 @@ def test_allowing_ports():
assert 100 in validator.allowed_ports
def test_requiring_invalid_component():
"""Verify that we validate required component names."""
with pytest.raises(ValueError):
validators.Validator().require_components('frob')
def test_use_of_password():
"""Verify the behaviour of {forbid,allow}_use_of_password."""
validator = validators.Validator()
@ -78,8 +84,51 @@ def test_forbidden_passwords(uri):
rfc3986.uri_reference('https://user@github.com/path?query'),
rfc3986.uri_reference('https://user@github.com/path?query#frag'),
rfc3986.uri_reference('//user@github.com'),
rfc3986.uri_reference('//github.com'),
rfc3986.uri_reference('https://github.com'),
])
def test_passwordless_uris_pass_validation(uri):
"""Verify password-less URLs validate properly."""
validator = validators.Validator().forbid_use_of_password()
validator.validate(uri)
@pytest.mark.parametrize('uri', [
rfc3986.uri_reference('https://'),
rfc3986.uri_reference('/path/to/resource'),
])
def test_missing_host_component(uri):
"""Verify that missing host components cause errors."""
validator = validators.Validator().require_components('host')
with pytest.raises(exceptions.MissingComponentError):
validator.validate(uri)
@pytest.mark.parametrize('uri', [
rfc3986.uri_reference('https://'),
rfc3986.uri_reference('//google.com'),
rfc3986.uri_reference('//google.com?query=value'),
rfc3986.uri_reference('//google.com#fragment'),
rfc3986.uri_reference('https://google.com'),
rfc3986.uri_reference('https://google.com#fragment'),
rfc3986.uri_reference('https://google.com?query=value'),
])
def test_missing_path_component(uri):
"""Verify that missing path components cause errors."""
validator = validators.Validator().require_components('path')
with pytest.raises(exceptions.MissingComponentError):
validator.validate(uri)
@pytest.mark.parametrize('uri', [
rfc3986.uri_reference('//google.com'),
rfc3986.uri_reference('//google.com?query=value'),
rfc3986.uri_reference('//google.com#fragment'),
])
def test_multiple_missing_components(uri):
"""Verify that multiple missing components are caught."""
validator = validators.Validator().require_components('scheme', 'path')
with pytest.raises(exceptions.MissingComponentError) as captured_exc:
validator.validate(uri)
exception = captured_exc.value
assert 2 == len(exception.args[-1])