Skip to content

Commit

Permalink
Update BlockAttachment to not send invalid JSON due to fields attribu…
Browse files Browse the repository at this point in the history
…te (#473)
  • Loading branch information
paul-griffith authored and RodneyU215 committed Aug 29, 2019
1 parent 557b038 commit aabf2d0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions slack/web/classes/attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,14 @@ def __init__(self, *, blocks: List[Block], color: Optional[str] = None):
super().__init__(text="", color=color)
self.blocks = list(blocks)

@JsonValidator("fields attribute cannot be populated on BlockAttachment")
def fields_attribute_absent(self):
return not self.fields

def to_dict(self) -> dict:
json = super().to_dict()
json.update({"blocks": extract_json(self.blocks)})
del json["fields"] # cannot supply fields and blocks at the same time
return json


Expand Down
15 changes: 15 additions & 0 deletions tests/web/classes/test_attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from slack.errors import SlackObjectFormationError
from slack.web.classes.actions import ActionButton, ActionLinkButton
from slack.web.classes.blocks import SectionBlock, ImageBlock
from slack.web.classes.attachments import (
Attachment,
BlockAttachment,
AttachmentField,
InteractiveAttachment,
)
Expand Down Expand Up @@ -206,3 +208,16 @@ def test_actions_length(self):
InteractiveAttachment(
text="some text", callback_id="abc123", actions=actions
).to_dict(),


class BlockAttachmentTests(unittest.TestCase):
def test_basic_json(self):
blocks = [
SectionBlock(text="Some text"),
ImageBlock(image_url="image.jpg", alt_text="an image")
]

self.assertDictEqual(
BlockAttachment(blocks=blocks).to_dict(), {"blocks": [b.to_dict() for b in blocks]}
)

0 comments on commit aabf2d0

Please sign in to comment.