Commit e23e093f authored by Ilias Tsitsimpis's avatar Ilias Tsitsimpis

Add 2 new exceptions to AstakosClient

* NoDisplayName
  raised if getDisplayName called with non-existing uuid

* NoUUID
  raised if getUUID called with non-existing display_name
parent 854b005a
......@@ -40,7 +40,8 @@ from copy import copy
import simplejson
from astakosclient.utils import retry, scheme_to_class
from astakosclient.errors import \
AstakosClientException, Unauthorized, BadRequest, NotFound, Forbidden
AstakosClientException, Unauthorized, BadRequest, NotFound, Forbidden, \
NoDisplayName, NoUUID
# --------------------------------------------------------------------
......@@ -190,8 +191,13 @@ class AstakosClient():
req_body = simplejson.dumps({'uuids': uuids})
data = self._callAstakos(
token, req_path, req_headers, req_body, "POST")
# XXX: check if exists
return data.get("uuid_catalog")
if "uuid_catalog" in data:
return data.get("uuid_catalog")
else:
m = "_uuidCatalog request returned %s. No uuid_catalog found" \
% data
self.logger.error(m)
raise AstakosClientException(m)
def getDisplayNames(self, token, uuids):
"""Return a uuid_catalog dictionary for the given uuids
......@@ -214,8 +220,10 @@ class AstakosClient():
self.logger.error(m)
raise ValueError(m)
uuid_dict = self.getDisplayNames(token, [uuid])
# XXX: check if exists
return uuid_dict.get(uuid)
if uuid in uuid_dict:
return uuid_dict.get(uuid)
else:
raise NoDisplayName(uuid)
def getServiceDisplayNames(self, token, uuids):
"""Return a uuid_catalog dict using a service's token"""
......@@ -229,8 +237,10 @@ class AstakosClient():
self.logger.error(m)
raise ValueError(m)
uuid_dict = self.getServiceDisplayNames(token, [uuid])
# XXX: check if exists
return uuid_dict.get(uuid)
if uuid in uuid_dict:
return uuid_dict.get(uuid)
else:
raise NoDisplayName(uuid)
# ----------------------------------
def _displayNameCatalog(self, token, display_names, req_path):
......@@ -238,8 +248,13 @@ class AstakosClient():
req_body = simplejson.dumps({'displaynames': display_names})
data = self._callAstakos(
token, req_path, req_headers, req_body, "POST")
# XXX: check if exists
return data.get("displayname_catalog")
if "displayname_catalog" in data:
return data.get("displayname_catalog")
else:
m = "_displayNameCatalog request returned %s. " \
"No displayname_catalog found" % data
self.logger.error(m)
raise AstakosClientException(m)
def getUUIDs(self, token, display_names):
"""Return a displayname_catalog for the given names
......@@ -262,8 +277,10 @@ class AstakosClient():
self.logger.error(m)
raise ValueError(m)
name_dict = self.getUUIDs(token, [display_name])
# XXX: check if exists
return name_dict.get(display_name)
if display_name in name_dict:
return name_dict.get(display_name)
else:
raise NoUUID(display_name)
def getServiceUUIDs(self, token, display_names):
"""Return a display_name catalog using a service's token"""
......@@ -277,8 +294,10 @@ class AstakosClient():
self.logger.error(m)
raise ValueError(m)
name_dict = self.getServiceUUIDs(token, [display_name])
# XXX: check if exists
return name_dict.get(display_name)
if display_name in name_dict:
return name_dict.get(display_name)
else:
raise NoUUID(display_name)
# ----------------------------------
def getServices(self):
......
......@@ -63,3 +63,17 @@ class NotFound(AstakosClientException):
def __init__(self, message):
"""404 Not Found"""
super(NotFound, self).__init__(message, 404)
class NoDisplayName(AstakosClientException):
def __init__(self, uuid):
"""No display name for the given uuid"""
message = "No display name for the given uuid: %s" % uuid
super(NoDisplayName, self).__init__(message)
class NoUUID(AstakosClientException):
def __init__(self, display_name):
"""No uuid for the given display name"""
message = "No uuid for the given display name: %s" % display_name
super(NoUUID, self).__init__(message)
......@@ -47,7 +47,8 @@ import simplejson
import astakosclient
from astakosclient import AstakosClient
from astakosclient.errors import \
AstakosClientException, Unauthorized, BadRequest, NotFound
AstakosClientException, Unauthorized, BadRequest, NotFound, \
NoDisplayName, NoUUID
# Use backported unittest functionality if Python < 2.7
try:
......@@ -575,6 +576,21 @@ class TestDisplayNames(unittest.TestCase):
self.fail("Shouldn't raise an Exception")
self.assertEqual(info, user_1['username'])
# ----------------------------------
# Get info with wrong uuid
def test_NoDisplayName(self):
global token_1
_mockRequest([_requestOk])
try:
client = AstakosClient("https://example.com")
client.getDisplayName(token_1, "1234")
except NoDisplayName:
pass
except:
self.fail("Should have raised NoDisplayName exception")
else:
self.fail("Should have raised NoDisplayName exception")
class TestGetUUIDs(unittest.TestCase):
"""Test cases for functions getUUIDs/getUUID"""
......@@ -624,6 +640,21 @@ class TestGetUUIDs(unittest.TestCase):
self.fail("Shouldn't raise an Exception")
self.assertEqual(info, user_1['uuid'])
# ----------------------------------
# Get uuid with wrong username
def test_NoUUID(self):
global token_1
_mockRequest([_requestOk])
try:
client = AstakosClient("https://example.com")
client.getUUID(token_1, "1234")
except NoUUID:
pass
except:
self.fail("Should have raised NoUUID exception")
else:
self.fail("Should have raised NoUUID exception")
# ----------------------------
# Run tests
......
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