Skip to content
Merged
22 changes: 2 additions & 20 deletions shotgun_api3/shotgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -2559,30 +2559,12 @@ def _upload_to_sg(self, entity_type, entity_id, path, field_name, display_name,

params.update(self._auth_params())

# If we ended up with a unicode string path, we need to encode it
# as a utf-8 string. If we don't, there's a chance that there will
# will be an attempt later on to encode it as an ascii string, and
# that will fail ungracefully if the path contains any non-ascii
# characters.
#
# On Windows, if the path contains non-ascii characters, the calls
# to open later in this method will fail to find the file if given
# a non-ascii-encoded string path. In that case, we're going to have
# to call open on the unicode path, but we'll use the encoded string
# for everything else.
path_to_open = path
if isinstance(path, six.text_type):
path = path.encode("utf-8")
if sys.platform != "win32":
path_to_open = path

if is_thumbnail:
url = urllib.parse.urlunparse((self.config.scheme, self.config.server,
"/upload/publish_thumbnail", None, None, None))
params["thumb_image"] = open(path_to_open, "rb")
params["thumb_image"] = open(path, "rb")
if field_name == "filmstrip_thumb_image" or field_name == "filmstrip_image":
params["filmstrip"] = True

else:
url = urllib.parse.urlunparse((self.config.scheme, self.config.server,
"/upload/upload_file", None, None, None))
Expand All @@ -2596,7 +2578,7 @@ def _upload_to_sg(self, entity_type, entity_id, path, field_name, display_name,
if tag_list:
params["tag_list"] = tag_list

params["file"] = open(path_to_open, "rb")
params["file"] = open(path, "rb")

result = self._send_form(url, params)

Expand Down
File renamed without changes
63 changes: 62 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def test_upload_download(self):
# test upload of non-ascii, unicode path
u_path = os.path.abspath(
os.path.expanduser(
glob.glob(os.path.join(six.text_type(this_dir), u'No*l.jpg'))[0]
glob.glob(os.path.join(six.text_type(this_dir), "Noëlご.jpg"))[0]
)
)

Expand Down Expand Up @@ -310,6 +310,67 @@ def test_upload_download(self):
# cleanup
os.remove(file_path)

@patch('shotgun_api3.Shotgun._send_form')
def test_upload_to_sg(self, mock_send_form):
"""
Upload an attachment tests for _upload_to_sg()
"""
if "localhost" in self.server_url:
self.skipTest("upload / down tests skipped for localhost")

self.sg.server_info["s3_direct_uploads_enabled"] = False
mock_send_form.method.assert_called_once()
mock_send_form.return_value = "1\n:123\nasd"
this_dir, _ = os.path.split(__file__)
u_path = os.path.abspath(
os.path.expanduser(
glob.glob(os.path.join(six.text_type(this_dir), "Noëlご.jpg"))[0]
)
)
upload_id = self.sg.upload(
"Ticket",
self.ticket['id'],
u_path,
'attachments',
tag_list="monkeys, everywhere, send, help"
)
mock_send_form_args, _ = mock_send_form.call_args
display_name_to_send = mock_send_form_args[1].get("display_name", "")
self.assertTrue(isinstance(upload_id, int))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have access to mock_send_form.call_args? I'm wondering if we can inspect if params["thumb_image"] and params["file"] values when calling _send_form are correct without unwanted prefix and suffix.

self.assertFalse(
display_name_to_send.startswith("b'") and
display_name_to_send.endswith("'")
)

upload_id = self.sg.upload(
"Ticket",
self.ticket['id'],
u_path,
'filmstrip_image',
tag_list="monkeys, everywhere, send, help",
)
self.assertTrue(isinstance(upload_id, int))
mock_send_form_args, _ = mock_send_form.call_args
display_name_to_send = mock_send_form_args[1].get("display_name", "")
self.assertTrue(isinstance(upload_id, int))
self.assertFalse(
display_name_to_send.startswith("b'") and
display_name_to_send.endswith("'")
)

mock_send_form.method.assert_called_once()
mock_send_form.return_value = "2\nIt can't be upload"
self.assertRaises(
shotgun_api3.ShotgunError,
self.sg.upload,
"Ticket",
self.ticket['id'],
u_path,
'attachments',
tag_list="monkeys, everywhere, send, help"
)
self.sg.server_info["s3_direct_uploads_enabled"] = True

def test_upload_thumbnail_in_create(self):
"""Upload a thumbnail via the create method"""
this_dir, _ = os.path.split(__file__)
Expand Down