Пример #1
0
def test_validate_first_message(data, raises):
    mock_message = mock.Mock(data=data)
    if raises is None:
        assert SlackBot._validate_first_message(mock_message) is None
    else:
        with pytest.raises(raises):
            SlackBot._validate_first_message(mock_message)
Пример #2
0
def test_instruction_list():
    bot = SlackBot(None, 'foo', None)
    instructions = bot._instruction_list([Unhandled()])
    assert instructions.startswith(SlackBot.INSTRUCTIONS.strip())
    assert '"@foo: help"' in instructions
    assert '"@foo: version"' in instructions
    assert instructions.endswith('a dummy handler')
Пример #3
0
def test_validate_first_message(data, raises):
    mock_message = mock.Mock(data=data)
    if raises is None:
        assert SlackBot._validate_first_message(mock_message) is None
    else:
        with pytest.raises(raises):
            SlackBot._validate_first_message(mock_message)
Пример #4
0
def test_format_message(randint):
    bot = SlackBot(None, None, None)
    expected = dict(
        id=randint.return_value,
        channel='foo',
        text='bar',
        type='message',
    )
    assert json.loads(bot._format_message('foo', 'bar')) == expected
Пример #5
0
def test_format_message(randint):
    bot = SlackBot(None, None, None)
    expected = dict(
        id=randint.return_value,
        channel='foo',
        text='bar',
        type='message',
    )
    assert json.loads(bot._format_message('foo', 'bar')) == expected
Пример #6
0
def test_instruction_list():
    bot = SlackBot(None, 'foo', None)
    def filter_():
        """foo"""
    def dispatch():
        """bar"""
    instructions = bot._instruction_list({filter_: dispatch})
    assert instructions.endswith('foo bar')
    assert instructions.startswith(SlackBot.INSTRUCTIONS.strip())
    assert '"@foo: help"' in instructions and '"@foo: version"' in instructions
Пример #7
0
async def test_handle_help_message(randint):
    bot = SlackBot('foo', None, None)
    mock_msg = mock.Mock(data=json.dumps(
        dict(channel='bar', text='<@foo>: help', type='message'), ), )
    expected = dict(
        channel='bar',
        id=randint.return_value,
        text=bot._instruction_list({}),
        type='message',
    )
    response = await bot.handle_message(mock_msg, {})
    assert json.loads(response) == expected
Пример #8
0
def test_instruction_list():
    bot = SlackBot(None, 'foo', None)

    def filter_():
        """foo"""

    def dispatch():
        """bar"""

    instructions = bot._instruction_list({filter_: dispatch})
    assert instructions.endswith('foo bar')
    assert instructions.startswith(SlackBot.INSTRUCTIONS.strip())
    assert '"@foo: help"' in instructions and '"@foo: version"' in instructions
Пример #9
0
async def test_handle_message_dispatch(randint):
    bot = SlackBot(None, None, None)
    mock_message = mock.Mock(data='{"channel": 123}')
    mock_socket = mock.MagicMock()
    bot.socket = mock_socket
    dummy_response = 'bar'
    await bot.handle_message(mock_message, [Handled(dummy_response)])
    expected = dict(
        channel=123,
        id=randint.return_value,
        text=dummy_response,
        type='message',
    )
    assert json.loads(mock_socket.send_str.call_args[0][0]) == expected
Пример #10
0
async def test_handle_help_message(randint):
    bot = SlackBot('foo', None, None)
    mock_msg = mock.Mock(
        data=json.dumps(
            dict(channel='bar', text='<@foo>: help', type='message'),
        ),
    )
    expected = dict(
        channel='bar',
        id=randint.return_value,
        text=bot._instruction_list({}),
        type='message',
    )
    response = await bot.handle_message(mock_msg, {})
    assert json.loads(response) == expected
Пример #11
0
def test_bot_handler_matching():
    bot = SlackBot('foo', None, None)
    handler = BotMessageHandler(bot)
    handler.PHRASE = 'bar'
    text = '<@foo>: bar'
    assert handler.matches(dict(type='message', text=text))
    assert handler.text == text
Пример #12
0
async def test_join_rtm_messages(ws_connect):
    mock_msg = mock.Mock(
        data='{"type": "hello", "channel": 123}',
        tp=aiohttp.MsgType.text,
    )
    mock_socket = AsyncIterable.from_test_data(
        mock_msg,
        close=None,
        receive=mock.Mock(data='{"type": "hello"}'),
        send_str=None,
    )
    ws_connect.return_value = AsyncContextManager(mock_socket)
    api = mock.CoroutineMock(
        spec=SlackBotApi,
        **{'execute_method.return_value': {'url': 'foo'}},
    )
    bot = SlackBot(None, None, api)
    data = {'channel': 'foo', 'text': 'bar'}
    await bot.join_rtm([Handled(data)])
    api.execute_method.assert_called_once_with(
        'rtm.start',
        simple_latest=True,
        no_unreads=True,
    )
    assert mock_socket.send_str.call_count == 1
Пример #13
0
async def test_handle_version_message(randint):
    bot = SlackBot('foo', None, None)
    mock_msg = mock.Mock(
        data=json.dumps(
            dict(channel='bar', text='<@foo>: version', type='message'),
        ),
    )
    expected = dict(
        channel='bar',
        id=randint.return_value,
        text=bot.VERSION,
        type='message',
    )
    mock_socket = mock.MagicMock()
    bot.socket = mock_socket
    await bot.handle_message(mock_msg, [])
    assert json.loads(mock_socket.send_str.call_args[0][0]) == expected
Пример #14
0
def test_init(randint):
    args = dict(id_='foo', user='******', api='baz')
    bot = SlackBot(**args)
    for attr, val in args.items():
        assert getattr(bot, attr) == val
    assert bot.address_as == '<@foo>: '
    assert bot.full_name == '<@foo>'
    assert next(bot._msg_ids) == randint.return_value
    randint.assert_called_once_with(1, 1000)
Пример #15
0
async def test_get_socket_url():
    api = mock.CoroutineMock(
        spec=SlackBotApi,
        **{'execute_method.return_value': {'url': 'foo'}},
    )
    bot = SlackBot(None, None, api)
    url = await bot._get_socket_url()
    assert url == 'foo'
    api.execute_method.assert_called_once_with(
        'rtm.start',
        simple_latest=True,
        no_unreads=True,
    )
Пример #16
0
async def test_handle_message_dispatch(randint):
    bot = SlackBot(None, None, None)
    mock_message = mock.Mock(data='{}')
    mock_filter_ = mock.Mock(return_value=True)
    mock_dispatch = mock.CoroutineMock(
        return_value=dict(channel='foo', text='bar'))
    response = await bot.handle_message(mock_message,
                                        {mock_filter_: mock_dispatch})
    expected = dict(
        id=randint.return_value,
        type='message',
        **mock_dispatch.return_value,
    )
    assert json.loads(response) == expected
    mock_filter_.assert_called_once_with(bot, {})
    mock_dispatch.assert_called_once_with(bot, {})
Пример #17
0
async def test_join_rtm_simple(ws_connect):
    ws_connect.return_value = AsyncContextManager(
        AsyncIterable.from_test_data(
            receive=mock.Mock(data='{"type": "hello"}'),
        )
    )
    api = mock.CoroutineMock(
        spec=SlackBotApi,
        **{'execute_method.return_value': {'url': 'foo'}},
    )
    bot = SlackBot(None, None, api)
    await bot.join_rtm()
    api.execute_method.assert_called_once_with(
        'rtm.start',
        simple_latest=True,
        no_unreads=True,
    )
Пример #18
0
async def test_join_rtm_error_messages(ws_connect, closed, calls):
    mock_msg = mock.Mock(tp=aiohttp.MsgType.closed)
    mock_socket = AsyncIterable.from_test_data(
        mock_msg,
        close=None,
        receive=mock.Mock(data='{"type": "hello"}'),
    )
    mock_socket.closed = closed
    ws_connect.return_value = AsyncContextManager(mock_socket)
    api = mock.CoroutineMock(
        spec=SlackBotApi,
        **{'execute_method.return_value': {'url': 'foo'}},
    )
    bot = SlackBot(None, None, api)
    await bot.join_rtm()
    api.execute_method.assert_called_once_with(
        'rtm.start',
        simple_latest=True,
        no_unreads=True,
    )
    assert mock_socket.close.call_count == calls
Пример #19
0
async def test_handle_unfiltered_message():
    bot = SlackBot(None, None, None)
    mock_msg = mock.Mock(data=json.dumps(dict(type='message')))
    await bot.handle_message(mock_msg, {lambda self, msg: False: None})
Пример #20
0
def test_unpack_message_broken_json():
    mock_message = mock.Mock(data='broken.json')
    with pytest.raises(json.JSONDecodeError):
        SlackBot._unpack_message(mock_message)
Пример #21
0
def test_to_me(input_, output):
    bot = SlackBot('foo', None, None)
    assert bot.message_is_to_me(input_) == output
Пример #22
0
async def test_handle_unfiltered_message():
    bot = SlackBot(None, None, None)
    mock_msg = mock.Mock(data=json.dumps(dict(type='message')))
    await bot.handle_message(mock_msg, [Unhandled()])
Пример #23
0
async def test_handle_error_message():
    bot = SlackBot(None, None, None)
    mock_msg = mock.Mock(data=json.dumps(dict(error={}, type='error')))
    with pytest.raises(SlackApiError):
        await bot.handle_message(mock_msg, [])
Пример #24
0
def test_to_me(input_, output):
    bot = SlackBot('foo', None, None)
    assert bot.message_is_to_me(input_) == output
Пример #25
0
def test_unpack_message_success():
    data = {'bar': 'foo'}
    mock_message = mock.Mock(data=json.dumps(data))
    assert SlackBot._unpack_message(mock_message) == data
Пример #26
0
def test_unpack_message_no_data():
    with pytest.raises(AttributeError):
        SlackBot._unpack_message(object())
Пример #27
0
def test_unpack_message_success():
    data = {'bar': 'foo'}
    mock_message = mock.Mock(data=json.dumps(data))
    assert SlackBot._unpack_message(mock_message) == data
Пример #28
0
def test_unpack_message_no_data():
    with pytest.raises(AttributeError):
        SlackBot._unpack_message(object())
Пример #29
0
def test_mentions_me(input_, output):
    bot = SlackBot('foo', None, None)
    assert bot.message_mentions_me(input_) == output
Пример #30
0
def test_mentions_me(input_, output):
    bot = SlackBot('foo', None, None)
    assert bot.message_mentions_me(input_) == output
Пример #31
0
def test_unpack_message_broken_json():
    mock_message = mock.Mock(data='broken.json')
    with pytest.raises(json.JSONDecodeError):
        SlackBot._unpack_message(mock_message)