Skip to content

Commit

Permalink
Fix #1258 Tuple value for blocks argument does not work for Web API c…
Browse files Browse the repository at this point in the history
…alls (#1259)
  • Loading branch information
tommasobertoni authored Sep 1, 2022
1 parent d2fd7ac commit 9c2ae69
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
6 changes: 3 additions & 3 deletions slack_sdk/web/internal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import warnings
from ssl import SSLContext
from typing import Dict, Union, Optional, Any, Sequence
from typing import Any, Dict, Optional, Sequence, Union
from urllib.parse import urljoin

from slack_sdk import version
Expand Down Expand Up @@ -189,12 +189,12 @@ def to_dict(obj: Union[Dict, Block, Attachment, Metadata]):
return obj

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

attachments = kwargs.get("attachments", None)
if attachments is not None and isinstance(attachments, list):
if attachments is not None and isinstance(attachments, Sequence):
dict_attachments = [to_dict(a) for a in attachments]
kwargs.update({"attachments": dict_attachments})

Expand Down
41 changes: 40 additions & 1 deletion tests/slack_sdk/web/test_internal_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import unittest
from slack_sdk.web.internal_utils import _build_unexpected_body_error_message
from typing import Dict, Sequence, Union

import pytest

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


class TestInternalUtils(unittest.TestCase):
Expand All @@ -16,3 +25,33 @@ def test_build_unexpected_body_error_message(self):
assert message.startswith(
"""Received a response in a non-JSON format: <!DOCTYPE html><html lang="en"><head><meta charset="utf-8">"""
)


@pytest.mark.parametrize("initial_blocks", [
[Block(block_id="42"), Block(block_id="24")], # list
(Block(block_id="42"), Block(block_id="24"),), # tuple
])
def test_can_parse_sequence_of_blocks(initial_blocks: Sequence[Union[Dict, Block]]):
kwargs = {"blocks": initial_blocks}

_parse_web_class_objects(kwargs)

assert kwargs["blocks"]

for block in kwargs["blocks"]:
assert isinstance(block, Dict)


@pytest.mark.parametrize("initial_attachments", [
[Attachment(text="foo"), Attachment(text="bar")], # list
(Attachment(text="foo"), Attachment(text="bar"),), # tuple
])
def test_can_parse_sequence_of_attachments(initial_attachments: Sequence[Union[Dict, Attachment]]):
kwargs = {"attachments": initial_attachments}

_parse_web_class_objects(kwargs)

assert kwargs["attachments"]

for attachment in kwargs["attachments"]:
assert isinstance(attachment, Dict)

0 comments on commit 9c2ae69

Please sign in to comment.