Skip to content

Commit

Permalink
Fix #1356 files_upload_v2 filename required even though content provi…
Browse files Browse the repository at this point in the history
…ded (#1361)
  • Loading branch information
seratch authored Apr 28, 2023
1 parent f37c691 commit ff743e2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
4 changes: 2 additions & 2 deletions slack_sdk/web/internal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ def _to_v2_file_upload_item(upload_file: Dict[str, Any]) -> Dict[str, Optional[A
raise SlackRequestError("content for file upload must be 'str' (UTF-8 encoded) or 'bytes' (for data)")

filename = upload_file.get("filename")
if upload_file.get("filename") is None and isinstance(file, str):
if filename is None:
# use the local filename if filename is missing
if upload_file.get("filename") is None:
if isinstance(file, str):
filename = file.split(os.path.sep)[-1]
else:
filename = "Uploaded file"
Expand Down
28 changes: 27 additions & 1 deletion tests/slack_sdk/web/test_internal_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import json
import unittest
from io import BytesIO
from typing import Dict, Sequence, Union

import pytest

from slack_sdk.models.attachments import Attachment
from slack_sdk.models.blocks import Block, DividerBlock
from slack_sdk.web.internal_utils import _build_unexpected_body_error_message, _parse_web_class_objects
from slack_sdk.web.internal_utils import (
_build_unexpected_body_error_message,
_parse_web_class_objects,
_to_v2_file_upload_item,
)


class TestInternalUtils(unittest.TestCase):
Expand Down Expand Up @@ -72,3 +77,24 @@ def test_can_parse_user_auth_blocks(self):
}
_parse_web_class_objects(kwargs)
assert isinstance(kwargs["user_auth_blocks"][0], dict)

def test_files_upload_v2_issue_1356(self):
content_item = _to_v2_file_upload_item({"content": "test"})
assert content_item.get("filename") == "Uploaded file"

filepath_item = _to_v2_file_upload_item({"file": "tests/slack_sdk/web/test_internal_utils.py"})
assert filepath_item.get("filename") == "test_internal_utils.py"
filepath_item = _to_v2_file_upload_item({"file": "tests/slack_sdk/web/test_internal_utils.py", "filename": "foo.py"})
assert filepath_item.get("filename") == "foo.py"

file_bytes = "This is a test!".encode("utf-8")
file_bytes_item = _to_v2_file_upload_item({"file": file_bytes})
assert file_bytes_item.get("filename") == "Uploaded file"
file_bytes_item = _to_v2_file_upload_item({"file": file_bytes, "filename": "foo.txt"})
assert file_bytes_item.get("filename") == "foo.txt"

file_io = BytesIO(file_bytes)
file_io_item = _to_v2_file_upload_item({"file": file_io})
assert file_io_item.get("filename") == "Uploaded file"
file_io_item = _to_v2_file_upload_item({"file": file_io, "filename": "foo.txt"})
assert file_io_item.get("filename") == "foo.txt"

0 comments on commit ff743e2

Please sign in to comment.