summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorChristopher Bartz <bartz@dkrz.de>2016-12-08 13:42:35 +0100
committerChristopher Bartz <bartz@dkrz.de>2017-01-19 16:34:26 +0100
commit3934bd606acc2333ee9ae63a40baa35928ef908d (patch)
tree1181d58436365ce6d9863866c112f78e6677a081 /tests
parentaea0585ddbc749b6f4d501430d41b671932c11a4 (diff)
downloadpython-swiftclient-3934bd606acc2333ee9ae63a40baa35928ef908d.tar.gz
prefix-based tempurls support
Implements client-side functionality for prefix-based tempurls. Please see: https://review.openstack.org/#/c/274048/ Change-Id: I8d7701daee888ed1120271a96c0660b01543ca2d
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/test_shell.py33
-rw-r--r--tests/unit/test_utils.py35
2 files changed, 66 insertions, 2 deletions
diff --git a/tests/unit/test_shell.py b/tests/unit/test_shell.py
index 00677df..f7e9c1a 100644
--- a/tests/unit/test_shell.py
+++ b/tests/unit/test_shell.py
@@ -1549,7 +1549,17 @@ class TestShell(unittest.TestCase):
"secret_key"]
swiftclient.shell.main(argv)
temp_url.assert_called_with(
- '/v1/AUTH_account/c/o', "60", 'secret_key', 'GET', absolute=False)
+ '/v1/AUTH_account/c/o', "60", 'secret_key', 'GET', absolute=False,
+ prefix=False)
+
+ @mock.patch('swiftclient.shell.generate_temp_url', return_value='')
+ def test_temp_url_prefix_based(self, temp_url):
+ argv = ["", "tempurl", "GET", "60", "/v1/AUTH_account/c/",
+ "secret_key", "--prefix-based"]
+ swiftclient.shell.main(argv)
+ temp_url.assert_called_with(
+ '/v1/AUTH_account/c/', "60", 'secret_key', 'GET', absolute=False,
+ prefix=True)
@mock.patch('swiftclient.shell.generate_temp_url', return_value='')
def test_absolute_expiry_temp_url(self, temp_url):
@@ -1557,7 +1567,8 @@ class TestShell(unittest.TestCase):
"secret_key", "--absolute"]
swiftclient.shell.main(argv)
temp_url.assert_called_with(
- '/v1/AUTH_account/c/o', "60", 'secret_key', 'GET', absolute=True)
+ '/v1/AUTH_account/c/o', "60", 'secret_key', 'GET', absolute=True,
+ prefix=False)
def test_temp_url_output(self):
argv = ["", "tempurl", "GET", "60", "/v1/a/c/o",
@@ -1575,6 +1586,15 @@ class TestShell(unittest.TestCase):
expected = "http://saio:8080%s" % expected
self.assertEqual(expected, output.out)
+ argv = ["", "tempurl", "GET", "60", "/v1/a/c/",
+ "secret_key", "--absolute", "--prefix"]
+ with CaptureOutput(suppress_systemexit=True) as output:
+ swiftclient.shell.main(argv)
+ sig = '00008c4be1573ba74fc2ab9bce02e3a93d04b349'
+ expected = ("/v1/a/c/?temp_url_sig=%s&temp_url_expires=60"
+ "&temp_url_prefix=\n" % sig)
+ self.assertEqual(expected, output.out)
+
def test_temp_url_error_output(self):
expected = 'path must be full path to an object e.g. /v1/a/c/o\n'
for bad_path in ('/v1/a/c', 'v1/a/c/o', '/v1/a/c/', '/v1/a//o',
@@ -1587,6 +1607,15 @@ class TestShell(unittest.TestCase):
'Expected %r but got %r for path %r' %
(expected, output.err, bad_path))
+ expected = 'path must at least contain /v1/a/c/\n'
+ argv = ["", "tempurl", "GET", "60", '/v1/a/c',
+ "secret_key", "--absolute", '--prefix-based']
+ with CaptureOutput(suppress_systemexit=True) as output:
+ swiftclient.shell.main(argv)
+ self.assertEqual(expected, output.err,
+ 'Expected %r but got %r for path %r' %
+ (expected, output.err, bad_path))
+
@mock.patch('swiftclient.service.Connection')
def test_capabilities(self, connection):
argv = ["", "capabilities"]
diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
index 787f645..cbea8d8 100644
--- a/tests/unit/test_utils.py
+++ b/tests/unit/test_utils.py
@@ -150,6 +150,35 @@ class TestTempURL(unittest.TestCase):
])
self.assertIsInstance(url, type(self.url))
+ @mock.patch('hmac.HMAC')
+ @mock.patch('time.time', return_value=1400000000)
+ def test_generate_temp_url_prefix(self, time_mock, hmac_mock):
+ hmac_mock().hexdigest.return_value = 'temp_url_signature'
+ prefixes = ['', 'o', 'p0/p1/']
+ for p in prefixes:
+ hmac_mock.reset_mock()
+ path = '/v1/AUTH_account/c/' + p
+ expected_url = path + ('?temp_url_sig=temp_url_signature'
+ '&temp_url_expires=1400003600'
+ '&temp_url_prefix=' + p)
+ expected_body = '\n'.join([
+ self.method,
+ '1400003600',
+ 'prefix:' + path,
+ ]).encode('utf-8')
+ url = u.generate_temp_url(path, self.seconds,
+ self.key, self.method, prefix=True)
+ key = self.key
+ if not isinstance(key, six.binary_type):
+ key = key.encode('utf-8')
+ self.assertEqual(url, expected_url)
+ self.assertEqual(hmac_mock.mock_calls, [
+ mock.call(key, expected_body, sha1),
+ mock.call().hexdigest(),
+ ])
+
+ self.assertIsInstance(url, type(path))
+
def test_generate_temp_url_invalid_path(self):
with self.assertRaises(ValueError) as exc_manager:
u.generate_temp_url(b'/v1/a/c/\xff', self.seconds, self.key,
@@ -221,6 +250,12 @@ class TestTempURL(unittest.TestCase):
self.assertEqual(exc_manager.exception.args[0],
'path must be full path to an object e.g. /v1/a/c/o')
+ with self.assertRaises(ValueError) as exc_manager:
+ u.generate_temp_url('/v1/a/c', 60, self.key, self.method,
+ prefix=True)
+ self.assertEqual(exc_manager.exception.args[0],
+ 'path must at least contain /v1/a/c/')
+
class TestTempURLUnicodePathAndKey(TestTempURL):
url = u'/v1/\u00e4/c/\u00f3'