Skip to content

Commit

Permalink
Merge pull request #870 from uc-cdis/fix/delete_with_no_urls
Browse files Browse the repository at this point in the history
Fix/delete with no urls
  • Loading branch information
mysterious-progression committed Feb 26, 2021
2 parents 9417655 + 1952025 commit b4ccb98
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
1 change: 0 additions & 1 deletion fence/blueprints/data/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

logger = get_logger(__name__)


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


Expand Down
15 changes: 9 additions & 6 deletions fence/blueprints/data/indexd.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,15 +472,14 @@ def delete_files(self, urls=None, delete_all=True):
urls (Optional[List[str]])
Return:
None
Response (str: message, int: status code)
"""
locations_to_delete = []
if urls is None and delete_all:
if not urls and delete_all:
locations_to_delete = self.indexed_file_locations
else:
locations_to_delete = [
location for location in locations_to_delete if location.url in urls
]
locations_to_delete = list(map(IndexedFileLocation.from_url, urls))
response = ("No URLs to delete", 200)
for location in locations_to_delete:
bucket = location.bucket_name()

Expand All @@ -496,7 +495,11 @@ def delete_files(self, urls=None, delete_all=True):
file_suffix, bucket
)
)
return location.delete(bucket, file_suffix)
response = location.delete(bucket, file_suffix)
# check status code not in 200s
if response[1] > 399:
break
return response

@login_required({"data"})
def delete(self):
Expand Down
64 changes: 64 additions & 0 deletions tests/data/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,3 +1213,67 @@ def json(self):
response = client.post("/data/multipart/complete", headers=headers, data=data)

assert response.status_code == 200, response


def test_delete_files(app, client, auth_client, encoded_creds_jwt, user_client):
fence.auth.config["MOCK_AUTH"] = True
did = str(uuid.uuid4())
did2 = str(uuid.uuid4())

indx = fence.blueprints.data.indexd.IndexedFile(did)
urls = [
"s3://bucket1/key-{}".format(did[:8]),
"s3://bucket1/key-{}".format(did2[:8]),
]
with patch("fence.blueprints.data.indexd.S3IndexedFileLocation") as s3mock:
s3mock.return_value.bucket_name.return_value = "bucket"
s3mock.return_value.file_name.return_value = "file"
mocklocation = s3mock.return_value

# no urls, no files, no error
mocklocation.delete.return_value = ("ok", 200)
indx.indexed_file_locations = []
message, status = indx.delete_files(urls=None, delete_all=True)
assert 0 == mocklocation.delete.call_count
assert status == 200

# urls, files, no error
mocklocation.reset_mock()
indx.indexed_file_locations = [mocklocation, mocklocation]
message, status = indx.delete_files(urls, delete_all=True)
assert 2 == mocklocation.delete.call_count
assert status == 200

# no urls, files, no error
mocklocation.reset_mock()
message, status = indx.delete_files(urls=None)
assert 2 == mocklocation.delete.call_count
assert status == 200

# case for urls subset of total locations without error
mocklocation.reset_mock()
urls = ["s3://bucket1/key-{}".format(did2[:8])]
message, status = indx.delete_files(urls, delete_all=True)
assert 1 == mocklocation.delete.call_count
assert status == 200

# no urls, files, error
mocklocation.reset_mock()
mocklocation.delete.return_value = ("bad response", 400)
message, status = indx.delete_files(urls=None, delete_all=True)
assert 1 == mocklocation.delete.call_count
assert status == 400

# urls, files, error
mocklocation.reset_mock()
message, status = indx.delete_files(urls)
assert 1 == mocklocation.delete.call_count
assert status == 400

# case for urls subset of total locations with error
mocklocation.reset_mock()
message, status = indx.delete_files(urls, delete_all=True)
assert 1 == mocklocation.delete.call_count
assert status == 400

fence.auth.config["MOCK_AUTH"] = False

0 comments on commit b4ccb98

Please sign in to comment.