Increase some middleware test coverage

This patch increases the test coverage of the following middlewares:
 - list_endpoints
 - crypto
 - crossdomain

Change-Id: I3dec85f61da07bd110bf42220d5ba46e11833a90
This commit is contained in:
Tim Burke 2019-04-12 22:13:41 -07:00 committed by Matthew Oliver
parent 5090a15f52
commit 2eb2451685
4 changed files with 26 additions and 19 deletions

View File

@ -142,10 +142,10 @@ class ListEndpointsMiddleware(object):
def _parse_path(self, request): def _parse_path(self, request):
""" """
Parse path parts of request into a tuple of version, account, Parse path parts of request into a tuple of version, account,
container, obj. Unspecified path parts are filled in as None, container, obj. Unspecified container or obj is filled in as
except version which is always returned as a float using the None; account is required; version is always returned as a
configured default response version if not specified in the float using the configured default response version if not
request. specified in the request.
:param request: the swob request :param request: the swob request
@ -208,8 +208,7 @@ class ListEndpointsMiddleware(object):
except ValueError as err: except ValueError as err:
return HTTPBadRequest(str(err))(env, start_response) return HTTPBadRequest(str(err))(env, start_response)
if account is not None: account = unquote(account)
account = unquote(account)
if container is not None: if container is not None:
container = unquote(container) container = unquote(container)
if obj is not None: if obj is not None:

View File

@ -273,6 +273,14 @@ class TestModuleMethods(unittest.TestCase):
expected = 'abc; swift_meta=%s' % self.serialized_meta_with_key expected = 'abc; swift_meta=%s' % self.serialized_meta_with_key
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
def check_bad_value(value):
with self.assertRaises(ValueError):
crypto_utils.append_crypto_meta(value, self.meta)
check_bad_value(None)
check_bad_value({})
check_bad_value(1)
def test_extract_crypto_meta(self): def test_extract_crypto_meta(self):
val, meta = crypto_utils.extract_crypto_meta( val, meta = crypto_utils.extract_crypto_meta(
'abc; swift_meta=%s' % self.serialized_meta) 'abc; swift_meta=%s' % self.serialized_meta)

View File

@ -26,7 +26,7 @@ sys.modules['kmip'] = mock.Mock()
sys.modules['kmip.pie'] = mock.Mock() sys.modules['kmip.pie'] = mock.Mock()
sys.modules['kmip.pie.client'] = mock.Mock() sys.modules['kmip.pie.client'] = mock.Mock()
from swift.common.middleware.crypto.kmip_keymaster import KmipKeyMaster from swift.common.middleware.crypto import kmip_keymaster
KMIP_CLIENT_CLASS = \ KMIP_CLIENT_CLASS = \
@ -91,7 +91,7 @@ class TestKmipKeymaster(unittest.TestCase):
secrets = {'1234': create_secret('AES', 256, b'x' * 32)} secrets = {'1234': create_secret('AES', 256, b'x' * 32)}
calls = [] calls = []
with mock.patch(KMIP_CLIENT_CLASS, create_mock_client(secrets, calls)): with mock.patch(KMIP_CLIENT_CLASS, create_mock_client(secrets, calls)):
km = KmipKeyMaster(None, conf) km = kmip_keymaster.filter_factory(conf)(None)
self.assertEqual({None: b'x' * 32}, km._root_secrets) self.assertEqual({None: b'x' * 32}, km._root_secrets)
self.assertEqual(None, km.active_secret_id) self.assertEqual(None, km.active_secret_id)
@ -113,7 +113,7 @@ class TestKmipKeymaster(unittest.TestCase):
'foobar': create_secret('AES', 256, b'y' * 32)} 'foobar': create_secret('AES', 256, b'y' * 32)}
calls = [] calls = []
with mock.patch(KMIP_CLIENT_CLASS, create_mock_client(secrets, calls)): with mock.patch(KMIP_CLIENT_CLASS, create_mock_client(secrets, calls)):
km = KmipKeyMaster(None, conf) km = kmip_keymaster.KmipKeyMaster(None, conf)
self.assertEqual({None: b'x' * 32, 'xyzzy': b'y' * 32, self.assertEqual({None: b'x' * 32, 'xyzzy': b'y' * 32,
'alt_secret_id': b'y' * 32}, 'alt_secret_id': b'y' * 32},
@ -139,7 +139,7 @@ class TestKmipKeymaster(unittest.TestCase):
with mock.patch(KMIP_CLIENT_CLASS, with mock.patch(KMIP_CLIENT_CLASS,
create_mock_client(secrets, calls)), \ create_mock_client(secrets, calls)), \
self.assertRaises(ValueError) as raised: self.assertRaises(ValueError) as raised:
KmipKeyMaster(None, conf) kmip_keymaster.KmipKeyMaster(None, conf)
self.assertEqual('No secret loaded for active_root_secret_id unknown', self.assertEqual('No secret loaded for active_root_secret_id unknown',
str(raised.exception)) str(raised.exception))
@ -158,7 +158,7 @@ class TestKmipKeymaster(unittest.TestCase):
secrets = {'4321': create_secret('AES', 256, b'x' * 32)} secrets = {'4321': create_secret('AES', 256, b'x' * 32)}
calls = [] calls = []
with mock.patch(KMIP_CLIENT_CLASS, create_mock_client(secrets, calls)): with mock.patch(KMIP_CLIENT_CLASS, create_mock_client(secrets, calls)):
km = KmipKeyMaster(None, conf) km = kmip_keymaster.KmipKeyMaster(None, conf)
self.assertEqual({None: b'x' * 32}, km._root_secrets) self.assertEqual({None: b'x' * 32}, km._root_secrets)
self.assertEqual(None, km.active_secret_id) self.assertEqual(None, km.active_secret_id)
self.assertEqual(km_config_file, km.keymaster_config_path) self.assertEqual(km_config_file, km.keymaster_config_path)
@ -185,7 +185,7 @@ class TestKmipKeymaster(unittest.TestCase):
'another id': create_secret('AES', 256, b'y' * 32)} 'another id': create_secret('AES', 256, b'y' * 32)}
calls = [] calls = []
with mock.patch(KMIP_CLIENT_CLASS, create_mock_client(secrets, calls)): with mock.patch(KMIP_CLIENT_CLASS, create_mock_client(secrets, calls)):
km = KmipKeyMaster(None, conf) km = kmip_keymaster.KmipKeyMaster(None, conf)
self.assertEqual({None: b'x' * 32, 'secret_id': b'y' * 32}, self.assertEqual({None: b'x' * 32, 'secret_id': b'y' * 32},
km._root_secrets) km._root_secrets)
self.assertEqual('secret_id', km.active_secret_id) self.assertEqual('secret_id', km.active_secret_id)
@ -205,7 +205,7 @@ class TestKmipKeymaster(unittest.TestCase):
'__name__': 'kmip_keymaster', '__name__': 'kmip_keymaster',
'key_id': '789'} 'key_id': '789'}
with self.assertRaises(ValueError) as cm: with self.assertRaises(ValueError) as cm:
KmipKeyMaster(None, conf) kmip_keymaster.KmipKeyMaster(None, conf)
self.assertIn('config cannot be read from conf dir', str(cm.exception)) self.assertIn('config cannot be read from conf dir', str(cm.exception))
# ...but a conf file in a conf dir could point back to itself for the # ...but a conf file in a conf dir could point back to itself for the
@ -228,7 +228,7 @@ class TestKmipKeymaster(unittest.TestCase):
secrets = {'789': create_secret('AES', 256, b'x' * 32)} secrets = {'789': create_secret('AES', 256, b'x' * 32)}
calls = [] calls = []
with mock.patch(KMIP_CLIENT_CLASS, create_mock_client(secrets, calls)): with mock.patch(KMIP_CLIENT_CLASS, create_mock_client(secrets, calls)):
km = KmipKeyMaster(None, conf) km = kmip_keymaster.KmipKeyMaster(None, conf)
self.assertEqual({None: b'x' * 32}, km._root_secrets) self.assertEqual({None: b'x' * 32}, km._root_secrets)
self.assertEqual(None, km.active_secret_id) self.assertEqual(None, km.active_secret_id)
self.assertEqual(km_config_file, km.keymaster_config_path) self.assertEqual(km_config_file, km.keymaster_config_path)
@ -247,7 +247,7 @@ class TestKmipKeymaster(unittest.TestCase):
with mock.patch(KMIP_CLIENT_CLASS, with mock.patch(KMIP_CLIENT_CLASS,
create_mock_client(secrets, calls)), \ create_mock_client(secrets, calls)), \
self.assertRaises(ValueError) as cm: self.assertRaises(ValueError) as cm:
KmipKeyMaster(None, conf) kmip_keymaster.KmipKeyMaster(None, conf)
self.assertIn('Expected key 1234 to be an AES-256 key', self.assertIn('Expected key 1234 to be an AES-256 key',
str(cm.exception)) str(cm.exception))
self.assertEqual(calls, [ self.assertEqual(calls, [
@ -264,7 +264,7 @@ class TestKmipKeymaster(unittest.TestCase):
with mock.patch(KMIP_CLIENT_CLASS, with mock.patch(KMIP_CLIENT_CLASS,
create_mock_client(secrets, calls)), \ create_mock_client(secrets, calls)), \
self.assertRaises(ValueError) as cm: self.assertRaises(ValueError) as cm:
KmipKeyMaster(None, conf) kmip_keymaster.KmipKeyMaster(None, conf)
self.assertIn('Expected key 1234 to be an AES-256 key', self.assertIn('Expected key 1234 to be an AES-256 key',
str(cm.exception)) str(cm.exception))
self.assertEqual(calls, [ self.assertEqual(calls, [
@ -280,7 +280,7 @@ class TestKmipKeymaster(unittest.TestCase):
with mock.patch(KMIP_CLIENT_CLASS, with mock.patch(KMIP_CLIENT_CLASS,
create_mock_client(secrets, calls)), \ create_mock_client(secrets, calls)), \
self.assertRaises(ValueError) as cm: self.assertRaises(ValueError) as cm:
KmipKeyMaster(None, conf) kmip_keymaster.KmipKeyMaster(None, conf)
self.assertEqual('No secret loaded for active_root_secret_id None', self.assertEqual('No secret loaded for active_root_secret_id None',
str(cm.exception)) str(cm.exception))
# We make the client, but never use it # We make the client, but never use it
@ -304,7 +304,7 @@ class TestKmipKeymaster(unittest.TestCase):
create_mock_client(secrets, calls)), \ create_mock_client(secrets, calls)), \
self.assertRaises(ValueError): self.assertRaises(ValueError):
# missing key_id, as above, but that's not the interesting bit # missing key_id, as above, but that's not the interesting bit
KmipKeyMaster(None, conf) kmip_keymaster.KmipKeyMaster(None, conf)
self.assertEqual(handler.messages, []) self.assertEqual(handler.messages, [])

View File

@ -33,7 +33,7 @@ def start_response(*args):
class TestCrossDomain(unittest.TestCase): class TestCrossDomain(unittest.TestCase):
def setUp(self): def setUp(self):
self.app = crossdomain.CrossDomainMiddleware(FakeApp(), {}) self.app = crossdomain.filter_factory({})(FakeApp())
# GET of /crossdomain.xml (default) # GET of /crossdomain.xml (default)
def test_crossdomain_default(self): def test_crossdomain_default(self):