-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from cloudblue/LITE-25362_LITE-25285
LITE-25362, LITE-25285 return list of variables instead of dict in se…
- Loading branch information
Showing
19 changed files
with
456 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from connect.eaas.core.testing.testclient import WebAppTestClient # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import pytest | ||
|
||
from connect.eaas.core.testing import WebAppTestClient | ||
|
||
|
||
@pytest.fixture | ||
def test_client_factory(): | ||
""" | ||
This fixture allows to instantiate a WebAppTestClient | ||
given a webapp class. | ||
""" | ||
|
||
def _get_client(webapp): | ||
return WebAppTestClient(webapp) | ||
|
||
return _get_client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import inspect | ||
import json | ||
from urllib.parse import urlparse | ||
|
||
from fastapi import FastAPI | ||
from fastapi.staticfiles import StaticFiles | ||
from starlette.routing import Match | ||
from starlette.testclient import TestClient | ||
|
||
from connect.client.testing import AsyncConnectClientMocker, ConnectClientMocker | ||
from connect.eaas.core.inject.models import Context | ||
|
||
|
||
class WebAppTestClient(TestClient): | ||
|
||
def __init__(self, webapp): | ||
self._webapp_class = webapp | ||
self._app = self._get_application() | ||
|
||
super().__init__(app=self._app, base_url='https://localhost/public/v1') | ||
|
||
self.headers = { | ||
'X-Connect-Api-Gateway-Url': self.base_url, | ||
'X-Connect-User-Agent': 'eaas-test-client', | ||
'X-Connect-Installation-Api-Key': 'ApiKey XXXX', | ||
} | ||
|
||
def request( | ||
self, | ||
method, | ||
url, | ||
params=None, | ||
data=None, | ||
headers=None, | ||
cookies=None, | ||
files=None, | ||
auth=None, | ||
timeout=None, | ||
allow_redirects=True, | ||
proxies=None, | ||
hooks=None, | ||
stream=None, | ||
verify=None, | ||
cert=None, | ||
json=None, | ||
context=None, | ||
installation=None, | ||
config=None, | ||
log_level=None, | ||
): | ||
headers = self._populate_internal_headers( | ||
headers or {}, | ||
context=context, | ||
installation=installation, | ||
config=config, | ||
log_level=log_level, | ||
) | ||
mocker = self._get_client_mocker(method, url) | ||
if installation and mocker: | ||
with mocker(self.base_url) as mocker: | ||
mocker.ns('devops').collection('installations').resource( | ||
installation['id'], | ||
).get(return_value=installation) | ||
return super().request( | ||
method, | ||
url, | ||
params=params, | ||
data=data, | ||
headers=headers, | ||
cookies=cookies, | ||
files=files, | ||
auth=auth, | ||
timeout=timeout, | ||
allow_redirects=allow_redirects, | ||
proxies=proxies, | ||
hooks=hooks, | ||
stream=stream, | ||
verify=verify, | ||
cert=cert, | ||
json=json, | ||
) | ||
return super().request( | ||
method, | ||
url, | ||
params=params, | ||
data=data, | ||
headers=headers, | ||
cookies=cookies, | ||
files=files, | ||
auth=auth, | ||
timeout=timeout, | ||
allow_redirects=allow_redirects, | ||
proxies=proxies, | ||
hooks=hooks, | ||
stream=stream, | ||
verify=verify, | ||
cert=cert, | ||
json=json, | ||
) | ||
|
||
def _get_client_mocker(self, method, url): | ||
path = urlparse(url).path | ||
for route in self.app.router.routes: | ||
match, child_scope = route.matches({'type': 'http', 'method': method, 'path': path}) | ||
if match == Match.FULL: | ||
if inspect.iscoroutinefunction(child_scope['endpoint']): | ||
return AsyncConnectClientMocker | ||
return ConnectClientMocker | ||
|
||
def _generate_call_context(self, installation): | ||
return Context(**{ | ||
'installation_id': installation['id'] if installation else 'EIN-000', | ||
'user_id': 'UR-000', | ||
'account_id': 'VA-000', | ||
'account_role': 'vendor', | ||
'call_source': 'ui', | ||
'call_type': 'user', | ||
}) | ||
|
||
def _populate_internal_headers( | ||
self, | ||
headers, | ||
config=None, | ||
installation=None, | ||
context=None, | ||
log_level=None, | ||
): | ||
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 | ||
headers['X-Connect-User-Id'] = context.user_id | ||
headers['X-Connect-Account-Id'] = context.account_id | ||
headers['X-Connect-Account-Role'] = context.account_role | ||
headers['X-Connect-Call-Source'] = context.call_source | ||
headers['X-Connect-Call-Type'] = context.call_type | ||
return headers | ||
|
||
def _get_application(self): | ||
app = FastAPI() | ||
|
||
auth_router, no_auth_router = self._webapp_class.get_routers() | ||
app.include_router(auth_router, prefix='/api') | ||
app.include_router(no_auth_router, prefix='/guest') | ||
|
||
static_root = self._webapp_class.get_static_root() | ||
if static_root: | ||
app.mount('/static', StaticFiles(directory=static_root), name='static') | ||
|
||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
def obfuscate_header(key, value): | ||
if key in ('authorization', 'authentication'): | ||
if value.startswith('ApiKey '): | ||
return value.split(':')[0] + ':' + '*' * 10 | ||
else: | ||
return '*' * 20 | ||
if key in ('cookie', 'set-cookie') and 'api_key="' in value: | ||
start_idx = value.index('api_key="') + len('api_key="') | ||
end_idx = value.index('"', start_idx) | ||
return f'{value[0:start_idx + 2]}******{value[end_idx - 2:]}' | ||
return value |
Oops, something went wrong.