Merge "validate_integer doesn't check non int properly"
This commit is contained in:
commit
81dde2cbdb
@ -1395,25 +1395,31 @@ class TestComparableMixin(test.TestCase):
|
|||||||
self.one._compare(1, self.one._cmpkey))
|
self.one._compare(1, self.one._cmpkey))
|
||||||
|
|
||||||
|
|
||||||
|
@ddt.ddt
|
||||||
class TestValidateInteger(test.TestCase):
|
class TestValidateInteger(test.TestCase):
|
||||||
|
|
||||||
def test_validate_integer_greater_than_max_int_limit(self):
|
@ddt.data(
|
||||||
value = (2 ** 31) + 1
|
(2 ** 31) + 1, # More than max value
|
||||||
|
-12, # Less than min value
|
||||||
|
2.05, # Float value
|
||||||
|
"12.05", # Float value in string format
|
||||||
|
"should be int", # String
|
||||||
|
u"test" # String in unicode format
|
||||||
|
)
|
||||||
|
def test_validate_integer_raise_assert(self, value):
|
||||||
self.assertRaises(webob.exc.HTTPBadRequest,
|
self.assertRaises(webob.exc.HTTPBadRequest,
|
||||||
utils.validate_integer,
|
utils.validate_integer,
|
||||||
value, 'limit', min_value=-1, max_value=(2 ** 31))
|
value, 'limit', min_value=-1, max_value=(2 ** 31))
|
||||||
|
|
||||||
def test_validate_integer_less_than_min_int_limit(self):
|
@ddt.data(
|
||||||
value = -12
|
"123", # integer in string format
|
||||||
self.assertRaises(webob.exc.HTTPBadRequest,
|
123, # integer
|
||||||
utils.validate_integer,
|
u"123" # integer in unicode format
|
||||||
value, 'limit', min_value=-1, max_value=(2 ** 31))
|
)
|
||||||
|
def test_validate_integer(self, value):
|
||||||
def test_validate_integer_invalid_limit(self):
|
res = utils.validate_integer(value, 'limit', min_value=-1,
|
||||||
value = "should_be_int"
|
max_value=(2 ** 31))
|
||||||
self.assertRaises(webob.exc.HTTPBadRequest,
|
self.assertEqual(123, res)
|
||||||
utils.validate_integer,
|
|
||||||
value, 'limit', min_value=-1, max_value=(2 ** 31))
|
|
||||||
|
|
||||||
|
|
||||||
@ddt.ddt
|
@ddt.ddt
|
||||||
|
@ -1038,11 +1038,10 @@ def validate_integer(value, name, min_value=None, max_value=None):
|
|||||||
:param max_length: the max_length of the integer
|
:param max_length: the max_length of the integer
|
||||||
:returns: integer
|
:returns: integer
|
||||||
"""
|
"""
|
||||||
try:
|
if not strutils.is_int_like(value):
|
||||||
value = int(value)
|
|
||||||
except (TypeError, ValueError, UnicodeEncodeError):
|
|
||||||
raise webob.exc.HTTPBadRequest(explanation=(
|
raise webob.exc.HTTPBadRequest(explanation=(
|
||||||
_('%s must be an integer.') % name))
|
_('%s must be an integer.') % name))
|
||||||
|
value = int(value)
|
||||||
|
|
||||||
if min_value is not None and value < min_value:
|
if min_value is not None and value < min_value:
|
||||||
raise webob.exc.HTTPBadRequest(
|
raise webob.exc.HTTPBadRequest(
|
||||||
|
Loading…
Reference in New Issue
Block a user