From 1b192f787eb6be16874526ab94114965be3c1036 Mon Sep 17 00:00:00 2001 From: Francesco Faraone Date: Thu, 24 Nov 2022 14:45:04 +0100 Subject: [PATCH] LITE-25758 allow web app for hub extensions --- connect/eaas/core/inject/common.py | 2 +- connect/eaas/core/inject/models.py | 4 ++-- connect/eaas/core/testing/testclient.py | 19 ++++++++++----- .../eaas/core/testing/test_testclient.py | 23 ++++++++++++++++++- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/connect/eaas/core/inject/common.py b/connect/eaas/core/inject/common.py index 05af295..ceb9dbe 100644 --- a/connect/eaas/core/inject/common.py +++ b/connect/eaas/core/inject/common.py @@ -10,7 +10,7 @@ def get_call_context( - x_connect_installation_id: str = Header(), + x_connect_installation_id: str = Header(None), x_connect_user_id: str = Header(), x_connect_account_id: str = Header(), x_connect_account_role: str = Header(), diff --git a/connect/eaas/core/inject/models.py b/connect/eaas/core/inject/models.py index 7a04aa9..850557e 100644 --- a/connect/eaas/core/inject/models.py +++ b/connect/eaas/core/inject/models.py @@ -1,10 +1,10 @@ -from typing import Literal +from typing import Literal, Optional from pydantic import BaseModel class Context(BaseModel): - installation_id: str + installation_id: Optional[str] = None user_id: str account_id: str account_role: str diff --git a/connect/eaas/core/testing/testclient.py b/connect/eaas/core/testing/testclient.py index d9f0e97..adc7f83 100644 --- a/connect/eaas/core/testing/testclient.py +++ b/connect/eaas/core/testing/testclient.py @@ -110,14 +110,14 @@ def _get_client_mocker(self, method, url): return ConnectClientMocker def _generate_call_context(self, installation): - return Context(**{ - 'installation_id': installation['id'] if installation else 'EIN-000', + return { + 'installation_id': installation['id'] if installation else None, 'user_id': 'UR-000', 'account_id': 'VA-000', 'account_role': 'vendor', 'call_source': 'ui', 'call_type': 'user', - }) + } def _populate_internal_headers( self, @@ -127,12 +127,19 @@ def _populate_internal_headers( context=None, log_level=None, ): + + if not context: + context = Context(**self._generate_call_context(installation)) + elif isinstance(context, dict): + ctx_data = self._generate_call_context(installation) + ctx_data.update(context) + context = Context(**ctx_data) + headers['X-Connect-Logging-Level'] = log_level or 'INFO' if config: headers['X-Connect-Config'] = json.dumps(config) - - context: Context = context or self._generate_call_context(installation) - headers['X-Connect-Installation-id'] = context.installation_id + if context.installation_id: + headers['X-Connect-Installation-id'] = context.installation_id headers['X-Connect-User-Id'] = context.user_id headers['X-Connect-Account-Id'] = context.account_id headers['X-Connect-Account-Role'] = context.account_role diff --git a/tests/connect/eaas/core/testing/test_testclient.py b/tests/connect/eaas/core/testing/test_testclient.py index 3a4e44e..3aee082 100644 --- a/tests/connect/eaas/core/testing/test_testclient.py +++ b/tests/connect/eaas/core/testing/test_testclient.py @@ -82,7 +82,7 @@ def test_get_call_context_default(webapp_mock): assert resp.status_code == 200 assert resp.json() == { - 'installation_id': 'EIN-000', + 'installation_id': None, 'user_id': 'UR-000', 'account_id': 'VA-000', 'account_role': 'vendor', @@ -109,6 +109,27 @@ def test_get_call_context_custom(webapp_mock): assert resp.json() == ctx +def test_get_call_context_merge(webapp_mock): + client = WebAppTestClient(webapp_mock) + + ctx = { + 'installation_id': 'EIN-111', + 'call_type': 'admin', + } + + resp = client.get('/api/context', context=ctx) + + assert resp.status_code == 200 + assert resp.json() == { + 'installation_id': 'EIN-111', + 'user_id': 'UR-000', + 'account_id': 'VA-000', + 'account_role': 'vendor', + 'call_source': 'ui', + 'call_type': 'admin', + } + + def test_not_found(webapp_mock): client = WebAppTestClient(webapp_mock) installation = {