Exemple #1
0
    def test_bot_conversation2(self, client):

        (_, account) = self.create_user_account()
        bulletin = self.create_bulletin(account, is_published=True)

        story1 = self.create_story(bulletin, lead='story1')
        self.create_fragment_answer(story1, action='c', text='continue')
        self.create_fragment_answer(story1, action='n', text='next')
        poll = self.create_fragment_poll(story1, text='poll')
        self.create_fragment_paragraph(story1, text='paragraph1')

        story2 = self.create_story(bulletin, lead='story2')
        self.create_fragment_answer(story2, action='c', text='continue')
        self.create_fragment_answer(story2, action='n', text='next')
        self.create_fragment_paragraph(story2, text='paragraph2')
        self.create_fragment_answer(story2, action='c', text='continue')
        self.create_fragment_answer(story2, action='n', text='next')

        bot = TelegramBot(account.id)
        bot.bot = Mock()

        self.create_chat_user(account)

        bot.handle_message(self.get_telegram_message(text=account.welcome_answer_2_option_1))
        assert bot.bot.sendMessage.call_args[0][1] == 'story1'

        bot.bot.reset_mock()
        bot.handle_message(self.get_telegram_message(text='continue'))
        assert bot.bot.sendMessage.call_args[0][1] == 'poll'

        answer1_poll = PollQuestions.objects.get(fragment=poll, text='answer1')
        data = str(poll.id) + ',' + str(answer1_poll.id)
        bot.bot.reset_mock()
        bot.handle_callback_query(self.get_telegram_callback_query(data=data))

        assert bot.bot.sendMessage.call_args_list[0][0][1] == 'paragraph1'
        assert bot.bot.sendMessage.call_args_list[1][0][1] == 'story2'

        bot.bot.reset_mock()
        bot.handle_message(self.get_telegram_message(text='continue'))

        assert bot.bot.sendMessage.call_args[0][1] == 'paragraph2'
Exemple #2
0
    def test_bot_poll(self, client):

        (_, account) = self.create_user_account()
        bulletin = self.create_bulletin(account, is_published=True)
        story = self.create_story(bulletin)
        self.create_fragment_answer(story)

        poll_1 = self.create_fragment_poll(story, text='poll1')
        paragraph_1 = self.create_fragment_paragraph(story, text='paragraph1')

        poll_2 = self.create_fragment_poll(story, text='poll2')
        paragraph_2 = self.create_fragment_paragraph(story, text='paragraph2')
        self.create_fragment_answer(story, text='finish answer')

        bot = TelegramBot(account.id)
        bot.bot = Mock()

        self.create_chat_user(account)

        bot.handle_message(self.get_telegram_message(text=account.welcome_answer_2_option_1))
        assert bot.bot.sendMessage.call_args[0][1] == 'lead1'
        bot.bot.reset_mock()
        bot.handle_message(self.get_telegram_message(text='continue'))

        assert bot.bot.sendMessage.call_args_list[0][0][1] == 'poll1'
        assert bot.bot.sendMessage.call_args_list[0][1]['reply_markup'][0][0][0][0] == 'answer1'
        assert bot.bot.sendMessage.call_args_list[0][1]['reply_markup'][0][1][0][0] == 'answer2'

        answer1_poll1 = PollQuestions.objects.get(fragment=poll_1, text='answer1')

        # user sent custom message, poll must be resent
        bot.bot.reset_mock()
        bot.handle_message(self.get_telegram_message(text='say something'))
        print(bot.bot.sendMessage.call_args_list)
        assert bot.bot.sendMessage.call_args_list[0][0][1] == 'poll1'
        assert bot.bot.sendMessage.call_args_list[0][1]['reply_markup'][0][0][0][0] == 'answer1'
        assert bot.bot.sendMessage.call_args_list[0][1]['reply_markup'][0][1][0][0] == 'answer2'

        data = str(poll_1.id) + ',' + str(answer1_poll1.id)
        bot.bot.reset_mock()
        bot.handle_callback_query(self.get_telegram_callback_query(data=data))

        answer1_poll1.reload()
        assert self.default_chat_id in answer1_poll1.users
        assert self.default_chat_id not in PollQuestions.objects.get(fragment=poll_1, text='answer2').users

        # send paragraph#1 and poll#2 after poll was answered
        answer2_poll2 = PollQuestions.objects.get(fragment=poll_2, text='answer2')
        assert bot.bot.sendMessage.call_args_list[0][0][1] == 'paragraph1'
        assert bot.bot.sendMessage.call_args_list[1][0][1] == 'poll2'
        assert bot.bot.sendMessage.call_args_list[1][1]['reply_markup'][0][0][0][0] == 'answer1'
        assert bot.bot.sendMessage.call_args_list[1][1]['reply_markup'][0][1][0][0] == 'answer2'

        data = str(poll_2.id) + ',' + str(answer2_poll2.id)
        bot.bot.reset_mock()
        bot.handle_callback_query(self.get_telegram_callback_query(data=data))

        # send paragraph2 and answer after poll
        assert bot.bot.sendMessage.call_args_list[0][0][1] == 'paragraph2'
        assert 'finish answer' in bot.bot.sendMessage.call_args[1]['reply_markup']['keyboard'][0]

        bot.bot.reset_mock()
        bot.handle_message(self.get_telegram_message(text='finish answer'))
        assert bot.bot.sendMessage.call_args_list[0][0][1] == 'You are up to date!'

        answer2_poll2.reload()
        assert self.default_chat_id in answer2_poll2.users
        assert self.default_chat_id not in PollQuestions.objects.get(fragment=poll_2, text='answer1').users