def test_start_bot_with_invalid_username(self):
     with self.assertRaises(ValueError) as e:
         procedures.start_bot(username=1234)
         self.assertEqual(
             str(e.exception),
             'String value expected for username in start bot '
             'request.')
 def test_start_bot_with_invalid_botid(self):
     with self.assertRaises(ValueError) as e:
         procedures.start_bot(botid='abc')
         self.assertEqual(
             str(e.exception),
             'Integer value expected for botid in start bot '
             'request.')
 def test_start_livebot_with_bad_token(self):
     bot = MyBot(token='dummy-token').save()
     self.assertIsNotNone(bot)
     with self.assertRaises(ValueError) as e:
         procedures.start_bot(botid=bot.bot_id)
         self.assertEqual(
             str(e.exception),
             'Bot:{username} registered with bad token can not '
             'be started.'.format(username=bot.username))
 def test_stopbot_previously_running_now_stopped_live_bot(self):
     bot = MyBot(token=CONSTANTS.LIVE_BOTS.get(1)).save()
     self.assertIsNotNone(bot)
     self.assertEqual(procedures.start_bot(botid=bot.bot_id), 1)
     self.assertEqual(procedures.stop_bot(botid=bot.bot_id), 1)
     bot = MyBot.objects(token=bot.token).first()
     self.assertFalse(bot.state)
     self.assertEqual(procedures.stop_bot(botid=bot.bot_id), -2)
    def test_start_livebot_with_valid_botid(self):
        bot = MyBot(token=CONSTANTS.LIVE_BOTS.get(1)).save()
        self.assertIsNotNone(bot)
        self.assertEqual(procedures.start_bot(botid=bot.bot_id), 1)
        bot = MyBot.objects(bot_id=bot.bot_id).first()
        self.assertTrue(bot.state)

        # Otherwise unittests doesn't end.
        self.assertEqual(procedures.stop_bot(botid=bot.bot_id), 1)
        bot = MyBot.objects(bot_id=bot.bot_id).first()
        self.assertFalse(bot.state)
    def test_stopbot_valid_running_bot_using_valid_botid(self):
        bot = Bot(token=CONSTANTS.LIVE_BOTS.get(1)).get_me()
        mybot = MyBot(token=CONSTANTS.LIVE_BOTS.get(1),
                      bot_id=bot.id,
                      username=bot.username,
                      first_name=bot.first_name,
                      last_name=bot.last_name).save()
        self.assertIsNotNone(mybot)
        self.assertEqual(procedures.start_bot(botid=mybot.bot_id), 1)

        self.assertEqual(procedures.stop_bot(botid=mybot.bot_id), 1)
        mybot = MyBot.objects(bot_id=mybot.bot_id).first()
        self.assertFalse(mybot.state)
    def test_start_livebot_with_valid_username(self):
        bot = Bot(token=CONSTANTS.LIVE_BOTS.get(1)).get_me()
        mybot = MyBot(token=CONSTANTS.LIVE_BOTS.get(1),
                      bot_id=bot.id,
                      username=bot.username,
                      first_name=bot.first_name,
                      last_name=bot.last_name).save()
        self.assertIsNotNone(mybot)
        self.assertEqual(procedures.start_bot(username=str(mybot.username)), 1)
        mybot = MyBot.objects(bot_id=mybot.bot_id).first()
        self.assertTrue(mybot.state)

        # Otherwise unittests doesn't end.
        self.assertEqual(procedures.stop_bot(botid=mybot.bot_id), 1)
        mybot = MyBot.objects(bot_id=mybot.bot_id).first()
        self.assertFalse(mybot.state)
 def test_start_bot_for_test_bot(self):
     bot = MyBot(token='dummy-token', test_bot=True).save()
     self.assertIsNotNone(bot)
     self.assertEqual(procedures.start_bot(botid=bot.bot_id), -2)
 def test_start_bot_for_non_existing_botid_username(self):
     self.assertEqual(
         procedures.start_bot(botid=1234, username='******'), -1)
 def test_start_bot_for_non_existing_botid(self):
     self.assertEqual(procedures.start_bot(botid=12345), -1)
 def test_start_bot_with_no_inputs(self):
     with self.assertRaises(ValueError) as e:
         procedures.start_bot()
         self.assertEqual(
             str(e.exception), 'No botid/username provided with start bot '
             'request.')
Beispiel #12
0
def edit_bot(bot_choice=0):
    """
    This function renders the page for Editing the bot. Editing the bot
    allows to enable/disable polling of the bot.
    :param bot_choice: Initial choice for selected bot.
    :return: .../editbot
    """
    form = EditBot(choose_bot=bot_choice, toggle='Toggle (Enable/Disable)')

    # Populate the initial set of choices for Bot DropDownList.
    form.choose_bot.choices = [(0, 'Select')] + \
                              list(MyBot.objects().values_list(
                                  'username', 'username'))

    if form.validate_on_submit():
        # Get list of bots
        bot = MyBot.objects(username__iexact=form.choose_bot.data).first()
        if bot is None:
            # Redirect to same page because no option selected.
            flash('Please select an option and then press submit.')
            return render_template('edit_bot.html', form=form)
        if bot.test_bot:
            # Redirect to same page because testbot cannot be started.
            web_logger.info('Attempt to start polling for a test bot bot:'
                            '{uname} via web api.'.format(uname=bot.username))
            flash('Testbot Bot:{username} attempt to start polling '
                  'failed.'.format(username=bot.username))
            form.status_field.data = 'Cannot be enabled.'
            return render_template('edit_bot.html', form=form)
        if not bot.state:
            # Bot is not polling currently, start polling.
            status = procedures.start_bot(botid=bot.bot_id,
                                          username=str(bot.username))
            if status == 1:
                web_logger.info('Bot:{uname} successfully started polling via '
                                'web api.'.format(uname=bot.username))
                flash('Bot:{username} successfully started polling.'.format(
                    username=bot.username))
                form.status_field.data = 'Enabled'
            elif status == 0:
                web_logger.info('Bot:{uname} could not start polling via '
                                'web api.'.format(uname=bot.username))
                flash('Failed to enable Bot:{username} for polling'.format(
                    username=bot.username))
                form.status_field.data = 'Failed to enable.'
                form.toggle = 'Enable'
        elif bot.state:
            # Bot is polling currently, stop polling.
            status = procedures.stop_bot(botid=bot.bot_id,
                                         username=str(bot.username))
            if status == 1:
                web_logger.info('Bot:{uname} successfully stopped polling via '
                                'web api.'.format(uname=bot.username))
                flash('Bot:{username} successfully stopped polling.'.format(
                    username=bot.username))
                form.status_field.data = 'Disabled'
            elif status == 0:
                web_logger.info('Bot:{uname} could not stop polling via '
                                'web api.'.format(uname=bot.username))
                flash('Failed to disable Bot:{username} for polling'.format(
                    username=bot.username))
                form.status_field.data = 'Failed to disable.'
        return render_template('edit_bot.html', form=form)
    # Responding to get Request
    return render_template('edit_bot.html', form=form)