class ProxyfilterTestCase(unittest2.TestCase): def setUp(self): # create a FakeConsole parser self.parser_conf = MainConfig(CfgConfigParser(allow_no_value=True)) self.parser_conf.loadFromString(r"""""") with logging_disabled(): from b3.fake import FakeConsole self.console = FakeConsole(self.parser_conf) # load the admin plugin with logging_disabled(): self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.ini') self.adminPlugin._commands = {} self.adminPlugin.onStartup() # make sure the admin plugin obtained by other plugins is our admin plugin when(self.console).getPlugin('admin').thenReturn(self.adminPlugin) when(self.console.config).get_external_plugins_dir().thenReturn(os.path.join(os.getcwd(), '..', '..')) # patch the Proxyfilter class not to execute # proxy scans in a multithreaded environment patch_proxy_filter() def tearDown(self): self.console.working = False
class IpbanTestCase(unittest2.TestCase): def setUp(self): self.parser_conf = MainConfig(CfgConfigParser(allow_no_value=True)) self.parser_conf.loadFromString(dedent(r"""""")) self.console = FakeConsole(self.parser_conf) self.console.gameName = 'f00' self.console.startup() with logging_disabled(): self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.ini') self.adminPlugin.onLoadConfig() self.adminPlugin.onStartup() self.evt_queue = [] # make sure the admin plugin obtained by other plugins is our admin plugin when(self.console).getPlugin('admin').thenReturn(self.adminPlugin) with logging_disabled(): from b3.fake import FakeClient # prepare a few players self.mike = FakeClient(self.console, name="Mike", exactName="Mike", guid="MIKEGUID", groupBits=16, ip='1.1.1.1') self.paul = FakeClient(self.console, name="Paul", exactName="Paul", guid="PAULGUID", groupBits=1, ip='2.2.2.2') self.john = FakeClient(self.console, name="John", exactName="John", guid="JOHNGUID", groupBits=0, ip='3.3.3.3') self.mary = FakeClient(self.console, name="Mary", exactName="Mary", guid="MARYGUID", groupBits=0, ip='4.4.4.4') self.conf = CfgConfigParser() self.p = IpbanPlugin(self.console, self.conf) # return some mock data when(self.p).getBanIps().thenReturn(['2.2.2.2', '6.6.6.6', '7.7.7.7']) when(self.p).getTempBanIps().thenReturn(['3.3.3.3', '8.8.8.8', '9.9.9.9']) def tearDown(self): self.console.working = False self.mike.disconnects() self.paul.disconnects() self.john.disconnects() self.mary.disconnects() def init(self, config_content=None): if config_content: self.conf.loadFromString(config_content) else: self.conf.loadFromString(dedent(r""" [settings] maxlevel: user """)) self.p.onLoadConfig() self.p.onStartup()
class B3TestCase(unittest.TestCase): def setUp(self): testcase_lock.acquire() flush_console_streams() # create a FakeConsole parser self.parser_conf = MainConfig(CfgConfigParser(allow_no_value=True)) self.parser_conf.loadFromString(r"""""") with logging_disabled(): from tests.fake import FakeConsole self.console = FakeConsole(self.parser_conf) self.console.screen = Mock() self.console.time = time.time self.console.upTime = Mock(return_value=3) self.console.cron.stop() def mock_error(msg, *args, **kwargs): print(("ERROR: %s" % msg) % args) self.console.error = mock_error def tearDown(self): flush_console_streams() unstub() testcase_lock.release() @contextmanager def assertRaiseEvent(self, event_type, event_client=None, event_data=None, event_target=None): """ USAGE: def test_team_change(self): # GIVEN self.client._team = TEAM_RED # THEN with self.assertRaiseEvent( event_type='EVT_CLIENT_TEAM_CHANGE', event_data=24, event_client=self.client, event_target=None): # WHEN self.client.team = 24 """ event_type_name = self.console.getEventName(event_type) self.assertIsNotNone(event_type_name, f"could not find event with name '{event_type}'") with patch.object(self.console, 'queueEvent') as queueEvent: yield if event_type is None: assert not queueEvent.called return assert queueEvent.called, "No event was fired" def assertEvent(queueEvent_call_args): eventraised = queueEvent_call_args[0][0] return type(eventraised) == Event \ and self.console.getEventName(eventraised.type) == event_type_name \ and eventraised.data == event_data \ and eventraised.target == event_target \ and eventraised.client == event_client if not any(map(assertEvent, queueEvent.call_args_list)): raise AssertionError("Event %s(%r) not fired" % (self.console.getEventName(event_type), { 'event_client': event_client, 'event_data': event_data, 'event_target': event_target }))
class IpbanTestCase(unittest2.TestCase): def setUp(self): self.parser_conf = MainConfig(CfgConfigParser(allow_no_value=True)) self.parser_conf.loadFromString(dedent(r"""""")) self.console = FakeConsole(self.parser_conf) self.console.gameName = 'f00' self.console.startup() with logging_disabled(): self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.ini') self.adminPlugin.onLoadConfig() self.adminPlugin.onStartup() self.evt_queue = [] # make sure the admin plugin obtained by other plugins is our admin plugin when(self.console).getPlugin('admin').thenReturn(self.adminPlugin) with logging_disabled(): from b3.fake import FakeClient # prepare a few players self.mike = FakeClient(self.console, name="Mike", exactName="Mike", guid="MIKEGUID", groupBits=16, ip='1.1.1.1') self.paul = FakeClient(self.console, name="Paul", exactName="Paul", guid="PAULGUID", groupBits=1, ip='2.2.2.2') self.john = FakeClient(self.console, name="John", exactName="John", guid="JOHNGUID", groupBits=0, ip='3.3.3.3') self.mary = FakeClient(self.console, name="Mary", exactName="Mary", guid="MARYGUID", groupBits=0, ip='4.4.4.4') self.conf = CfgConfigParser() self.p = IpbanPlugin(self.console, self.conf) # return some mock data when(self.p).getBanIps().thenReturn(['2.2.2.2', '6.6.6.6', '7.7.7.7']) when(self.p).getTempBanIps().thenReturn( ['3.3.3.3', '8.8.8.8', '9.9.9.9']) def tearDown(self): self.console.working = False self.mike.disconnects() self.paul.disconnects() self.john.disconnects() self.mary.disconnects() def init(self, config_content=None): if config_content: self.conf.loadFromString(config_content) else: self.conf.loadFromString( dedent(r""" [settings] maxlevel: user """)) self.p.onLoadConfig() self.p.onStartup()
class B3TestCase(unittest.TestCase): def setUp(self): testcase_lock.acquire() flush_console_streams() # create a FakeConsole parser self.parser_conf = MainConfig(CfgConfigParser(allow_no_value=True)) self.parser_conf.loadFromString(r"""""") with logging_disabled(): from b3.fake import FakeConsole self.console = FakeConsole(self.parser_conf) self.console.screen = Mock() self.console.time = time.time self.console.upTime = Mock(return_value=3) self.console.cron.stop() def myError(msg, *args, **kwargs): print(("ERROR: %s" % msg) % args) self.console.error = myError def tearDown(self): flush_console_streams() testcase_lock.release() @contextmanager def assertRaiseEvent(self, event_type, event_client=None, event_data=None, event_target=None): """ USAGE: def test_team_change(self): # GIVEN self.client._team = TEAM_RED # THEN with self.assertRaiseEvent( event_type='EVT_CLIENT_TEAM_CHANGE', event_data=24, event_client=self.client, event_target=None): # WHEN self.client.team = 24 """ if type(event_type) is basestring: event_type_name = event_type else: event_type_name = self.console.getEventName(event_type) self.assertIsNotNone(event_type_name, "could not find event with name '%s'" % event_type) with patch.object(self.console, 'queueEvent') as queueEvent: yield if event_type is None: assert not queueEvent.called return assert queueEvent.called, "No event was fired" def assertEvent(queueEvent_call_args): eventraised = queueEvent_call_args[0][0] return type(eventraised) == Event \ and self.console.getEventName(eventraised.type) == event_type_name \ and eventraised.data == event_data \ and eventraised.target == event_target \ and eventraised.client == event_client if not any(map(assertEvent, queueEvent.call_args_list)): raise AssertionError("Event %s(%r) not fired" % (self.console.getEventName(event_type), { 'event_client': event_client, 'event_data': event_data, 'event_target': event_target }))