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

[3.2.x] Fix #891 by adding warnings for missing text parameter #910

Merged
merged 2 commits into from
Jan 8, 2021
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
10 changes: 9 additions & 1 deletion slack_sdk/web/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
import slack_sdk.errors as e
from slack_sdk.models.views import View
from .async_base_client import AsyncBaseClient, AsyncSlackResponse
from .internal_utils import _parse_web_class_objects, _update_call_participants
from .internal_utils import (
_parse_web_class_objects,
_update_call_participants,
_warn_if_text_is_missing,
)


class AsyncWebClient(AsyncBaseClient):
Expand Down Expand Up @@ -1103,6 +1107,7 @@ async def chat_postEphemeral(
"""
kwargs.update({"channel": channel, "user": user})
_parse_web_class_objects(kwargs)
_warn_if_text_is_missing("chat.postEphemeral", kwargs)
return await self.api_call("chat.postEphemeral", json=kwargs)

async def chat_postMessage(self, *, channel: str, **kwargs) -> AsyncSlackResponse:
Expand All @@ -1118,6 +1123,7 @@ async def chat_postMessage(self, *, channel: str, **kwargs) -> AsyncSlackRespons
"""
kwargs.update({"channel": channel})
_parse_web_class_objects(kwargs)
_warn_if_text_is_missing("chat.postMessage", kwargs)
return await self.api_call("chat.postMessage", json=kwargs)

async def chat_scheduleMessage(
Expand All @@ -1132,6 +1138,7 @@ async def chat_scheduleMessage(
"""
kwargs.update({"channel": channel, "post_at": post_at, "text": text})
_parse_web_class_objects(kwargs)
_warn_if_text_is_missing("chat.scheduleMessage", kwargs)
return await self.api_call("chat.scheduleMessage", json=kwargs)

async def chat_unfurl(
Expand Down Expand Up @@ -1164,6 +1171,7 @@ async def chat_update(
"""
kwargs.update({"channel": channel, "ts": ts})
_parse_web_class_objects(kwargs)
_warn_if_text_is_missing("chat.update", kwargs)
return await self.api_call("chat.update", json=kwargs)

async def chat_scheduledMessages_list(self, **kwargs) -> AsyncSlackResponse:
Expand Down
10 changes: 9 additions & 1 deletion slack_sdk/web/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import slack_sdk.errors as e
from slack_sdk.models.views import View
from .base_client import BaseClient, SlackResponse
from .internal_utils import _parse_web_class_objects, _update_call_participants
from .internal_utils import (
_parse_web_class_objects,
_update_call_participants,
_warn_if_text_is_missing,
)


class WebClient(BaseClient):
Expand Down Expand Up @@ -1042,6 +1046,7 @@ def chat_postEphemeral(self, *, channel: str, user: str, **kwargs) -> SlackRespo
"""
kwargs.update({"channel": channel, "user": user})
_parse_web_class_objects(kwargs)
_warn_if_text_is_missing("chat.postEphemeral", kwargs)
return self.api_call("chat.postEphemeral", json=kwargs)

def chat_postMessage(self, *, channel: str, **kwargs) -> SlackResponse:
Expand All @@ -1057,6 +1062,7 @@ def chat_postMessage(self, *, channel: str, **kwargs) -> SlackResponse:
"""
kwargs.update({"channel": channel})
_parse_web_class_objects(kwargs)
_warn_if_text_is_missing("chat.postMessage", kwargs)
return self.api_call("chat.postMessage", json=kwargs)

def chat_scheduleMessage(
Expand All @@ -1071,6 +1077,7 @@ def chat_scheduleMessage(
"""
kwargs.update({"channel": channel, "post_at": post_at, "text": text})
_parse_web_class_objects(kwargs)
_warn_if_text_is_missing("chat.scheduleMessage", kwargs)
return self.api_call("chat.scheduleMessage", json=kwargs)

def chat_unfurl(
Expand Down Expand Up @@ -1101,6 +1108,7 @@ def chat_update(self, *, channel: str, ts: str, **kwargs) -> SlackResponse:
"""
kwargs.update({"channel": channel, "ts": ts})
_parse_web_class_objects(kwargs)
_warn_if_text_is_missing("chat.update", kwargs)
return self.api_call("chat.update", json=kwargs)

def chat_scheduledMessages_list(self, **kwargs) -> SlackResponse:
Expand Down
18 changes: 18 additions & 0 deletions slack_sdk/web/internal_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import os
import platform
import sys
import warnings
from ssl import SSLContext
from typing import Dict, Union, Optional, Any, Sequence
from urllib.parse import urljoin
Expand Down Expand Up @@ -226,3 +228,19 @@ def _to_0_or_1_if_bool(v: Any) -> Union[Any, str]:
if isinstance(v, bool):
return "1" if v else "0"
return v


def _warn_if_text_is_missing(endpoint: str, kwargs: Dict[str, Any]) -> None:
text = kwargs.get("text")
if text is None or len(text.strip()) == 0:
message = (
f"The `text` argument is missing in the request payload for a {endpoint} call - "
"It's a best practice to always provide a text argument when posting a message. "
"The `text` is used in places where `blocks` cannot be rendered such as: "
"system push notifications, assistive technology such as screen readers, etc."
)
# for unit tests etc.
skip_deprecation = os.environ.get("SKIP_SLACK_SDK_WARNING")
if skip_deprecation:
return
warnings.warn(message, UserWarning)
10 changes: 9 additions & 1 deletion slack_sdk/web/legacy_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
import slack_sdk.errors as e
from slack_sdk.models.views import View
from .legacy_base_client import LegacyBaseClient, SlackResponse
from .internal_utils import _parse_web_class_objects, _update_call_participants
from .internal_utils import (
_parse_web_class_objects,
_update_call_participants,
_warn_if_text_is_missing,
)


class LegacyWebClient(LegacyBaseClient):
Expand Down Expand Up @@ -1099,6 +1103,7 @@ def chat_postEphemeral(
"""
kwargs.update({"channel": channel, "user": user})
_parse_web_class_objects(kwargs)
_warn_if_text_is_missing("chat.postEphemeral", kwargs)
return self.api_call("chat.postEphemeral", json=kwargs)

def chat_postMessage(
Expand All @@ -1116,6 +1121,7 @@ def chat_postMessage(
"""
kwargs.update({"channel": channel})
_parse_web_class_objects(kwargs)
_warn_if_text_is_missing("chat.postMessage", kwargs)
return self.api_call("chat.postMessage", json=kwargs)

def chat_scheduleMessage(
Expand All @@ -1130,6 +1136,7 @@ def chat_scheduleMessage(
"""
kwargs.update({"channel": channel, "post_at": post_at, "text": text})
_parse_web_class_objects(kwargs)
_warn_if_text_is_missing("chat.scheduleMessage", kwargs)
return self.api_call("chat.scheduleMessage", json=kwargs)

def chat_unfurl(
Expand Down Expand Up @@ -1162,6 +1169,7 @@ def chat_update(
"""
kwargs.update({"channel": channel, "ts": ts})
_parse_web_class_objects(kwargs)
_warn_if_text_is_missing("chat.update", kwargs)
return self.api_call("chat.update", json=kwargs)

def chat_scheduledMessages_list(self, **kwargs) -> Union[Future, SlackResponse]:
Expand Down
37 changes: 37 additions & 0 deletions tests/slack_sdk/web/test_web_client_issue_891.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import unittest

from slack_sdk import WebClient
from tests.web.mock_web_api_server import (
setup_mock_web_api_server,
cleanup_mock_web_api_server,
)


class TestWebClient_Issue_891(unittest.TestCase):
def setUp(self):
setup_mock_web_api_server(self)

def tearDown(self):
cleanup_mock_web_api_server(self)

def test_missing_text_warnings_chat_postMessage(self):
client = WebClient(base_url="http://localhost:8888", token="xoxb-api_test")
resp = client.chat_postMessage(channel="C111", blocks=[])
self.assertIsNone(resp["error"])

def test_missing_text_warnings_chat_postEphemeral(self):
client = WebClient(base_url="http://localhost:8888", token="xoxb-api_test")
resp = client.chat_postEphemeral(channel="C111", user="U111", blocks=[])
self.assertIsNone(resp["error"])

def test_missing_text_warnings_chat_scheduleMessage(self):
client = WebClient(base_url="http://localhost:8888", token="xoxb-api_test")
resp = client.chat_scheduleMessage(
channel="C111", post_at="299876400", text="", blocks=[]
)
self.assertIsNone(resp["error"])

def test_missing_text_warnings_chat_update(self):
client = WebClient(base_url="http://localhost:8888", token="xoxb-api_test")
resp = client.chat_update(channel="C111", ts="111.222", blocks=[])
self.assertIsNone(resp["error"])
42 changes: 42 additions & 0 deletions tests/slack_sdk_async/web/test_web_client_issue_891.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import unittest

from slack_sdk.web.async_client import AsyncWebClient
from tests.helpers import async_test
from tests.slack_sdk.web.mock_web_api_server import (
setup_mock_web_api_server,
cleanup_mock_web_api_server,
)


class TestWebClient_Issue_829(unittest.TestCase):
def setUp(self):
setup_mock_web_api_server(self)

def tearDown(self):
cleanup_mock_web_api_server(self)

@async_test
async def test_missing_text_warnings_chat_postMessage(self):
client = AsyncWebClient(base_url="http://localhost:8888", token="xoxb-api_test")
resp = await client.chat_postMessage(channel="C111", blocks=[])
self.assertIsNone(resp["error"])

@async_test
async def test_missing_text_warnings_chat_postEphemeral(self):
client = AsyncWebClient(base_url="http://localhost:8888", token="xoxb-api_test")
resp = await client.chat_postEphemeral(channel="C111", user="U111", blocks=[])
self.assertIsNone(resp["error"])

@async_test
async def test_missing_text_warnings_chat_scheduleMessage(self):
client = AsyncWebClient(base_url="http://localhost:8888", token="xoxb-api_test")
resp = await client.chat_scheduleMessage(
channel="C111", post_at="299876400", text="", blocks=[]
)
self.assertIsNone(resp["error"])

@async_test
async def test_missing_text_warnings_chat_update(self):
client = AsyncWebClient(base_url="http://localhost:8888", token="xoxb-api_test")
resp = await client.chat_update(channel="C111", ts="111.222", blocks=[])
self.assertIsNone(resp["error"])
37 changes: 37 additions & 0 deletions tests/web/test_web_client_issue_891.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import unittest

from slack import WebClient
from tests.web.mock_web_api_server import (
setup_mock_web_api_server,
cleanup_mock_web_api_server,
)


class TestWebClient_Issue_891(unittest.TestCase):
def setUp(self):
setup_mock_web_api_server(self)

def tearDown(self):
cleanup_mock_web_api_server(self)

def test_missing_text_warnings_chat_postMessage(self):
client = WebClient(base_url="http://localhost:8888", token="xoxb-api_test")
resp = client.chat_postMessage(channel="C111", blocks=[])
self.assertIsNone(resp["error"])

def test_missing_text_warnings_chat_postEphemeral(self):
client = WebClient(base_url="http://localhost:8888", token="xoxb-api_test")
resp = client.chat_postEphemeral(channel="C111", user="U111", blocks=[])
self.assertIsNone(resp["error"])

def test_missing_text_warnings_chat_scheduleMessage(self):
client = WebClient(base_url="http://localhost:8888", token="xoxb-api_test")
resp = client.chat_scheduleMessage(
channel="C111", post_at="299876400", text="", blocks=[]
)
self.assertIsNone(resp["error"])

def test_missing_text_warnings_chat_update(self):
client = WebClient(base_url="http://localhost:8888", token="xoxb-api_test")
resp = client.chat_update(channel="C111", ts="111.222", blocks=[])
self.assertIsNone(resp["error"])