Example #1
0
def test_bicingbot_command_unknown(get_bot, Bicing, DatabaseConnection):
    get_bot.return_value = mock.MagicMock()
    Bicing.return_value = mock.MagicMock()
    DatabaseConnection.return_value = mock.MagicMock()
    DatabaseConnection().get_groups_names.return_value = []

    bicingbot_commands(chat_id, 'unknown')

    # Check that get_station has not been called
    Bicing().get_station.assert_not_called()

    # Check that send message has been called with correct arguments
    get_bot().send_message.assert_called_with(chat_id=chat_id, text=STRINGS['es']['unknown_command'])
Example #2
0
def test_bicingbot_command_station(get_bot, Bicing):
    get_bot.return_value = mock.MagicMock()
    Bicing.return_value = mock.MagicMock()

    bicingbot_commands(chat_id, '155')

    # Check that get_station has been called
    Bicing().get_station.assert_called_with(155)

    # Check that send message has been called with correct arguments
    get_bot().send_message.assert_called_once()
    _, args = get_bot().send_message.call_args_list[0]
    assert args['chat_id'] == chat_id
    assert args['text'].count('\n') == 1
Example #3
0
def test_bicingbot_command_unknown(get_bot, Bicing, DatabaseConnection):
    get_bot.return_value = mock.MagicMock()
    Bicing.return_value = mock.MagicMock()
    DatabaseConnection.return_value = mock.MagicMock()
    DatabaseConnection().get_groups_names.return_value = []

    bicingbot_commands(chat_id, 'unknown')

    # Check that get_station has not been called
    Bicing().get_station.assert_not_called()

    # Check that send message has been called with correct arguments
    get_bot().send_message.assert_called_with(
        chat_id=chat_id, text=STRINGS['es']['unknown_command'])
Example #4
0
def test_bicingbot_command_station(get_bot, Bicing):
    get_bot.return_value = mock.MagicMock()
    Bicing.return_value = mock.MagicMock()

    bicingbot_commands(chat_id, '155')

    # Check that get_station has been called
    Bicing().get_station.assert_called_with(155)

    # Check that send message has been called with correct arguments
    get_bot().send_message.assert_called_once()
    _, args = get_bot().send_message.call_args_list[0]
    assert args['chat_id'] == chat_id
    assert args['text'].count('\n') == 1
Example #5
0
def test_bicingbot_command_group(get_bot, Bicing, DatabaseConnection):
    get_bot.return_value = mock.MagicMock()
    Bicing.return_value = mock.MagicMock()
    DatabaseConnection.return_value = mock.MagicMock()
    DatabaseConnection().get_groups_names.return_value = ['casa']
    DatabaseConnection().get_group.return_value = {'chat_id': chat_id,
                                                   'name': 'casa',
                                                   'stations': [153, 154, 339, 165, 166]}

    bicingbot_commands(chat_id, 'casa')

    # Check that 5 stations have been found
    assert Bicing().get_station.call_count == 5

    # Check that send message has been called with correct arguments
    get_bot().send_message.assert_called_once()
    _, args = get_bot().send_message.call_args_list[0]
    assert args['chat_id'] == chat_id
    assert args['text'].count('\n') == 5
Example #6
0
def test_bicingbot_command_group(get_bot, Bicing, DatabaseConnection):
    get_bot.return_value = mock.MagicMock()
    Bicing.return_value = mock.MagicMock()
    DatabaseConnection.return_value = mock.MagicMock()
    DatabaseConnection().get_groups_names.return_value = ['casa']
    DatabaseConnection().get_group.return_value = {
        'chat_id': chat_id,
        'name': 'casa',
        'stations': [153, 154, 339, 165, 166]
    }

    bicingbot_commands(chat_id, 'casa')

    # Check that 5 stations have been found
    assert Bicing().get_station.call_count == 5

    # Check that send message has been called with correct arguments
    get_bot().send_message.assert_called_once()
    _, args = get_bot().send_message.call_args_list[0]
    assert args['chat_id'] == chat_id
    assert args['text'].count('\n') == 5
Example #7
0
def webhook_handler():
    """
    Handles requests from BicingBot users.
    """
    # Reads request from Telegram user
    update = telegram.Update.de_json(request.get_json(force=True))
    logger.debug("Received message '{}'".format(update.message))
    try:
        if update.message.chat.type == 'private':
            chat_id = update.message.chat.id
            text = update.message.text
            logger.info("Received message '{}' from chat_id={}".format(text, chat_id))
            bicingbot_commands(chat_id, text)
        else:
            logger.warn("Received message with wrong chat type: {}".format(update.message))
    except AttributeError:
        chat_id = update.callback_query.message.chat.id
        data = update.callback_query.data
        logger.info("Received callback query response '{}' from chat_id={}".format(data, chat_id))
        bicingbot_callback(chat_id, data, update.callback_query)

    return ''
Example #8
0
def test_bicingbot_command_newgroup_name(newgroup_command):
    groups.GROUPS_CACHE[chat_id] = {'status': 1, 'name': None, 'stations': []}
    bicingbot_commands(chat_id, 'casa')
    newgroup_command.assert_called_with(chat_id, 'casa')
    del groups.GROUPS_CACHE[chat_id]
Example #9
0
def test_bicingbot_command_newgroup():
    COMMANDS['newgroup']['method'] = mock.MagicMock()
    bicingbot_commands(chat_id, 'newgroup')
    COMMANDS['newgroup']['method'].assert_called_with(chat_id, 'newgroup')
Example #10
0
def test_bicingbot_command_language():
    COMMANDS['language']['method'] = mock.MagicMock()
    bicingbot_commands(chat_id, 'language')
    COMMANDS['language']['method'].assert_called_with(chat_id, 'language')
Example #11
0
def test_bicingbot_command_newgroup_name(newgroup_command):
    groups.GROUPS_CACHE[chat_id] = {'status': 1, 'name': None, 'stations': []}
    bicingbot_commands(chat_id, 'casa')
    newgroup_command.assert_called_with(chat_id, 'casa')
    del groups.GROUPS_CACHE[chat_id]
Example #12
0
def test_bicingbot_command_newgroup():
    COMMANDS['newgroup']['method'] = mock.MagicMock()
    bicingbot_commands(chat_id, 'newgroup')
    COMMANDS['newgroup']['method'].assert_called_with(chat_id, 'newgroup')
Example #13
0
def test_bicingbot_command_language():
    COMMANDS['language']['method'] = mock.MagicMock()
    bicingbot_commands(chat_id, 'language')
    COMMANDS['language']['method'].assert_called_with(chat_id, 'language')