|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import unittest |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from integration_tests.env_variable_names import ( |
| 8 | + SLACK_SDK_TEST_BOT_TOKEN, |
| 9 | + SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID, |
| 10 | +) |
| 11 | +from integration_tests.helpers import async_test |
| 12 | +from slack_sdk.errors import SlackRequestError |
| 13 | +from slack_sdk.web import WebClient |
| 14 | +from slack_sdk.web.async_client import AsyncWebClient |
| 15 | +from slack_sdk.web.legacy_client import LegacyWebClient |
| 16 | + |
| 17 | + |
| 18 | +class TestWebClient_FilesUploads_V2(unittest.TestCase): |
| 19 | + """Runs integration tests with real Slack API""" |
| 20 | + |
| 21 | + def setUp(self): |
| 22 | + if not hasattr(self, "logger"): |
| 23 | + self.logger = logging.getLogger(__name__) |
| 24 | + self.bot_token = os.environ[SLACK_SDK_TEST_BOT_TOKEN] |
| 25 | + self.async_client: AsyncWebClient = AsyncWebClient(token=self.bot_token) |
| 26 | + self.sync_client: WebClient = WebClient(token=self.bot_token) |
| 27 | + self.legacy_client: WebClient = LegacyWebClient(token=self.bot_token) |
| 28 | + self.legacy_client_async: WebClient = LegacyWebClient(token=self.bot_token, run_async=True) |
| 29 | + self.channel_id = os.environ[SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID] |
| 30 | + |
| 31 | + def tearDown(self): |
| 32 | + pass |
| 33 | + |
| 34 | + # ------------------------- |
| 35 | + # file operations |
| 36 | + |
| 37 | + def test_uploading_text_files(self): |
| 38 | + client = self.sync_client |
| 39 | + file = __file__ |
| 40 | + upload = client.files_upload_v2( |
| 41 | + channels=self.channel_id, |
| 42 | + file=file, |
| 43 | + title="Test code", |
| 44 | + ) |
| 45 | + self.assertIsNotNone(upload) |
| 46 | + |
| 47 | + def test_uploading_text_files_legacy(self): |
| 48 | + client = self.legacy_client |
| 49 | + file = __file__ |
| 50 | + upload = client.files_upload_v2( |
| 51 | + channels=self.channel_id, |
| 52 | + file=file, |
| 53 | + title="Test code", |
| 54 | + ) |
| 55 | + self.assertIsNotNone(upload) |
| 56 | + |
| 57 | + def test_uploading_multiple_files(self): |
| 58 | + client = self.sync_client |
| 59 | + file = __file__ |
| 60 | + upload = client.files_upload_v2( |
| 61 | + file_uploads=[ |
| 62 | + { |
| 63 | + "file": file, |
| 64 | + "title": "Test code", |
| 65 | + }, |
| 66 | + { |
| 67 | + "content": "Hi there!", |
| 68 | + "title": "Text data", |
| 69 | + "filename": "hi-there.txt", |
| 70 | + }, |
| 71 | + ], |
| 72 | + channel=self.channel_id, |
| 73 | + initial_comment="Here are files :wave:", |
| 74 | + ) |
| 75 | + self.assertIsNotNone(upload) |
| 76 | + |
| 77 | + @async_test |
| 78 | + async def test_uploading_text_files_async(self): |
| 79 | + client = self.async_client |
| 80 | + file, filename = __file__, os.path.basename(__file__) |
| 81 | + upload = await client.files_upload_v2( |
| 82 | + channels=self.channel_id, |
| 83 | + title="Good Old Slack Logo", |
| 84 | + filename=filename, |
| 85 | + file=file, |
| 86 | + ) |
| 87 | + self.assertIsNotNone(upload) |
| 88 | + |
| 89 | + deletion = await client.files_delete(file=upload["file"]["id"]) |
| 90 | + self.assertIsNotNone(deletion) |
| 91 | + |
| 92 | + @async_test |
| 93 | + async def test_uploading_text_files_legacy_async(self): |
| 94 | + client = self.legacy_client_async |
| 95 | + file, filename = __file__, os.path.basename(__file__) |
| 96 | + try: |
| 97 | + await client.files_upload_v2( |
| 98 | + channels=self.channel_id, |
| 99 | + title="Good Old Slack Logo", |
| 100 | + filename=filename, |
| 101 | + file=file, |
| 102 | + ) |
| 103 | + pytest.fail("Raising SlackRequestError is expected here") |
| 104 | + except SlackRequestError: |
| 105 | + pass |
| 106 | + |
| 107 | + def test_uploading_binary_files(self): |
| 108 | + client = self.sync_client |
| 109 | + current_dir = os.path.dirname(__file__) |
| 110 | + file = f"{current_dir}/../../tests/data/slack_logo.png" |
| 111 | + upload = client.files_upload_v2( |
| 112 | + channels=self.channel_id, |
| 113 | + title="Good Old Slack Logo", |
| 114 | + filename="slack_logo.png", |
| 115 | + file=file, |
| 116 | + ) |
| 117 | + self.assertIsNotNone(upload) |
| 118 | + |
| 119 | + deletion = client.files_delete(file=upload["file"]["id"]) |
| 120 | + self.assertIsNotNone(deletion) |
| 121 | + |
| 122 | + def test_uploading_binary_files_as_content(self): |
| 123 | + client = self.sync_client |
| 124 | + current_dir = os.path.dirname(__file__) |
| 125 | + file = f"{current_dir}/../../tests/data/slack_logo.png" |
| 126 | + with open(file, "rb") as f: |
| 127 | + content = f.read() |
| 128 | + upload = client.files_upload_v2( |
| 129 | + channels=self.channel_id, |
| 130 | + title="Good Old Slack Logo", |
| 131 | + filename="slack_logo.png", |
| 132 | + content=content, |
| 133 | + ) |
| 134 | + self.assertIsNotNone(upload) |
| 135 | + |
| 136 | + deletion = client.files_delete(file=upload["file"]["id"]) |
| 137 | + self.assertIsNotNone(deletion) |
| 138 | + |
| 139 | + @async_test |
| 140 | + async def test_uploading_binary_files_async(self): |
| 141 | + client = self.async_client |
| 142 | + current_dir = os.path.dirname(__file__) |
| 143 | + file = f"{current_dir}/../../tests/data/slack_logo.png" |
| 144 | + upload = await client.files_upload_v2( |
| 145 | + channels=self.channel_id, |
| 146 | + title="Good Old Slack Logo", |
| 147 | + filename="slack_logo.png", |
| 148 | + file=file, |
| 149 | + ) |
| 150 | + self.assertIsNotNone(upload) |
| 151 | + |
| 152 | + deletion = await client.files_delete(file=upload["file"]["id"]) |
| 153 | + self.assertIsNotNone(deletion) |
| 154 | + |
| 155 | + def test_uploading_file_with_token_param(self): |
| 156 | + client = WebClient() |
| 157 | + current_dir = os.path.dirname(__file__) |
| 158 | + file = f"{current_dir}/../../tests/data/slack_logo.png" |
| 159 | + upload = client.files_upload_v2( |
| 160 | + token=self.bot_token, |
| 161 | + channels=self.channel_id, |
| 162 | + title="Good Old Slack Logo", |
| 163 | + filename="slack_logo.png", |
| 164 | + file=file, |
| 165 | + ) |
| 166 | + self.assertIsNotNone(upload) |
| 167 | + |
| 168 | + deletion = client.files_delete( |
| 169 | + token=self.bot_token, |
| 170 | + file=upload["file"]["id"], |
| 171 | + ) |
| 172 | + self.assertIsNotNone(deletion) |
| 173 | + |
| 174 | + @async_test |
| 175 | + async def test_uploading_file_with_token_param_async(self): |
| 176 | + client = AsyncWebClient() |
| 177 | + current_dir = os.path.dirname(__file__) |
| 178 | + file = f"{current_dir}/../../tests/data/slack_logo.png" |
| 179 | + upload = await client.files_upload_v2( |
| 180 | + token=self.bot_token, |
| 181 | + channels=self.channel_id, |
| 182 | + title="Good Old Slack Logo", |
| 183 | + filename="slack_logo.png", |
| 184 | + file=file, |
| 185 | + ) |
| 186 | + self.assertIsNotNone(upload) |
| 187 | + |
| 188 | + deletion = await client.files_delete( |
| 189 | + token=self.bot_token, |
| 190 | + file=upload["file"]["id"], |
| 191 | + ) |
| 192 | + self.assertIsNotNone(deletion) |
0 commit comments