From de5c1e4f281f59d550b476919b27ac4e2aae14ac Mon Sep 17 00:00:00 2001 From: zhhuabj Date: Mon, 1 Apr 2013 13:30:00 +0800 Subject: [PATCH] Make "shared" filter more compatible with diff DBs For the type BOOLEAN, in sqlalchemy, it's mapped to BOOLEAN type if the backend database supports it, otherwise, it's mapped to one of the Integer types, like SMALLINT, and restrict the values to 1(True) and 0(False). query_filter = (... | model.shared)) The above filter will generate a SQL where clause like: where ... OR xxx.shared This is not supported in databases which don't support BOOLEAN type. Change it as below to make it more compatible: query_filter = (... | model.shared == True)) It will generate a SQL where clause as below: where ... OR xxx.shared = ? In Python, True == 1, so this change is compatible with both databases supporting BOOLEAN and those not supporting it. Fix bug 1161195 Change-Id: Ic0ce0816d63b576a3469de0ed92cae4b19a3690e --- quantum/db/db_base_plugin_v2.py | 2 +- .../tests/unit/midonet/test_midonet_plugin.py | 3 +++ quantum/tests/unit/test_db_plugin.py | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/quantum/db/db_base_plugin_v2.py b/quantum/db/db_base_plugin_v2.py index 6182c45e2..7c3738cbc 100644 --- a/quantum/db/db_base_plugin_v2.py +++ b/quantum/db/db_base_plugin_v2.py @@ -98,7 +98,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2): if not context.is_admin and hasattr(model, 'tenant_id'): if hasattr(model, 'shared'): query_filter = ((model.tenant_id == context.tenant_id) | - (model.shared)) + (model.shared == True)) else: query_filter = (model.tenant_id == context.tenant_id) # Execute query hooks registered from mixins and plugins diff --git a/quantum/tests/unit/midonet/test_midonet_plugin.py b/quantum/tests/unit/midonet/test_midonet_plugin.py index fad116e33..11bb9b063 100644 --- a/quantum/tests/unit/midonet/test_midonet_plugin.py +++ b/quantum/tests/unit/midonet/test_midonet_plugin.py @@ -180,6 +180,9 @@ class TestMidonetNetworksV2(test_plugin.TestNetworksV2, def test_list_networks_with_parameters_invalid_values(self): pass + def test_list_shared_networks_with_non_admin_user(self): + pass + def test_show_network_with_subnet(self): pass diff --git a/quantum/tests/unit/test_db_plugin.py b/quantum/tests/unit/test_db_plugin.py index 6d18227dc..4801813b3 100644 --- a/quantum/tests/unit/test_db_plugin.py +++ b/quantum/tests/unit/test_db_plugin.py @@ -2240,6 +2240,22 @@ class TestNetworksV2(QuantumDbPluginV2TestCase): res = req.get_response(self.api) self.assertEqual(400, res.status_int) + def test_list_shared_networks_with_non_admin_user(self): + with contextlib.nested(self.network(shared=False, + name='net1', + tenant_id='tenant1'), + self.network(shared=True, + name='net2', + tenant_id='another_tenant'), + self.network(shared=False, + name='net3', + tenant_id='another_tenant') + ) as (net1, net2, net3): + ctx = context.Context(user_id='non_admin', + tenant_id='tenant1', + is_admin=False) + self._test_list_resources('network', (net1, net2), ctx) + def test_show_network(self): with self.network(name='net1') as net: req = self.new_show_request('networks', net['network']['id']) -- 2.45.2