def test_basic_json(self): self.assertDictEqual( Attachment(text="some text").to_dict(), {"text": "some text", "fields": []} ) self.assertDictEqual( Attachment( text="attachment text", title="Attachment", fallback="fallback_text", pretext="some_pretext", title_link="link in title", fields=[ AttachmentField(title=f"field_{i}_title", value=f"field_{i}_value") for i in range(5) ], color="#FFFF00", author_name="John Doe", author_link="http://johndoeisthebest.com", author_icon="http://johndoeisthebest.com/avatar.jpg", thumb_url="thumbnail URL", footer="and a footer", footer_icon="link to footer icon", ts=123456789, markdown_in=["fields"], ).to_dict(), { "text": "attachment text", "author_name": "John Doe", "author_link": "http://johndoeisthebest.com", "author_icon": "http://johndoeisthebest.com/avatar.jpg", "footer": "and a footer", "title": "Attachment", "footer_icon": "link to footer icon", "pretext": "some_pretext", "ts": 123456789, "fallback": "fallback_text", "title_link": "link in title", "color": "#FFFF00", "thumb_url": "thumbnail URL", "fields": [ {"title": "field_0_title", "value": "field_0_value", "short": True}, {"title": "field_1_title", "value": "field_1_value", "short": True}, {"title": "field_2_title", "value": "field_2_value", "short": True}, {"title": "field_3_title", "value": "field_3_value", "short": True}, {"title": "field_4_title", "value": "field_4_value", "short": True}, ], "mrkdwn_in": ["fields"], }, )
def test_with_attachments(self): url = os.environ[SLACK_SDK_TEST_INCOMING_WEBHOOK_URL] webhook = WebhookClient(url) response = webhook.send( text="fallback", attachments=[ Attachment( text="attachment text", title="Attachment", fallback="fallback_text", pretext="some_pretext", title_link="link in title", fields=[ AttachmentField(title=f"field_{i}_title", value=f"field_{i}_value") for i in range(5) ], color="#FFFF00", author_name="John Doe", author_link="http://johndoeisthebest.com", author_icon="http://johndoeisthebest.com/avatar.jpg", thumb_url="thumbnail URL", footer="and a footer", footer_icon="link to footer icon", ts=123456789, markdown_in=["fields"], ) ], ) self.assertEqual(200, response.status_code) self.assertEqual("ok", response.body)
def test_send_attachments(self): client = WebhookClient("http://localhost:8888") resp = client.send( text="hello!", response_type="ephemeral", attachments=[{ "color": "#f2c744", "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": "This is a mrkdwn section block :ghost: *this is bold*, and ~this is crossed out~, and <https://google.com|this is a link>", }, }, { "type": "divider" }, { "type": "section", "text": { "type": "mrkdwn", "text": "Pick a date for the deadline.", }, "accessory": { "type": "datepicker", "initial_date": "1990-04-28", "placeholder": { "type": "plain_text", "text": "Select a date", }, }, }, ], }], ) self.assertEqual("ok", resp.body) resp = client.send( text="hello!", response_type="ephemeral", attachments=[ Attachment( text="attachment text", title="Attachment", fallback="fallback_text", pretext="some_pretext", title_link="link in title", fields=[ AttachmentField(title=f"field_{i}_title", value=f"field_{i}_value") for i in range(5) ], color="#FFFF00", author_name="John Doe", author_link="http://johndoeisthebest.com", author_icon="http://johndoeisthebest.com/avatar.jpg", thumb_url="thumbnail URL", footer="and a footer", footer_icon="link to footer icon", ts=123456789, markdown_in=["fields"], ) ], ) self.assertEqual("ok", resp.body)
def setUp(self) -> None: self.simple = Attachment(text="some_text")
class AttachmentTests(unittest.TestCase): def setUp(self) -> None: self.simple = Attachment(text="some_text") def test_basic_json(self): self.assertDictEqual( Attachment(text="some text").to_dict(), {"text": "some text", "fields": []} ) self.assertDictEqual( Attachment( text="attachment text", title="Attachment", fallback="fallback_text", pretext="some_pretext", title_link="link in title", fields=[ AttachmentField(title=f"field_{i}_title", value=f"field_{i}_value") for i in range(5) ], color="#FFFF00", author_name="John Doe", author_link="http://johndoeisthebest.com", author_icon="http://johndoeisthebest.com/avatar.jpg", thumb_url="thumbnail URL", footer="and a footer", footer_icon="link to footer icon", ts=123456789, markdown_in=["fields"], ).to_dict(), { "text": "attachment text", "author_name": "John Doe", "author_link": "http://johndoeisthebest.com", "author_icon": "http://johndoeisthebest.com/avatar.jpg", "footer": "and a footer", "title": "Attachment", "footer_icon": "link to footer icon", "pretext": "some_pretext", "ts": 123456789, "fallback": "fallback_text", "title_link": "link in title", "color": "#FFFF00", "thumb_url": "thumbnail URL", "fields": [ {"title": "field_0_title", "value": "field_0_value", "short": True}, {"title": "field_1_title", "value": "field_1_value", "short": True}, {"title": "field_2_title", "value": "field_2_value", "short": True}, {"title": "field_3_title", "value": "field_3_value", "short": True}, {"title": "field_4_title", "value": "field_4_value", "short": True}, ], "mrkdwn_in": ["fields"], }, ) def test_footer_length(self): with self.assertRaises(SlackObjectFormationError): self.simple.footer = STRING_301_CHARS self.simple.to_dict() def test_ts_without_footer(self): with self.assertRaises(SlackObjectFormationError): self.simple.ts = 123456789 self.simple.to_dict() def test_markdown_in_invalid(self): with self.assertRaises(SlackObjectFormationError): self.simple.markdown_in = ["nothing"] self.simple.to_dict() def test_color_valid(self): with self.assertRaises(SlackObjectFormationError): self.simple.color = "red" self.simple.to_dict() with self.assertRaises(SlackObjectFormationError): self.simple.color = "#ZZZZZZ" self.simple.to_dict() self.simple.color = "#bada55" self.assertEqual(self.simple.to_dict()["color"], "#bada55") self.simple.color = "good" self.assertEqual(self.simple.to_dict()["color"], "good") def test_image_url_and_thumb_url(self): with self.assertRaises(SlackObjectFormationError): self.simple.thumb_url = "some URL" self.simple.image_url = "some URL" self.simple.to_dict() self.simple.image_url = None self.simple.to_dict() def author_name_without_author_link(self): with self.assertRaises(SlackObjectFormationError): self.simple.author_name = "http://google.com" self.simple.to_dict() self.simple.author_name = None self.simple.to_dict() def author_icon_without_author_name(self): with self.assertRaises(SlackObjectFormationError): self.simple.author_icon = "http://google.com/images.jpg" self.simple.to_dict() self.simple.author_icon = None self.simple.to_dict()