class TestLibraryExitExit(asynctest.TestCase):
    def setUp(self):
        self.channel = Mock(spec=Channel)
        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}
        self.mock_globals.running = True

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

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

    async def test(self):
        self.assertIs(await exit.exit(self.send), True)
        self.assertIs(self.mock_globals.running, False)
        self.send.assert_called_once_with(StrContains('Goodbye'))
        self.mock_part.assert_called_once_with('botgotsthis')
        self.mock_sleep.assert_called_once_with(TypeMatch(float))
示例#2
0
class TestManageBotAutoJoinAutoJoinPriority(asynctest.TestCase):
    def setUp(self):
        self.database = MagicMock(spec=DatabaseMain)
        self.database.__aenter__.return_value = self.database
        self.database.__aexit__.return_value = False
        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

    async def test(self):
        self.database.setAutoJoinPriority.return_value = True
        self.assertIs(
            await autojoin.auto_join_priority('botgotsthis', 0, self.send),
            True)
        self.database.setAutoJoinPriority.assert_called_once_with(
            'botgotsthis', 0)
        self.send.assert_called_once_with(
            StrContains('botgotsthis', 'priority', '0'))

    async def test_not_existing(self):
        self.database.setAutoJoinPriority.return_value = False
        self.assertIs(
            await autojoin.auto_join_priority('botgotsthis', 0, self.send),
            True)
        self.database.setAutoJoinPriority.assert_called_once_with(
            'botgotsthis', 0)
        self.send.assert_called_once_with(StrContains('botgotsthis', 'never'))
示例#3
0
class TestLibraryFeatureRemove(asynctest.TestCase):
    def setUp(self):
        self.data = Mock(spec=CacheStore)
        self.send = Mock(spec=send)

        patcher = patch('lib.items.feature')
        self.addCleanup(patcher.stop)
        self.mock_feature = patcher.start()
        self.mock_feature.features.return_value = {'feature': 'Feature'}

    async def test(self):
        self.data.hasFeature.return_value = True
        self.assertIs(
            await library.feature_remove(self.data, 'botgotsthis', 'feature',
                                         self.send), True)
        self.send.assert_called_once_with(StrContains('Feature', 'disable'))
        self.data.hasFeature.assert_called_once_with('botgotsthis', 'feature')
        self.data.removeFeature.assert_called_once_with(
            'botgotsthis', 'feature')

    async def test_existing(self):
        self.data.hasFeature.return_value = False
        self.assertIs(
            await library.feature_remove(self.data, 'botgotsthis', 'feature',
                                         self.send), True)
        self.send.assert_called_once_with(
            StrContains('Feature', 'not', 'enable'))
        self.data.hasFeature.assert_called_once_with('botgotsthis', 'feature')
        self.assertFalse(self.data.removeFeature.called)
示例#4
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)
async def test_find_user_details(mock_user_model_find_user_by_id: mock.Mock):
    mock_user_model_find_user_by_id.return_value = fixtures.user_fixture()
    result = await domain.find_user_by_id(user_id=dto.UserID("000000000000000000000001"))

    assert result == fixtures.user_fixture()
    mock_user_model_find_user_by_id.assert_called_once_with(
        user_id=dto.UserID("000000000000000000000001")
    )
def test_user_create_success(mock_domain_create_user: mock.Mock):
    body = fixtures.unsaved_user_json_fixture()
    mock_domain_create_user.return_value = fixtures.user_fixture()

    response = client.post("/users", json=body)

    expected_response = (201, fixtures.user_json_fixture())
    assert (response.status_code, response.json()) == expected_response
    mock_domain_create_user.assert_called_once_with(fixtures.unsaved_user_fixture())
def test_user_detail_not_found(mock_domain_find_user_by_id: mock.Mock):
    mock_domain_find_user_by_id.return_value = None

    response = client.get("/users/00000000000000000000000f")

    expected_response = (404, {"detail": "User not found."})
    assert (response.status_code, response.json()) == expected_response
    mock_domain_find_user_by_id.assert_called_once_with(
        user_id=dto.UserID("00000000000000000000000f")
    )
def test_user_detail_success(mock_domain_find_user_by_id: mock.Mock):
    mock_domain_find_user_by_id.return_value = fixtures.user_fixture()

    response = client.get("/users/00000000000000000000000a")

    expected_response = (200, fixtures.user_json_fixture())
    assert (response.status_code, response.json()) == expected_response
    mock_domain_find_user_by_id.assert_called_once_with(
        user_id=dto.UserID("00000000000000000000000a")
    )
async def test_get_version(mock_service_client_get: mock.Mock):
    mock_service_client_get.return_value = mock.Mock(
        status_code=200, json=lambda: {"version": "6.6.6"}
    )

    result = await cat_api.get_version()

    expected_result = {"version": "6.6.6"}
    assert result == expected_result
    mock_service_client_get.assert_called_once_with("https://cat.gengo.com/api/status")
示例#10
0
async def test_user_create_success(mock_user_model_create_user: mock.Mock, mock_utcnow: mock.Mock):
    mock_utcnow.return_value = datetime(2019, 1, 1, 23, 59, tzinfo=UTC)
    mock_user_model_create_user.return_value = fixtures.user_fixture()

    result = await domain.create_user(fixtures.new_user_fixture())

    assert result == fixtures.user_fixture()
    mock_utcnow.assert_called_once()
    mock_user_model_create_user.assert_called_once_with(
        fixtures.new_user_fixture(), datetime(2019, 1, 1, 23, 59, tzinfo=UTC)
    )
class TestManageBotBannedDeleteBannedChannel(asynctest.TestCase):
    def setUp(self):
        self.database = MagicMock(spec=DatabaseMain)
        self.database.__aenter__.return_value = self.database
        self.database.__aexit__.return_value = False
        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

    async def test(self):
        self.database.isChannelBannedReason.return_value = 'Kappa'
        self.database.removeBannedChannel.return_value = True
        self.assertIs(
            await banned.delete_banned_channel('megotsthis', 'Kappa',
                                               'botgotsthis', self.send), True)
        self.database.isChannelBannedReason.assert_called_once_with(
            'megotsthis')
        self.database.removeBannedChannel.assert_called_once_with(
            'megotsthis', 'Kappa', 'botgotsthis')
        self.send.assert_called_once_with(StrContains('megotsthis', 'unban'))

    async def test_blank(self):
        self.database.isChannelBannedReason.return_value = ''
        self.database.removeBannedChannel.return_value = True
        self.assertIs(
            await banned.delete_banned_channel('megotsthis', 'Kappa',
                                               'botgotsthis', self.send), True)
        self.database.isChannelBannedReason.assert_called_once_with(
            'megotsthis')
        self.database.removeBannedChannel.assert_called_once_with(
            'megotsthis', 'Kappa', 'botgotsthis')
        self.send.assert_called_once_with(StrContains('megotsthis', 'unban'))

    async def test_not_banned(self):
        self.database.isChannelBannedReason.return_value = None
        self.assertIs(
            await banned.delete_banned_channel('megotsthis', 'Kappa',
                                               'botgotsthis', self.send), True)
        self.database.isChannelBannedReason.assert_called_once_with(
            'megotsthis')
        self.assertFalse(self.database.removeBannedChannel.called)
        self.send.assert_called_once_with(
            StrContains('megotsthis', 'not', 'ban'))

    async def test_database_error(self):
        self.database.isChannelBannedReason.return_value = 'Kappa'
        self.database.removeBannedChannel.return_value = False
        self.assertIs(
            await banned.delete_banned_channel('megotsthis', 'Kappa',
                                               'botgotsthis', self.send), True)
        self.database.isChannelBannedReason.assert_called_once_with(
            'megotsthis')
        self.database.removeBannedChannel.assert_called_once_with(
            'megotsthis', 'Kappa', 'botgotsthis')
        self.send.assert_called_once_with(
            StrContains('megotsthis', 'not', 'unban'))
class TestLibraryChannelEmpty(unittest.TestCase):
    def setUp(self):
        self.send = Mock(spec=send)
        self.channel = Mock(spec=Channel)

        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(channel.empty('botgotsthis', self.send), True)
        self.send.assert_called_once_with(
            StrContains('all', 'messages', 'botgotsthis'))
        self.channel.clear.assert_called_once_with()

    def test_non_existing(self):
        self.assertIs(channel.empty('', self.send), False)
        self.assertFalse(self.send.called)
        self.channel.clear.assert_not_called()
示例#13
0
class TestLibraryBroadcasterAutoJoinDelete(asynctest.TestCase):
    def setUp(self):
        self.database = Mock(spec=DatabaseMain)
        self.send = Mock(spec=send)

    async def test(self):
        self.database.discardAutoJoin.return_value = True
        self.assertIs(
            await library.auto_join_delete(self.database, 'botgotsthis',
                                           self.send), True)
        self.database.discardAutoJoin.assert_called_once_with('botgotsthis')
        self.send.assert_called_once_with(StrContains('botgotsthis',
                                                      'disable'))

    async def test_not_existing(self):
        self.database.discardAutoJoin.return_value = False
        self.assertIs(
            await library.auto_join_delete(self.database, 'botgotsthis',
                                           self.send), True)
        self.database.discardAutoJoin.assert_called_once_with('botgotsthis')
        self.send.assert_called_once_with(StrContains('botgotsthis', 'never'))
class TestManageBotManagerInsertManager(asynctest.TestCase):
    def setUp(self):
        self.database = Mock(spec=DatabaseMain)
        self.send = Mock(spec=send)

    async def test(self):
        self.database.isBotManager.return_value = False
        self.database.addBotManager.return_value = True
        self.assertIs(
            await manager.insert_manager('megotsthis', self.database,
                                         self.send), True)
        self.database.isBotManager.assert_called_once_with('megotsthis')
        self.database.addBotManager.assert_called_once_with('megotsthis')
        self.send.assert_called_once_with(StrContains('megotsthis', 'manager'))

    async def test_manager(self):
        self.database.isBotManager.return_value = True
        self.assertIs(
            await manager.insert_manager('megotsthis', self.database,
                                         self.send), True)
        self.database.isBotManager.assert_called_once_with('megotsthis')
        self.assertFalse(self.database.addBotManager.called)
        self.send.assert_called_once_with(
            StrContains('megotsthis', 'already', 'manager'))

    async def test_database_error(self):
        self.database.isBotManager.return_value = False
        self.database.addBotManager.return_value = False
        self.assertIs(
            await manager.insert_manager('megotsthis', self.database,
                                         self.send), True)
        self.database.isBotManager.assert_called_once_with('megotsthis')
        self.database.addBotManager.assert_called_once_with('megotsthis')
        self.send.assert_called_once_with(
            StrContains('megotsthis', 'not', 'add', 'manager', 'Error'))
class TestLibraryChannelJoin(asynctest.TestCase):
    def setUp(self):
        self.database = MagicMock(spec=DatabaseMain)
        self.database.__aenter__.return_value = self.database
        self.database.__aexit__.return_value = False
        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('bot.utils.joinChannel', autospec=True)
        self.addCleanup(patcher.stop)
        self.mock_join = patcher.start()

    async def test(self):
        self.database.isChannelBannedReason.return_value = None
        self.database.getAutoJoinsPriority.return_value = math.inf
        self.mock_join.return_value = True
        self.assertIs(
            await channel.join('botgotsthis', self.send), True)
        self.send.assert_called_once_with(StrContains('Join', 'botgotsthis'))
        self.database.isChannelBannedReason.assert_called_once_with(
            'botgotsthis')
        self.database.getAutoJoinsPriority.assert_called_once_with(
            'botgotsthis')
        self.mock_join.assert_called_with('botgotsthis', math.inf)

    async def test_auto_join(self):
        self.database.isChannelBannedReason.return_value = None
        self.database.getAutoJoinsPriority.return_value = 0
        self.mock_join.return_value = True
        self.assertIs(
            await channel.join('botgotsthis', self.send), True)
        self.send.assert_called_once_with(StrContains('Join', 'botgotsthis'))
        self.database.isChannelBannedReason.assert_called_once_with(
            'botgotsthis')
        self.database.getAutoJoinsPriority.assert_called_once_with(
            'botgotsthis')
        self.mock_join.assert_called_with('botgotsthis', 0)

    async def test_already_joined(self):
        self.database.isChannelBannedReason.return_value = None
        self.database.getAutoJoinsPriority.return_value = math.inf
        self.mock_join.return_value = False
        self.assertIs(
            await channel.join('botgotsthis', self.send), True)
        self.send.assert_called_once_with(
            StrContains('Already', 'join', 'botgotsthis'))
        self.database.isChannelBannedReason.assert_called_once_with(
            'botgotsthis')
        self.database.getAutoJoinsPriority.assert_called_once_with(
            'botgotsthis')
        self.mock_join.assert_called_with('botgotsthis', math.inf)
示例#16
0
class TestLibraryBroadcasterCome(asynctest.TestCase):
    def setUp(self):
        self.tags = IrcMessageTags()
        self.database = MagicMock(spec=DatabaseMain)
        self.database.__aenter__.return_value = self.database
        self.database.__aexit__.return_value = False
        self.send = Mock(spec=send)

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

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

    async def test(self):
        self.database.isChannelBannedReason.return_value = None
        self.database.getAutoJoinsPriority.return_value = 0
        self.mock_join.return_value = True
        self.assertIs(await library.come('botgotsthis', self.send), True)
        self.send.assert_called_once_with(StrContains('Joining',
                                                      'botgotsthis'))
        self.database.isChannelBannedReason.assert_called_once_with(
            'botgotsthis')
        self.database.getAutoJoinsPriority.assert_called_once_with(
            'botgotsthis')
        self.mock_join.assert_called_once_with('botgotsthis', 0)

    async def test_existing(self):
        self.database.isChannelBannedReason.return_value = None
        self.database.getAutoJoinsPriority.return_value = 0
        self.mock_join.return_value = False
        self.assertIs(await library.come('botgotsthis', self.send), True)
        self.send.assert_called_once_with(StrContains('in', 'botgotsthis'))
        self.database.isChannelBannedReason.assert_called_once_with(
            'botgotsthis')
        self.database.getAutoJoinsPriority.assert_called_once_with(
            'botgotsthis')
        self.mock_join.assert_called_once_with('botgotsthis', 0)

    async def test_banned(self):
        self.database.isChannelBannedReason.return_value = ''
        self.assertIs(await library.come('botgotsthis', self.send), True)
        self.send.assert_called_once_with(StrContains('botgotsthis', 'banned'))
        self.database.isChannelBannedReason.assert_called_once_with(
            'botgotsthis')
        self.assertFalse(self.database.getAutoJoinsPriority.called)
        self.assertFalse(self.mock_join.called)
class TestManageBotBannedListBannedChannels(asynctest.TestCase):
    def setUp(self):
        self.database = MagicMock(spec=DatabaseMain)
        self.database.__aenter__.return_value = self.database
        self.database.__aexit__.return_value = False
        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

    async def test(self):
        self.database.listBannedChannels.return_value = AsyncIterator([])
        self.assertIs(await banned.list_banned_channels(self.send), True)
        self.send.assert_called_once_with(StrContains('no'))

    @patch('lib.helper.message.messagesFromItems', autospec=True)
    async def test_one(self, mock_messages):
        self.database.listBannedChannels.return_value = AsyncIterator(
            ['botgotsthis'])
        mock_messages.return_value = ''
        self.assertIs(await banned.list_banned_channels(self.send), True)
        mock_messages.assert_called_once_with(['botgotsthis'],
                                              StrContains('Banned'))
        self.send.assert_called_once_with('')

    @patch('lib.helper.message.messagesFromItems', autospec=True)
    async def test_many(self, mock_messages):
        self.database.listBannedChannels.return_value = AsyncIterator(
            ['botgotsthis', 'megotsthis'])
        mock_messages.return_value = ''
        self.assertIs(await banned.list_banned_channels(self.send), True)
        mock_messages.assert_called_once_with(['botgotsthis', 'megotsthis'],
                                              StrContains('Banned'))
        self.send.assert_called_once_with('')
示例#18
0
class TestFeatureLibrary(asynctest.TestCase):
    def setUp(self):
        self.data = Mock(spec=CacheStore)
        self.send = Mock(spec=send)

        patcher = patch('lib.items.feature')
        self.addCleanup(patcher.stop)
        self.mock_feature = patcher.start()
        self.mock_feature.features.return_value = {
            'feature': 'Feature',
            'none': None
        }

        patcher = patch('lib.helper.parser.get_response')
        self.addCleanup(patcher.stop)
        self.mock_response = patcher.start()

        patcher = patch(library.__name__ + '.feature_add')
        self.addCleanup(patcher.stop)
        self.mock_add = patcher.start()

        patcher = patch(library.__name__ + '.feature_remove')
        self.addCleanup(patcher.stop)
        self.mock_remove = patcher.start()

    async def test_add(self):
        self.mock_add.return_value = True
        self.mock_response.return_value = parser.Yes
        self.assertIs(
            await library.feature(self.data, 'botgotsthis',
                                  Message('!feature feature'), self.send),
            True)
        self.assertFalse(self.send.called)
        self.assertTrue(self.mock_response.called)
        self.mock_add.assert_called_once_with(self.data, 'botgotsthis',
                                              'feature', self.send)
        self.assertFalse(self.mock_remove.called)

    async def test_remove(self):
        self.mock_remove.return_value = True
        self.mock_response.return_value = parser.No
        self.assertIs(
            await library.feature(self.data, 'botgotsthis',
                                  Message('!feature feature'), self.send),
            True)
        self.assertFalse(self.send.called)
        self.assertTrue(self.mock_response.called)
        self.mock_remove.assert_called_once_with(self.data, 'botgotsthis',
                                                 'feature', self.send)
        self.assertFalse(self.mock_add.called)

    async def test_not_existing_feature(self):
        self.assertIs(
            await library.feature(self.data, 'botgotsthis',
                                  Message('!feature does_not_exist'),
                                  self.send), True)
        self.send.assert_called_once_with(
            StrContains('feature', 'does_not_exist'))
        self.assertFalse(self.mock_response.called)
        self.assertFalse(self.mock_add.called)
        self.assertFalse(self.mock_remove.called)

    async def test_feature_none(self):
        self.assertIs(
            await library.feature(self.data, 'botgotsthis',
                                  Message('!feature none'), self.send), True)
        self.send.assert_called_once_with(StrContains('feature', 'none'))
        self.assertFalse(self.mock_response.called)
        self.assertFalse(self.mock_add.called)
        self.assertFalse(self.mock_remove.called)

    async def test_bad_param(self):
        self.mock_response.return_value = parser.Unknown
        self.assertIs(
            await library.feature(self.data, 'botgotsthis',
                                  Message('!feature feature Kappa'),
                                  self.send), True)
        self.send.assert_called_once_with(StrContains('parameter', 'kappa'))
        self.assertTrue(self.mock_response.called)
        self.assertFalse(self.mock_add.called)
        self.assertFalse(self.mock_remove.called)
示例#19
0
 async def test_it_initializes_a_delegate_if_delegate_class_is_provided(
         self):
     delegate_class = Mock()
     JsonQueue(Mock(), Mock(), Mock(), delegate_class=delegate_class)
     delegate_class.assert_called_once_with()
示例#20
0
async def test_get_version_from_cat_api(mock_services_cat_api_get_version: mock.Mock):
    mock_services_cat_api_get_version.return_value = {"version": "6.6.6"}
    result = await domain.get_version_from_cat_api()

    assert result == {"version": "6.6.6"}
    mock_services_cat_api_get_version.assert_called_once_with()
 def test(self, mock_clear):
     mock_send = Mock(spec=send)
     self.assertIs(channel.empty_all(mock_send), True)
     mock_clear.assert_called_once_with()
     mock_send.assert_called_once_with(StrContains('all', 'messages'))
示例#22
0
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()
示例#23
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'))
示例#24
0
class TestLibraryBroadcasterAutoJoinAdd(asynctest.TestCase):
    def setUp(self):
        self.database = Mock(spec=DatabaseMain)
        self.send = Mock(spec=send)

        patcher = patch('bot.globals', autospec=True)
        self.addCleanup(patcher.stop)
        self.mock_globals = patcher.start()
        self.mock_globals.cluster = Mock(spec=ConnectionHandler)

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

    async def test(self):
        self.mock_join.return_value = True
        self.database.saveAutoJoin.return_value = True
        self.database.getAutoJoinsPriority.return_value = 0
        self.assertIs(
            await library.auto_join_add(self.database, 'botgotsthis',
                                        self.send), True)
        self.send.assert_called_once_with(
            StrContains('botgotsthis', 'enable', 'join'))
        self.database.discardAutoJoin.assert_not_called()
        self.database.saveAutoJoin.assert_called_once_with('botgotsthis', 0)
        self.database.getAutoJoinsPriority.assert_called_once_with(
            'botgotsthis')
        self.mock_join.assert_called_once_with('botgotsthis', 0)

    async def test_existing(self):
        self.mock_join.return_value = True
        self.database.saveAutoJoin.return_value = False
        self.database.getAutoJoinsPriority.return_value = 0
        self.assertIs(
            await library.auto_join_add(self.database, 'botgotsthis',
                                        self.send), True)
        self.send.assert_called_once_with(StrContains('botgotsthis',
                                                      'already'))
        self.database.discardAutoJoin.assert_not_called()
        self.database.saveAutoJoin.assert_called_once_with('botgotsthis', 0)
        self.database.getAutoJoinsPriority.assert_called_once_with(
            'botgotsthis')
        self.mock_join.assert_called_once_with('botgotsthis', 0)

    async def test_joined(self):
        self.mock_join.return_value = False
        self.database.saveAutoJoin.return_value = True
        self.database.getAutoJoinsPriority.return_value = 0
        self.assertIs(
            await library.auto_join_add(self.database, 'botgotsthis',
                                        self.send), True)
        self.send.assert_called_once_with(StrContains('botgotsthis',
                                                      'enabled'))
        self.database.discardAutoJoin.assert_not_called()
        self.database.saveAutoJoin.assert_called_once_with('botgotsthis', 0)
        self.database.getAutoJoinsPriority.assert_called_once_with(
            'botgotsthis')
        self.mock_join.assert_called_once_with('botgotsthis', 0)

    async def test_joined_existing(self):
        self.mock_join.return_value = False
        self.database.saveAutoJoin.return_value = False
        self.database.getAutoJoinsPriority.return_value = 0
        self.assertIs(
            await library.auto_join_add(self.database, 'botgotsthis',
                                        self.send), True)
        self.send.assert_called_once_with(
            StrContains('botgotsthis', 'already', 'enable', 'in chat'))
        self.database.discardAutoJoin.assert_not_called()
        self.database.saveAutoJoin.assert_called_once_with('botgotsthis', 0)
        self.database.getAutoJoinsPriority.assert_called_once_with(
            'botgotsthis')
        self.mock_join.assert_called_once_with('botgotsthis', 0)

    async def test_not_possible_1(self):
        self.mock_join.return_value = False
        self.database.saveAutoJoin.return_value = True
        self.database.getAutoJoinsPriority.return_value = 0
        self.assertIs(
            await library.auto_join_add(self.database, 'botgotsthis',
                                        self.send), True)
        self.send.assert_called_once_with(StrContains('botgotsthis', 'enable'))
        self.database.discardAutoJoin.assert_not_called()
        self.database.saveAutoJoin.assert_called_once_with('botgotsthis', 0)
        self.database.getAutoJoinsPriority.assert_called_once_with(
            'botgotsthis')
        self.mock_join.assert_called_once_with('botgotsthis', 0)

    async def test_not_possible_1_existing(self):
        self.mock_join.return_value = False
        self.database.saveAutoJoin.return_value = False
        self.database.getAutoJoinsPriority.return_value = 0
        self.assertIs(
            await library.auto_join_add(self.database, 'botgotsthis',
                                        self.send), True)
        self.send.assert_called_once_with(
            StrContains('botgotsthis', 'already', 'enabled', 'in chat'))
        self.database.discardAutoJoin.assert_not_called()
        self.database.saveAutoJoin.assert_called_once_with('botgotsthis', 0)
        self.database.getAutoJoinsPriority.assert_called_once_with(
            'botgotsthis')
        self.mock_join.assert_called_once_with('botgotsthis', 0)
class TestManageBotBannedInsertBannedChannel(asynctest.TestCase):
    def setUp(self):
        self.database = MagicMock(spec=DatabaseMain)
        self.database.__aenter__.return_value = self.database
        self.database.__aexit__.return_value = False
        self.send = Mock(spec=send)

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

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

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

    async def test(self):
        self.database.isChannelBannedReason.return_value = None
        self.database.addBannedChannel.return_value = True
        self.assertIs(
            await banned.insert_banned_channel('megotsthis', 'Kappa',
                                               'botgotsthis', self.send), True)
        self.database.isChannelBannedReason.assert_called_once_with(
            'megotsthis')
        self.database.addBannedChannel.assert_called_once_with(
            'megotsthis', 'Kappa', 'botgotsthis')
        self.database.discardAutoJoin.assert_called_once_with('megotsthis')
        self.mock_part.assert_called_once_with('megotsthis')
        self.send.assert_called_once_with(StrContains('megotsthis', 'ban'))

    async def test_banned(self):
        self.database.isChannelBannedReason.return_value = 'DansGame'
        self.assertIs(
            await banned.insert_banned_channel('megotsthis', 'Kappa',
                                               'botgotsthis', self.send), True)
        self.database.isChannelBannedReason.assert_called_once_with(
            'megotsthis')
        self.assertFalse(self.database.addBannedChannel.called)
        self.assertFalse(self.database.discardAutoJoin.called)
        self.assertFalse(self.mock_part.called)
        self.send.assert_called_once_with(
            StrContains('megotsthis', 'ban', 'DansGame'))

    async def test_banned_blank(self):
        self.database.isChannelBannedReason.return_value = ''
        self.assertIs(
            await banned.insert_banned_channel('megotsthis', 'Kappa',
                                               'botgotsthis', self.send), True)
        self.database.isChannelBannedReason.assert_called_once_with(
            'megotsthis')
        self.assertFalse(self.database.addBannedChannel.called)
        self.assertFalse(self.database.discardAutoJoin.called)
        self.assertFalse(self.mock_part.called)
        self.send.assert_called_once_with(StrContains('megotsthis', 'ban'))

    async def test_database_error(self):
        self.database.isChannelBannedReason.return_value = None
        self.database.addBannedChannel.return_value = False
        self.assertIs(
            await banned.insert_banned_channel('megotsthis', 'Kappa',
                                               'botgotsthis', self.send), True)
        self.database.isChannelBannedReason.assert_called_once_with(
            'megotsthis')
        self.database.addBannedChannel.assert_called_once_with(
            'megotsthis', 'Kappa', 'botgotsthis')
        self.assertFalse(self.database.discardAutoJoin.called)
        self.assertFalse(self.mock_part.called)
        self.send.assert_called_once_with(
            StrContains('megotsthis', 'not', 'ban'))

    async def test_bot(self):
        self.database.isChannelBannedReason.return_value = 'Kappa'
        self.assertIs(
            await banned.insert_banned_channel('botgotsthis', 'Kappa',
                                               'botgotsthis', self.send), True)
        self.assertFalse(self.database.isChannelBannedReason.called)
        self.assertFalse(self.database.addBannedChannel.called)
        self.assertFalse(self.database.discardAutoJoin.called)
        self.assertFalse(self.mock_part.called)
        self.send.assert_called_once_with(StrContains('not', 'ban', 'bot'))