Merge "py3: port cli form_signature and unit tests"

This commit is contained in:
Zuul 2018-03-15 02:19:36 +00:00 committed by Gerrit Code Review
commit 5fa1c9e8bb
3 changed files with 28 additions and 15 deletions

View File

@ -17,6 +17,7 @@ Script for generating a form signature for use with FormPost middleware.
"""
from __future__ import print_function
import hmac
import six
from hashlib import sha1
from os.path import basename
from time import time
@ -92,8 +93,14 @@ def main(argv):
print('For example: /v1/account/container')
print(' Or: /v1/account/container/object_prefix')
return 1
sig = hmac.new(key, '%s\n%s\n%s\n%s\n%s' % (path, redirect, max_file_size,
max_file_count, expires),
data = '%s\n%s\n%s\n%s\n%s' % (path, redirect, max_file_size,
max_file_count, expires)
if six.PY3:
data = data if isinstance(data, six.binary_type) else \
data.encode('utf8')
key = key if isinstance(key, six.binary_type) else \
key.encode('utf8')
sig = hmac.new(key, data,
sha1).hexdigest()
print(' Expires:', expires)
print('Signature:', sig)

View File

@ -17,7 +17,7 @@
import hashlib
import hmac
import mock
from six import StringIO
import six
import unittest
from swift.cli import form_signature
@ -33,14 +33,19 @@ class TestFormSignature(unittest.TestCase):
max_file_size = str(int(1024 * 1024 * 1024 * 3.14159)) # π GiB
max_file_count = '3'
expected_signature = hmac.new(
key,
"\n".join((
data = "\n".join((
path, redirect, max_file_size, max_file_count,
str(int(the_time + expires)))),
hashlib.sha1).hexdigest()
str(int(the_time + expires))))
out = StringIO()
if six.PY3:
data = data if isinstance(data, six.binary_type) else \
data.encode('utf8')
key = key if isinstance(key, six.binary_type) else \
key.encode('utf8')
expected_signature = hmac.new(key, data, hashlib.sha1).hexdigest()
out = six.StringIO()
with mock.patch('swift.cli.form_signature.time', lambda: the_time):
with mock.patch('sys.stdout', out):
exitcode = form_signature.main([
@ -59,7 +64,7 @@ class TestFormSignature(unittest.TestCase):
self.assertIn(sig_input, out.getvalue())
def test_too_few_args(self):
out = StringIO()
out = six.StringIO()
with mock.patch('sys.stdout', out):
exitcode = form_signature.main([
'/path/to/swift-form-signature',
@ -70,7 +75,7 @@ class TestFormSignature(unittest.TestCase):
self.assertIn(usage, out.getvalue())
def test_invalid_filesize_arg(self):
out = StringIO()
out = six.StringIO()
key = 'secret squirrel'
with mock.patch('sys.stdout', out):
exitcode = form_signature.main([
@ -79,7 +84,7 @@ class TestFormSignature(unittest.TestCase):
self.assertNotEqual(exitcode, 0)
def test_invalid_filecount_arg(self):
out = StringIO()
out = six.StringIO()
key = 'secret squirrel'
with mock.patch('sys.stdout', out):
exitcode = form_signature.main([
@ -88,7 +93,7 @@ class TestFormSignature(unittest.TestCase):
self.assertNotEqual(exitcode, 0)
def test_invalid_path_arg(self):
out = StringIO()
out = six.StringIO()
key = 'secret squirrel'
with mock.patch('sys.stdout', out):
exitcode = form_signature.main([
@ -97,7 +102,7 @@ class TestFormSignature(unittest.TestCase):
self.assertNotEqual(exitcode, 0)
def test_invalid_seconds_arg(self):
out = StringIO()
out = six.StringIO()
key = 'secret squirrel'
with mock.patch('sys.stdout', out):
exitcode = form_signature.main([

View File

@ -30,6 +30,7 @@ setenv = VIRTUAL_ENV={envdir}
commands =
nosetests \
test/unit/cli/test_dispersion_report.py \
test/unit/cli/test_form_signature.py \
test/unit/cli/test_info.py \
test/unit/cli/test_relinker.py \
test/unit/cli/test_ring_builder_analyzer.py \