Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files.upload v2 support #1272

Merged
merged 11 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs-src-v2/basic_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,9 @@ Uploading files

.. code-block:: python

response = client.files_upload(
channels="C3UKJTQAC",
file="files.pdf",
response = client.files_upload_v2(
Copy link
Member Author

Choose a reason for hiding this comment

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

I haven't yet generated the page. We will update the live site once we release a new version including the change.

channel="C3UKJTQAC",
file="./files.pdf",
title="Test upload"
)

Expand Down
192 changes: 192 additions & 0 deletions integration_tests/web/test_files_upload_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import logging
import os
import unittest

import pytest

from integration_tests.env_variable_names import (
SLACK_SDK_TEST_BOT_TOKEN,
SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID,
)
from integration_tests.helpers import async_test
from slack_sdk.errors import SlackRequestError
from slack_sdk.web import WebClient
from slack_sdk.web.async_client import AsyncWebClient
from slack_sdk.web.legacy_client import LegacyWebClient


class TestWebClient_FilesUploads_V2(unittest.TestCase):
"""Runs integration tests with real Slack API"""

def setUp(self):
if not hasattr(self, "logger"):
self.logger = logging.getLogger(__name__)
self.bot_token = os.environ[SLACK_SDK_TEST_BOT_TOKEN]
self.async_client: AsyncWebClient = AsyncWebClient(token=self.bot_token)
self.sync_client: WebClient = WebClient(token=self.bot_token)
self.legacy_client: WebClient = LegacyWebClient(token=self.bot_token)
self.legacy_client_async: WebClient = LegacyWebClient(token=self.bot_token, run_async=True)
self.channel_id = os.environ[SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID]

def tearDown(self):
pass

# -------------------------
# file operations

def test_uploading_text_files(self):
client = self.sync_client
file = __file__
upload = client.files_upload_v2(
channels=self.channel_id,
file=file,
title="Test code",
)
self.assertIsNotNone(upload)

def test_uploading_text_files_legacy(self):
client = self.legacy_client
file = __file__
upload = client.files_upload_v2(
channels=self.channel_id,
file=file,
title="Test code",
)
self.assertIsNotNone(upload)

def test_uploading_multiple_files(self):
client = self.sync_client
file = __file__
upload = client.files_upload_v2(
file_uploads=[
{
"file": file,
"title": "Test code",
},
{
"content": "Hi there!",
"title": "Text data",
"filename": "hi-there.txt",
},
],
channel=self.channel_id,
initial_comment="Here are files :wave:",
)
self.assertIsNotNone(upload)

@async_test
async def test_uploading_text_files_async(self):
client = self.async_client
file, filename = __file__, os.path.basename(__file__)
upload = await client.files_upload_v2(
channels=self.channel_id,
title="Good Old Slack Logo",
filename=filename,
file=file,
)
self.assertIsNotNone(upload)

deletion = await client.files_delete(file=upload["file"]["id"])
self.assertIsNotNone(deletion)

@async_test
async def test_uploading_text_files_legacy_async(self):
client = self.legacy_client_async
file, filename = __file__, os.path.basename(__file__)
try:
await client.files_upload_v2(
channels=self.channel_id,
title="Good Old Slack Logo",
filename=filename,
file=file,
)
pytest.fail("Raising SlackRequestError is expected here")
except SlackRequestError:
pass

def test_uploading_binary_files(self):
client = self.sync_client
current_dir = os.path.dirname(__file__)
file = f"{current_dir}/../../tests/data/slack_logo.png"
upload = client.files_upload_v2(
channels=self.channel_id,
title="Good Old Slack Logo",
filename="slack_logo.png",
file=file,
)
self.assertIsNotNone(upload)

deletion = client.files_delete(file=upload["file"]["id"])
self.assertIsNotNone(deletion)

def test_uploading_binary_files_as_content(self):
client = self.sync_client
current_dir = os.path.dirname(__file__)
file = f"{current_dir}/../../tests/data/slack_logo.png"
with open(file, "rb") as f:
content = f.read()
upload = client.files_upload_v2(
channels=self.channel_id,
title="Good Old Slack Logo",
filename="slack_logo.png",
content=content,
)
self.assertIsNotNone(upload)

deletion = client.files_delete(file=upload["file"]["id"])
self.assertIsNotNone(deletion)

@async_test
async def test_uploading_binary_files_async(self):
client = self.async_client
current_dir = os.path.dirname(__file__)
file = f"{current_dir}/../../tests/data/slack_logo.png"
upload = await client.files_upload_v2(
channels=self.channel_id,
title="Good Old Slack Logo",
filename="slack_logo.png",
file=file,
)
self.assertIsNotNone(upload)

deletion = await client.files_delete(file=upload["file"]["id"])
self.assertIsNotNone(deletion)

def test_uploading_file_with_token_param(self):
client = WebClient()
current_dir = os.path.dirname(__file__)
file = f"{current_dir}/../../tests/data/slack_logo.png"
upload = client.files_upload_v2(
token=self.bot_token,
channels=self.channel_id,
title="Good Old Slack Logo",
filename="slack_logo.png",
file=file,
)
self.assertIsNotNone(upload)

deletion = client.files_delete(
token=self.bot_token,
file=upload["file"]["id"],
)
self.assertIsNotNone(deletion)

@async_test
async def test_uploading_file_with_token_param_async(self):
client = AsyncWebClient()
current_dir = os.path.dirname(__file__)
file = f"{current_dir}/../../tests/data/slack_logo.png"
upload = await client.files_upload_v2(
token=self.bot_token,
channels=self.channel_id,
title="Good Old Slack Logo",
filename="slack_logo.png",
file=file,
)
self.assertIsNotNone(upload)

deletion = await client.files_delete(
token=self.bot_token,
file=upload["file"]["id"],
)
self.assertIsNotNone(deletion)
4 changes: 2 additions & 2 deletions integration_tests/web/test_web_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ async def test_uploading_binary_files_async(self):
)
self.assertIsNotNone(upload)

deletion = client.files_delete(file=upload["file"]["id"])
deletion = await client.files_delete(file=upload["file"]["id"])
Copy link
Member Author

Choose a reason for hiding this comment

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

Just improvements to existing test code

self.assertIsNotNone(deletion)

def test_uploading_file_with_token_param(self):
Expand Down Expand Up @@ -278,7 +278,7 @@ async def test_uploading_file_with_token_param_async(self):
)
self.assertIsNotNone(upload)

deletion = client.files_delete(
deletion = await client.files_delete(
token=self.bot_token,
file=upload["file"]["id"],
)
Expand Down
25 changes: 25 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,31 @@ def run(self):
async_source,
)
async_source = re.sub("= WebClient\(", "= AsyncWebClient(", async_source)
async_source = re.sub(
" self.files_getUploadURLExternal\(",
" await self.files_getUploadURLExternal(",
async_source,
)
async_source = re.sub(
" self.files_completeUploadExternal\(",
" await self.files_completeUploadExternal(",
async_source,
)
async_source = re.sub(
" self.files_info\(",
" await self.files_info(",
async_source,
)
async_source = re.sub(
"_attach_full_file_metadata",
"_attach_full_file_metadata_async",
async_source,
)
async_source = re.sub(
" _attach_full_file_metadata_async\(",
" await _attach_full_file_metadata_async(",
async_source,
)
with open(f"{here}/slack_sdk/web/async_client.py", "w") as output:
output.write(async_source)

Expand Down
Loading