Skip to content

Commit 4f5e4c3

Browse files
authored
issue 596: Return 0 bytes is no representation found (#597)
1 parent 0725aa1 commit 4f5e4c3

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

boxsdk/object/file.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ def get_thumbnail_representation(self, dimensions, extension='png'):
771771
Retrieve a thumbnail image for the file.
772772
773773
:param dimensions:
774-
The width by height size of this representation in pixels (i.e. '92x92')
774+
The width by height size of this representation in pixels (e.g. '92x92')
775775
:type dimensions:
776776
`unicode`
777777
:param extension:
@@ -785,10 +785,12 @@ def get_thumbnail_representation(self, dimensions, extension='png'):
785785
"""
786786
rep_hints = '[{0}?dimensions={1}]'.format(extension, dimensions)
787787
representation = self.get_representation_info(rep_hints)
788-
url = representation[0]['content']['url_template']
789-
url = url.replace('{+asset_path}', '')
790-
response = self._session.get(url, expect_json_response=False)
791-
return response.content
788+
if len(representation):
789+
url = representation[0]['content']['url_template']
790+
url = url.replace('{+asset_path}', '')
791+
response = self._session.get(url, expect_json_response=False)
792+
return response.content
793+
return b''
792794

793795
@api_call
794796
def copy(self, parent_folder, name=None, file_version=None):

test/unit/object/test_file.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,3 +868,37 @@ def test_get_thumbnail_representation(
868868
params={'fields': 'representations'})
869869
mock_box_session.get.assert_any_call(content_url, expect_json_response=False)
870870
assert thumb == mock_content_response.content
871+
872+
873+
def test_get_thumbnail_representation_not_found(
874+
test_file,
875+
mock_box_session,
876+
mock_content_response,
877+
):
878+
representation_url = '{0}/files/{1}'.format(API.BASE_API_URL, test_file.object_id)
879+
dimensions = '100x100'
880+
extension = 'jpg'
881+
882+
mock_representations_response = Mock()
883+
mock_representations_response.json.return_value = {
884+
'etag': '1',
885+
'id': test_file.object_id,
886+
'representations': {
887+
'entries': [],
888+
},
889+
'type': 'file'
890+
}
891+
892+
mock_box_session.get.side_effect = [mock_representations_response, mock_content_response]
893+
894+
thumb = test_file.get_thumbnail_representation(
895+
dimensions=dimensions,
896+
extension=extension,
897+
)
898+
899+
mock_box_session.get.assert_any_call(
900+
representation_url,
901+
headers={'X-Rep-Hints': '[{}?dimensions={}]'.format(extension, dimensions)},
902+
params={'fields': 'representations'},
903+
)
904+
assert b'' == thumb

0 commit comments

Comments
 (0)