Skip to content

Commit ebb508d

Browse files
authored
Fix #770 by adding bytes type param support (#771)
1 parent 936f21a commit ebb508d

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
import unittest
3+
from io import BytesIO
4+
5+
from integration_tests.env_variable_names import \
6+
SLACK_SDK_TEST_BOT_TOKEN, \
7+
SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID
8+
from integration_tests.helpers import async_test
9+
from slack import WebClient
10+
11+
12+
class TestWebClient(unittest.TestCase):
13+
"""Runs integration tests with real Slack API
14+
15+
export SLACK_SDK_TEST_BOT_TOKEN=xoxb-xxx
16+
export SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID=C111
17+
python setup.py run_integration_tests --test-target integration_tests/web/test_issue_770.py
18+
19+
https://github.com/slackapi/python-slackclient/issues/770
20+
"""
21+
22+
def setUp(self):
23+
self.bot_token = os.environ[SLACK_SDK_TEST_BOT_TOKEN]
24+
self.channel_ids = ",".join([os.environ[SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID]])
25+
26+
def tearDown(self):
27+
pass
28+
29+
def test_bytes_for_file_param_bytes(self):
30+
client: WebClient = WebClient(token=self.bot_token, run_async=False)
31+
bytes = BytesIO(bytearray("This is a test (bytes)", "utf-8")).getvalue()
32+
upload = client.files_upload(file=bytes, filename="test.txt", channels=self.channel_ids)
33+
self.assertIsNotNone(upload)
34+
deletion = client.files_delete(file=upload["file"]["id"])
35+
self.assertIsNotNone(deletion)
36+
37+
@async_test
38+
async def test_bytes_for_file_param_bytes_async(self):
39+
client: WebClient = WebClient(token=self.bot_token, run_async=True)
40+
bytes = BytesIO(bytearray("This is a test (bytes)", "utf-8")).getvalue()
41+
upload = await client.files_upload(file=bytes, filename="test.txt", channels=self.channel_ids)
42+
self.assertIsNotNone(upload)
43+
deletion = await client.files_delete(file=upload["file"]["id"])
44+
self.assertIsNotNone(deletion)

slack/web/base_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def convert_params(values: dict) -> dict:
319319
f: BinaryIO = open(v.encode("utf-8", "ignore"), "rb")
320320
files_to_close.append(f)
321321
request_data.update({k: f})
322-
elif isinstance(v, bytearray):
322+
elif isinstance(v, (bytearray, bytes)):
323323
request_data.update({k: io.BytesIO(v)})
324324
else:
325325
request_data.update({k: v})

0 commit comments

Comments
 (0)