-
Notifications
You must be signed in to change notification settings - Fork 833
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2e7ebaa
Add files.upload v2 support
seratch 11ffaa2
Fix type hint errors
seratch a8448f0
Fix a bug detected by pytype!
seratch 1e64791
Fix for legacy client
seratch f7dcca0
Fix for legacy client
seratch 0247d64
Fix for pytype validation
seratch a30f930
Update the internals for deprecated parameters
seratch 0ee6267
Fix a bug
seratch 2588655
Rename upload_files to file_uploads
seratch 7d11ad0
Rename internal method
seratch 1d0eed1
Add warning log
seratch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
@@ -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"], | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.