Skip to content
Snippets Groups Projects
Commit 91630fbe authored by Sebastian Lobinger's avatar Sebastian Lobinger
Browse files

Merge branch 'master' into 16-update-documentation

parents 67b9dd1b a9f98fe3
No related branches found
No related tags found
1 merge request!12Resolve "Dokumentation aktualisieren"
...@@ -38,6 +38,7 @@ class SAMSProxy: ...@@ -38,6 +38,7 @@ class SAMSProxy:
def _pass_param(self, method, paramType, paramDict = {}): def _pass_param(self, method, paramType, paramDict = {}):
paramDict.update(self._filter_session(method, paramType)) paramDict.update(self._filter_session(method, paramType))
paramDict.update(self._filter_token(paramType))
return paramDict return paramDict
def _filter_session(self, method, paramType): def _filter_session(self, method, paramType):
...@@ -50,6 +51,12 @@ class SAMSProxy: ...@@ -50,6 +51,12 @@ class SAMSProxy:
filteredSession[filteredParams[param]['name']] = str(session.get(param)) filteredSession[filteredParams[param]['name']] = str(session.get(param))
return filteredSession return filteredSession
def _filter_token(self, paramType: str) -> dict:
if paramType == self._proxySpec.get('token', {}).get('param-type'):
return {
self._proxySpec['token']['name']: self._proxySpec['token']['value']}
return {}
def _get_first_dict(self, *possibleDicts) -> dict: def _get_first_dict(self, *possibleDicts) -> dict:
for possibleDict in possibleDicts: for possibleDict in possibleDicts:
if isinstance(possibleDict, dict): if isinstance(possibleDict, dict):
......
...@@ -26,30 +26,37 @@ class TestSAMSHub(unittest.TestCase): ...@@ -26,30 +26,37 @@ class TestSAMSHub(unittest.TestCase):
def test_mandatory_keys_in_spec(self): def test_mandatory_keys_in_spec(self):
""" ProxySpecKeyMissing is raised if something is mising in spec""" """ ProxySpecKeyMissing is raised if something is mising in spec"""
with self.assertRaises(ProxySpecKeyMssing): proxySpecs = [
SAMSProxy(proxySpec = {'out': 'http://baz.foo'}) {'out': 'http://baz.foo'},
SAMSProxy(proxySpec = {'in': '/foo/bar'}) {'in': '/foo/bar'},
SAMSProxy(proxySpec = {'in': '/foo/bar', 'out': 'http://baz.foo' {'in': '/foo/bar', 'out': 'http://baz.foo'
, 'token': {'name': 'user'}}) , 'token': {'name': 'user'}},
SAMSProxy(proxySpec = {'in': '/foo/bar', 'out': 'http://baz.foo' {'in': '/foo/bar', 'out': 'http://baz.foo'
, 'token': {'value': 'user'}}) , 'token': {'value': 'user'}},
SAMSProxy(proxySpec = {'in': '/foo/bar', 'out': 'http://baz.foo' {'in': '/foo/bar', 'out': 'http://baz.foo'
, 'session_passthrough': {}}) , 'token': {'name': 'user', 'value': 'user'}},
SAMSProxy(proxySpec = {'in': '/foo/bar', 'out': 'http://baz.foo' {'in': '/foo/bar', 'out': 'http://baz.foo'
, 'session_passthrough': {'default': {}}}) , 'session-passthrough': {}},
SAMSProxy(proxySpec = {'in': '/foo/bar', 'out': 'http://baz.foo' {'in': '/foo/bar', 'out': 'http://baz.foo'
, 'session_passthrough': {'default': {'user': {'name': 'u'}}}}) , 'session-passthrough': {'default': {}}},
SAMSProxy(proxySpec = {'in': '/foo/bar', 'out': 'http://baz.foo' {'in': '/foo/bar', 'out': 'http://baz.foo'
, 'session_passthrough': {'default': {'user': {'param_type': 'body'}}}}) , 'session-passthrough': {'default': {'user': {'name': 'u'}}}},
{'in': '/foo/bar', 'out': 'http://baz.foo'
, 'session-passthrough': {'default': {'user': {'param_type': 'body'}}}}
]
for method in ('get', 'post', 'delete', 'put', 'update'): for method in ('get', 'post', 'delete', 'put', 'update'):
SAMSProxy(proxySpec = {'in': '/foo/bar', 'out': 'http://baz.foo' proxySpecs.append({'in': '/foo/bar', 'out': 'http://baz.foo',
, 'session_passthrough': {'default': { 'session-passthrough': {'default': {
'user': {'param_type': 'body', 'name': 'u'} 'user': {'param_type': 'body', 'name': 'u'}
}, method: {'user': {'param_type': 'body'}}}}) }, method: {'user': {'param_type': 'body'}}}})
SAMSProxy(proxySpec = {'in': '/foo/bar', 'out': 'http://baz.foo' proxySpecs.append({'in': '/foo/bar', 'out': 'http://baz.foo',
, 'session_passthrough': {'default': { 'session-passthrough': {'default': {
'user': {'param_type': 'body', 'name': 'u'} 'user': {'param_type': 'body', 'name': 'u'}
}, method: {'user': {'name': 'u'}}}}) }, method: {'user': {'name': 'u'}}}})
for proxySpec in proxySpecs:
with self.subTest(proxySpec):
with self.assertRaises(ProxySpecKeyMssing):
SAMSProxy(proxySpec = proxySpec)
class TestSAMSHubWithThreadedAPI(unittest.TestCase): class TestSAMSHubWithThreadedAPI(unittest.TestCase):
...@@ -188,3 +195,32 @@ class TestSAMSHubWithThreadedAPI(unittest.TestCase): ...@@ -188,3 +195,32 @@ class TestSAMSHubWithThreadedAPI(unittest.TestCase):
self.assertEqual(resDict['url']['u'], 'foo') self.assertEqual(resDict['url']['u'], 'foo')
self.assertEqual(resDict['body']['g'], 'bar') self.assertEqual(resDict['body']['g'], 'bar')
self.assertEqual(resDict['header']['Lang'], 'klingon') self.assertEqual(resDict['header']['Lang'], 'klingon')
def test_token_passthrough(self):
"""The proxy passes the specified token and the other params"""
for param_type in ('header', 'url', 'body'):
proxyApp = Flask('test')
proxyApp.config['TESTING'] = True
proxy = SAMSProxy(
proxySpec = {
'in': '/proxy', 'out': 'http://localhost:4711',
'token': {
'name': 'Api-Token', 'value': '53CUR34P170K3N',
'param-type': param_type
}
}
)
with self.subTest(param_type):
proxyApp.add_url_rule(rule = proxy.urlRule, endpoint = 'proxy',
view_func = proxy.proxy, methods=['GET', 'POST', 'PUT', 'UPDATE',
'PATCH', 'DELETE', 'OPTIONS'])
with proxyApp.test_client(self) as client:
res = client.get(path='/proxy/passthrough',
query_string={'u':'foo'}, data={'g':'bar'},
headers={'lang': 'klingon'})
resDict = json.loads(res.data.decode('utf-8'))
self.assertEqual(resDict['url']['u'], 'foo')
self.assertEqual(resDict['body']['g'], 'bar')
self.assertEqual(resDict['header']['Lang'], 'klingon')
eprint(resDict[param_type])
self.assertEquals(resDict[param_type]['Api-Token'], '53CUR34P170K3N')
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment