Merge "check all_tenants value in share api"

This commit is contained in:
Zuul 2018-08-04 02:03:24 +00:00 committed by Gerrit Code Review
commit 5926bd0c68
4 changed files with 73 additions and 1 deletions

View File

@ -1518,7 +1518,7 @@ class API(base.Base):
shares = self.db.share_get_all_by_share_server(
context, search_opts.pop('share_server_id'), filters=filters,
sort_key=sort_key, sort_dir=sort_dir)
elif (context.is_admin and 'all_tenants' in search_opts):
elif (context.is_admin and utils.is_all_tenants(search_opts)):
shares = self.db.share_get_all(
context, filters=filters, sort_key=sort_key, sort_dir=sort_dir)
else:

View File

@ -248,6 +248,37 @@ class ShareAPITestCase(test.TestCase):
ctx, sort_dir='desc', sort_key='created_at', filters={})
self.assertEqual(_FAKE_LIST_OF_ALL_SHARES, shares)
def test_get_all_admin_filter_by_all_tenants_with_blank(self):
ctx = context.RequestContext('fake_uid', 'fake_pid_1', is_admin=True)
self.mock_object(db_api, 'share_get_all',
mock.Mock(return_value=_FAKE_LIST_OF_ALL_SHARES))
shares = self.api.get_all(ctx, {'all_tenants': ''})
share_api.policy.check_policy.assert_called_once_with(
ctx, 'share', 'get_all')
db_api.share_get_all.assert_called_once_with(
ctx, sort_dir='desc', sort_key='created_at', filters={})
self.assertEqual(_FAKE_LIST_OF_ALL_SHARES, shares)
def test_get_all_admin_filter_by_all_tenants_with_false(self):
ctx = context.RequestContext('fake_uid', 'fake_pid_1', is_admin=True)
self.mock_object(db_api, 'share_get_all_by_project',
mock.Mock(return_value=_FAKE_LIST_OF_ALL_SHARES[0]))
shares = self.api.get_all(ctx, {'all_tenants': 'false'})
share_api.policy.check_policy.assert_called_once_with(
ctx, 'share', 'get_all')
db_api.share_get_all_by_project.assert_called_once_with(
ctx, sort_dir='desc', sort_key='created_at',
project_id='fake_pid_1', filters={}, is_public=False
)
self.assertEqual(_FAKE_LIST_OF_ALL_SHARES[0], shares)
def test_get_all_admin_filter_by_all_tenants_with_invaild_value(self):
ctx = context.RequestContext('fake_uid', 'fake_pid_1', is_admin=True)
self.mock_object(db_api, 'share_get_all')
self.assertRaises(
exception.InvalidInput,
self.api.get_all, ctx, {'all_tenants': 'wonk'})
@ddt.data(
({'share_server_id': 'fake_share_server'}, 'list_by_share_server_id'),
({'host': 'fake_host'}, 'list_by_host'),

View File

@ -854,3 +854,26 @@ class TestDisableNotifications(test.TestCase):
group='oslo_messaging_notifications')
result = self._decorated_method()
self.assertEqual(utils.DO_NOTHING, result)
@ddt.ddt
class TestAllTenantsValueCase(test.TestCase):
@ddt.data(None, '', '1', 'true', 'True')
def test_is_all_tenants_true(self, value):
search_opts = {'all_tenants': value}
self.assertTrue(utils.is_all_tenants(search_opts))
self.assertIn('all_tenants', search_opts)
@ddt.data('0', 'false', 'False')
def test_is_all_tenants_false(self, value):
search_opts = {'all_tenants': value}
self.assertFalse(utils.is_all_tenants(search_opts))
self.assertIn('all_tenants', search_opts)
def test_is_all_tenants_missing(self):
self.assertFalse(utils.is_all_tenants({}))
def test_is_all_tenants_invalid(self):
search_opts = {'all_tenants': 'wonk'}
self.assertRaises(exception.InvalidInput, utils.is_all_tenants,
search_opts)

View File

@ -414,6 +414,24 @@ def is_valid_ip_address(ip_address, ip_version):
return False
def is_all_tenants(search_opts):
"""Checks to see if the all_tenants flag is in search_opts
:param dict search_opts: The search options for a request
:returns: boolean indicating if all_tenants are being requested or not
"""
all_tenants = search_opts.get('all_tenants')
if all_tenants:
try:
all_tenants = strutils.bool_from_string(all_tenants, True)
except ValueError as err:
raise exception.InvalidInput(six.text_type(err))
else:
# The empty string is considered enabling all_tenants
all_tenants = 'all_tenants' in search_opts
return all_tenants
class IsAMatcher(object):
def __init__(self, expected_value=None):
self.expected_value = expected_value