diff --git a/boxsdk/object/file.py b/boxsdk/object/file.py index 821f89764..341f30f89 100644 --- a/boxsdk/object/file.py +++ b/boxsdk/object/file.py @@ -771,7 +771,7 @@ def get_thumbnail_representation(self, dimensions, extension='png'): Retrieve a thumbnail image for the file. :param dimensions: - The width by height size of this representation in pixels (i.e. '92x92') + The width by height size of this representation in pixels (e.g. '92x92') :type dimensions: `unicode` :param extension: @@ -785,10 +785,12 @@ def get_thumbnail_representation(self, dimensions, extension='png'): """ rep_hints = '[{0}?dimensions={1}]'.format(extension, dimensions) representation = self.get_representation_info(rep_hints) - url = representation[0]['content']['url_template'] - url = url.replace('{+asset_path}', '') - response = self._session.get(url, expect_json_response=False) - return response.content + if len(representation): + url = representation[0]['content']['url_template'] + url = url.replace('{+asset_path}', '') + response = self._session.get(url, expect_json_response=False) + return response.content + return b'' @api_call def copy(self, parent_folder, name=None, file_version=None): diff --git a/test/unit/object/test_file.py b/test/unit/object/test_file.py index 767d71e83..03a4d65ce 100644 --- a/test/unit/object/test_file.py +++ b/test/unit/object/test_file.py @@ -868,3 +868,37 @@ def test_get_thumbnail_representation( params={'fields': 'representations'}) mock_box_session.get.assert_any_call(content_url, expect_json_response=False) assert thumb == mock_content_response.content + + +def test_get_thumbnail_representation_not_found( + test_file, + mock_box_session, + mock_content_response, +): + representation_url = '{0}/files/{1}'.format(API.BASE_API_URL, test_file.object_id) + dimensions = '100x100' + extension = 'jpg' + + mock_representations_response = Mock() + mock_representations_response.json.return_value = { + 'etag': '1', + 'id': test_file.object_id, + 'representations': { + 'entries': [], + }, + 'type': 'file' + } + + mock_box_session.get.side_effect = [mock_representations_response, mock_content_response] + + thumb = test_file.get_thumbnail_representation( + dimensions=dimensions, + extension=extension, + ) + + mock_box_session.get.assert_any_call( + representation_url, + headers={'X-Rep-Hints': '[{}?dimensions={}]'.format(extension, dimensions)}, + params={'fields': 'representations'}, + ) + assert b'' == thumb