Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update exception file get download URL #866

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions boxsdk/object/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from datetime import datetime
from typing import TYPE_CHECKING, Optional, Tuple, Union, IO, Iterable, List, Any
from boxsdk.exception import BoxException

from boxsdk.util.datetime_formatter import normalize_date_to_rfc3339_format
from .item import Item
Expand Down Expand Up @@ -177,6 +178,9 @@ def get_download_url(self, file_version: Optional['FileVersion'] = None) -> str:
expect_json_response=False,
allow_redirects=False,
)
if 'location' not in box_response.headers:
raise BoxException('Download URL is not present in the response.')

return box_response.headers['location']

@api_call
Expand Down
16 changes: 15 additions & 1 deletion test/unit/object/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pytest_lazyfixture import lazy_fixture

from boxsdk.config import API
from boxsdk.exception import BoxAPIException
from boxsdk.exception import BoxAPIException, BoxException
from boxsdk.object.comment import Comment
from boxsdk.object.file import File
from boxsdk.object.file_version import FileVersion
Expand Down Expand Up @@ -237,6 +237,20 @@ def test_get_download_url(test_file, mock_box_session):
assert url == download_url


def test_get_download_url_failed(test_file, mock_box_session):
expected_url = f'{API.BASE_API_URL}/files/{test_file.object_id}/content'
mock_box_session.get.return_value.headers = {}
with pytest.raises(BoxException) as exc_info:
test_file.get_download_url()
assert exc_info.value.args[0] == 'Download URL is not present in the response.'
mock_box_session.get.assert_called_once_with(
expected_url,
params=None,
expect_json_response=False,
allow_redirects=False
)


def test_get_download_url_file_version(test_file, test_file_version, mock_box_session):
expected_url = f'{API.BASE_API_URL}/files/{test_file.object_id}/content'
download_url = 'https://dl.boxcloud.com/sdjhfgksdjfgshdbg'
Expand Down
Loading