diff --git a/.secrets.baseline b/.secrets.baseline index f87eeee8..713d98c7 100644 --- a/.secrets.baseline +++ b/.secrets.baseline @@ -3,7 +3,7 @@ "files": null, "lines": null }, - "generated_at": "2021-05-25T17:04:07Z", + "generated_at": "2021-09-30T18:17:02Z", "plugins_used": [ { "name": "AWSKeyDetector" diff --git a/indexd/drs/blueprint.py b/indexd/drs/blueprint.py index 2134452c..dd2706c9 100644 --- a/indexd/drs/blueprint.py +++ b/indexd/drs/blueprint.py @@ -84,6 +84,32 @@ def get_signed_url(object_id, access_id): return res, 200 +def create_drs_uri(did): + """ + Return ga4gh-compilant drs format uri + + Args: + did(str): did of drs object + """ + + default_prefix = blueprint.index_driver.config.get("DEFAULT_PREFIX") + + if not default_prefix: + # For env without DEFAULT_PREFIX, uri will not be drs compliant + accession = did + self_uri = "drs://{}".format(accession) + else: + accession = ( + did.replace(default_prefix, "", 1).replace("/", "", 1).replace(":", "", 1) + ) + + self_uri = "drs://{}:{}".format( + default_prefix.replace("/", "", 1).replace(":", "", 1), accession + ) + + return self_uri + + def indexd_to_drs(record, expand=False): """ Convert record to ga4gh-compilant format @@ -101,7 +127,7 @@ def indexd_to_drs(record, expand=False): else record["bundle_id"] ) - self_uri = "drs://" + flask.current_app.hostname + "/" + did + self_uri = create_drs_uri(did) name = record["file_name"] if "file_name" in record else record["name"] @@ -197,7 +223,7 @@ def bundle_to_drs(record, expand=False, is_content=False): else record["bundle_id"] ) - drs_uri = "drs://" + flask.current_app.hostname + "/" + did + drs_uri = create_drs_uri(did) name = record["file_name"] if "file_name" in record else record["name"] diff --git a/tests/test_drs.py b/tests/test_drs.py index c68cdb1c..3eecfa13 100644 --- a/tests/test_drs.py +++ b/tests/test_drs.py @@ -56,7 +56,26 @@ def test_drs_get(client, user): assert rec_2["checksums"][0]["checksum"] == data["hashes"][k] assert rec_2["checksums"][0]["type"] == k assert rec_2["version"] - assert rec_2["self_uri"] == "drs://fictitious-commons.io/" + rec_1["did"] + assert rec_2["self_uri"] == "drs://testprefix:" + rec_1["did"].split(":")[1] + + +def test_drs_get_no_default(client, user): + # Change default index driver settings to use no prefix + settings["config"]["INDEX"]["driver"].config["DEFAULT_PREFIX"] = None + settings["config"]["INDEX"]["driver"].config["ADD_PREFIX_ALIAS"] = False + + data = get_doc() + did = "ad8f4658-6acd-4f96-0dd8-3709890c959f" + data["did"] = did + res_1 = client.post("/index/", json=data, headers=user) + assert res_1.status_code == 200 + res_2 = client.get("/ga4gh/drs/v1/objects/" + did) + assert res_2.status_code == 200 + rec_2 = res_2.json + assert rec_2["self_uri"] == "drs://" + did + + settings["config"]["INDEX"]["driver"].config["DEFAULT_PREFIX"] = "testprefix:" + settings["config"]["INDEX"]["driver"].config["ADD_PREFIX_ALIAS"] = True def test_drs_multiple_endpointurl(client, user): @@ -187,11 +206,11 @@ def test_get_presigned_url_wrong_access_id(client, user): def test_get_drs_with_encoded_slash(client, user): data = get_doc() - data["did"] = "dg.TEST/ed8f4658-6acd-4f96-9dd8-3709890c959e" + data["did"] = "testprefix:ed8f4658-6acd-4f96-9dd8-3709890c959e" res_1 = client.post("/index/", json=data, headers=user) assert res_1.status_code == 200 rec_1 = res_1.json - did = "dg.TEST%2Fed8f4658-6acd-4f96-9dd8-3709890c959e" + did = "testprefix%3aed8f4658-6acd-4f96-9dd8-3709890c959e" res_2 = client.get("/ga4gh/drs/v1/objects/" + did) assert res_2.status_code == 200 rec_2 = res_2.json @@ -201,7 +220,7 @@ def test_get_drs_with_encoded_slash(client, user): assert rec_2["checksums"][0]["checksum"] == data["hashes"][k] assert rec_2["checksums"][0]["type"] == k assert rec_2["version"] - assert rec_2["self_uri"] == "drs://fictitious-commons.io/" + rec_1["did"] + assert rec_2["self_uri"] == "drs://testprefix:" + rec_1["did"].split(":")[1] @responses.activate