Пример #1
0
    def test_is_admin_on_channel(self):
        command = RestrcitCommand()
        chat = Chat(id=1, type="supergroup", title="channel 1")
        chat.get_administrators = MagicMock(return_value=[
            AttrDict({"user": AttrDict({"id": 1})}),
            AttrDict({"user": AttrDict({"id": 2})}),
        ])
        self.bot.get_chat = MagicMock(return_value=chat)

        # is not admin
        self.assertFalse(command.is_admin_on_channel(self.bot, chat.id, 3))

        # is admin
        self.assertTrue(command.is_admin_on_channel(self.bot, chat.id, 1))
Пример #2
0
    def register(self):
        handlers = [
            MessageHandler(
                Filters.status_update.new_chat_members,
                NewChatMembersFilter(self.app).handler,
            ),
            MessageHandler(Filters.status_update.chat_created,
                           ChatCreatedFilter(self.app).handler),
            MessageHandler(Filters.status_update.migrate,
                           MigrateFilter(self.app).handler),
            MessageHandler(
                Filters.status_update.left_chat_member,
                LeftChatMemberFilter(self.app).handler,
            ),
            MessageHandler(Filters.location,
                           LocationFilter(self.app).handler,
                           pass_user_data=True),
            CommandHandler("start",
                           StartCommand(self.app).handler,
                           pass_args=True),
            CommandHandler("hi",
                           StartCommand(self.app).handler,
                           pass_args=True),
            CommandHandler("help",
                           HelpCommand(self.app).handler),
            CommandHandler("restrict",
                           RestrcitCommand(self.app).handler),
            CommandHandler("stats",
                           StatsCommand(self.app).handler),
            MessageHandler(Filters.private & Filters.command,
                           UnknownCommand(self.app).handler),
        ]

        for handler in handlers:
            self.dispatcher.add_handler(handler)
Пример #3
0
    def test(self):
        command = RestrcitCommand()

        update_event = self.fake_update()
        reply_text = update_event.message.reply_text = MagicMock(
            return_value=None)
        channel = Channel.query.filter_by(chat_id="1").first()
        context = MagicMock()
        context.bot = self.bot
        context.args = []

        # no arguments case
        command.handler(update_event, context)

        db.session.refresh(channel)
        self.assertFalse(channel.restrict)
        reply_text.assert_called()
        self.assertEqual(reply_text.call_count, 1)
        reply_text.assert_has_calls([call("Usage: /restrict <channel>")])

        reply_text.reset_mock()

        # channel is none
        context.args = ["test ab"]
        command.handler(update_event, context)

        db.session.refresh(channel)
        self.assertFalse(channel.restrict)
        reply_text.assert_called()
        self.assertEqual(reply_text.call_count, 1)
        reply_text.assert_has_calls([
            call(
                "test ab is not registered to the bot, please invite me there first."
            )
        ])

        reply_text.reset_mock()

        # user is not an admin
        command.is_admin_on_channel = MagicMock(return_value=False)

        context.args = ["test"]
        command.handler(update_event, context)

        db.session.refresh(channel)
        self.assertFalse(channel.restrict)
        reply_text.assert_called()
        self.assertEqual(reply_text.call_count, 1)
        reply_text.assert_has_calls(
            [call("You must be an admin on <test> to restrict it.")])

        reply_text.reset_mock()

        # channel is already restricted
        channel.restrict = True
        db.session.commit()
        command.is_admin_on_channel = MagicMock(return_value=True)

        context.args = ["test"]
        command.handler(update_event, context)

        db.session.refresh(channel)
        self.assertTrue(channel.restrict)
        reply_text.assert_called()
        self.assertEqual(reply_text.call_count, 1)
        reply_text.assert_has_calls([call("<test> is already restricted.")])

        channel.restrict = False
        db.session.commit()
        reply_text.reset_mock()

        # can't restrict channel
        command.can_restrict_channel = MagicMock(return_value=False)

        context.args = ["test"]
        command.handler(update_event, context)

        db.session.refresh(channel)
        self.assertFalse(channel.restrict)
        reply_text.assert_called()
        self.assertEqual(reply_text.call_count, 1)
        reply_text.assert_has_calls([
            call(
                "test must be a supergroup and the bot must be have admin permissions"
            )
        ])

        reply_text.reset_mock()

        # can restrict channel
        command.can_restrict_channel = MagicMock(return_value=True)

        context.args = ["test"]
        command.handler(update_event, context)

        db.session.refresh(channel)
        self.assertTrue(channel.restrict)
        reply_text.assert_called()
        self.assertEqual(reply_text.call_count, 1)
        reply_text.assert_has_calls([
            call(
                "New users joining <test> will be asked to verify themselves before being able to send new messages"
            )
        ])

        reply_text.reset_mock()