Exemplo n.º 1
0
def test_welcome(uqcsbot: MockUQCSBot):
    """
    Test !welcome for a member joining UQCS.
    """
    events = [
        generate_event_object(MESSAGE_TYPE_CHANNEL_CREATED,
                              channel={
                                  'id': 'general',
                                  'name': 'general',
                                  'is_public': True
                              }),
        generate_event_object(MESSAGE_TYPE_CHANNEL_CREATED,
                              channel={
                                  'id': 'announcements',
                                  'name': 'announcements',
                                  'is_public': True
                              }),
        generate_event_object(MESSAGE_TYPE_MEMBER_JOINED_CHANNEL,
                              channel='announcements',
                              user=TEST_USER_ID)
    ]
    with patch('time.sleep') as mock_sleep:
        mock_sleep.return_value = None
        for event in events:
            uqcsbot._run_handlers(event)

    general_messages = uqcsbot.test_messages.get('general', [])
    direct_messages = uqcsbot.test_messages.get(TEST_DIRECT_ID, [])
    print(general_messages, direct_messages)
    assert len(direct_messages) > 0
    assert general_messages[-1]['text'] == f'Welcome, <@{TEST_USER_ID}>!'
Exemplo n.º 2
0
def test_welcome_milestone(uqcsbot: MockUQCSBot):
    """
    Test !welcome for a member joining on a milestone number.
    """
    events = [generate_event_object(MESSAGE_TYPE_CHANNEL_CREATED,
                                    channel={'id': 'general', 'name': 'general',
                                             'is_public': True}),
              generate_event_object(MESSAGE_TYPE_CHANNEL_CREATED,
                                    channel={'id': 'announcements', 'name': 'announcements',
                                             'is_public': True})]
    for i in range(MEMBER_MILESTONE):
        user_id = 'U' + str(i).rjust(10, '0')
        uqcsbot.test_users[user_id] = {'id': user_id}
        uqcsbot.test_channels[user_id] = {'id': user_id, 'name': user_id}
        events.extend((generate_event_object(MESSAGE_TYPE_CHANNEL_CREATED,
                                             channel={'id': user_id, 'name': user_id}),
                       generate_event_object(MESSAGE_TYPE_TEAM_JOIN, user={'id': user_id}),
                       generate_event_object(MESSAGE_TYPE_MEMBER_JOINED_CHANNEL,
                                             channel='announcements', user=user_id)))
    with patch('time.sleep') as mock_sleep:
        mock_sleep.return_value = None
        for event in events:
            uqcsbot._run_handlers(event)

    general_messages = uqcsbot.test_messages.get('general', [])
    assert len(general_messages) == MEMBER_MILESTONE + 1
    assert general_messages[-1]['text'] == f':tada: {MEMBER_MILESTONE} members! :tada:'
Exemplo n.º 3
0
def test_emoji_log(uqcsbot: MockUQCSBot):
    """
    Test !emoji_log for a member adding or removing emoji.
    """
    events = [
        # next event triggers the channel_log function,
        # and so a #uqcs-meta channel needs to exist to post to
        generate_event_object(MESSAGE_TYPE_CHANNEL_CREATED,
                              channel={
                                  'id': 'uqcs-meta',
                                  'name': 'uqcs-meta',
                                  'is_public': True
                              }),
        generate_event_object(MESSAGE_TYPE_CHANNEL_CREATED,
                              channel={
                                  'id': 'emoji-request',
                                  'name': 'emoji-request',
                                  'is_public': True
                              }),
        # normal add comes with an image URL
        generate_event_object(MESSAGE_TYPE_EMOJI_CHANGED,
                              subtype='add',
                              name='emoji_add_test',
                              value='https://bleh.gif'),
        # alias add comes with the alias name
        generate_event_object(MESSAGE_TYPE_EMOJI_CHANGED,
                              subtype='add',
                              name='alias_add',
                              value='alias:emoji_add_test'),
        generate_event_object(MESSAGE_TYPE_EMOJI_CHANGED,
                              subtype='remove',
                              names=['emoji_remove_test']),
        # if base emoji is removed, aliases go too
        generate_event_object(
            MESSAGE_TYPE_EMOJI_CHANGED,
            subtype='remove',
            names=['emoji_remove_test', 'emoji_remove_alias']),
        # no subtype is valid according to slack API
        generate_event_object(MESSAGE_TYPE_EMOJI_CHANGED,
                              names='emoji_remove_test')
    ]
    with patch('time.sleep') as mock_sleep:
        mock_sleep.return_value = None
        for event in events:
            uqcsbot._run_handlers(event)

    emoji_messages = uqcsbot.test_messages.get('emoji-request', [])
    print(emoji_messages)
    assert len(emoji_messages) == 4
    assert emoji_messages[0][
        'text'] == 'Emoji added: :emoji_add_test: (`:emoji_add_test:`)'
    assert (
        emoji_messages[1]['text'] ==
        'Emoji alias added: `:alias_add:` :arrow_right: `:emoji_add_test:` (:alias_add:)'
    )
    assert emoji_messages[2]['text'] == 'Emoji removed: `:emoji_remove_test:`'
    assert (emoji_messages[3]['text'] ==
            'Emojis removed: `:emoji_remove_test:`, `:emoji_remove_alias:`')
Exemplo n.º 4
0
def test_channel_log(uqcsbot: MockUQCSBot):
    """
    Test channel_log for a member creating a channel
    """
    uqcsbot._run_handlers(generate_event_object(MESSAGE_TYPE_CHANNEL_CREATED,
                                                channel={'id': 'uqcs-meta',
                                                         'name': 'uqcs-meta',
                                                         'is_public': True}))

    message = uqcsbot.test_messages.get('uqcs-meta', [])
    assert len(message) == 1
    assert message[0]['text'] == 'New Channel Created: <#uqcs-meta|uqcs-meta>'
Exemplo n.º 5
0
def test_emoji_log(uqcsbot: MockUQCSBot):
    """
    Test !emoji_log for a member adding or removing emoji.
    """
    events = [
        generate_event_object(MESSAGE_TYPE_CHANNEL_CREATED,
                              channel={
                                  'id': 'emoji-request',
                                  'name': 'emoji-request',
                                  'is_public': True
                              }),
        generate_event_object(MESSAGE_TYPE_EMOJI_CHANGED,
                              subtype='add',
                              name='emoji_add_test'),
        generate_event_object(MESSAGE_TYPE_EMOJI_CHANGED,
                              subtype='remove',
                              names=['emoji_remove_test']),
        # if base emoji is removed, aliases go too
        generate_event_object(
            MESSAGE_TYPE_EMOJI_CHANGED,
            subtype='remove',
            names=['emoji_remove_test', 'emoji_remove_alias']),
        # no subtype is valid according to slack API
        generate_event_object(MESSAGE_TYPE_EMOJI_CHANGED,
                              names='emoji_remove_test')
    ]
    with patch('time.sleep') as mock_sleep:
        mock_sleep.return_value = None
        for event in events:
            uqcsbot._run_handlers(event)

    emoji_messages = uqcsbot.test_messages.get('emoji-request', [])
    print(emoji_messages)
    assert len(emoji_messages) == 3
    assert emoji_messages[0][
        'text'] == 'Emoji added: :emoji_add_test: (`:emoji_add_test:`)'
    assert emoji_messages[1]['text'] == 'Emoji removed: `:emoji_remove_test:`'
    assert (emoji_messages[2]['text'] ==
            'Emojis removed: `:emoji_remove_test:`, `:emoji_remove_alias:`')