Skip to content

Commit

Permalink
Merge 1e1e342 into b32ad4a
Browse files Browse the repository at this point in the history
  • Loading branch information
congminh1254 committed Jan 18, 2024
2 parents b32ad4a + 1e1e342 commit 601481e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions boxsdk/object/file.py
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
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

0 comments on commit 601481e

Please sign in to comment.