Exemplo n.º 1
0
    def test_confirm(self, conversation, sticker, callback_query_update):
        conversation.sticker = sticker
        conversation.labels = ["label1", "label2", "label3"]
        conversation.change_state = mock.MagicMock(autospec=True,
                                                   return_value=True)
        update = callback_query_update(
            conversation,
            stickertaggerbot.callback_data.CallbackData.ButtonText.CONFIRM)

        with mock.patch(base_patch_path_labels + ".User.id_exists",
                        mock.MagicMock(return_value=False)):
            database_user = \
                callbacks.labels_callback.models.User.from_telegram_user(
                    conversation.user, conversation.chat.id)

        # TODO: Use a more elegant way to do this
        with mock.patch(base_patch_path_labels + ".User.get",
                        mock.MagicMock(autospec=True,
                                       return_value=database_user)), \
             mock.patch(*get_or_create(conversation)), \
             mock.patch(base_patch_path_labels + ".Association",
                        mock.MagicMock(autospec=True)), \
             mock.patch(base_patch_path_labels + ".database.session.commit",
                        mock.MagicMock(autospec=True)):
            run_handler(callbacks.create_callback_handler, update)

        time.sleep(2)
        clear_all_tables()

        assert_sent_message_once(message.Text.Other.SUCCESS)
    def test_one_label(self, conversation):
        label = "label"

        sticker = telegram_factories.StickerFactory()
        conversation.sticker = sticker

        user = telegram_factories.UserFactory()
        conversation.user = user

        update = telegram_factories.MessageUpdateFactory(
            message__text=label,
            message__from_user=user)

        with mock.patch(*get_or_create(conversation)):
            run_handler(stickertaggerbot.handlers.labels.create_labels_handler,
                        update)

        assert message.Message.set_content.call_count == 2
        assert message.Message.send.call_count == 2

        first_call_contents, second_call_contents = \
            message.Message.set_content.call_args_list
        assert first_call_contents[0][1] == sticker
        assert second_call_contents[0][1] == \
               message.Text.Instruction.CONFIRM.value + label
    def test_empty_labels(self, conversation):
        conversation.labels = None
        update = telegram_factories.MessageUpdateFactory(message__text="")

        with mock.patch(*get_or_create(conversation)):
            run_handler(stickertaggerbot.handlers.labels.create_labels_handler,
                        update)

        assert_sent_message_once(message.Text.Error.LABEL_MISSING)
    def test_no_conversation(self):
        update = telegram_factories.MessageUpdateFactory(
            message__text="message")

        with mock.patch(*get_or_create(None)):
            run_handler(stickertaggerbot.handlers.labels.create_labels_handler,
                        update)

        assert_sent_message_once(message.Text.Error.NOT_STARTED)
Exemplo n.º 5
0
    def test_new_user(self):
        update = telegram_factories.CommandUpdateFactory(
            message__command="start")
        user_id = update.effective_user.id

        run_handler(start.create_command_start_handler, update)

        start.models.User.get.assert_called_once_with(user_id)
        start.models.User.add_to_database.assert_called_once_with()
        assert_sent_message_once(message.Text.Instruction.START)
Exemplo n.º 6
0
    def test_cancel(self, conversation, sticker, callback_query_update):
        conversation.sticker = sticker

        update = callback_query_update(
            conversation,
            stickertaggerbot.callback_data.CallbackData.ButtonText.CANCEL)

        with mock.patch(*get_or_create(conversation)):
            run_handler(callbacks.create_callback_handler, update)

        assert_sent_message_once(message.Text.Instruction.RE_LABEL)
    def test_interrupted_conversation(self, conversation):
        conversation.change_state = mock.Mock(
            autospec=True, side_effect=ValueError(States.IDLE))
        update = telegram_factories.MessageUpdateFactory(
            message__from_user=conversation.user)

        with mock.patch(*get_or_create(conversation)):
            run_handler(stickertaggerbot.handlers.labels.create_labels_handler,
                        update)

        assert_sent_message_once(message.Text.Error.RESTART)
Exemplo n.º 8
0
    def test_new_sticker(self, update_maker, conversation):
        update = update_maker(conversation)

        with mock.patch(*get_or_create(conversation)):
            run_handler(
                stickertaggerbot.handlers.sticker.create_sticker_handler,
                update)

        assert conversation.sticker == update.effective_message.sticker
        conversation.change_state.assert_called_once()
        assert conversation.rollback_state.call_args is None
        assert_sent_message_once(message.Text.Instruction.LABEL)
Exemplo n.º 9
0
    def test_future_timed_out(self, update_maker, conversation):
        update = update_maker(conversation)
        conversation.get_future_result = mock.MagicMock(autospec=True,
                                                        return_value=None)

        with mock.patch(*get_or_create(conversation)):
            run_handler(
                stickertaggerbot.handlers.sticker.create_sticker_handler,
                update)

        conversation.change_state.assert_called_once()
        conversation.rollback_state.assert_called_once()
        assert_sent_message_once(message.Text.Error.UNKNOWN)
Exemplo n.º 10
0
    def test_sticker_exists(self, update_maker, conversation):
        update = update_maker(conversation)

        conversation.get_future_result = mock.MagicMock(return_value=False)

        with mock.patch(*get_or_create(conversation)):
            run_handler(
                stickertaggerbot.handlers.sticker.create_sticker_handler,
                update)

        conversation.change_state.assert_called_once()
        conversation.rollback_state.assert_called_once()
        assert_sent_message_once(message.Text.Error.STICKER_EXISTS)
Exemplo n.º 11
0
    def test_interrupted_conversation(self, update_maker, conversation):
        update = update_maker(conversation)

        conversation.change_state = mock.MagicMock(autospec=True,
                                                   side_effect=ValueError(
                                                       States.IDLE))

        with mock.patch(*get_or_create(conversation)):
            run_handler(
                stickertaggerbot.handlers.sticker.create_sticker_handler,
                update)

        conversation.change_state.assert_called_once()
        assert conversation.rollback_state.call_args is None
        assert_sent_message_once(message.Text.Error.RESTART)
Exemplo n.º 12
0
    def test_single_label(self):
        query_string = "label_0"
        sticker_id = "sticker_0"

        result_id = Sticker.generate_result_id(sticker_id)
        update = telegram_factories.ChosenInlineResultUpdateFactory(
            chosen_inline_result__query=query_string,
            chosen_inline_result__result_id=result_id)

        run_handler(chosen_inline_result.create_chosen_inline_result_handler,
                    update)

        user_id = update.effective_user.id
        chosen_inline_result.models.Association.increment_usage.assert_called_once_with(
            user_id, sticker_id, [query_string])
    def test_no_stickers(self):
        self.set_user_association(1)

        update = telegram_factories.InlineQueryUpdateFactory(
            inline_query__query="label", inline_query__bot=bot)

        result = inline_query_result.Text(
            update.update_id, message.Text.Inline.NO_RESULTS.value,
            message.Text.Inline.CHAT_TO_LABEL.value)

        with mock.patch(base_patch_path + ".inline_query_result.Text",
                        mock.MagicMock(autospec=True, return_value=result)):
            run_handler(inline_query.create_inline_query_handler, update)

        bot.answer_inline_query.assert_called_once_with(update.inline_query.id,
                                                        results=[result],
                                                        cache_time=2,
                                                        is_personal=True)
    def test_one_label(self):
        self.set_user_association(1)

        update = telegram_factories.InlineQueryUpdateFactory(
            inline_query__query="label", inline_query__bot=bot)

        sticker_id = telegram_factories.StickerFactory().file_id
        sticker_result = inline_query_result.Sticker(sticker_id)

        with mock.patch(
                base_patch_path + ".models.Association.get_sticker_ids",
                mock.MagicMock(autospec=True, return_value=[sticker_id])), \
             mock.patch(base_patch_path + ".inline_query_result.Sticker",
                        mock.MagicMock(return_value=sticker_result)):
            run_handler(inline_query.create_inline_query_handler, update)

        bot.answer_inline_query.assert_called_once_with(update.inline_query.id,
                                                        [sticker_result],
                                                        is_personal=True)
Exemplo n.º 15
0
    def test_help(self):
        update = telegram_factories.CommandUpdateFactory(
            message__command="help")

        run_handler(start.create_command_start_handler, update)
        assert_sent_message_once(message.Text.Instruction.HELP)