Skip to content

Commit

Permalink
PXP-7251 Fix for GUID prefixes (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulineribeyre authored Dec 1, 2020
1 parent 8e32c03 commit 222e645
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 334 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ codacy-coverage = "*"
pytest="~=4.3"
pytest-mock="~=1.10"
pytest-flask="~=0.15.0"
unittest2="~=1.1.0"

[packages]
flask="~=1.1.2"
Expand Down
269 changes: 149 additions & 120 deletions Pipfile.lock

Large diffs are not rendered by default.

24 changes: 15 additions & 9 deletions manifestservice/manifests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

blueprint = flask.Blueprint("manifests", __name__)


@blueprint.route("/", methods=["GET"])
def get_manifests():
"""
Expand Down Expand Up @@ -123,11 +124,12 @@ def put_manifest():

return flask.jsonify(ret), 200


@blueprint.route("/cohorts", methods=["GET"])
def get_cohorts():
"""
Returns a list of filenames -- which are GUIDs -- corresponding to the user's exported
PFBs. We find the appropriate folder ("prefix") in the bucket by asking Fence for
Returns a list of filenames -- which are GUIDs -- corresponding to the user's exported
PFBs. We find the appropriate folder ("prefix") in the bucket by asking Fence for
info about the user's access token.
---
responses:
Expand All @@ -154,6 +156,7 @@ def get_cohorts():

return flask.jsonify(json_to_return), 200


@blueprint.route("/cohorts", methods=["PUT", "POST"])
def put_pfb_guid():
"""
Expand Down Expand Up @@ -346,11 +349,11 @@ def _list_files_in_bucket(bucket_name, folder):
Lists the files in an s3 bucket. Returns a dictionary.
The return value is of the form
{
"manifests:" [
"manifests:" [
# For files in the root of the user folder
{ "filename": <filename>, "last_modified": <timestamp> }, ...
],
"cohorts": [
"cohorts": [
# For files in the cohorts/ folder
{ "filename": <filename>, "last_modified": <timestamp> }, ...
]
Expand All @@ -371,15 +374,18 @@ def _list_files_in_bucket(bucket_name, folder):
bucket_objects = bucket.objects.filter(Prefix=folder + "/")
for object_summary in bucket_objects:
file_marker = {
"filename": ntpath.basename(object_summary.key),
"last_modified": object_summary.last_modified.strftime(
"%Y-%m-%d %H:%M:%S"
),
"last_modified_timestamp": datetime.timestamp(object_summary.last_modified),
"last_modified_timestamp": datetime.timestamp(
object_summary.last_modified
),
}
if not "cohorts/" in object_summary.key:
file_marker["filename"] = ntpath.basename(object_summary.key)
manifests.append(file_marker)
else:
file_marker["filename"] = object_summary.key.split("cohorts/")[1]
guids.append(file_marker)
except Exception as e:
logger.error(
Expand All @@ -388,9 +394,9 @@ def _list_files_in_bucket(bucket_name, folder):
)
)
return str(e), False
manifests_sorted = sorted(manifests, key = lambda i: i['last_modified_timestamp'])
guids_sorted = sorted(guids, key = lambda i: i['last_modified_timestamp'])

manifests_sorted = sorted(manifests, key=lambda i: i["last_modified_timestamp"])
guids_sorted = sorted(guids, key=lambda i: i["last_modified_timestamp"])

rv = {"manifests": manifests_sorted, "cohorts": guids_sorted}
return rv, True
Expand Down
75 changes: 0 additions & 75 deletions tests/broken_s3_test.py

This file was deleted.

85 changes: 85 additions & 0 deletions tests/cohorts_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import json as json_utils

from manifestservice.manifests import _list_files_in_bucket


def test_POST_successful_GUID_add(client, mocks):
"""
Test the Export PFB to Workspace pathway: a cohort is added to the bucket.
Note that because s3 is being mocked, only an integration test can properly
verify file creation.
"""
test_guid = "5183a350-9d56-4084-8a03-6471cafeb7fe"
post_body = {"guid": test_guid}

headers = {"Content-Type": "application/json", "Accept": "application/json"}
r = client.post("/cohorts", data=json_utils.dumps(post_body), headers=headers)

assert r.status_code == 200
assert mocks["_authenticate_user"].call_count == 1
assert mocks["_get_file_contents"].call_count == 0
assert mocks["_add_manifest_to_bucket"].call_count == 0
assert mocks["_add_GUID_to_bucket"].call_count == 1

json = r.json
new_guid = json["filename"]

assert new_guid is not None
assert type(new_guid) is str


def test_GET_cohorts(client, mocks):
"""
Test GET /cohorts
"""
headers = {"Content-Type": "application/json", "Accept": "application/json"}
r = client.get("/cohorts", headers=headers)

assert r.status_code == 200
assert mocks["_authenticate_user"].call_count == 1
assert mocks["_get_file_contents"].call_count == 0
assert mocks["_add_manifest_to_bucket"].call_count == 0
assert mocks["_add_GUID_to_bucket"].call_count == 0
assert mocks["_list_files_in_bucket"].call_count == 1

json = r.json
cohorts_returned = json["cohorts"]
assert len(cohorts_returned) == 1
# From the s3 mock
assert cohorts_returned[0]["filename"] == "18e32c12-a053-4ac5-90a5-f01f70b5c2be"


def test_GET_cohorts_broken_s3(client, broken_s3_mocks):
"""
Test GET /cohorts if s3 creds are broken
"""
headers = {"Content-Type": "application/json", "Accept": "application/json"}
r = client.get("/cohorts", headers=headers)

assert r.status_code == 500
assert broken_s3_mocks["_authenticate_user"].call_count == 1

response = r.json
assert len(response.keys()) == 1
assert response["error"] == "Currently unable to connect to s3."


def test_list_files_in_bucket(client, mocked_bucket):
"""
Test that prefixes are not removed from GUIDs when listing cohorts
in buckets
"""
result, ok = _list_files_in_bucket("fake_bucket_name", "fake_folder")
assert ok, result

manifests = result["manifests"]
assert len(manifests) == 1
assert manifests[0]["filename"] == "my-manifest.json"

cohorts = result["cohorts"]
assert len(cohorts) == 2
for cohort in cohorts:
if "without-prefix" in cohort["filename"]:
assert cohort["filename"] == "guid-without-prefix"
else:
assert cohort["filename"] == "dg.mytest/guid-with-prefix"
Loading

0 comments on commit 222e645

Please sign in to comment.