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

Update BlockAttachment to not send invalid JSON due to fields attribute #473

Merged
merged 1 commit into from
Aug 29, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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]}
)