Skip to content

Commit

Permalink
Fix #1297 Building user_auth_blocks with slack_sdk.models class objec…
Browse files Browse the repository at this point in the history
…ts for chat.unfurl API call fails (#1301)
  • Loading branch information
seratch authored Nov 16, 2022
1 parent 3610592 commit 9e0fa3d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 10 deletions.
3 changes: 2 additions & 1 deletion slack_sdk/web/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2131,7 +2131,7 @@ async def chat_unfurl(
ts: Optional[str] = None,
source: Optional[str] = None,
unfurl_id: Optional[str] = None,
unfurls: Dict[str, Dict],
unfurls: Optional[Dict[str, Dict]] = None, # or user_auth_*
user_auth_blocks: Optional[Union[str, Sequence[Union[Dict, Block]]]] = None,
user_auth_message: Optional[str] = None,
user_auth_required: Optional[bool] = None,
Expand All @@ -2154,6 +2154,7 @@ async def chat_unfurl(
"user_auth_url": user_auth_url,
}
)
_parse_web_class_objects(kwargs) # for user_auth_blocks
kwargs = _remove_none_values(kwargs)
# NOTE: intentionally using json over params for API methods using blocks/attachments
return await self.api_call("chat.unfurl", json=kwargs)
Expand Down
3 changes: 2 additions & 1 deletion slack_sdk/web/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2122,7 +2122,7 @@ def chat_unfurl(
ts: Optional[str] = None,
source: Optional[str] = None,
unfurl_id: Optional[str] = None,
unfurls: Dict[str, Dict],
unfurls: Optional[Dict[str, Dict]] = None, # or user_auth_*
user_auth_blocks: Optional[Union[str, Sequence[Union[Dict, Block]]]] = None,
user_auth_message: Optional[str] = None,
user_auth_required: Optional[bool] = None,
Expand All @@ -2145,6 +2145,7 @@ def chat_unfurl(
"user_auth_url": user_auth_url,
}
)
_parse_web_class_objects(kwargs) # for user_auth_blocks
kwargs = _remove_none_values(kwargs)
# NOTE: intentionally using json over params for API methods using blocks/attachments
return self.api_call("chat.unfurl", json=kwargs)
Expand Down
9 changes: 5 additions & 4 deletions slack_sdk/web/internal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,11 @@ def to_dict(obj: Union[Dict, Block, Attachment, Metadata]):
return obj.to_dict()
return obj

blocks = kwargs.get("blocks", None)
if blocks is not None and isinstance(blocks, Sequence) and (not isinstance(blocks, str)):
dict_blocks = [to_dict(b) for b in blocks]
kwargs.update({"blocks": dict_blocks})
for blocks_name in ["blocks", "user_auth_blocks"]:
blocks = kwargs.get(blocks_name, None)
if blocks is not None and isinstance(blocks, Sequence) and (not isinstance(blocks, str)):
dict_blocks = [to_dict(b) for b in blocks]
kwargs.update({blocks_name: dict_blocks})

attachments = kwargs.get("attachments", None)
if attachments is not None and isinstance(attachments, Sequence) and (not isinstance(attachments, str)):
Expand Down
3 changes: 2 additions & 1 deletion slack_sdk/web/legacy_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2133,7 +2133,7 @@ def chat_unfurl(
ts: Optional[str] = None,
source: Optional[str] = None,
unfurl_id: Optional[str] = None,
unfurls: Dict[str, Dict],
unfurls: Optional[Dict[str, Dict]] = None, # or user_auth_*
user_auth_blocks: Optional[Union[str, Sequence[Union[Dict, Block]]]] = None,
user_auth_message: Optional[str] = None,
user_auth_required: Optional[bool] = None,
Expand All @@ -2156,6 +2156,7 @@ def chat_unfurl(
"user_auth_url": user_auth_url,
}
)
_parse_web_class_objects(kwargs) # for user_auth_blocks
kwargs = _remove_none_values(kwargs)
# NOTE: intentionally using json over params for API methods using blocks/attachments
return self.api_call("chat.unfurl", json=kwargs)
Expand Down
12 changes: 11 additions & 1 deletion tests/slack_sdk/web/test_internal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from slack_sdk.models.attachments import Attachment
from slack_sdk.models.blocks import Block
from slack_sdk.models.blocks import Block, DividerBlock
from slack_sdk.web.internal_utils import _build_unexpected_body_error_message, _parse_web_class_objects


Expand Down Expand Up @@ -62,3 +62,13 @@ def test_can_parse_str_attachments(self):
_parse_web_class_objects(kwargs)
assert isinstance(kwargs["attachments"], str)
assert input == kwargs["attachments"]

def test_can_parse_user_auth_blocks(self):
kwargs = {
"channel": "C12345",
"ts": "1111.2222",
"unfurls": {},
"user_auth_blocks": [DividerBlock(), DividerBlock()],
}
_parse_web_class_objects(kwargs)
assert isinstance(kwargs["user_auth_blocks"][0], dict)
11 changes: 11 additions & 0 deletions tests/slack_sdk/web/test_web_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import slack_sdk.errors as err
from slack_sdk import WebClient
from slack_sdk.models.blocks import DividerBlock
from slack_sdk.models.metadata import Metadata
from tests.slack_sdk.web.mock_web_api_server import (
setup_mock_web_api_server,
Expand Down Expand Up @@ -218,3 +219,13 @@ def test_message_metadata(self):
),
)
self.assertIsNone(scheduled.get("error"))

def test_user_auth_blocks(self):
client = self.client
new_message = client.chat_unfurl(
channel="C12345",
ts="1111.2222",
unfurls={},
user_auth_blocks=[DividerBlock(), DividerBlock()],
)
self.assertIsNone(new_message.get("error"))
15 changes: 13 additions & 2 deletions tests/slack_sdk_async/web/test_async_web_client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import re
import unittest

import aiohttp

import slack_sdk.errors as err
from slack_sdk.models.blocks import DividerBlock
from slack_sdk.web.async_client import AsyncWebClient
from tests.slack_sdk_async.helpers import async_test
from tests.slack_sdk.web.mock_web_api_server import (
Expand Down Expand Up @@ -153,3 +152,15 @@ async def test_default_team_id(self):
client = AsyncWebClient(base_url="http://localhost:8888", team_id="T_DEFAULT")
resp = await client.users_list(token="xoxb-users_list_pagination")
self.assertIsNone(resp["error"])

@async_test
async def test_user_auth_blocks(self):
self.client.token = "xoxb-api_test"
client = self.client
new_message = await client.chat_unfurl(
channel="C12345",
ts="1111.2222",
unfurls={},
user_auth_blocks=[DividerBlock(), DividerBlock()],
)
self.assertIsNone(new_message.get("error"))

0 comments on commit 9e0fa3d

Please sign in to comment.