def test_attachment_class(): fallback = "fallback" color = "black" pretext = "pretext" author_name = "item4" author_link = "https://item4.github.io/" author_icon = "https://item4.github.io/static/images/item4.png" title = "title" text = "text" fields = [Field("field1", "1", False), Field("field2", "2", True)] image_url = ( "https://item4.github.io/static/images/favicon/apple-icon-60x60.png") thumb_url = ( "https://item4.github.io/static/images/favicon/apple-icon-57x57.png") footer = "footer" footer_icon = ( "https://item4.github.io/static/images/favicon/apple-icon-72x72.png") ts = 123456 attach = Attachment( fallback=fallback, color=color, pretext=pretext, author_name=author_name, author_link=author_link, author_icon=author_icon, title=title, text=text, fields=fields, image_url=image_url, thumb_url=thumb_url, footer=footer, footer_icon=footer_icon, ts=ts, ) assert attach.fallback == fallback assert attach.color == color assert attach.pretext == pretext assert attach.author_name == author_name assert attach.author_link == author_link assert attach.author_icon == author_icon assert attach.title == title assert attach.text == text assert len(attach.fields) == 2 assert attach.fields[0].title == "field1" assert attach.fields[1].title == "field2" assert attach.image_url == image_url assert attach.thumb_url == thumb_url assert attach.footer == footer assert attach.footer_icon == footer_icon assert attach.ts == ts assert attach.actions is None
def test_attachment_class(): fallback = 'fallback' color = 'black' pretext = 'pretext' author_name = 'item4' author_link = 'https://item4.github.io/' author_icon = 'https://item4.github.io/static/images/item4.png' title = 'title' text = 'text' fields = [Field('field1', '1', False), Field('field2', '2', True)] image_url = ( 'https://item4.github.io/static/images/favicon/apple-icon-60x60.png') thumb_url = ( 'https://item4.github.io/static/images/favicon/apple-icon-57x57.png') footer = 'footer' footer_icon = ( 'https://item4.github.io/static/images/favicon/apple-icon-72x72.png') ts = 123456 attach = Attachment( fallback=fallback, color=color, pretext=pretext, author_name=author_name, author_link=author_link, author_icon=author_icon, title=title, text=text, fields=fields, image_url=image_url, thumb_url=thumb_url, footer=footer, footer_icon=footer_icon, ts=ts, ) assert attach.fallback == fallback assert attach.color == color assert attach.pretext == pretext assert attach.author_name == author_name assert attach.author_link == author_link assert attach.author_icon == author_icon assert attach.title == title assert attach.text == text assert len(attach.fields) == 2 assert attach.fields[0].title == 'field1' assert attach.fields[1].title == 'field2' assert attach.image_url == image_url assert attach.thumb_url == thumb_url assert attach.footer == footer assert attach.footer_icon == footer_icon assert attach.ts == ts assert attach.actions is None
def test_to_json(): field = to_test_type(Field('title val', 'value val', True)) assert field == { 'title': 'title val', 'value': 'value val', 'short': '1', } attachment = to_test_type( Attachment( fallback='fallback val', title='title val', fields=[Field('field title1', 'field value1', False)], actions=[ Action( name='action1 name', text='action1 text', type='button', data_source='external', options=[ OptionField(text='a1 o1 text', value='a1 o1 value') ], style='danger', min_query_length=100, confirm=Confirmation(text='confirm text'), selected_options=[ OptionField(text='a1 so1 text', value='a1 so1 value',) ], value='action1 value', url='action1 url', ), Action( name='action2 name', text='action2 text', type='select', option_groups=[ OptionFieldGroup( text='a2 og1 text', options=[ OptionField( text='a2 og1 o1 text', value='a2 og1 o1 value', ), ], ) ], ), ], ) ) assert attachment == { 'fallback': 'fallback val', 'title': 'title val', 'fields': [ {'title': 'field title1', 'value': 'field value1', 'short': '0'} ], 'actions': [ { 'name': 'action1 name', 'text': 'action1 text', 'type': 'button', 'data_source': 'external', 'options': [{'text': 'a1 o1 text', 'value': 'a1 o1 value'}], 'style': 'danger', 'min_query_length': 100, 'confirm': {'text': 'confirm text'}, 'selected_options': [ {'text': 'a1 so1 text', 'value': 'a1 so1 value'}, ], 'value': 'action1 value', 'url': 'action1 url', }, { 'name': 'action2 name', 'text': 'action2 text', 'type': 'select', 'option_groups': [ { 'text': 'a2 og1 text', 'options': [ { 'text': 'a2 og1 o1 text', 'value': 'a2 og1 o1 value', }, ], }, ], }, ], }
def test_to_json(): field = to_test_type(Field("title val", "value val", True)) assert field == { "title": "title val", "value": "value val", "short": "1", } attachment = to_test_type( Attachment( fallback="fallback val", title="title val", fields=[Field("field title1", "field value1", False)], actions=[ Action( name="action1 name", text="action1 text", type="button", data_source="external", options=[ OptionField(text="a1 o1 text", value="a1 o1 value") ], style="danger", min_query_length=100, confirm=Confirmation(text="confirm text"), selected_options=[ OptionField( text="a1 so1 text", value="a1 so1 value", ) ], value="action1 value", url="action1 url", ), Action( name="action2 name", text="action2 text", type="select", option_groups=[ OptionFieldGroup( text="a2 og1 text", options=[ OptionField( text="a2 og1 o1 text", value="a2 og1 o1 value", ), ], ) ], ), ], )) assert attachment == { "fallback": "fallback val", "title": "title val", "fields": [{ "title": "field title1", "value": "field value1", "short": "0" }], "actions": [ { "name": "action1 name", "text": "action1 text", "type": "button", "data_source": "external", "options": [{ "text": "a1 o1 text", "value": "a1 o1 value" }], "style": "danger", "min_query_length": 100, "confirm": { "text": "confirm text" }, "selected_options": [ { "text": "a1 so1 text", "value": "a1 so1 value" }, ], "value": "action1 value", "url": "action1 url", }, { "name": "action2 name", "text": "action2 text", "type": "select", "option_groups": [ { "text": "a2 og1 text", "options": [ { "text": "a2 og1 o1 text", "value": "a2 og1 o1 value", }, ], }, ], }, ], }
async def test_slack_api_chat_post_ephemeral(bot): channel_id = "C1234" user_id = "U5555" attachments = [ Attachment( fallback="fallback val", title="title val", fields=[Field("field title1", "field value1", False)], ) ] blocks = [Divider()] text = "text val" parse = "text" username = "******" icon_url = ( "https://item4.github.io/static/images/favicon/apple-icon-57x57.png") icon_emoji = ":cake:" thread_ts = "12.34" with pytest.raises(TypeError): await bot.api.chat.postEphemeral(channel=channel_id, user=user_id) await bot.api.chat.postEphemeral( channel=channel_id, user=user_id, text=text, ) call = bot.call_queue.pop() assert call.method == "chat.postEphemeral" assert call.data == { "channel": channel_id, "user": user_id, "text": text, } assert call.json_mode await bot.api.chat.postEphemeral( channel=channel_id, user=user_id, attachments=attachments, ) call = bot.call_queue.pop() assert call.method == "chat.postEphemeral" assert call.data == { "channel": channel_id, "user": user_id, "attachments": [ { "fallback": "fallback val", "title": "title val", "fields": [ { "title": "field title1", "value": "field value1", "short": False, }, ], }, ], } assert call.json_mode await bot.api.chat.postEphemeral( as_user=False, attachments=attachments, blocks=blocks, channel=channel_id, icon_emoji=icon_emoji, icon_url=icon_url, link_names=True, parse=parse, text=text, thread_ts=thread_ts, user=user_id, username=username, token="KIRITO", ) call = bot.call_queue.pop() assert call.method == "chat.postEphemeral" assert call.data == { "channel": channel_id, "user": user_id, "text": text, "parse": parse, "link_names": True, "attachments": [ { "fallback": "fallback val", "title": "title val", "fields": [ { "title": "field title1", "value": "field value1", "short": False, }, ], }, ], "blocks": [{ "type": "divider" }], "username": username, "as_user": False, "icon_url": icon_url, "icon_emoji": icon_emoji, "thread_ts": thread_ts, } assert call.json_mode assert call.token == "KIRITO"
async def test_slack_api_chat_post_ephemeral(): bot = FakeBot() channel = bot.add_channel('C4567', 'test') user = bot.add_user('U0987', 'kirito') channel_id = 'C1234' user_id = 'U5555' attachments = [ Attachment( fallback='fallback val', title='title val', fields=[Field('field title1', 'field value1', False)], ) ] blocks = [Divider()] text = 'text val' parse = 'text' username = '******' icon_url = ( 'https://item4.github.io/static/images/favicon/apple-icon-57x57.png') icon_emoji = ':cake:' thread_ts = '12.34' bot = FakeBot() with pytest.raises(TypeError): await bot.api.chat.postEphemeral(channel=channel, user=user) await bot.api.chat.postEphemeral( channel=channel, user=user, text=text, ) call = bot.call_queue.pop() assert call.method == 'chat.postEphemeral' assert call.data == { 'channel': channel.id, 'user': user.id, 'text': text, } assert call.json_mode await bot.api.chat.postEphemeral( channel=channel_id, user=user_id, attachments=attachments, ) call = bot.call_queue.pop() assert call.method == 'chat.postEphemeral' assert call.data == { 'channel': channel_id, 'user': user_id, 'attachments': [ { 'fallback': 'fallback val', 'title': 'title val', 'fields': [ { 'title': 'field title1', 'value': 'field value1', 'short': False, }, ], }, ], } assert call.json_mode await bot.api.chat.postEphemeral( as_user=False, attachments=attachments, blocks=blocks, channel=channel, icon_emoji=icon_emoji, icon_url=icon_url, link_names=True, parse=parse, text=text, thread_ts=thread_ts, user=user, username=username, token='KIRITO', ) call = bot.call_queue.pop() assert call.method == 'chat.postEphemeral' assert call.data == { 'channel': channel.id, 'user': user.id, 'text': text, 'parse': parse, 'link_names': True, 'attachments': [ { 'fallback': 'fallback val', 'title': 'title val', 'fields': [ { 'title': 'field title1', 'value': 'field value1', 'short': False, }, ], }, ], 'blocks': [{ 'type': 'divider' }], 'username': username, 'as_user': False, 'icon_url': icon_url, 'icon_emoji': icon_emoji, 'thread_ts': thread_ts, } assert call.json_mode assert call.token == 'KIRITO'
async def test_slack_api_chat_post_message(): bot = FakeBot() channel = bot.add_channel('C4567', 'test') channel_id = 'C1234' attachments = [ Attachment( fallback='fallback val', title='title val', fields=[Field('field title1', 'field value1', False)], ) ] blocks = [Divider()] text = 'text val' parse = 'text' username = '******' icon_url = ( 'https://item4.github.io/static/images/favicon/apple-icon-57x57.png') icon_emoji = ':cake:' thread_ts = '12.34' mrkdwn = False bot = FakeBot() with pytest.raises(TypeError): await bot.api.chat.postMessage(channel=channel) await bot.api.chat.postMessage(channel=channel, text=text, as_user=True) call = bot.call_queue.pop() assert call.method == 'chat.postMessage' assert call.data == { 'channel': channel.id, 'text': text, 'as_user': True, 'mrkdwn': True, } assert call.json_mode await bot.api.chat.postMessage(channel=channel_id, attachments=attachments) call = bot.call_queue.pop() assert call.method == 'chat.postMessage' assert call.data == { 'channel': channel_id, 'attachments': [ { 'fallback': 'fallback val', 'title': 'title val', 'fields': [ { 'title': 'field title1', 'value': 'field value1', 'short': False, }, ], }, ], 'mrkdwn': True, } assert call.json_mode await bot.api.chat.postMessage( as_user=False, attachments=attachments, blocks=blocks, channel=channel, icon_emoji=icon_emoji, icon_url=icon_url, link_names=True, mrkdwn=mrkdwn, parse=parse, reply_broadcast=True, text=text, thread_ts=thread_ts, unfurl_links=False, unfurl_media=True, username=username, token='KIRITO', ) call = bot.call_queue.pop() assert call.method == 'chat.postMessage' assert call.data == { 'channel': channel.id, 'text': text, 'parse': parse, 'link_names': True, 'attachments': [ { 'fallback': 'fallback val', 'title': 'title val', 'fields': [ { 'title': 'field title1', 'value': 'field value1', 'short': False, }, ], }, ], 'blocks': [{ 'type': 'divider' }], 'unfurl_links': False, 'unfurl_media': True, 'username': username, 'as_user': False, 'icon_url': icon_url, 'icon_emoji': icon_emoji, 'thread_ts': thread_ts, 'reply_broadcast': True, 'mrkdwn': mrkdwn, } assert call.json_mode assert call.token == 'KIRITO'
async def test_slack_api_chat_post_message(): bot = FakeBot() channel = bot.add_channel('C4567', 'test') channel_id = 'C1234' attachments = [ Attachment( fallback='fallback val', title='title val', fields=[Field('field title1', 'field value1', False)], ) ] text = 'text val' parse = 'text' username = '******' icon_url = ( 'https://item4.github.io/static/images/favicon/apple-icon-57x57.png') icon_emoji = ':cake:' thread_ts = '12.34' bot = FakeBot() with pytest.raises(TypeError): await bot.api.chat.postMessage(channel=channel) await bot.api.chat.postMessage(channel=channel, text=text, as_user=True) call = bot.call_queue.pop() assert call.method == 'chat.postMessage' assert call.data == { 'channel': channel.id, 'text': text, 'as_user': bool2str(True), } await bot.api.chat.postMessage(channel=channel_id, attachments=attachments) call = bot.call_queue.pop() assert call.method == 'chat.postMessage' assert call.data == { 'channel': channel_id, 'attachments': ('[{"fallback":"fallback val","title":"title val",' '"fields":[{"title":"field title1",' '"value":"field value1","short":"0"}]}]'), } await bot.api.chat.postMessage( channel=channel, text=text, parse=parse, link_names=True, attachments=attachments, unfurl_links=False, unfurl_media=True, username=username, as_user=False, icon_url=icon_url, icon_emoji=icon_emoji, thread_ts=thread_ts, reply_broadcast=True, response_type='in_channel', replace_original=False, delete_original=True, ) call = bot.call_queue.pop() assert call.method == 'chat.postMessage' assert call.data == { 'channel': channel.id, 'text': text, 'parse': parse, 'link_names': bool2str(True), 'attachments': ('[{"fallback":"fallback val","title":"title val",' '"fields":[{"title":"field title1",' '"value":"field value1","short":"0"}]}]'), 'unfurl_links': bool2str(False), 'unfurl_media': bool2str(True), 'username': username, 'as_user': bool2str(False), 'icon_url': icon_url, 'icon_emoji': icon_emoji, 'thread_ts': thread_ts, 'reply_broadcast': bool2str(True), 'response_type': 'in_channel', 'replace_original': bool2str(False), 'delete_original': bool2str(True), }