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:
parent
1c479fa865
commit
beb9ac8c19
|
@ -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):
|
||||
|
|
|
@ -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')
|
Loading…
Reference in New Issue