Use native override of RequestContext.from_environ

The from_environ method provides the native interface (by keyword
arguments) to pass additional arguments to build a RequestContext
instance.

Also fix the ignored kwargs.

Change-Id: Id02e2212e1877c7913218d87188ba8b359ce2757
Signed-off-by: Takashi Kajinami <kajinamit@oss.nttdata.com>
This commit is contained in:
Takashi Kajinami 2025-12-14 05:18:51 +09:00
parent acb78ef560
commit 2570f7559f
3 changed files with 11 additions and 12 deletions

View file

@ -106,17 +106,13 @@ class ContextHook(hooks.PecanHook):
super(ContextHook, self).__init__()
def before(self, state):
is_public_api = state.request.environ.get('is_public_api', False)
auth_token_info = state.request.environ.get('keystone.token_info')
# set the global_request_id if we have an inbound request id
gr_id = state.request.headers.get(INBOUND_HEADER, "")
if re.match(ID_FORMAT, gr_id):
state.request.environ[GLOBAL_REQ_ID] = gr_id
ctx = context.RequestContext.from_environ(
state.request.environ,
is_public_api=is_public_api,
auth_token_info=auth_token_info)
state.request.environ)
# Do not pass any token with context for noauth mode
if cfg.CONF.auth_strategy != 'keystone':
ctx.auth_token = None

View file

@ -77,9 +77,15 @@ class RequestContext(context.RequestContext):
:param environ: The environment dictionary associated with a request.
:type environ: dict
"""
context = super().from_environ(environ)
context.is_public_api = environ.get('is_public_api', False)
context.auth_token_info = environ.get('keystone.token_info')
is_public_api = environ.get('is_public_api', False)
auth_token_info = environ.get('keystone.token_info')
context = super().from_environ(
environ,
is_public_api=is_public_api,
auth_token_info=auth_token_info,
**kwargs
)
return context
def to_dict(self):

View file

@ -225,11 +225,8 @@ class TestContextHook(base.BaseApiTest):
ctx.to_policy_values.return_value = policy_dict
mock_policy.return_value = False
context_hook.before(reqstate)
creds_dict = {'is_public_api': is_public_api}
if auth_token_info:
creds_dict['auth_token_info'] = auth_token_info
mock_ctx.from_environ.assert_called_once_with(
environ, **creds_dict)
mock_ctx.from_environ.assert_called_once_with(environ)
mock_policy.assert_not_called()
if auth_strategy == 'noauth':
self.assertIsNone(ctx.auth_token)