Commit 91478081 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Fix unittests for kamaki.clients.astakos

Refs: #4337, #4312
parent b276abde
......@@ -54,7 +54,7 @@ class AstakosClient(Client):
:raises AssertionError: if no token exists (either param or member)
"""
token = token or self.token or self.tokenlist[0]
token = token or self.token
assert token, 'No token provided'
return token[0] if (
isinstance(token, list) or isinstance(token, tuple)) else token
......@@ -68,14 +68,14 @@ class AstakosClient(Client):
"""
token = self._resolve_token(token)
astakos = SynnefoAstakosClient(
token, self.base_url, logger=getLogger('_my_.astakosclient'))
token, self.base_url, logger=getLogger('astakosclient'))
r = astakos.get_endpoints()
uuid = r['access']['user']['id']
self._uuids[token] = uuid
self._cache[uuid] = r
self._astakos[uuid] = astakos
self._uuids2usernames[token] = dict()
self._usernames2uuids[token] = dict()
self._uuids2usernames[uuid] = dict()
self._usernames2uuids[uuid] = dict()
return self._cache[uuid]
def get_token(self, uuid):
......@@ -180,15 +180,17 @@ class AstakosClient(Client):
def uuids2usernames(self, uuids, token=None):
token = self._resolve_token(token)
self._validate_token(token)
astakos = self._astakos[self._uuids[token]]
if set(uuids).difference(self._uuids2usernames[token]):
self._uuids2usernames[token].update(astakos.get_usernames(uuids))
return self._uuids2usernames[token]
uuid = self._uuids[token]
astakos = self._astakos[uuid]
if set(uuids).difference(self._uuids2usernames[uuid]):
self._uuids2usernames[uuid].update(astakos.get_usernames(uuids))
return self._uuids2usernames[uuid]
def usernames2uuids(self, usernames, token=None):
token = self._resolve_token(token)
self._validate_token(token)
astakos = self._astakos[self._uuids[token]]
if set(usernames).difference(self._usernames2uuids[token]):
self._usernames2uuids[token].update(astakos.get_uuids(usernames))
return self._usernames2uuids
uuid = self._uuids[token]
astakos = self._astakos[uuid]
if set(usernames).difference(self._usernames2uuids[uuid]):
self._usernames2uuids[uuid].update(astakos.get_uuids(usernames))
return self._usernames2uuids[uuid]
......@@ -32,33 +32,13 @@
# or implied, of GRNET S.A.
from mock import patch, call
from logging import getLogger
from unittest import TestCase
from kamaki.clients import astakos, ClientError
from kamaki.clients import ClientError
example = dict(
access=dict(
serviceCatalog=[
dict(name='service name 1', type='compute', endpoints=[
dict(versionId='v1', publicUrl='http://1.1.1.1/v1'),
dict(versionId='v2', publicUrl='http://1.1.1.1/v2')]),
dict(name='service name 2', type='image', endpoints=[
dict(versionId='v2', publicUrl='http://1.1.1.1/v2'),
dict(versionId='v2.1', publicUrl='http://1.1.1.1/v2/xtra')])
],
user=dict(
name='Simple Name',
username='User Full Name',
auth_token_expires='1362583796000',
auth_token_created='1359991796000',
email=['user@example.gr'],
id=42,
uuid='aus3r-uu1d-f0r-73s71ng-as7ak0s')
)
)
example0 = dict(
access=dict(
token=dict(
expires="2013-07-14T10:07:42.481134+00:00",
......@@ -94,7 +74,7 @@ class FR(object):
status = None
status_code = 200
astakos_pkg = 'kamaki.clients.astakos.AstakosClient'
astakos_pkg = 'kamaki.clients.astakos'
class AstakosClient(TestCase):
......@@ -112,22 +92,40 @@ class AstakosClient(TestCase):
def setUp(self):
self.url = 'https://astakos.example.com'
self.token = 'ast@k0sT0k3n=='
self.client = astakos.AstakosClient(self.url, self.token)
from kamaki.clients.astakos import AstakosClient as AC
self.client = AC(self.url, self.token)
def tearDown(self):
FR.json = example
@patch('%s.post' % astakos_pkg, return_value=FR())
def _authenticate(self, post):
@patch(
'%s.SynnefoAstakosClient.__init__' % astakos_pkg, return_value=None)
@patch(
'%s.SynnefoAstakosClient.get_endpoints' % astakos_pkg,
return_value=example)
def _authenticate(self, get_endpoints, sac):
r = self.client.authenticate()
send_body = dict(auth=dict(token=dict(id=self.token)))
self.assertEqual(post.mock_calls[-1], call('/tokens', json=send_body))
self.cached = True
self.assertEqual(
sac.mock_calls[-1], call(self.token, self.url,
logger=getLogger('astakosclient')))
self.assertEqual(get_endpoints.mock_calls[-1], call())
return r
def test_authenticate(self):
r = self._authenticate()
self.assert_dicts_are_equal(r, example)
uuid = example['access']['user']['id']
self.assert_dicts_are_equal(self.client._uuids, {self.token: uuid})
self.assert_dicts_are_equal(self.client._cache, {uuid: r})
from astakosclient import AstakosClient as SAC
self.assertTrue(isinstance(self.client._astakos[uuid], SAC))
self.assert_dicts_are_equal(self.client._uuids2usernames, {uuid: {}})
self.assert_dicts_are_equal(self.client._usernames2uuids, {uuid: {}})
def test_get_token(self):
self._authenticate()
uuid = self.client._uuids.values()[0]
self.assertEqual(self.client.get_token(uuid), self.token)
def test_get_services(self):
if not self.cached:
......@@ -176,12 +174,55 @@ class AstakosClient(TestCase):
def test_list_users(self):
if not self.cached:
self._authenticate()
FR.json = example0
FR.json = example
self._authenticate()
r = self.client.list_users()
self.assertTrue(len(r) == 1)
self.assertEqual(r[0]['auth_token'], self.token)
@patch(
'%s.SynnefoAstakosClient.get_usernames' % astakos_pkg,
return_value={42: 'username42', 43: 'username43'})
def test_uuids2usernames(self, get_usernames):
from astakosclient import AstakosClientException
self.assertRaises(
AstakosClientException, self.client.uuids2usernames, [42, 43])
with patch(
'%s.SynnefoAstakosClient.__init__' % astakos_pkg,
return_value=None) as sac:
with patch(
'%s.SynnefoAstakosClient.get_endpoints' % astakos_pkg,
return_value=example) as get_endpoints:
r = self.client.uuids2usernames([42, 43])
self.assert_dicts_are_equal(
r, {42: 'username42', 43: 'username43'})
self.assertEqual(sac.mock_calls[-1], call(
self.token, self.url, logger=getLogger('astakosclient')))
self.assertEqual(get_endpoints.mock_calls[-1], call())
self.assertEqual(get_usernames.mock_calls[-1], call([42, 43]))
@patch(
'%s.SynnefoAstakosClient.get_uuids' % astakos_pkg,
return_value={'username42': 42, 'username43': 43})
def test_usernames2uuids(self, get_uuids):
from astakosclient import AstakosClientException
self.assertRaises(
AstakosClientException, self.client.usernames2uuids, ['u1', 'u2'])
with patch(
'%s.SynnefoAstakosClient.__init__' % astakos_pkg,
return_value=None) as sac:
with patch(
'%s.SynnefoAstakosClient.get_endpoints' % astakos_pkg,
return_value=example) as get_endpoints:
r = self.client.usernames2uuids(['u1', 'u2'])
self.assert_dicts_are_equal(
r, {'username42': 42, 'username43': 43})
self.assertEqual(sac.mock_calls[-1], call(
self.token, self.url, logger=getLogger('astakosclient')))
self.assertEqual(get_endpoints.mock_calls[-1], call())
self.assertEqual(get_uuids.mock_calls[-1], call(['u1', 'u2']))
if __name__ == '__main__':
from sys import argv
from kamaki.clients.test import runTestCase
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment