Add more tests around existing validation
This commit is contained in:
parent
259207214f
commit
46ecb84683
|
@ -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):
|
||||
|
|
|
@ -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])
|
||||
|
|
Loading…
Reference in New Issue