From 6acac14aaffaebd6ee00325de57a710e459e6708 Mon Sep 17 00:00:00 2001 From: Shri Javadekar <shrinand@maginatics.com> Date: Thu, 16 May 2013 22:51:43 -0700 Subject: [PATCH] Move common code into a separate function. test_get_valid and test_get_valid_key2 test tempurl with a single key and two keys respectively. This change moves common code for these tests into a separate function. Change-Id: Ifee211285e27df03d041a4c357b7c1f0c96ed6a8 --- test/unit/common/middleware/test_tempurl.py | 31 ++++++++------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/test/unit/common/middleware/test_tempurl.py b/test/unit/common/middleware/test_tempurl.py index 127c970bfb..79d77a2dc9 100644 --- a/test/unit/common/middleware/test_tempurl.py +++ b/test/unit/common/middleware/test_tempurl.py @@ -100,17 +100,11 @@ class TestTempURL(unittest.TestCase): environ={'REQUEST_METHOD': 'OPTIONS'}).get_response(self.tempurl) self.assertEquals(resp.status_int, 200) - def test_get_valid(self): - method = 'GET' - expires = int(time() + 86400) - path = '/v1/a/c/o' - key = 'abc' - hmac_body = '%s\n%s\n%s' % (method, expires, path) - sig = hmac.new(key, hmac_body, sha1).hexdigest() + def assert_valid_sig(self, expires, path, keys, sig): req = self._make_request(path, environ={'QUERY_STRING': 'temp_url_sig=%s&temp_url_expires=%s' % (sig, expires)}) - req.environ['swift.cache'].set('temp-url-keys/a', [key]) + req.environ['swift.cache'].set('temp-url-keys/a', keys) self.tempurl.app = FakeApp(iter([('200 Ok', (), '123')])) resp = req.get_response(self.tempurl) self.assertEquals(resp.status_int, 200) @@ -119,6 +113,15 @@ class TestTempURL(unittest.TestCase): self.assertEquals(req.environ['swift.authorize_override'], True) self.assertEquals(req.environ['REMOTE_USER'], '.wsgi.tempurl') + def test_get_valid(self): + method = 'GET' + expires = int(time() + 86400) + path = '/v1/a/c/o' + key = 'abc' + hmac_body = '%s\n%s\n%s' % (method, expires, path) + sig = hmac.new(key, hmac_body, sha1).hexdigest() + self.assert_valid_sig(expires, path, [key], sig) + def test_get_valid_key2(self): method = 'GET' expires = int(time() + 86400) @@ -129,17 +132,7 @@ class TestTempURL(unittest.TestCase): sig1 = hmac.new(key1, hmac_body, sha1).hexdigest() sig2 = hmac.new(key2, hmac_body, sha1).hexdigest() for sig in (sig1, sig2): - req = self._make_request(path, - environ={'QUERY_STRING': - 'temp_url_sig=%s&temp_url_expires=%s' % (sig, expires)}) - req.environ['swift.cache'].set('temp-url-keys/a', [key1, key2]) - self.tempurl.app = FakeApp(iter([('200 Ok', (), '123')])) - resp = req.get_response(self.tempurl) - self.assertEquals(resp.status_int, 200) - self.assertEquals(resp.headers['content-disposition'], - 'attachment; filename="o"') - self.assertEquals(req.environ['swift.authorize_override'], True) - self.assertEquals(req.environ['REMOTE_USER'], '.wsgi.tempurl') + self.assert_valid_sig(expires, path, [key1, key2], sig) def test_get_valid_with_filename(self): method = 'GET'