Commit 63077d24 authored by Luc Yriarte's avatar Luc Yriarte
Browse files

Make urn decoding a bit more robust

parent d6a8844f
Pipeline #48636 failed with stages
in 22 minutes and 39 seconds
......@@ -13,7 +13,7 @@
# limitations under the License.
import uuid
from typing import Tuple
from typing import Tuple, Optional
class BulkId:
......@@ -30,7 +30,9 @@ class BulkId:
# Returns a tuple (<uuid> : str, <prefix> : str)
@classmethod
def bulk_urn_decode(cls, urn: str) -> Tuple[str, str]:
def bulk_urn_decode(cls, urn: str) -> Tuple[str, Optional[str]]:
if urn is None:
raise ValueError('attempted to decode empty urn')
parts = urn.split(":")
if len(parts) < 4:
return str(uuid.UUID(urn)), None
......
......@@ -22,6 +22,8 @@ from fastapi.testclient import TestClient
from fastapi import Header, status
from odes_storage.models import CreateUpdateRecordsResponse, Record
from app.bulk_persistence import BulkId
from app.model.osdu_model import Wellbore, Well
from app.clients import SearchServiceClient, StorageRecordServiceClient
......@@ -198,3 +200,22 @@ def test_get_record_success(client, base_url, id, record_obj):
# assert it validates the input object schema
record_obj.validate(response.json())
# urn decode test
def test_decode_urn_no_prefix():
uuid, prefix = BulkId.bulk_urn_decode("urn:uuid:489768d2-eee1-4a8f-ae95-7b0c30b0dcd8")
assert uuid == "489768d2-eee1-4a8f-ae95-7b0c30b0dcd8"
assert prefix is None
def test_decode_urn_with_prefix():
uuid, prefix = BulkId.bulk_urn_decode("urn:myprefix:uuid:489768d2-eee1-4a8f-ae95-7b0c30b0dcd8")
assert uuid == "489768d2-eee1-4a8f-ae95-7b0c30b0dcd8"
assert prefix == 'myprefix'
def test_decode_urn_none():
uuid = None
try:
uuid, prefix = BulkId.bulk_urn_decode(None)
except ValueError:
pass
assert uuid is None
\ No newline at end of file
Supports Markdown
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