Skip to content
Snippets Groups Projects
Commit a9f98fe3 authored by slobinger's avatar slobinger
Browse files

Merge branch '6-SAMSProxy_development' into 'master'

Resolve "SAMSProxy entwickeln"

Closes #6

See merge request !11
parents 0cc2e906 83ff9aa4
Branches
Tags
1 merge request!11Resolve "SAMSProxy entwickeln"
...@@ -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,7 +51,13 @@ class SAMSProxy: ...@@ -50,7 +51,13 @@ class SAMSProxy:
filteredSession[filteredParams[param]['name']] = str(session.get(param)) filteredSession[filteredParams[param]['name']] = str(session.get(param))
return filteredSession return filteredSession
def _get_first_dict(self, *possibleDicts)->dict: 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:
for possibleDict in possibleDicts: for possibleDict in possibleDicts:
if isinstance(possibleDict, dict): if isinstance(possibleDict, dict):
return possibleDict return possibleDict
......
...@@ -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'}}}},
for method in ('get', 'post', 'delete', 'put', 'update'): {'in': '/foo/bar', 'out': 'http://baz.foo'
SAMSProxy(proxySpec = {'in': '/foo/bar', 'out': 'http://baz.foo' , 'session-passthrough': {'default': {'user': {'param_type': 'body'}}}}
, 'session_passthrough': {'default': { ]
'user': {'param_type': 'body', 'name': 'u'} for method in ('get', 'post', 'delete', 'put', 'update'):
}, method: {'user': {'param_type': 'body'}}}}) proxySpecs.append({'in': '/foo/bar', 'out': 'http://baz.foo',
SAMSProxy(proxySpec = {'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': {'name': 'u'}}}}) proxySpecs.append({'in': '/foo/bar', 'out': 'http://baz.foo',
'session-passthrough': {'default': {
'user': {'param_type': 'body', '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):
...@@ -187,4 +194,33 @@ class TestSAMSHubWithThreadedAPI(unittest.TestCase): ...@@ -187,4 +194,33 @@ class TestSAMSHubWithThreadedAPI(unittest.TestCase):
resDict = json.loads(res.data.decode('utf-8')) resDict = json.loads(res.data.decode('utf-8'))
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')
\ No newline at end of file
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