Merge "Provide a helper to load a context from environment"
This commit is contained in:
@@ -106,6 +106,28 @@ class RequestContext(object):
|
||||
request_id=ctx.get("request_id"),
|
||||
resource_uuid=ctx.get("resource_uuid"))
|
||||
|
||||
@classmethod
|
||||
def from_environ(cls, environ, **kwargs):
|
||||
"""Load a context object from a request environment.
|
||||
|
||||
If keyword arguments are provided then they override the values in the
|
||||
request environment.
|
||||
|
||||
:param environ: The environment dictionary associated with a request.
|
||||
:type environ: dict
|
||||
"""
|
||||
# Load a new context object from the environment variables set by
|
||||
# auth_token middleware. See:
|
||||
# http://docs.openstack.org/developer/keystonemiddleware/api/keystonemiddleware.auth_token.html#what-auth-token-adds-to-the-request-for-use-by-the-openstack-service
|
||||
kwargs.setdefault('auth_token', environ.get('HTTP_X_AUTH_TOKEN'))
|
||||
kwargs.setdefault('user', environ.get('HTTP_X_USER_ID'))
|
||||
kwargs.setdefault('tenant', environ.get('HTTP_X_PROJECT_ID'))
|
||||
kwargs.setdefault('user_domain', environ.get('HTTP_X_USER_DOMAIN_ID'))
|
||||
kwargs.setdefault('project_domain',
|
||||
environ.get('HTTP_X_PROJECT_DOMAIN_ID'))
|
||||
|
||||
return cls(**kwargs)
|
||||
|
||||
|
||||
def get_admin_context(show_deleted=False):
|
||||
"""Create an administrator context."""
|
||||
|
||||
@@ -13,12 +13,18 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import uuid
|
||||
|
||||
from oslotest import base as test_base
|
||||
|
||||
from oslo_context import context
|
||||
from oslo_context import fixture
|
||||
|
||||
|
||||
class Object(object):
|
||||
pass
|
||||
|
||||
|
||||
class ContextTest(test_base.BaseTestCase):
|
||||
|
||||
def setUp(self):
|
||||
@@ -92,3 +98,24 @@ class ContextTest(test_base.BaseTestCase):
|
||||
self.assertFalse(context.is_user_context(ctx))
|
||||
ctx = context.RequestContext(is_admin=False)
|
||||
self.assertTrue(context.is_user_context(ctx))
|
||||
|
||||
def test_from_environ_variables(self):
|
||||
auth_token = uuid.uuid4().hex
|
||||
user_id = uuid.uuid4().hex
|
||||
project_id = uuid.uuid4().hex
|
||||
user_domain_id = uuid.uuid4().hex
|
||||
project_domain_id = uuid.uuid4().hex
|
||||
|
||||
environ = {'HTTP_X_AUTH_TOKEN': auth_token,
|
||||
'HTTP_X_USER_ID': user_id,
|
||||
'HTTP_X_PROJECT_ID': project_id,
|
||||
'HTTP_X_USER_DOMAIN_ID': user_domain_id,
|
||||
'HTTP_X_PROJECT_DOMAIN_ID': project_domain_id}
|
||||
|
||||
ctx = context.RequestContext.from_environ(environ)
|
||||
|
||||
self.assertEqual(auth_token, ctx.auth_token)
|
||||
self.assertEqual(user_id, ctx.user)
|
||||
self.assertEqual(project_id, ctx.tenant)
|
||||
self.assertEqual(user_domain_id, ctx.user_domain)
|
||||
self.assertEqual(project_domain_id, ctx.project_domain)
|
||||
|
||||
Reference in New Issue
Block a user