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
def init(self, xml_content, cfg_content): xml_parser = XmlConfigParser() xml_parser.loadFromString(xml_content) conf_xml = MainConfig(xml_parser) cfg_parser = CfgConfigParser(allow_no_value=True) cfg_parser.loadFromString(cfg_content) conf_cfg = MainConfig(cfg_parser) return conf_xml, conf_cfg
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'])
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()
def setUp(self): # create a FakeConsole parser parser_ini_conf = CfgConfigParser() parser_ini_conf.loadFromString(r'''''') self.parser_main_conf = MainConfig(parser_ini_conf) with logging_disabled(): from tests.fake import FakeConsole self.console = FakeConsole(self.parser_main_conf) 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) # create our plugin instance self.p = SpreePlugin(self.console, CfgConfigParser()) with logging_disabled(): from tests.fake import FakeClient self.mike = FakeClient(console=self.console, name="Mike", guid="MIKEGUID", groupBits=1) self.bill = FakeClient(console=self.console, name="Bill", guid="BILLGUID", groupBits=1)
def setUp(self): # create a Iourt42 parser parser_conf = XmlConfigParser() parser_conf.loadFromString(dedent(r""" <configuration> <settings name="server"> <set name="game_log"></set> </settings> </configuration> """)) self.parser_conf = MainConfig(parser_conf) self.console = Iourt42Parser(self.parser_conf) # initialize some fixed cvars which will be used by both the plugin and the iourt42 parser when(self.console).getCvar('auth').thenReturn(Cvar('auth', value='0')) when(self.console).getCvar('fs_basepath').thenReturn(Cvar('fs_basepath', value='/fake/basepath')) when(self.console).getCvar('fs_homepath').thenReturn(Cvar('fs_homepath', value='/fake/homepath')) when(self.console).getCvar('fs_game').thenReturn(Cvar('fs_game', value='q3ut4')) when(self.console).getCvar('gamename').thenReturn(Cvar('gamename', value='q3urt42')) # start the parser self.console.startup() with logging_disabled(): self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.ini') self.adminPlugin.onLoadConfig() self.adminPlugin.onStartup() # make sure the admin plugin obtained by other plugins is our admin plugin when(self.console).getPlugin('admin').thenReturn(self.adminPlugin) self.conf = CfgConfigParser() self.conf.loadFromString(dedent(r""" [settings] demorecord: no skipstandardmaps: yes minleveldelete: senioradmin [commands] delrecord: guest maprecord: guest mapinfo: guest record: guest setway: senioradmin topruns: guest map: fulladmin maps: user setnextmap: admin """)) self.p = JumperPlugin(self.console, self.conf) self.p.onLoadConfig() self.p.onStartup() # load fixed json data (do not contact urtjumpers api for testing) when(self.p).getMapsDataFromApi().thenReturn(json.loads(MAPDATA_JSON))
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 setUp(self): # create a FakeConsole parser parser_ini_conf = CfgConfigParser() parser_ini_conf.loadFromString(r'''''') self.parser_main_conf = MainConfig(parser_ini_conf) with logging_disabled(): from b3.fake import FakeConsole self.console = FakeConsole(self.parser_main_conf) 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) # simulate geolocation plugin registering events self.console.createEvent('EVT_CLIENT_GEOLOCATION_SUCCESS', 'Event client geolocation success') self.console.screen = Mock() self.console.time = time.time self.console.upTime = Mock(return_value=1000) self.console.cron.stop() # create our plugin instance self.p = CountryfilterPlugin(self.console, CfgConfigParser()) with logging_disabled(): from b3.fake import FakeClient self.mike = FakeClient(console=self.console, name="Mike", guid="MIKEGUID", ip='1.2.3.4', groupBits=0) self.bill = FakeClient(console=self.console, name="Bill", guid="BILLGUID", ip='2.3.4.5', groupBits=0) self.mike.location = LOCATION_MIKE self.bill.location = LOCATION_BILL
def setUp(self): # create a Iourt42 parser parser_conf = XmlConfigParser() parser_conf.loadFromString(dedent(r""" <configuration> <settings name="server"> <set name="game_log"></set> </settings> </configuration> """)) self.parser_conf = MainConfig(parser_conf) self.console = Iourt42Parser(self.parser_conf) # initialize some fixed cvars which will be used by both the plugin and the iourt42 parser when(self.console).getCvar('auth').thenReturn(Cvar('auth', value='0')) when(self.console).getCvar('fs_basepath').thenReturn(Cvar('fs_basepath', value='/fake/basepath')) when(self.console).getCvar('fs_homepath').thenReturn(Cvar('fs_homepath', value='/fake/homepath')) when(self.console).getCvar('fs_game').thenReturn(Cvar('fs_game', value='q3ut4')) when(self.console).getCvar('gamename').thenReturn(Cvar('gamename', value='q3urt42')) # start the parser self.console.startup() with logging_disabled(): self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.ini') self.adminPlugin.onLoadConfig() self.adminPlugin.onStartup() # 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 # create some clients self.mike = FakeClient(console=self.console, name="Mike", guid="mikeguid", team=TEAM_RED, groupBits=128) self.bill = FakeClient(console=self.console, name="Bill", guid="billguid", team=TEAM_BLUE, groupBits=16) self.mark = FakeClient(console=self.console, name="Mark", guid="markguid", team=TEAM_RED, groupBits=2) self.sara = FakeClient(console=self.console, name="Sara", guid="saraguid", team=TEAM_SPEC, groupBits=1) self.conf = CfgConfigParser() self.p = CallvotePlugin(self.console, self.conf)
def setUp(self): self.conf = MainConfig( load(b3.getAbsolutePath('@b3/conf/b3.distribution.ini'))) log = logging.getLogger('output') log.setLevel(logging.DEBUG)
def setUp(self): # create a FakeConsole parser parser_ini_conf = CfgConfigParser() parser_ini_conf.loadFromString(r'''''') self.parser_main_conf = MainConfig(parser_ini_conf) with logging_disabled(): from b3.fake import FakeConsole self.console = FakeConsole(self.parser_main_conf) 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) # simulate geolocation plugin registering events self.console.createEvent('EVT_CLIENT_GEOLOCATION_SUCCESS', 'Event client geolocation success') self.console.screen = Mock() self.console.time = time.time self.console.upTime = Mock(return_value=1000) self.console.cron.stop() self.conf = CfgConfigParser() self.conf.loadFromString( dedent(r""" [settings] announce: yes [messages] client_connect: ^7$name ^3from ^7$city ^3(^7$country^3) connected cmd_locate: ^7$name ^3is connected from ^7$city ^3(^7$country^3) cmd_locate_failed: ^7Could not locate ^1$name cmd_distance: ^7$name ^3is ^7$distance ^3km away from you cmd_distance_self: ^7Sorry, I'm not that smart...meh! cmd_distance_failed: ^7Could not compute distance with ^1$name cmd_isp: ^7$name ^3is using ^7$isp ^3as isp cmd_isp_failed: ^7Could not determine ^1$name ^7isp [commands] locate: user distance: user isp: mod """)) self.p = LocationPlugin(self.console, self.conf) self.p.onLoadConfig() self.p.onStartup() with logging_disabled(): from b3.fake import FakeClient self.mike = FakeClient(console=self.console, name="Mike", guid="MIKEGUID", groupBits=1) self.bill = FakeClient(console=self.console, name="Bill", guid="BILLGUID", groupBits=16) self.mike.location = LOCATION_MIKE self.bill.location = LOCATION_BILL
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 }))
def setUp(self): # create a Iourt43 parser parser_conf = XmlConfigParser() parser_conf.loadFromString( dedent(r""" <configuration> <settings name="server"> <set name="game_log"></set> </settings> </configuration> """)) self.parser_conf = MainConfig(parser_conf) self.console = Iourt43Parser(self.parser_conf) # initialize some fixed cvars which will be used by both the plugin and the iourt42 parser when(self.console).getCvar('auth').thenReturn(Cvar('auth', value='0')) when(self.console).getCvar('auth_owners').thenReturn(None) when(self.console).getCvar('fs_basepath').thenReturn( Cvar('fs_basepath', value='/fake/basepath')) when(self.console).getCvar('fs_homepath').thenReturn( Cvar('fs_homepath', value='/fake/homepath')) when(self.console).getCvar('fs_game').thenReturn( Cvar('fs_game', value='q3ut4')) when(self.console).getCvar('gamename').thenReturn( Cvar('gamename', value='q3urt43')) # start the parser self.console.startup() with logging_disabled(): self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.ini') self.adminPlugin.onLoadConfig() self.adminPlugin.onStartup() # make sure the admin plugin obtained by other plugins is our admin plugin when(self.console).getPlugin('admin').thenReturn(self.adminPlugin) self.conf = CfgConfigParser() self.conf.loadFromString( dedent(r""" [settings] enabled: on minlevel: 40 nbtop: 5 [messages] msg_5: ^3$name ^4$score ^7HE grenade kills! """)) self.p = NaderPlugin(self.console, self.conf) self.p.onLoadConfig() self.p.onStartup() with logging_disabled(): from tests.fake import FakeClient # create some clients self.mike = FakeClient(console=self.console, name="Mike", guid="mikeguid", team=TEAM_BLUE, groupBits=1) self.mark = FakeClient(console=self.console, name="Mark", guid="markguid", team=TEAM_BLUE, groupBits=1) self.bill = FakeClient(console=self.console, name="Bill", guid="billguid", team=TEAM_RED, groupBits=1) self.mike.connects("1") self.bill.connects("2") self.mark.connects("3")
def setUp(self): # create a Iourt42 parser parser_conf = XmlConfigParser() parser_conf.loadFromString( dedent(r""" <configuration> <settings name="server"> <set name="game_log"></set> </settings> </configuration> """)) self.parser_conf = MainConfig(parser_conf) self.console = Iourt42Parser(self.parser_conf) # initialize some fixed cvars which will be used by both the plugin and the iourt42 parser when(self.console).getCvar('auth').thenReturn(Cvar('auth', value='0')) when(self.console).getCvar('fs_basepath').thenReturn( Cvar('fs_basepath', value='/fake/basepath')) when(self.console).getCvar('fs_homepath').thenReturn( Cvar('fs_homepath', value='/fake/homepath')) when(self.console).getCvar('fs_game').thenReturn( Cvar('fs_game', value='q3ut4')) when(self.console).getCvar('gamename').thenReturn( Cvar('gamename', value='q3urt42')) # start the parser self.console.startup() with logging_disabled(): self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.ini') self.adminPlugin.onLoadConfig() self.adminPlugin.onStartup() # make sure the admin plugin obtained by other plugins is our admin plugin when(self.console).getPlugin('admin').thenReturn(self.adminPlugin) self.conf = CfgConfigParser() self.conf.loadFromString( dedent(r""" [settings] firstkill: on firsttk: on firsths: on [commands] firstkill: superadmin firsttk: superadmin firsths: superadmin [messages] ## $client = the client who made the kill ## $target = the client who suffered the kill first_kill: ^2First Kill^3: $client killed $target first_kill_by_headshot: ^2First Kill ^5by Headshot^3: $client killed $target first_teamkill: ^1First TeamKill^3: $client teamkilled $target """)) self.p = FirstkillPlugin(self.console, self.conf) self.p.onLoadConfig() self.p.onStartup() with logging_disabled(): from b3.fake import FakeClient # create some clients self.mike = FakeClient(console=self.console, name="Mike", guid="mikeguid", team=TEAM_BLUE, groupBits=1) self.mark = FakeClient(console=self.console, name="Mark", guid="markguid", team=TEAM_BLUE, groupBits=1) self.bill = FakeClient(console=self.console, name="Bill", guid="billguid", team=TEAM_RED, groupBits=1) self.mike.connects("1") self.bill.connects("2") self.mark.connects("3")
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 }))