Skip to content

Commit

Permalink
Handle negative limits.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyli committed May 1, 2015
1 parent b82cfed commit 26d557c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
3 changes: 3 additions & 0 deletions mimic/model/nova_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,9 @@ def request_list(self, http_get_request, include_details, absolutize_url,
except ValueError:
return dumps(bad_request("limit param must be an integer",
http_get_request))
if limit < 0:
return dumps(bad_request("limit param must be positive",
http_get_request))

to_be_listed = to_be_listed[:limit]

Expand Down
27 changes: 22 additions & 5 deletions mimic/test/test_nova.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,11 @@ def test_with_invalid_marker(self):
},
error_body)

def test_with_invalid_limit(self):
def _check_invalid_limit(self, limit, message):
"""
If a limit that can't be converted into an integer is passed, no
matter what other parameters there are, return with a 400 bad request.
Make a request with an invalid limit against every possible
combination of parameters, and assert that a 400 bad request is
returned with the given message.
"""
self.make_nova_app()
self.create_servers(2, lambda i: 'server')
Expand All @@ -522,17 +523,33 @@ def test_with_invalid_limit(self):

for path in ('/servers', '/servers/detail'):
for combo in combos:
combo['limit'] = 'a'
combo['limit'] = limit
error_body = self.list_servers(path, combo, code=400)
self.assertEqual(
{
"badRequest": {
"message": "limit param must be an integer",
"message": message,
"code": 400
}
},
error_body)

def test_with_non_int_limit(self):
"""
If a limit that can't be converted into an integer is passed, no
matter what other parameters there are, return with a 400 bad request.
"""
for non_int in ('a', '0.1', '[]'):
self._check_invalid_limit(
non_int, "limit param must be an integer")

def test_with_negative_limit(self):
"""
If a negative limit is passed, no matter what other parameters there
are, return 400 with a bad request.
"""
self._check_invalid_limit('-1', "limit param must be positive")

def test_with_limit_as_0(self):
"""
If a limit of 0 is passed, no matter what other parameters there are,
Expand Down

0 comments on commit 26d557c

Please sign in to comment.