From beb9ac8c19a36bd9479d540a19ea507ef5b5b8f5 Mon Sep 17 00:00:00 2001 From: John Vrbanac Date: Mon, 20 Apr 2015 18:11:48 -0500 Subject: [PATCH] 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 --- barbicanclient/base.py | 16 +++++++++------- barbicanclient/tests/test_base.py | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 barbicanclient/tests/test_base.py diff --git a/barbicanclient/base.py b/barbicanclient/base.py index ec3f4aad..541db0e7 100644 --- a/barbicanclient/base.py +++ b/barbicanclient/base.py @@ -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): diff --git a/barbicanclient/tests/test_base.py b/barbicanclient/tests/test_base.py new file mode 100644 index 00000000..a186c4c4 --- /dev/null +++ b/barbicanclient/tests/test_base.py @@ -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')