Ejemplo n.º 1
0
 def test_add_bot_procedure_with_no_inputs(self):
     with self.assertRaises(ValueError) as e:
         procedures.add_bot()
         self.assertEqual(
             str(e.exception),
             'No/Bad token(String expected) provided to add new'
             ' bot.')
Ejemplo n.º 2
0
 def test_add_livebot_with_invalid_token(self):
     bad_token = 'dummy-token'
     with self.assertRaises(ValueError) as e:
         procedures.add_bot(token=bad_token)
         self.assertEqual(
             str(e.exception), 'Invalid token:{tokn} used for adding live '
             'bot.'.format(tokn=bad_token))
Ejemplo n.º 3
0
def add_bot():
    """
    This function renders the add_bot form where user can add a new bot to
    database by providing token for the bot.
    :return: .../addbot
    """
    form = AddBotForm()
    if form.validate_on_submit():
        bot = MyBot.objects(token=form.token.data).first()
        if bot is not None:  # Duplicate token user for adding bot.
            flash('Another bot Bot:{username} is already registered with '
                  'given token.'.format(username=bot.username))
            # Redirect user to Bot's homepage.
            return redirect(url_for('web_ui.bot_info', botid=bot.bot_id))
        if form.is_test_bot.data:
            # if requested token is for a testbot.
            status = procedures.add_bot(token=form.token.data, testing=True)
            web_logger.info('New testbot bot:{uname} added by web api.'.format(
                uname=status[0]))
            flash('Testbot Bot:{username} successfully added to '
                  'database.'.format(username=status[0]))
            return redirect(
                url_for('web_ui.bot_info',
                        botid=MyBot.objects(
                            username__iexact=status[0]).first().bot_id))
        else:
            try:
                # Add the bot.
                status = procedures.add_bot(token=form.token.data,
                                            testing=False)
                # Redirect to bot info page.
                if status[1]:
                    web_logger.info(
                        'New live bot:{uname} added by web api and '
                        'started polling.'.format(uname=status[0]))
                    flash('New bot:{username} successfully added and '
                          'started polling.'.format(username=status[0]))
                    return redirect(
                        url_for(
                            'web_ui.bot_info',
                            botid=MyBot.objects(
                                username__iexact=status[0]).first().bot_id))
                else:
                    # Redirect to Edit bot page to start polling again.
                    web_logger.info(
                        'New live bot:{uname} added by web api and '
                        'did not start polling.'.format(uname=status[0]))
                    flash('New bot:{username} successfully added but '
                          'did not start polling.'.format(username=status[0]))
                    return redirect(
                        url_for(
                            'web_ui.edit_bot',
                            bot_choice=MyBot.objects(
                                username__iexact=status[0]).first().bot_id))

            except Exception as e:
                web_logger.error(
                    'Error:{msg} during adding new bot.'.format(msg=e.message))
                return render_template('500.html', error_message=e.message)
    return render_template('add_bot.html', form=form)
Ejemplo n.º 4
0
 def test_add_livebot_with_duplicate_token(self):
     live_token = CONSTANTS.LIVE_BOTS.get(1)
     # Add a live bot with valid token.
     MyBot(token=live_token).save()
     self.assertIsNotNone(MyBot.objects(token=live_token).first())
     self.assertEqual(MyBot.objects.count(), 1)
     with self.assertRaises(ValueError) as e:
         procedures.add_bot(token=live_token)
         self.assertEqual(
             str(e.exception),
             'Bot with given token{tokn} is already present in '
             'database.'.format(tokn=live_token))
Ejemplo n.º 5
0
 def test_add_testbot_with_duplicate_token(self):
     bad_token = 'dummy-token'
     # Add a test bot with bad token.
     MyBot(token=bad_token, test_bot=True).save()
     self.assertIsNotNone(MyBot.objects(token=bad_token).first())
     self.assertEqual(MyBot.objects.count(), 1)
     with self.assertRaises(ValueError) as e:
         procedures.add_bot(token=bad_token, testing=True)
         self.assertEqual(
             str(e.exception),
             'Bot with given token{tokn} is already present in '
             'database.'.format(tokn=bad_token))
Ejemplo n.º 6
0
    def test_add_testbot_valid_token(self):
        status = procedures.add_bot(token='dummy_token', testing=True)
        self.assertIsNotNone(status[0])
        self.assertFalse(status[1])
        self.assertTrue('testbot-' in status[0])

        bot = MyBot.objects(username=status[0]).first()
        self.assertIsNotNone(bot)
        self.assertTrue(bot.test_bot)
        self.assertEqual(bot.first_name, 'test')
        self.assertEqual(bot.last_name, 'bot')
Ejemplo n.º 7
0
    def test_add_livebot_with_valid_token(self):
        status = procedures.add_bot(token=CONSTANTS.LIVE_BOTS.get(1))
        # Get bot information from Telegram API.
        bot = Bot(token=CONSTANTS.LIVE_BOTS.get(1)).get_me()
        self.assertIsNotNone(status[0])
        self.assertEqual(status[0], bot.username)
        self.assertTrue(status[1])

        mybot = MyBot.objects(username=status[0]).first()
        self.assertIsNotNone(mybot)
        self.assertFalse(mybot.test_bot)
        self.assertEqual(mybot.first_name, bot.first_name)
        self.assertEqual(mybot.last_name, mybot.last_name)
        self.assertEqual(mybot.username, bot.username)
        # Otherwise unittests doesn't end.
        self.assertEqual(procedures.stop_bot(mybot.bot_id), 1)
Ejemplo n.º 8
0
def load_live_bots():
    """
    This function adds the set of live bots whose tokens are given in
    CONSTANTS file.
    :return:
    """
    from botapp.api_helpers import procedures
    total = 0
    started = 0
    for key in CONSTANTS.LIVE_BOTS.keys():
        try:
            status = procedures.add_bot(CONSTANTS.LIVE_BOTS.get(key),
                                        testing=False)
            total += 1
            started += 1 if status[1] else 0
        except:
            pass

    print '%d live bots added. %d bots started polling.' % (total, started)
Ejemplo n.º 9
0
    def test_start_bot_for_live_bot_valid_botid_wildcard_username(self):
        self.assertTrue(
            procedures.add_bot(token=CONSTANTS.LIVE_BOTS.get(1),
                               testing=False)[1])
        bot = MyBot.objects(token=CONSTANTS.LIVE_BOTS.get(1)).first()
        self.assertIsNotNone(bot)
        # Stop bot from polling.
        self.assertEqual(procedures.stop_bot(botid=bot.bot_id), 1)

        response = self.client.put(url_for('botapi.start_bot',
                                           botid=bot.bot_id,
                                           username='******'),
                                   headers=self.get_api_headers())
        json_response = json.loads(response.data.decode('utf-8'))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            json_response['message'],
            "Polling started for bot {name}".format(name=bot.bot_id))
        bot = MyBot.objects(token=CONSTANTS.LIVE_BOTS.get(1)).first()
        self.assertTrue(bot.state)
        self.assertEqual(procedures.stop_bot(botid=bot.bot_id), 1)
Ejemplo n.º 10
0
    def test_edit_valid_bot(self):
        base_address = 'http://127.0.0.1:5000/web/index'
        # navigate to home page
        self.client.get(base_address)
        # Navigate to filering page
        self.client.find_element_by_link_text('Edit-Bot').click()
        self.assertTrue(
            re.search('Toggle\s+\(Enable/\s+Disable\)\s+Bot',
                      self.client.page_source, re.IGNORECASE))

        # add a test bot
        self.assertTrue(procedures.add_bot(token=CONSTANTS.LIVE_BOTS.get(1)))
        bot = MyBot.objects(token=CONSTANTS.LIVE_BOTS.get(1),
                            test_bot=False).first()
        self.assertIsNotNone(bot)
        self.assertTrue(bot.state)
        # self.client.find_elements_by_name('status_field').send_keys('tomato')
        self.client.find_element_by_name('choose_bot').send_keys(
            bot.username.lower())
        self.client.find_element_by_name('toggle').click()
        # check for success
        self.assertTrue('Bot:{uname} successfully stopped polling'.format(
            uname=bot.username) in self.client.page_source)
        self.assertTrue('Disabled' in self.client.page_source)

        # Enable the bot
        self.client.find_element_by_name('choose_bot').send_keys(bot.username)
        self.client.find_element_by_name('toggle').click()

        # check for success
        self.assertTrue('Bot:{uname} successfully started polling'.format(
            uname=bot.username) in self.client.page_source)
        self.assertTrue('Enabled' in self.client.page_source)

        # Force disable live bot from polling.
        self.assertEqual(procedures.stop_bot(botid=bot.bot_id), 1)
Ejemplo n.º 11
0
 def test_add_bot_with_invalid_token_type(self):
     with self.assertRaises(ValueError) as e:
         procedures.add_bot(token=1)
         self.assertEqual(
             str(e.exception), 'Invalid token:{tokn} used for adding live '
             'bot.'.format(tokn=1))