Skip to content

Commit

Permalink
adds test for no expiration url
Browse files Browse the repository at this point in the history
  • Loading branch information
BWMac committed Sep 3, 2024
1 parent 4194b54 commit 272e41b
Showing 1 changed file with 65 additions and 10 deletions.
75 changes: 65 additions & 10 deletions tests/unit/synapseclient/core/unit_test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,31 +834,87 @@ async def test_download_md5_mismatch_local_file(self) -> None:
assert not mocked_remove.called

async def test_download_expired_url(self, syn: Synapse) -> None:
url = "http://www.ayy.lmao/filerino.txt"
new_url = "http://www.ayy.lmao/new_url.txt"
url = "http://www.ayy.lmao/filerino.txt?Expires=0"
new_url = "http://www.ayy.lmao/new_url.txt?Expires=1715000000"
contents = "\n".join(str(i) for i in range(1000))
temp_destination = os.path.normpath(
os.path.expanduser("~/fake/path/filerino.txt.temp")
)
destination = os.path.normpath(os.path.expanduser("~/fake/path/filerino.txt"))

partial_content_break = len(contents) // 7 * 3
mock_requests_get = MockRequestGetFunction(
[
create_mock_response(
url,
"stream",
contents=contents[:partial_content_break],
contents=contents,
buffer_size=1024,
partial_end=len(contents),
status_code=403,
status_code=200,
),
]
)
with patch.object(
syn._requests_session, "get", side_effect=mock_requests_get
) as mocked_get, patch(
"synapseclient.core.download.download_functions._pre_signed_url_expiration_time",
return_value=datetime.datetime(1900, 1, 1, tzinfo=datetime.timezone.utc),
) as mocked_pre_signed_url_expiration_time, patch(
"synapseclient.core.download.download_functions.get_file_handle_for_download",
return_value={"preSignedURL": new_url},
) as mocked_get_file_handle_for_download, patch.object(
Synapse, "_generate_headers", side_effect=mock_generate_headers
), patch.object(
utils, "temp_download_filename", return_value=temp_destination
), patch(
"synapseclient.core.download.download_functions.open",
new_callable=mock_open(),
create=True,
), patch.object(
hashlib, "new"
) as mocked_hashlib_new, patch.object(
shutil, "move"
), patch.object(
os, "remove"
):
mocked_hashlib_new.return_value.hexdigest.return_value = "fake md5 is fake"
# WHEN I call download_from_url with an expired url
download_from_url(
url=url,
destination=destination,
entity_id=OBJECT_ID,
file_handle_associate_type=OBJECT_TYPE,
expected_md5="fake md5 is fake",
)
# I expect the expired url to be identified
mocked_pre_signed_url_expiration_time.assert_called_once_with(url)
# AND I expect the URL to be refreshed
mocked_get_file_handle_for_download.assert_called_once()
# AND I expect the download to be retried with the new URL
mocked_get.assert_called_with(
url=new_url,
headers=mock_generate_headers(self),
stream=True,
allow_redirects=False,
auth=None,
)

async def test_download_url_no_expiration(self, syn: Synapse) -> None:
url = "http://www.ayy.lmao/filerino.txt"
contents = "\n".join(str(i) for i in range(1000))
temp_destination = os.path.normpath(
os.path.expanduser("~/fake/path/filerino.txt.temp")
)
destination = os.path.normpath(os.path.expanduser("~/fake/path/filerino.txt"))

mock_requests_get = MockRequestGetFunction(
[
create_mock_response(
url,
"stream",
contents=contents,
buffer_size=1024,
partial_start=len(contents),
partial_end=len(contents),
status_code=200,
),
]
Expand All @@ -870,7 +926,6 @@ async def test_download_expired_url(self, syn: Synapse) -> None:
return_value=datetime.datetime(1900, 1, 1, tzinfo=datetime.timezone.utc),
) as mocked_pre_signed_url_expiration_time, patch(
"synapseclient.core.download.download_functions.get_file_handle_for_download",
return_value={"preSignedURL": new_url},
) as mocked_get_file_handle_for_download, patch.object(
Synapse, "_generate_headers", side_effect=mock_generate_headers
), patch.object(
Expand All @@ -896,12 +951,12 @@ async def test_download_expired_url(self, syn: Synapse) -> None:
expected_md5="fake md5 is fake",
)
# I expect the expired url to be identified
mocked_pre_signed_url_expiration_time.assert_called_once_with(url)
mocked_pre_signed_url_expiration_time.assert_not_called()
# AND I expect the URL to be refreshed
mocked_get_file_handle_for_download.assert_called_once()
mocked_get_file_handle_for_download.assert_not_called()
# AND I expect the download to be retried with the new URL
mocked_get.assert_called_with(
url=new_url,
url=url,
headers=mock_generate_headers(self),
stream=True,
allow_redirects=False,
Expand Down

0 comments on commit 272e41b

Please sign in to comment.