Exemple #1
0
 def setUp(self):
     # For use within the tests we nee some stuff. Starting with a Mockbot
     self.bot = Mockbot()
     # Some generators for users and chats
     self.cg = ChatGenerator()
     # And a Messagegenerator, CallbackQueryGenerator and updater (for use with the bot.)
     self.mg = MessageGenerator(self.bot)
     self.cqg = CallbackQueryGenerator(self.bot)
     self.updater = Updater(bot=self.bot)
Exemple #2
0
    def test_required_auto_set(self):
        u = self.cqg.get_callback_query(inline_message_id=True,
                                        data="test-data")
        self.assertIsInstance(u.callback_query.from_user, User)
        self.assertIsInstance(u.callback_query.chat_instance, str)
        bot = Mockbot(username="******")
        cqg2 = CallbackQueryGenerator(bot=bot)
        self.assertEqual(bot.username, cqg2.bot.username)

        with self.assertRaises(BadBotException):
            cqg3 = CallbackQueryGenerator(bot="bot")
Exemple #3
0
 def setUpClass(self):
     # For use within the tests we nee some stuff. Starting with a Mockbot
     self.bot = Mockbot()
     self.bot.request = telegram.utils.request.Request()
     # Some generators for users and chats
     self.ug = UserGenerator()
     self.cg = ChatGenerator()
     # And a Messagegenerator,CallbackQueryGenerator and updater (for use with the bot.)
     self.mg = MessageGenerator(self.bot)
     self.cqg = CallbackQueryGenerator(self.bot)
     self.updater = Updater(bot=self.bot)
     init_db("test.sqlite3")
     install_commands()
     add_all_handlers(self.updater.dispatcher)
     with db_session:
         for listable_type in Listable.__subclasses__():
             for i in range(6):
                 listable_type(name=listable_type._discriminator_ + " " +
                               str(i),
                               url="https://url" + str(i) + ".com",
                               validated=True)
     self.updater.start_polling()
Exemple #4
0
 def setUp(self):
     self.cqg = CallbackQueryGenerator()
Exemple #5
0
class TestMessageGeneratorChannelPost(unittest.TestCase):
    def setUp(self):
        self.cqg = CallbackQueryGenerator()

    def test_invalid_calls(self):
        with self.assertRaisesRegexp(BadCallbackQueryException,
                                     "message and inline_message_id"):
            self.cqg.get_callback_query()
        with self.assertRaisesRegexp(BadCallbackQueryException,
                                     "message and inline_message_id"):
            self.cqg.get_callback_query(message=True, inline_message_id=True)
        with self.assertRaisesRegexp(BadCallbackQueryException,
                                     "data and game_short_name"):
            self.cqg.get_callback_query(message=True)
        with self.assertRaisesRegexp(BadCallbackQueryException,
                                     "data and game_short_name"):
            self.cqg.get_callback_query(message=True,
                                        data="test-data",
                                        game_short_name="mygame")

    def test_required_auto_set(self):
        u = self.cqg.get_callback_query(inline_message_id=True,
                                        data="test-data")
        self.assertIsInstance(u.callback_query.from_user, User)
        self.assertIsInstance(u.callback_query.chat_instance, str)
        bot = Mockbot(username="******")
        cqg2 = CallbackQueryGenerator(bot=bot)
        self.assertEqual(bot.username, cqg2.bot.username)

        with self.assertRaises(BadBotException):
            cqg3 = CallbackQueryGenerator(bot="bot")

    def test_message(self):
        mg = MessageGenerator()
        message = mg.get_message().message
        u = self.cqg.get_callback_query(message=message, data="test-data")
        self.assertIsInstance(u, Update)
        self.assertIsInstance(u.callback_query, CallbackQuery)
        self.assertEqual(u.callback_query.message.message_id,
                         message.message_id)

        u = self.cqg.get_callback_query(message=True, data="test-data")
        self.assertIsInstance(u.callback_query.message, Message)
        self.assertEqual(u.callback_query.message.from_user.username,
                         self.cqg.bot.username)

        with self.assertRaises(BadMessageException):
            self.cqg.get_callback_query(message="message", data="test-data")

    def test_inline_message_id(self):
        u = self.cqg.get_callback_query(inline_message_id="myidilike",
                                        data="test-data")
        self.assertEqual(u.callback_query.inline_message_id, "myidilike")
        u = self.cqg.get_callback_query(inline_message_id=True,
                                        data="test-data")
        self.assertIsInstance(u.callback_query.inline_message_id, str)

        with self.assertRaisesRegexp(BadCallbackQueryException,
                                     "string or True"):
            self.cqg.get_callback_query(inline_message_id=3.98,
                                        data="test-data")

    def test_user(self):
        ug = UserGenerator()
        user = ug.get_user()
        u = self.cqg.get_callback_query(user=user,
                                        message=True,
                                        data="test-data")
        self.assertEqual(user.id, u.callback_query.from_user.id)
        self.assertNotEqual(user.id, u.callback_query.message.from_user.id)

        with self.assertRaises(BadUserException):
            u = self.cqg.get_callback_query(user="******",
                                            inline_message_id=True,
                                            data="test-data")
Exemple #6
0
class TestInlineKeyboard(unittest.TestCase):
    def setUp(self):
        # For use within the tests we nee some stuff. Starting with a Mockbot
        self.bot = Mockbot()
        # Some generators for users and chats
        self.cg = ChatGenerator()
        # And a Messagegenerator, CallbackQueryGenerator and updater (for use with the bot.)
        self.mg = MessageGenerator(self.bot)
        self.cqg = CallbackQueryGenerator(self.bot)
        self.updater = Updater(bot=self.bot)

    def test_callback(self):
        # first insert the callbackhandler, register it and start polling
        def button(bot, update):
            query = update.callback_query

            bot.editMessageText(text="Selected option: %s" % query.data,
                                chat_id=query.message.chat_id,
                                message_id=query.message.message_id)

        dp = self.updater.dispatcher
        dp.add_handler(CallbackQueryHandler(button))
        self.updater.start_polling()

        # the start callback in this example generates a message that will be edited, so let's mimick that message
        # for future reference
        keyboard = [[
            InlineKeyboardButton("Option 1", callback_data='1'),
            InlineKeyboardButton("Option 2", callback_data='2')
        ], [InlineKeyboardButton("Option 3", callback_data='3')]]

        reply_markup = InlineKeyboardMarkup(keyboard)
        chat = self.cg.get_chat()
        start_message = self.bot.sendMessage(chat_id=chat.id,
                                             text='Please choose:',
                                             reply_markup=reply_markup)

        # now let's create some callback query's to send
        u1 = self.cqg.get_callback_query(message=start_message, data="1")
        u2 = self.cqg.get_callback_query(message=start_message, data="2")
        u3 = self.cqg.get_callback_query(message=start_message, data="3")

        # And test them one by one
        self.bot.insertUpdate(u1)
        data = self.bot.sent_messages[-1]
        self.assertEqual(data['text'], "Selected option: 1")
        self.assertEqual(data['chat_id'], start_message.chat.id)
        self.assertEqual(data['message_id'], start_message.message_id)
        self.bot.insertUpdate(u2)
        data = self.bot.sent_messages[-1]
        self.assertEqual(data['text'], "Selected option: 2")
        self.assertEqual(data['chat_id'], start_message.chat.id)
        self.assertEqual(data['message_id'], start_message.message_id)
        self.bot.insertUpdate(u3)
        data = self.bot.sent_messages[-1]
        self.assertEqual(data['text'], "Selected option: 3")
        self.assertEqual(data['chat_id'], start_message.chat.id)
        self.assertEqual(data['message_id'], start_message.message_id)

        # stop polling
        self.updater.stop()
Exemple #7
0
class TestDCUBABot(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        # For use within the tests we nee some stuff. Starting with a Mockbot
        self.bot = Mockbot()
        self.bot.request = telegram.utils.request.Request()
        # Some generators for users and chats
        self.ug = UserGenerator()
        self.cg = ChatGenerator()
        # And a Messagegenerator,CallbackQueryGenerator and updater (for use with the bot.)
        self.mg = MessageGenerator(self.bot)
        self.cqg = CallbackQueryGenerator(self.bot)
        self.updater = Updater(bot=self.bot)
        init_db("test.sqlite3")
        install_commands()
        add_all_handlers(self.updater.dispatcher)
        with db_session:
            for listable_type in Listable.__subclasses__():
                for i in range(6):
                    listable_type(name=listable_type._discriminator_ + " " +
                                  str(i),
                                  url="https://url" + str(i) + ".com",
                                  validated=True)
        self.updater.start_polling()

    @classmethod
    def tearDownClass(self):
        self.updater.stop()
        os.remove("test.sqlite3")

    @classmethod
    def sendCommand(self, command, chat_id=None):
        user = self.ug.get_user(first_name="Test", last_name="The Bot")
        if chat_id:
            chat = self.cg.get_chat(chat_id)
        else:
            chat = self.cg.get_chat(user=user)
        update = self.mg.get_message(user=user, chat=chat, text=command)
        self.bot.insertUpdate(update)
        return user, chat

    # TODO: Cleanup this
    def assert_bot_response(self,
                            message_text,
                            response_text,
                            chat_id=None,
                            random=False):
        if isinstance(response_text, str):
            response_text = [response_text]

        sent_messages = self.bot.sent_messages
        sent_messages_before = len(sent_messages)
        self.sendCommand(message_text, chat_id=chat_id)
        response_sent_messages = len(sent_messages) - sent_messages_before
        expected_sent_messages = 0 if not response_text else\
            (1 if random else len(response_text))
        self.assertEqual(response_sent_messages, expected_sent_messages)

        for i in range(response_sent_messages):
            sent = sent_messages[sent_messages_before + i]
            self.assertEqual(sent['method'], "sendMessage")
            if not random:
                self.assertEqual(sent['text'], response_text[i])
            else:
                self.assertIn(sent['text'], response_text)

    def get_keyboard(self, message):
        return json.loads(message['reply_markup'])['inline_keyboard']

    def button_in_list(self, name, url, list_command):
        self.sendCommand("/" + list_command)
        inline_keyboard = self.get_keyboard(self.bot.sent_messages[-1])
        for row in inline_keyboard:
            for button in row:
                if button["text"] == name and button["url"] == url:
                    return True
        return False

    def test_help(self):
        with db_session:
            for c in Command.select():
                c.description = ""
            Command(name="comandoSinDescripcion1")
            Command(name="comandoConDescripcion1", description="Descripción 1")
            Command(name="comandoSinDescripcion2")
            Command(name="comandoConDescripcion2", description="Descripción 2")
            Command(name="comandoSinDescripcion3")
            Command(name="comandoConDescripcion3", description="Descripción 3")

        self.assert_bot_response("/help",
                                 ("/comandoConDescripcion1 - Descripción 1\n"
                                  "/comandoConDescripcion2 - Descripción 2\n"
                                  "/comandoConDescripcion3 - Descripción 3\n"))

    def test_start(self):
        self.assert_bot_response(
            "/start",
            "Hola, ¿qué tal? ¡Mandame /help si no sabés qué puedo hacer!")

    def test_estasvivo(self):
        self.assert_bot_response("/estasvivo", "Sí, estoy vivo.")

    def test_rozendioanalisis(self):
        self.assert_bot_response("/rozendioanalisis",
                                 "¡Sí, Rozen ya dio el final de análisis!")

    # TODO: Rename
    def list_test(self, command, listable_type):
        self.assert_bot_response(command, "Grupos: ")

        # Assertions on keyboard
        inline_keyboard = self.get_keyboard(self.bot.sent_messages[-1])
        self.assertEqual(len(inline_keyboard), 2)  # Number of rows
        for i in range(2):
            row = inline_keyboard[i]
            self.assertEqual((len(row)), 3)  # Number of columns
            for j in range(3):
                button = row[j]
                button_number = i * 3 + j
                self.assertEqual(
                    button['text'],
                    listable_type._discriminator_ + " " + str(button_number))
                self.assertEqual(button['url'],
                                 "https://url" + str(button_number) + ".com")
                self.assertEqual(button['callback_data'], button['url'])

    def test_listar(self):
        self.list_test("/listar", Obligatoria)

    def test_listaroptativa(self):
        self.list_test("/listaroptativa", Optativa)

    def test_listarotro(self):
        self.list_test("/listarotro", Otro)

    def suggestion_test(self, command, list_command, listable_type):
        name = "Sugerido"
        url = "sugerido.com"
        error_message = "Hiciste algo mal, la idea es que pongas:\n" +\
                        command + " <nombre>|<link>"

        # Invalid command usages
        self.assert_bot_response(command, error_message)
        self.assert_bot_response(command + " " + name, error_message)
        self.assert_bot_response(command + " " + name + "|", error_message)
        self.assert_bot_response(command + " |" + url, error_message)
        self.assert_bot_response(command + " |", error_message)
        self.assert_bot_response(command + " " + name + "|" + url + "|sobra",
                                 error_message)

        # Make a group suggestion to accept
        self.assert_bot_response(command + " " + name + "|" + url, [
            listable_type.__name__ + ": " + name + "\n" + url,
            "OK, se lo mando a Rozen."
        ])

        # Assertions on keyboard
        inline_keyboard = self.get_keyboard(self.bot.sent_messages[-2])
        self.assertEqual(len(inline_keyboard), 1)  # Number of rows
        row = inline_keyboard[0]
        self.assertEqual(len(row), 2)  # Number of columns
        self.assertEqual(row[0]["text"], "Aceptar")
        self.assertEqual(row[1]["text"], "Rechazar")

        # The suggested group shouldn't be listed
        self.assertFalse(self.button_in_list(name, url, list_command))

        # Pressing the "Aceptar" button makes the group listable
        u = self.cqg.get_callback_query(message=self.mg.get_message().message,
                                        data=row[0]["callback_data"])
        self.bot.insertUpdate(u)
        self.assertTrue(self.button_in_list(name, url, list_command))
        with db_session:
            delete(l for l in Listable if l.name == name)

        # Make a group suggestion to reject
        self.sendCommand(command + " " + name + "|" + url)
        inline_keyboard = self.get_keyboard(self.bot.sent_messages[-2])
        row = inline_keyboard[0]

        # Pressing the "Rechazar" button doesn't make the group listable
        u = self.cqg.get_callback_query(message=self.mg.get_message().message,
                                        data=row[1]["callback_data"])
        self.bot.insertUpdate(u)
        self.assertFalse(self.button_in_list(name, url, list_command))

        # The database is clean of rejected suggestions
        with db_session:
            self.assertEqual(count(l for l in Listable if l.name == name), 0)

    def test_sugerirgrupo(self):
        self.suggestion_test("/sugerirgrupo", "listar", Obligatoria)

    def test_sugeriroptativa(self):
        self.suggestion_test("/sugeriroptativa", "listaroptativa", Optativa)

    def test_sugerirotro(self):
        self.suggestion_test("/sugerirotro", "listarotro", Otro)

    def test_logger(self):
        with self.assertLogs("DCUBABOT", level='INFO') as cm:
            user, _ = self.sendCommand("/listar")
            first_message = 'INFO:DCUBABOT:' + str(user.id) + ': /listar'
            user, _ = self.sendCommand("/estasvivo")
            second_message = 'INFO:DCUBABOT:' + str(user.id) + ': /estasvivo'
            self.assertEqual(cm.output, [first_message, second_message])

    def test_cubawiki(self):
        cubawiki_url = "https://www.cubawiki.com.ar/index.php/Segundo_Parcial_del_10/12/18"
        positive_chat_id = -123456
        negative_chat_id_no_cubawiki = -654321
        negative_chat_id_no_entry = -123321
        with db_session:
            Obligatoria(name="Cubawiki",
                        url="test.com",
                        chat_id=positive_chat_id,
                        cubawiki_url=cubawiki_url)
            Obligatoria(name="Cubawiki",
                        url="test.com",
                        chat_id=negative_chat_id_no_cubawiki)

        # Positive test case
        self.assert_bot_response("/cubawiki",
                                 cubawiki_url,
                                 chat_id=positive_chat_id)

        # Negative test cases
        self.assert_bot_response("/cubawiki",
                                 None,
                                 chat_id=negative_chat_id_no_cubawiki)
        self.assert_bot_response("/cubawiki",
                                 None,
                                 chat_id=negative_chat_id_no_entry)

        with db_session:
            delete(o for o in Obligatoria if o.name == "Cubawiki")

    def test_felizdia(self):
        today = datetime.datetime(2019, 1, 1)
        self.assertEqual(felizdia_text(today), "Feliz 1 de Enero")
        today = datetime.datetime(2019, 2, 1)
        self.assertEqual(felizdia_text(today), "Feliz 1 de Febrero")
        today = datetime.datetime(2019, 3, 1)
        self.assertEqual(felizdia_text(today), "Feliz 1 de Marzo")
        today = datetime.datetime(2019, 4, 4)
        self.assertEqual(felizdia_text(today), "Feliz 4 de Abril")
        today = datetime.datetime(2019, 5, 21)
        self.assertEqual(felizdia_text(today), "Feliz 21 de Mayo")

    # TODO: Test randomness?
    def test_noitip(self):
        noitips = ["me siento boludeadisimo", "Not this shit again", "noitip"]
        with db_session:
            for phrase in noitips:
                Noitip(text=phrase)

        self.assert_bot_response("/noitip", noitips, random=True)

    def test_asm(self):
        with db_session:
            AsmInstruction(mnemonic="AAD",
                           summary="ASCII Adjust AX Before Division",
                           url="https://www.felixcloutier.com/x86/aad")
            AsmInstruction(mnemonic="ADD",
                           summary="Add",
                           url="https://www.felixcloutier.com/x86/add")
            AsmInstruction(
                mnemonic="ADDPD",
                summary="Add Packed Double-Precision Floating-Point Values",
                url="https://www.felixcloutier.com/x86/addpd")
            AsmInstruction(mnemonic="MOV",
                           summary="Move to/from Control Registers",
                           url="http://www.felixcloutier.com/x86/MOV-1.html")
            AsmInstruction(mnemonic="MOV",
                           summary="Move to/from Debug Registers",
                           url="http://www.felixcloutier.com/x86/MOV-2.html")
            AsmInstruction(
                mnemonic="INT n",
                summary="Call to Interrupt Procedure",
                url="http://www.felixcloutier.com/x86/INT%20n:INTO:INT%203.html"
            )

        not_found = "No pude encontrar esa instrucción."
        possibles = not_found + "\nQuizás quisiste decir:"
        add_info = ("[ADD] Descripción: Add.\n"
                    "Más info: https://www.felixcloutier.com/x86/add")
        addpd_info = (
            "[ADDPD] Descripción: Add Packed Double-Precision Floating-Point Values.\n"
            "Más info: https://www.felixcloutier.com/x86/addpd")
        mov1_info = ("[MOV] Descripción: Move to/from Control Registers.\n"
                     "Más info: http://www.felixcloutier.com/x86/MOV-1.html")
        mov2_info = ("[MOV] Descripción: Move to/from Debug Registers.\n"
                     "Más info: http://www.felixcloutier.com/x86/MOV-2.html")
        intn_info = (
            "[INT n] Descripción: Call to Interrupt Procedure.\n"
            "Más info: http://www.felixcloutier.com/x86/INT%20n:INTO:INT%203.html"
        )

        self.assert_bot_response("/asm", "No me pasaste ninguna instrucción.")
        self.assert_bot_response("/asm add", add_info)
        self.assert_bot_response("/asm ADDPD", addpd_info)
        self.assert_bot_response("/asm a", not_found)
        self.assert_bot_response("/asm Adp", possibles + "\n" + add_info)
        self.assert_bot_response("/asm ADDPS", possibles + "\n" + addpd_info)
        self.assert_bot_response(
            "/asm addP", possibles + "\n" + add_info + "\n" + addpd_info)
        self.assert_bot_response("/asm MOV", mov1_info + "\n" + mov2_info)
        self.assert_bot_response("/asm INT n", intn_info)