示例#1
0
class TestLibraryBroadcasterLeave(asynctest.TestCase):
    def setUp(self):
        patcher = patch('bot.config', autospec=True)
        self.addCleanup(patcher.stop)
        self.mock_config = patcher.start()
        self.mock_config.botnick = 'botgotsthis'

        self.send = Mock(spec=send)

        patcher = patch('asyncio.sleep')
        self.addCleanup(patcher.stop)
        self.mock_sleep = patcher.start()

        patcher = patch('bot.utils.partChannel', autospec=True)
        self.addCleanup(patcher.stop)
        self.mock_part = patcher.start()

    async def test(self):
        self.assertIs(await library.leave('megotsthis', self.send), True)
        self.send.assert_called_once_with(StrContains('megotsthis', 'Bye'))
        self.mock_sleep.assert_called_once_with(TypeMatch(float))
        self.mock_part.assert_called_once_with('megotsthis')

    async def test_bot(self):
        self.assertIs(await library.leave('botgotsthis', self.send), False)
        self.send.assert_not_called()
        self.assertFalse(self.mock_sleep.called)
        self.assertFalse(self.mock_part.called)
class TestLibraryBroadcasterEmpty(unittest.TestCase):
    def setUp(self):
        self.channel = Mock(spec=Channel)
        self.channel.channel = 'botgotsthis'
        self.send = Mock(spec=send)

        patcher = patch('bot.globals', autospec=True)
        self.addCleanup(patcher.stop)
        self.mock_globals = patcher.start()
        self.mock_globals.channels = {'botgotsthis': self.channel}

    def test(self):
        self.assertIs(chat.empty('botgotsthis', self.send), True)
        self.send.assert_called_once_with(
            StrContains('Clear', 'messages', 'botgotsthis'))
        self.channel.clear.assert_called_once_with()

    def test_not_existing(self):
        self.assertIs(chat.empty('megotsthis', self.send), True)
        self.send.assert_not_called()
        self.channel.clear.assert_not_called()
class TestLibraryChannelPart(unittest.TestCase):
    def setUp(self):
        self.send = Mock(spec=send)

        patcher = patch('bot.config', autospec=True)
        self.addCleanup(patcher.stop)
        self.mock_config = patcher.start()
        self.mock_config.botnick = 'botgotsthis'

        patcher = patch('bot.utils.partChannel', autospec=True)
        self.addCleanup(patcher.stop)
        self.mock_part = patcher.start()

    def test(self):
        self.assertIs(channel.part('megotsthis', self.send), True)
        self.send.assert_called_with(StrContains('Leav', 'megotsthis'))
        self.mock_part.assert_called_with('megotsthis')

    def test_bot_channel(self):
        self.assertIs(channel.part('botgotsthis', self.send), False)
        self.send.assert_not_called()
        self.assertFalse(self.mock_part.called)
class TestLibraryBroadcasterSetTimeOutLevel(asynctest.TestCase):
    def setUp(self):
        self.data = Mock(spec=CacheStore)
        self.send = Mock(spec=send)

        patcher = patch('bot.config', autospec=True)
        self.addCleanup(patcher.stop)
        self.mock_config = patcher.start()
        self.mock_config.moderatorDefaultTimeout = [60, 600, 0]

    async def test_1(self):
        self.assertIs(
            await chat.set_timeout_level(self.data, 'botgotsthis', self.send,
                                         Message('!settimeoutlevel-1 1')),
            True)
        self.data.setChatProperty.assert_called_once_with(
            'botgotsthis', 'timeoutLength0', '1')
        self.send.assert_called_once_with(
            StrContains('timeout', '1st', '1 second'))

    async def test_1_default(self):
        self.assertIs(
            await chat.set_timeout_level(self.data, 'botgotsthis', self.send,
                                         Message('!settimeoutlevel-1')), True)
        self.data.setChatProperty.assert_called_once_with(
            'botgotsthis', 'timeoutLength0', None)
        self.send.assert_called_once_with(
            StrContains('timeout', '1st', 'default'))

    async def test_2(self):
        self.assertIs(
            await chat.set_timeout_level(self.data, 'botgotsthis', self.send,
                                         Message('!settimeoutlevel-2 3600')),
            True)
        self.data.setChatProperty.assert_called_once_with(
            'botgotsthis', 'timeoutLength1', '3600')
        self.send.assert_called_once_with(
            StrContains('timeout', '2nd', '3600 seconds'))

    async def test_2_default(self):
        self.assertIs(
            await chat.set_timeout_level(self.data, 'botgotsthis', self.send,
                                         Message('!settimeoutlevel-2')), True)
        self.data.setChatProperty.assert_called_once_with(
            'botgotsthis', 'timeoutLength1', None)
        self.send.assert_called_once_with(
            StrContains('timeout', '2nd', 'default'))

    async def test_3(self):
        self.assertIs(
            await chat.set_timeout_level(self.data, 'botgotsthis', self.send,
                                         Message('!settimeoutlevel-3 0')),
            True)
        self.data.setChatProperty.assert_called_once_with(
            'botgotsthis', 'timeoutLength2', '0')
        self.send.assert_called_once_with(StrContains('timeout', '3rd', 'ban'))

    async def test_3_default(self):
        self.assertIs(
            await chat.set_timeout_level(self.data, 'botgotsthis', self.send,
                                         Message('!settimeoutlevel-3')), True)
        self.data.setChatProperty.assert_called_once_with(
            'botgotsthis', 'timeoutLength2', None)
        self.send.assert_called_once_with(
            StrContains('timeout', '3rd', 'default'))

    async def test_0(self):
        self.assertIs(
            await chat.set_timeout_level(self.data, 'botgotsthis', self.send,
                                         Message('!settimeoutlevel-0')), False)
        self.data.setChatProperty.assert_not_called()
        self.send.assert_not_called()

    async def test_4(self):
        self.assertIs(
            await chat.set_timeout_level(self.data, 'botgotsthis', self.send,
                                         Message('!settimeoutlevel-4')), False)
        self.data.setChatProperty.assert_not_called()
        self.send.assert_not_called()
示例#5
0
class TestLibraryBroadcasterAutoJoin(asynctest.TestCase):
    def setUp(self):
        self.database = MagicMock(spec=DatabaseMain)
        self.database.__aenter__.return_value = self.database
        self.database.__aexit__.return_value = False
        self.database.isChannelBannedReason.return_value = None
        self.send = Mock(spec=send)

        patcher = patch.object(DatabaseMain, 'acquire')
        self.addCleanup(patcher.stop)
        self.mock_database = patcher.start()
        self.mock_database.return_value = self.database

        patcher = patch(library.__name__ + '.auto_join_add')
        self.addCleanup(patcher.stop)
        self.mock_add = patcher.start()
        self.mock_add.return_value = True

        patcher = patch(library.__name__ + '.auto_join_delete')
        self.addCleanup(patcher.stop)
        self.mock_delete = patcher.start()
        self.mock_delete.return_value = True

    async def test(self):
        self.assertIs(
            await library.auto_join('botgotsthis', self.send,
                                    Message('!autojoin')), True)
        self.database.isChannelBannedReason.assert_called_once_with(
            'botgotsthis')
        self.mock_add.assert_called_once_with(self.database, 'botgotsthis',
                                              self.send)
        self.assertFalse(self.mock_delete.called)
        self.send.assert_not_called()

    async def test_add(self):
        self.assertIs(
            await library.auto_join('botgotsthis', self.send,
                                    Message('!autojoin yes')), True)
        self.database.isChannelBannedReason.assert_called_once_with(
            'botgotsthis')
        self.mock_add.assert_called_once_with(self.database, 'botgotsthis',
                                              self.send)
        self.assertFalse(self.mock_delete.called)
        self.send.assert_not_called()

    async def test_delete(self):
        self.assertIs(
            await library.auto_join('botgotsthis', self.send,
                                    Message('!autojoin delete')), True)
        self.database.isChannelBannedReason.assert_called_once_with(
            'botgotsthis')
        self.mock_delete.assert_called_once_with(self.database, 'botgotsthis',
                                                 self.send)
        self.assertFalse(self.mock_add.called)
        self.send.assert_not_called()

    async def test_banned(self):
        self.database.isChannelBannedReason.return_value = ''
        self.assertIs(
            await library.auto_join('botgotsthis', self.send,
                                    Message('!autojoin')), True)
        self.database.isChannelBannedReason.assert_called_once_with(
            'botgotsthis')
        self.assertFalse(self.mock_add.called)
        self.assertFalse(self.mock_delete.called)
        self.send.assert_called_once_with(StrContains('banned', 'botgotsthis'))

    async def test_banned_delete(self):
        self.database.isChannelBannedReason.return_value = ''
        self.assertIs(
            await library.auto_join('botgotsthis', self.send,
                                    Message('!autojoin delete')), True)
        self.database.isChannelBannedReason.assert_called_once_with(
            'botgotsthis')
        self.assertFalse(self.mock_add.called)
        self.assertFalse(self.mock_delete.called)
        self.send.assert_called_once_with(StrContains('banned', 'botgotsthis'))