Skip to content

Commit 39775bb

Browse files
authored
refactor(web): build the file size error from MAX_V2_FILE_SIZE (#2050)
1 parent 1a0c75c commit 39775bb

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

‎slack_sdk/web/internal_utils.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ def _to_v2_file_upload_item(upload_file: Dict[str, Any]) -> Dict[str, Optional[A
345345
if file_size > MAX_V2_FILE_SIZE:
346346
filename = upload_file.get("filename") or os.path.basename(os.fspath(file))
347347
raise SlackRequestError(
348-
f"Maximum upload file size is 1GB (file size: {file_size} bytes for filename: {filename})"
348+
f"Maximum upload file size is {MAX_V2_FILE_SIZE} bytes "
349+
f"(file size: {file_size} bytes for filename: {filename})"
349350
)
350351
with open(os.fsencode(file), "rb") as readable:
351352
data = readable.read()
@@ -378,7 +379,9 @@ def _to_v2_file_upload_item(upload_file: Dict[str, Any]) -> Dict[str, Optional[A
378379
raise SlackRequestError(f"File content not found for filename: {filename}, title: {title}")
379380

380381
if len(data) > MAX_V2_FILE_SIZE:
381-
raise SlackRequestError(f"Maximum upload file size is 1GB (file size: {len(data)} bytes for filename: {filename})")
382+
raise SlackRequestError(
383+
f"Maximum upload file size is {MAX_V2_FILE_SIZE} bytes (file size: {len(data)} bytes for filename: {filename})"
384+
)
382385

383386
if title is None:
384387
title = filename # to be consistent with files.upload API

‎tests/slack_sdk/web/test_internal_utils.py‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,26 @@ def test_to_v2_file_upload_item_can_accept_file_as_path(self):
150150
assert upload_item_path == upload_item_str
151151
assert upload_item_str.get("filename") == "test_internal_utils.py"
152152

153-
def test_to_v2_file_upload_item_raises_when_file_path_exceeds_1gb(self):
154-
with patch("os.path.getsize", return_value=1_073_741_825):
153+
def test_to_v2_file_upload_item_raises_when_file_path_exceeds_max_size(self):
154+
with patch("slack_sdk.web.internal_utils.MAX_V2_FILE_SIZE", 10), patch("os.path.getsize", return_value=11):
155155
with self.assertRaises(SlackRequestError) as ctx:
156156
_to_v2_file_upload_item({"file": "tests/slack_sdk/web/test_internal_utils.py"})
157-
self.assertIn("Maximum upload file size is 1GB", str(ctx.exception))
158-
self.assertIn("1073741825 bytes", str(ctx.exception))
157+
self.assertIn("Maximum upload file size is 10 bytes", str(ctx.exception))
158+
self.assertIn("file size: 11 bytes", str(ctx.exception))
159159

160-
def test_to_v2_file_upload_item_raises_when_content_exceeds_1gb(self):
160+
def test_to_v2_file_upload_item_raises_when_content_exceeds_max_size(self):
161161
with patch("slack_sdk.web.internal_utils.MAX_V2_FILE_SIZE", 10):
162162
with self.assertRaises(SlackRequestError) as ctx:
163-
_to_v2_file_upload_item({"content": "this content is longer than 10 bytes", "filename": "test.txt"})
164-
self.assertIn("Maximum upload file size is 1GB", str(ctx.exception))
163+
_to_v2_file_upload_item({"content": "x" * 11, "filename": "test.txt"})
164+
self.assertIn("Maximum upload file size is 10 bytes", str(ctx.exception))
165+
self.assertIn("file size: 11 bytes", str(ctx.exception))
165166
self.assertIn("test.txt", str(ctx.exception))
166167

168+
def test_to_v2_file_upload_item_accepts_content_at_max_size(self):
169+
with patch("slack_sdk.web.internal_utils.MAX_V2_FILE_SIZE", 10):
170+
item = _to_v2_file_upload_item({"content": "x" * 10, "filename": "test.txt"})
171+
self.assertEqual(item["length"], 10)
172+
167173
def test_next_cursor_is_present(self):
168174
assert _next_cursor_is_present({"next_cursor": "next-page"}) is True
169175
assert _next_cursor_is_present({"next_cursor": ""}) is False

0 commit comments

Comments
 (0)