Commit ca6ba0b4 authored by Ilias Tsitsimpis's avatar Ilias Tsitsimpis Committed by Christos Stavrakakis

Implement getUUIDs/getUUID methods

parent e35a3608
...@@ -228,6 +228,37 @@ class AstakosClient(): ...@@ -228,6 +228,37 @@ class AstakosClient():
# XXX: check if exists # XXX: check if exists
return uuid_dict.get(uuid) return uuid_dict.get(uuid)
# ----------------------------------
def getUUIDs(self, token, display_names):
"""Return a displayname_catalog for the given names
Keyword arguments:
token -- user's token (string)
display_names -- list of user names (list of strings)
The returned displayname_catalog is a dictionary with
the names as keys and the corresponding uuids as values
"""
req_headers = {'content-type': 'application/json'}
req_body = simplejson.dumps({'displaynames': display_names})
req_path = "/user_catalogs"
data = self._callAstakos(
token, req_path, req_headers, req_body, "POST")
# XXX: check if exists
return data.get("displayname_catalog")
def getUUID(self, token, display_name):
"""Return the uuid of a name (see getUUIDs)"""
if not display_name:
m = "No display_name was given"
self.logger.error(m)
raise ValueError(m)
name_dict = self.getUUIDs(token, [display_name])
# XXX: check if exists
return name_dict.get(display_name)
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# Private functions # Private functions
......
...@@ -153,13 +153,26 @@ def _reqCatalogs(conn, method, url, **kwargs): ...@@ -153,13 +153,26 @@ def _reqCatalogs(conn, method, url, **kwargs):
# Return # Return
body = simplejson.loads(kwargs['body']) body = simplejson.loads(kwargs['body'])
uuids = body['uuids'] if 'uuids' in body:
catalogs = {} # Return uuid_catalog
if user_1['uuid'] in uuids: uuids = body['uuids']
catalogs[user_1['uuid']] = user_1['username'] catalogs = {}
if user_2['uuid'] in uuids: if user_1['uuid'] in uuids:
catalogs[user_2['uuid']] = user_2['username'] catalogs[user_1['uuid']] = user_1['username']
return_catalog = {"displayname_catalog": {}, "uuid_catalog": catalogs} if user_2['uuid'] in uuids:
catalogs[user_2['uuid']] = user_2['username']
return_catalog = {"displayname_catalog": {}, "uuid_catalog": catalogs}
elif 'displaynames' in body:
# Return displayname_catalog
names = body['displaynames']
catalogs = {}
if user_1['username'] in names:
catalogs[user_1['username']] = user_1['uuid']
if user_2['username'] in names:
catalogs[user_2['username']] = user_2['uuid']
return_catalog = {"displayname_catalog": catalogs, "uuid_catalog": {}}
else:
return_catalog = {"displayname_catalog": {}, "uuid_catalog": {}}
return (simplejson.dumps(return_catalog), 200) return (simplejson.dumps(return_catalog), 200)
...@@ -527,7 +540,7 @@ class TestDisplayNames(unittest.TestCase): ...@@ -527,7 +540,7 @@ class TestDisplayNames(unittest.TestCase):
# ---------------------------------- # ----------------------------------
# Get Info for both users # Get Info for both users
def test_DisplayNamesWithGet(self): def test_DisplayNames(self):
"""Test getDisplayNames with both users""" """Test getDisplayNames with both users"""
global token_1, user_1, user_2 global token_1, user_1, user_2
_mockRequest([_requestOk]) _mockRequest([_requestOk])
...@@ -555,6 +568,54 @@ class TestDisplayNames(unittest.TestCase): ...@@ -555,6 +568,54 @@ class TestDisplayNames(unittest.TestCase):
self.assertEqual(info, user_1['username']) self.assertEqual(info, user_1['username'])
class TestGetUUIDs(unittest.TestCase):
"""Test cases for functions getUUIDs/getUUID"""
# ----------------------------------
# Test the response we get for invalid token
def test_InvalidToken(self):
"""Test the response we get for invalid token (using pool)"""
global user_1
token = "skaksaFlBl+fasFdaf24sx=="
_mockRequest([_requestOk])
try:
client = AstakosClient("https://example.com")
client.getUUIDs(token, [user_1['username']])
except AstakosClientException as err:
if err.status != 401:
self.fail("Should have returned 401 (Invalid X-Auth-Token)")
else:
self.fail("Should have returned 401 (Invalid X-Auth-Token)")
# ----------------------------------
# Get info for both users
def test_UUIDs(self):
"""Test getUUIDs with both users"""
global token_1, user_1, user_2
_mockRequest([_requestOk])
try:
client = AstakosClient("https://example.com")
catalog = client.getUUIDs(
token_1, [user_1['username'], user_2['username']])
except:
self.fail("Shouldn't raise an Exception")
self.assertEqual(catalog[user_1['username']], user_1['uuid'])
self.assertEqual(catalog[user_2['username']], user_2['uuid'])
# ----------------------------------
# Get uuid for user 2
def test_GetUUIDUserTwo(self):
"""Test getUUID for User Two"""
global token_1, user_2
_mockRequest([_requestOffline, _requestOk])
try:
client = AstakosClient("https://example.com", retry=1)
info = client.getUUID(token_2, user_1['username'])
except:
self.fail("Shouldn't raise an Exception")
self.assertEqual(info, user_1['uuid'])
# ---------------------------- # ----------------------------
# Run tests # Run tests
if __name__ == "__main__": if __name__ == "__main__":
......
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