Add more tests around validation

This commit is contained in:
Ian Cordasco 2017-03-09 06:56:27 -06:00
parent 46ecb84683
commit 74966158aa
No known key found for this signature in database
GPG Key ID: 656D3395E4A9791A
2 changed files with 27 additions and 3 deletions

View File

@ -110,7 +110,7 @@ class Validator(object):
for port in ports:
port_int = int(port, base=10)
if 0 <= port_int <= 65535:
self.allowed_ports.add(port_int)
self.allowed_ports.add(port)
return self
def allow_use_of_password(self):
@ -170,6 +170,10 @@ class Validator(object):
if required_components:
ensure_required_components_exist(uri, required_components)
ensure_one_of(self.allowed_schemes, uri, 'scheme')
ensure_one_of(self.allowed_hosts, uri, 'host')
ensure_one_of(self.allowed_ports, uri, 'port')
def check_password(uri):
"""Assert that there is no password present in the uri."""
@ -182,6 +186,15 @@ def check_password(uri):
raise exceptions.PasswordForbidden(uri)
def ensure_one_of(allowed_values, uri, attribute):
"""Assert that the uri's attribute is one of the allowed values."""
value = getattr(uri, attribute)
if allowed_values and value not in allowed_values:
raise exceptions.UnpermittedComponentError(
attribute, value, allowed_values,
)
def ensure_required_components_exist(uri, required_components):
"""Assert that all required components are present in the URI."""
missing_components = sorted([

View File

@ -42,8 +42,8 @@ def test_allowing_ports():
"""Verify the ability select ports to be allowed."""
validator = validators.Validator().allow_ports('80', '100')
assert 80 in validator.allowed_ports
assert 100 in validator.allowed_ports
assert '80' in validator.allowed_ports
assert '100' in validator.allowed_ports
def test_requiring_invalid_component():
@ -132,3 +132,14 @@ def test_multiple_missing_components(uri):
validator.validate(uri)
exception = captured_exc.value
assert 2 == len(exception.args[-1])
@pytest.mark.parametrize('uri', [
rfc3986.uri_reference('smtp://'),
rfc3986.uri_reference('telnet://'),
])
def test_ensure_uri_has_a_scheme(uri):
"""Verify validation with allowed schemes."""
validator = validators.Validator().allow_schemes('https', 'http')
with pytest.raises(exceptions.UnpermittedComponentError):
validator.validate(uri)