Cleaning up validate_ref()

Making validate_ref() more pythonic and readable by using the
tuple return instead. Also, added explicit tests for validate_ref.

Change-Id: Ib056050c36b2ffe8a7476c5724e26146b5fcd43c
This commit is contained in:
John Vrbanac 2015-04-20 18:11:48 -05:00
parent 1c479fa865
commit beb9ac8c19
2 changed files with 23 additions and 7 deletions

View File

@ -17,23 +17,25 @@ Base utilities to build API operation managers.
"""
import uuid
import six
def filter_null_keys(dictionary):
return dict(((k, v) for k, v in dictionary.items() if v is not None))
def validate_ref(ref, entity):
"""Verifies that there is a real uuid at the end of the uri
:return: Returns True for easier testing
:raises ValueError: If it cannot correctly parse the uuid in the ref.
"""
try:
# Split out the UUID from the ref URL
url = six.moves.urllib.parse.urlparse(ref)
parts = url.path.rstrip('/').split('/')
# Attempt to load the UUID with uuid, which will raise if invalid
uuid.UUID(parts[-1])
_, entity_uuid = ref.rstrip('/').rsplit('/', 1)
uuid.UUID(entity_uuid)
except:
raise ValueError('{0} incorrectly specified.'.format(entity))
return True
class ImmutableException(Exception):
def __init__(self, attribute=None):

View File

@ -0,0 +1,14 @@
import testtools
from barbicanclient import base
class TestValidateRef(testtools.TestCase):
def test_valid_ref(self):
ref = 'http://localhost/ff2ca003-5ebb-4b61-8a17-3f9c54ef6356'
self.assertTrue(base.validate_ref(ref, 'Thing'))
def test_invalid_uuid(self):
ref = 'http://localhost/not_a_uuid'
self.assertRaises(ValueError, base.validate_ref, ref, 'Thing')