Merge "Make tempurl subcommand insist on whole number seconds"

This commit is contained in:
Jenkins 2016-09-15 02:34:01 +00:00 committed by Gerrit Code Review
commit 0ec6b7b162
2 changed files with 25 additions and 8 deletions

View File

@ -80,17 +80,19 @@ def generate_temp_url(path, seconds, key, method, absolute=False):
:param absolute: if True then the seconds parameter is interpreted as an
absolute Unix time, otherwise seconds is interpreted as a relative time
offset from current time.
:raises: ValueError if seconds is not a positive integer or path is not to
:raises: ValueError if seconds is not a whole number or path is not to
an object.
:raises: TypeError if seconds is not an integer
:return: the path portion of a temporary URL
"""
try:
seconds = float(seconds)
if not seconds.is_integer():
raise ValueError()
seconds = int(seconds)
except ValueError:
raise TypeError('seconds must be an integer')
if seconds < 0:
raise ValueError('seconds must be a positive integer')
raise ValueError()
except ValueError:
raise ValueError('seconds must be a whole number')
if isinstance(path, six.binary_type):
try:

View File

@ -170,15 +170,30 @@ class TestTempURL(unittest.TestCase):
self.assertEqual(url, expected_url)
def test_generate_temp_url_bad_seconds(self):
with self.assertRaises(TypeError) as exc_manager:
with self.assertRaises(ValueError) as exc_manager:
u.generate_temp_url(self.url, 'not_an_int', self.key, self.method)
self.assertEqual(exc_manager.exception.args[0],
'seconds must be an integer')
'seconds must be a whole number')
with self.assertRaises(ValueError) as exc_manager:
u.generate_temp_url(self.url, -1, self.key, self.method)
self.assertEqual(exc_manager.exception.args[0],
'seconds must be a positive integer')
'seconds must be a whole number')
with self.assertRaises(ValueError) as exc_manager:
u.generate_temp_url(self.url, 1.1, self.key, self.method)
self.assertEqual(exc_manager.exception.args[0],
'seconds must be a whole number')
with self.assertRaises(ValueError) as exc_manager:
u.generate_temp_url(self.url, '-1', self.key, self.method)
self.assertEqual(exc_manager.exception.args[0],
'seconds must be a whole number')
with self.assertRaises(ValueError) as exc_manager:
u.generate_temp_url(self.url, '1.1', self.key, self.method)
self.assertEqual(exc_manager.exception.args[0],
'seconds must be a whole number')
def test_generate_temp_url_bad_path(self):
with self.assertRaises(ValueError) as exc_manager: