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

    newgroup_command(chat_id, 'newgroup')
    newgroup_command(chat_id, 'casa')
    newgroup_command(chat_id, '1')

    newgroup_command(chat_id, 'not a number')

    # Check bot calls and temporal cache
    get_bot().send_message.assert_called_with(
        chat_id=chat_id, text=STRINGS['es']['newgroup_unknown_command'])
    assert GROUPS_CACHE[chat_id]['status'] == GROUP_STATUS_NEWGROUP_STATIONS
    assert GROUPS_CACHE[chat_id]['name'] == 'casa'
    assert GROUPS_CACHE[chat_id]['stations'] == [1]

    newgroup_command(chat_id, 'end')

    # Check bot and database calls
    get_bot().send_message.assert_called_with(
        chat_id=chat_id, text=STRINGS['es']['newgroup_created'].format('casa'))
    DatabaseConnection().delete_group.assert_called_with(chat_id=chat_id,
                                                         name='casa')
    DatabaseConnection().create_group.assert_called_with(chat_id=chat_id,
                                                         name='casa',
                                                         stations=[1])
    commands_get_bot().send_message.assert_called_once()
    assert chat_id not in GROUPS_CACHE
Example #2
0
def bicingbot_commands(chat_id, text):
    """
    Handles bicingbot specific commands and sends the corresponding messages to the user

    :param chat_id: Telegram chat id
    :param text: command to be executed
    """
    text = normalize_command_name(text)
    command_method = get_command_method(text)
    if get_group_status(chat_id) > GROUP_STATUS_INIT:
        newgroup_command(chat_id, text)
    elif command_method:
        command_method(chat_id, text)
    elif is_integer(text):
        logger.info('COMMAND /station {}: chat_id={}'.format(text, chat_id))
        send_stations_status(chat_id, [int(text)])
    else:
        db_connection = DatabaseConnection()
        if text in db_connection.get_groups_names(chat_id):
            logger.info('COMMAND /group {}: chat_id={}'.format(text, chat_id))
            group = db_connection.get_group(chat_id, text)
            send_stations_status(chat_id, group['stations'])
        else:
            logger.info('UNKNOWN COMMAND {}: chat_id={}'.format(text, chat_id))
            get_bot().send_message(chat_id=chat_id,
                                   text=tr('unknown_command', chat_id))
        db_connection.close()
Example #3
0
def test_newgroup_command_number_stations_limit(get_bot, DatabaseConnection, Bicing, commands_get_bot):
    get_bot.return_value = mock.MagicMock()
    DatabaseConnection.return_value = mock.MagicMock()
    DatabaseConnection().get_groups_names.return_value = []
    Bicing.return_value = mock.MagicMock()
    commands_get_bot.return_value = mock.MagicMock()
    del_group_status(chat_id)

    newgroup_command(chat_id, 'newgroup')
    newgroup_command(chat_id, 'casa')
    for i in range(MAX_NUMBER_STATIONS):
        newgroup_command(chat_id, str(i))

    newgroup_command(chat_id, '1000')

    # Check bot calls and temporal cache
    expected_text = STRINGS['es']['newgroup_number_stations_limit'].format(MAX_NUMBER_STATIONS)
    get_bot().send_message.assert_called_with(chat_id=chat_id, text=expected_text)
    assert GROUPS_CACHE[chat_id]['status'] == GROUP_STATUS_NEWGROUP_STATIONS
    assert GROUPS_CACHE[chat_id]['name'] == 'casa'
    assert GROUPS_CACHE[chat_id]['stations'] == [i for i in range(MAX_NUMBER_STATIONS)]

    newgroup_command(chat_id, 'end')

    # Check bot and database calls
    get_bot().send_message.assert_called_with(chat_id=chat_id, text=STRINGS['es']['newgroup_created'].format('casa'))
    DatabaseConnection().delete_group.assert_called_with(chat_id=chat_id, name='casa')
    DatabaseConnection().create_group.assert_called_with(chat_id=chat_id, name='casa',
                                                         stations=[i for i in range(MAX_NUMBER_STATIONS)])
    commands_get_bot().send_message.assert_called_once()
    assert chat_id not in GROUPS_CACHE
Example #4
0
def test_newgroup_command_wrong_station(get_bot, DatabaseConnection, Bicing, commands_get_bot):
    get_bot.return_value = mock.MagicMock()
    DatabaseConnection.return_value = mock.MagicMock()
    DatabaseConnection().get_groups_names.return_value = []
    Bicing.return_value = mock.MagicMock()
    commands_get_bot.return_value = mock.MagicMock()
    del_group_status(chat_id)

    newgroup_command(chat_id, 'newgroup')
    newgroup_command(chat_id, 'casa')
    newgroup_command(chat_id, '1')

    newgroup_command(chat_id, 'not a number')

    # Check bot calls and temporal cache
    get_bot().send_message.assert_called_with(chat_id=chat_id, text=STRINGS['es']['newgroup_unknown_command'])
    assert GROUPS_CACHE[chat_id]['status'] == GROUP_STATUS_NEWGROUP_STATIONS
    assert GROUPS_CACHE[chat_id]['name'] == 'casa'
    assert GROUPS_CACHE[chat_id]['stations'] == [1]

    newgroup_command(chat_id, 'end')

    # Check bot and database calls
    get_bot().send_message.assert_called_with(chat_id=chat_id, text=STRINGS['es']['newgroup_created'].format('casa'))
    DatabaseConnection().delete_group.assert_called_with(chat_id=chat_id, name='casa')
    DatabaseConnection().create_group.assert_called_with(chat_id=chat_id, name='casa', stations=[1])
    commands_get_bot().send_message.assert_called_once()
    assert chat_id not in GROUPS_CACHE
Example #5
0
def test_newgroup_command_existing_name_overwrite(get_bot, DatabaseConnection, Bicing, commands_get_bot):
    get_bot.return_value = mock.MagicMock()
    DatabaseConnection.return_value = mock.MagicMock()
    DatabaseConnection().get_groups_names.return_value = ['casa']
    DatabaseConnection().get_group.return_value = {'chat_id': 333, 'name': 'casa', 'stations': [1, 2, 3]}
    Bicing.return_value = mock.MagicMock()
    commands_get_bot.return_value = mock.MagicMock()
    del_group_status(chat_id)

    newgroup_command(chat_id, 'newgroup')

    # Check warning message is sent
    newgroup_command(chat_id, 'casa')
    message = STRINGS['es']['newgroup_name_already_existing'].format(STRINGS['es']['newgroup_stations'].lower())
    get_bot().send_message.assert_called_with(chat_id=chat_id, text=message)
    assert GROUPS_CACHE[chat_id]['status'] == GROUP_STATUS_NEWGROUP_STATIONS

    newgroup_command(chat_id, '1')
    newgroup_command(chat_id, '2')
    newgroup_command(chat_id, 'end')

    # Check bot and database calls
    get_bot().send_message.assert_called_with(chat_id=chat_id, text=STRINGS['es']['newgroup_created'].format('casa'))
    DatabaseConnection().delete_group.assert_called_with(chat_id=chat_id, name='casa')
    DatabaseConnection().create_group.assert_called_with(chat_id=chat_id, name='casa', stations=[1, 2])
    commands_get_bot().send_message.assert_called_once()
    assert chat_id not in GROUPS_CACHE
Example #6
0
def test_newgroup_command_number_groups_limit(get_bot, DatabaseConnection):
    get_bot.return_value = mock.MagicMock()
    DatabaseConnection.return_value = mock.MagicMock()
    DatabaseConnection().get_groups_names.return_value = ['casa{}'.format(i) for i in range(MAX_NUMBER_GROUPS)]
    del_group_status(chat_id)

    newgroup_command(chat_id, 'newgroup')

    expected_text = STRINGS['es']['newgroup_number_groups_limit'].format(MAX_NUMBER_GROUPS)
    get_bot().send_message.assert_called_with(chat_id=chat_id, text=expected_text)
    assert chat_id not in GROUPS_CACHE
Example #7
0
def test_newgroup_command_cancel(get_bot, DatabaseConnection):
    get_bot.return_value = mock.MagicMock()
    del_group_status(chat_id)

    newgroup_command(chat_id, 'newgroup')

    newgroup_command(chat_id, 'end')

    # Check bot and database calls
    get_bot().send_message.assert_called_with(chat_id=chat_id, text=STRINGS['es']['newgroup_not_created'])
    DatabaseConnection().create_group.assert_not_called()
    assert chat_id not in GROUPS_CACHE
Example #8
0
def test_newgroup_command_cancel(get_bot, DatabaseConnection):
    get_bot.return_value = mock.MagicMock()
    del_group_status(chat_id)

    newgroup_command(chat_id, 'newgroup')

    newgroup_command(chat_id, 'end')

    # Check bot and database calls
    get_bot().send_message.assert_called_with(
        chat_id=chat_id, text=STRINGS['es']['newgroup_not_created'])
    DatabaseConnection().create_group.assert_not_called()
    assert chat_id not in GROUPS_CACHE
Example #9
0
def test_newgroup_command_bad_formatted_name(get_bot, DatabaseConnection):
    get_bot.return_value = mock.MagicMock()
    DatabaseConnection.return_value = mock.MagicMock()
    DatabaseConnection().get_groups_names.return_value = []
    del_group_status(chat_id)

    newgroup_command(chat_id, 'newgroup')

    # Check name is number
    newgroup_command(chat_id, '1')
    get_bot().send_message.assert_called_with(
        chat_id=chat_id, text=STRINGS['es']['newgroup_name_format_error'])
    assert GROUPS_CACHE[chat_id]['status'] == GROUP_STATUS_NEWGROUP_NAME

    # Check name starts with /
    newgroup_command(chat_id, '/casa')
    get_bot().send_message.assert_called_with(
        chat_id=chat_id, text=STRINGS['es']['newgroup_name_format_error'])
    assert GROUPS_CACHE[chat_id]['status'] == GROUP_STATUS_NEWGROUP_NAME

    # Check name is a command
    newgroup_command(chat_id, 'settings')
    get_bot().send_message.assert_called_with(
        chat_id=chat_id, text=STRINGS['es']['newgroup_name_format_error'])
    assert GROUPS_CACHE[chat_id]['status'] == GROUP_STATUS_NEWGROUP_NAME
Example #10
0
def test_newgroup_command_number_groups_limit(get_bot, DatabaseConnection):
    get_bot.return_value = mock.MagicMock()
    DatabaseConnection.return_value = mock.MagicMock()
    DatabaseConnection().get_groups_names.return_value = [
        'casa{}'.format(i) for i in range(MAX_NUMBER_GROUPS)
    ]
    del_group_status(chat_id)

    newgroup_command(chat_id, 'newgroup')

    expected_text = STRINGS['es']['newgroup_number_groups_limit'].format(
        MAX_NUMBER_GROUPS)
    get_bot().send_message.assert_called_with(chat_id=chat_id,
                                              text=expected_text)
    assert chat_id not in GROUPS_CACHE
Example #11
0
def test_newgroup_command_number_stations_limit(get_bot, DatabaseConnection,
                                                Bicing, commands_get_bot):
    get_bot.return_value = mock.MagicMock()
    DatabaseConnection.return_value = mock.MagicMock()
    DatabaseConnection().get_groups_names.return_value = []
    Bicing.return_value = mock.MagicMock()
    commands_get_bot.return_value = mock.MagicMock()
    del_group_status(chat_id)

    newgroup_command(chat_id, 'newgroup')
    newgroup_command(chat_id, 'casa')
    for i in range(MAX_NUMBER_STATIONS):
        newgroup_command(chat_id, str(i))

    newgroup_command(chat_id, '1000')

    # Check bot calls and temporal cache
    expected_text = STRINGS['es']['newgroup_number_stations_limit'].format(
        MAX_NUMBER_STATIONS)
    get_bot().send_message.assert_called_with(chat_id=chat_id,
                                              text=expected_text)
    assert GROUPS_CACHE[chat_id]['status'] == GROUP_STATUS_NEWGROUP_STATIONS
    assert GROUPS_CACHE[chat_id]['name'] == 'casa'
    assert GROUPS_CACHE[chat_id]['stations'] == [
        i for i in range(MAX_NUMBER_STATIONS)
    ]

    newgroup_command(chat_id, 'end')

    # Check bot and database calls
    get_bot().send_message.assert_called_with(
        chat_id=chat_id, text=STRINGS['es']['newgroup_created'].format('casa'))
    DatabaseConnection().delete_group.assert_called_with(chat_id=chat_id,
                                                         name='casa')
    DatabaseConnection().create_group.assert_called_with(
        chat_id=chat_id,
        name='casa',
        stations=[i for i in range(MAX_NUMBER_STATIONS)])
    commands_get_bot().send_message.assert_called_once()
    assert chat_id not in GROUPS_CACHE
Example #12
0
def test_newgroup_command_existing_name_cancel(get_bot, DatabaseConnection):
    get_bot.return_value = mock.MagicMock()
    DatabaseConnection.return_value = mock.MagicMock()
    DatabaseConnection().get_groups_names.return_value = ['casa']
    DatabaseConnection().get_group.return_value = {
        'chat_id': 333,
        'name': 'casa',
        'stations': [1, 2, 3]
    }
    del_group_status(chat_id)

    newgroup_command(chat_id, 'newgroup')

    # Check warning message is sent
    newgroup_command(chat_id, 'casa')
    message = STRINGS['es']['newgroup_name_already_existing'].format(
        STRINGS['es']['newgroup_stations'].lower())
    get_bot().send_message.assert_called_with(chat_id=chat_id, text=message)
    assert GROUPS_CACHE[chat_id]['status'] == GROUP_STATUS_NEWGROUP_STATIONS

    newgroup_command(chat_id, 'end')

    # Check bot and database calls
    get_bot().send_message.assert_called_with(
        chat_id=chat_id,
        text=STRINGS['es']['newgroup_not_overwrite'].format('casa'))
    DatabaseConnection().create_group.assert_not_called()
    assert chat_id not in GROUPS_CACHE
Example #13
0
def test_newgroup_command_bad_formatted_name(get_bot, DatabaseConnection):
    get_bot.return_value = mock.MagicMock()
    DatabaseConnection.return_value = mock.MagicMock()
    DatabaseConnection().get_groups_names.return_value = []
    del_group_status(chat_id)

    newgroup_command(chat_id, 'newgroup')

    # Check name is number
    newgroup_command(chat_id, '1')
    get_bot().send_message.assert_called_with(chat_id=chat_id, text=STRINGS['es']['newgroup_name_format_error'])
    assert GROUPS_CACHE[chat_id]['status'] == GROUP_STATUS_NEWGROUP_NAME

    # Check name starts with /
    newgroup_command(chat_id, '/casa')
    get_bot().send_message.assert_called_with(chat_id=chat_id, text=STRINGS['es']['newgroup_name_format_error'])
    assert GROUPS_CACHE[chat_id]['status'] == GROUP_STATUS_NEWGROUP_NAME

    # Check name is a command
    newgroup_command(chat_id, 'settings')
    get_bot().send_message.assert_called_with(chat_id=chat_id, text=STRINGS['es']['newgroup_name_format_error'])
    assert GROUPS_CACHE[chat_id]['status'] == GROUP_STATUS_NEWGROUP_NAME