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 str(attach) == f"Attachment(title='{title}')" attach.add_field('field3', '3') assert len(attach.fields) == 3 assert attach.fields[0].title == 'field1' assert attach.fields[1].title == 'field2' assert attach.fields[2].title == 'field3'
def test_slack_encoder_class(): def dumps(o): return json.dumps(o, cls=SlackEncoder, separators=(',', ':')) assert dumps(Field('title val', 'value val', True)) == ( '{"title":"title val","value":"value val","short":"1"}' ) assert dumps(Attachment( fallback='fallback val', title='title val', fields=[Field('field title1', 'field value1', False)], )) == ( '{"fallback":"fallback val","title":"title val","fields":' '[{"title":"field title1","value":"field value1","short":"0"}]}' ) class Dummy: pass with pytest.raises(TypeError): dumps(Dummy()) with pytest.raises(TypeError): dumps(ChannelFromConfig('general')) with pytest.raises(TypeError): dumps(ChannelsFromConfig('commons'))
def test_slack_encoder_class(): def encode(o): return ujson.loads( json.dumps(o, cls=SlackEncoder, separators=(',', ':'))) field = encode(Field('title val', 'value val', True)) assert field == { 'title': 'title val', 'value': 'value val', 'short': '1', } attachment = encode( 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=[ OptionGroup( 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', }, ], }, ], }], } class Dummy: pass with pytest.raises(TypeError): encode(Dummy()) with pytest.raises(TypeError): encode(ChannelFromConfig('general')) with pytest.raises(TypeError): encode(ChannelsFromConfig('commons'))
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), }