diff --git a/neutron/common/rpc.py b/neutron/common/rpc.py index bf8ec82c5bf..06357f7234a 100644 --- a/neutron/common/rpc.py +++ b/neutron/common/rpc.py @@ -229,13 +229,7 @@ class RequestContextSerializer(om_serializer.Serializer): trace_info = rpc_ctxt_dict.pop("trace_info", None) if trace_info: profiler.init(**trace_info) - user_id = rpc_ctxt_dict.pop('user_id', None) - if not user_id: - user_id = rpc_ctxt_dict.pop('user', None) - tenant_id = rpc_ctxt_dict.pop('tenant_id', None) - if not tenant_id: - tenant_id = rpc_ctxt_dict.pop('project_id', None) - return context.Context(user_id, tenant_id, **rpc_ctxt_dict) + return context.Context.from_dict(rpc_ctxt_dict) @profiler.trace_cls("rpc") diff --git a/neutron/context.py b/neutron/context.py index 840a8f68565..22a5e451ad7 100644 --- a/neutron/context.py +++ b/neutron/context.py @@ -35,14 +35,11 @@ class ContextBase(oslo_context.RequestContext): def __init__(self, user_id, tenant_id, is_admin=None, roles=None, timestamp=None, request_id=None, tenant_name=None, user_name=None, overwrite=True, auth_token=None, - is_advsvc=None, **kwargs): + is_advsvc=None): """Object initialization. :param overwrite: Set to False to ensure that the greenthread local copy of the index is not overwritten. - - :param kwargs: Extra arguments that might be present, but we ignore - because they possibly came in from older rpc messages. """ super(ContextBase, self).__init__(auth_token=auth_token, user=user_id, tenant=tenant_id, @@ -97,7 +94,15 @@ class ContextBase(oslo_context.RequestContext): @classmethod def from_dict(cls, values): - return cls(**values) + return cls(user_id=values.get('user_id', values.get('user')), + tenant_id=values.get('tenant_id', values.get('project_id')), + is_admin=values.get('is_admin'), + roles=values.get('roles'), + timestamp=values.get('timestamp'), + request_id=values.get('request_id'), + tenant_name=values.get('tenant_name'), + user_name=values.get('user_name'), + auth_token=values.get('auth_token')) def elevated(self): """Return a version of this context with admin flag set.""" diff --git a/neutron/tests/unit/api/v2/test_resource.py b/neutron/tests/unit/api/v2/test_resource.py index 6c436c6e4a4..bdb5d8fcbad 100644 --- a/neutron/tests/unit/api/v2/test_resource.py +++ b/neutron/tests/unit/api/v2/test_resource.py @@ -93,7 +93,7 @@ class RequestTestCase(base.BaseTestCase): def test_request_context_elevated(self): user_context = context.Context( - 'fake_user', 'fake_project', admin=False) + 'fake_user', 'fake_project', is_admin=False) self.assertFalse(user_context.is_admin) admin_context = user_context.elevated() self.assertFalse(user_context.is_admin) diff --git a/neutron/tests/unit/common/test_rpc.py b/neutron/tests/unit/common/test_rpc.py index 2501fafc78d..21bff8244ef 100644 --- a/neutron/tests/unit/common/test_rpc.py +++ b/neutron/tests/unit/common/test_rpc.py @@ -23,7 +23,6 @@ from oslo_messaging import conffixture as messaging_conffixture import testtools from neutron.common import rpc -from neutron import context from neutron.tests import base @@ -234,43 +233,51 @@ class TestRequestContextSerializer(base.DietTestCase): context.to_dict.assert_called_once_with() - @mock.patch.object(context, 'Context') - def test_deserialize_context(self, mock_con): - context = mock.Mock() - context.copy.return_value = {'foo': 'bar', - 'user_id': 1, - 'tenant_id': 1} + @mock.patch('neutron.policy.check_is_advsvc', return_val=False) + @mock.patch('neutron.policy.check_is_admin', return_val=False) + def test_deserialize_context(self, m, n): + context_dict = {'foo': 'bar', + 'user_id': 1, + 'tenant_id': 1} - self.ser.deserialize_context(context) - mock_con.assert_called_once_with(1, 1, foo='bar') + c = self.ser.deserialize_context(context_dict) - @mock.patch.object(context, 'Context') - def test_deserialize_context_no_user_id(self, mock_con): - context = mock.Mock() - context.copy.return_value = {'foo': 'bar', - 'user': 1, - 'tenant_id': 1} + self.assertEqual(1, c.user_id) + self.assertEqual(1, c.project_id) - self.ser.deserialize_context(context) - mock_con.assert_called_once_with(1, 1, foo='bar') + @mock.patch('neutron.policy.check_is_advsvc', return_val=False) + @mock.patch('neutron.policy.check_is_admin', return_val=False) + def test_deserialize_context_no_user_id(self, m, n): + context_dict = {'foo': 'bar', + 'user': 1, + 'tenant_id': 1} - @mock.patch.object(context, 'Context') - def test_deserialize_context_no_tenant_id(self, mock_con): - context = mock.Mock() - context.copy.return_value = {'foo': 'bar', - 'user_id': 1, - 'project_id': 1} + c = self.ser.deserialize_context(context_dict) - self.ser.deserialize_context(context) - mock_con.assert_called_once_with(1, 1, foo='bar') + self.assertEqual(1, c.user_id) + self.assertEqual(1, c.project_id) - @mock.patch.object(context, 'Context') - def test_deserialize_context_no_ids(self, mock_con): - context = mock.Mock() - context.copy.return_value = {'foo': 'bar'} + @mock.patch('neutron.policy.check_is_advsvc', return_val=False) + @mock.patch('neutron.policy.check_is_admin', return_val=False) + def test_deserialize_context_no_tenant_id(self, m, n): + context_dict = {'foo': 'bar', + 'user_id': 1, + 'project_id': 1} - self.ser.deserialize_context(context) - mock_con.assert_called_once_with(None, None, foo='bar') + c = self.ser.deserialize_context(context_dict) + + self.assertEqual(1, c.user_id) + self.assertEqual(1, c.project_id) + + @mock.patch('neutron.policy.check_is_advsvc', return_val=False) + @mock.patch('neutron.policy.check_is_admin', return_val=False) + def test_deserialize_context_no_ids(self, m, n): + context_dict = {'foo': 'bar'} + + c = self.ser.deserialize_context(context_dict) + + self.assertIsNone(c.user_id) + self.assertIsNone(c.project_id) class ServiceTestCase(base.DietTestCase):