class Admin_functional_test(B3TestCase): """ tests from a class inheriting from Admin_functional_test must call self.init() """ def setUp(self): B3TestCase.setUp(self) self.conf = CfgConfigParser() self.p = AdminPlugin(self.console, self.conf) def init(self, config_content=None): """ optionally specify a config for the plugin. If called with no parameter, then the default config is loaded """ if config_content is None: self.conf.load(b3.getAbsolutePath("@b3/conf/plugin_admin.ini")) else: self.conf.loadFromString(config_content) self.p._commands = {} self.p.onLoadConfig() self.p.onStartup() self.joe = FakeClient(self.console, name="Joe", exactName="Joe", guid="joeguid", groupBits=128, team=TEAM_RED) self.mike = FakeClient(self.console, name="Mike", exactName="Mike", guid="mikeguid", groupBits=1, team=TEAM_BLUE)
class AdvTestCase(B3TestCase): """ Ease test cases that need an working B3 console and need to control the ADV plugin config """ def setUp(self): self.log = logging.getLogger('output') self.log.propagate = False B3TestCase.setUp(self) self.adminPluginConf = CfgConfigParser() self.adminPluginConf.load(ADMIN_CONFIG_FILE) self.adminPlugin = AdminPlugin(self.console, self.adminPluginConf) when(self.console).getPlugin("admin").thenReturn(self.adminPlugin) self.adminPlugin.onLoadConfig() self.adminPlugin.onStartup() self.console.startup() self.log.propagate = True global ADV_CONFIG_CONTENT, ADV_CONFIG_FILE, ADMIN_CONFIG_FILE, timer_patcher if os.path.exists(ADV_CONFIG_FILE): with open(ADV_CONFIG_FILE, 'r') as f: ADV_CONFIG_CONTENT = f.read() timer_patcher = patch('threading.Timer') timer_patcher.start() def tearDown(self): B3TestCase.tearDown(self) timer_patcher.stop() unstub() def init_plugin(self, config_content=None): conf = None if config_content: conf = XmlConfigParser() conf.setXml(config_content) elif ADV_CONFIG_CONTENT: conf = XmlConfigParser() conf.setXml(ADV_CONFIG_CONTENT) else: unittest.skip("cannot get default plugin config file at %s" % ADV_CONFIG_FILE) self.p = AdvPlugin(self.console, conf) self.p.save = Mock() self.conf = self.p.config self.log.setLevel(logging.DEBUG) self.log.info( "============================= Adv plugin: loading config ============================" ) self.p.onLoadConfig() self.log.info( "============================= Adv plugin: starting =================================" ) self.p.onStartup()
class Test_Tk_default_config(B3TestCase): def setUp(self): super(Test_Tk_default_config, self).setUp() self.console.gameName = 'f00' self.conf = CfgConfigParser() self.conf.load(b3.getAbsolutePath('@b3/conf/plugin_tk.ini')) self.p = TkPlugin(self.console, self.conf) self.p.onLoadConfig() def test_settings(self): self.assertEqual(400, self.p._maxPoints) self.assertEqual(40, self.p._maxLevel) self.assertEqual({ 0: (2.0, 1.0, 2), 1: (2.0, 1.0, 2), 2: (1.0, 0.5, 1), 20: (1.0, 0.5, 0), 40: (0.75, 0.5, 0) }, self.p._levels) self.assertEqual(7, self.p._round_grace) self.assertEqual("sfire", self.p._issue_warning) self.assertTrue(self.p._grudge_enable) self.assertTrue(self.p._private_messages) self.assertEqual(100, self.p._damage_threshold) self.assertEqual(2, self.p._warn_level) self.assertEqual(0, self.p._tkpointsHalflife) self.assertEqual('1h', self.p._tk_warn_duration) def test_messages(self): self.assertEqual("^7team damage over limit", self.p.config.get('messages', 'ban')) self.assertEqual("^7$vname^7 has forgiven $aname [^3$points^7]", self.p.config.get('messages', 'forgive')) self.assertEqual("^7$vname^7 has a ^1grudge ^7against $aname [^3$points^7]", self.p.config.get('messages', 'grudged')) self.assertEqual("^7$vname^7 has forgiven $attackers", self.p.config.get('messages', 'forgive_many')) self.assertEqual("^1ALERT^7: $name^7 auto-kick if not forgiven. Type ^3!forgive $cid ^7to forgive. [^3damage: $points^7]", self.p.config.get('messages', 'forgive_warning')) self.assertEqual("^7no one to forgive", self.p.config.get('messages', 'no_forgive')) self.assertEqual("^7Forgive who? %s", self.p.config.get('messages', 'players')) self.assertEqual("^7$name^7 has ^3$points^7 TK points", self.p.config.get('messages', 'forgive_info')) self.assertEqual("^7$name^7 cleared of ^3$points^7 TK points", self.p.config.get('messages', 'forgive_clear')) self.assertEqual("^3Do not attack teammates, ^1Attacked: ^7$vname ^7[^3$points^7]", self.p.config.get('messages', 'tk_warning_reason')) def test__default_messages(self): conf_items = self.p.config.items('messages') for conf_message_id, conf_message in conf_items: if conf_message_id not in self.p._default_messages: self.fail("%s should be added to the _default_messages dict" % conf_message_id) if conf_message != self.p._default_messages[conf_message_id]: self.fail("default message in the _default_messages dict for %s does not match the message from the config file" % conf_message_id) for default_message_id in self.p._default_messages: if default_message_id not in zip(*conf_items)[0]: self.fail("%s exists in the _default_messages dict, but not in the config file" % default_message_id)
class XlrstatsTestCase(B3TestCase): def setUp(self): """ This method is called before each test. It is meant to set up the SUT (System Under Test) in a manner that will ease the testing of its features. """ with logging_disabled(): # The B3TestCase class provides us a working B3 environment that does not require any database connexion. # The B3 console is then accessible with self.console B3TestCase.setUp(self) # set additional B3 console stuff that will be used by the XLRstats plugin self.console.gameName = "MyGame" self.parser_conf.add_section('b3') self.parser_conf.set('b3', 'time_zone', 'GMT') # we make our own AdminPlugin and make sure it is the one return in any case self.adminPlugin = AdminPlugin(self.console, DEFAULT_ADMIN_CONFIG_FILE) when(self.console).getPlugin("admin").thenReturn(self.adminPlugin) self.adminPlugin.onLoadConfig() self.adminPlugin.onStartup() # We need a config for the Xlrstats plugin self.conf = CfgConfigParser() # It is an empty config but we can fill it up later # Now we create an instance of the SUT (System Under Test) which is the XlrstatsPlugin self.p = XlrstatsPlugin(self.console, self.conf) when(self.console).getPlugin("xlrstats").thenReturn(self.p) # create a client object to represent the game server with patch("b3.clients.Clients.authorizeClients"): # we patch authorizeClients or it will spawn a thread # with a 5 second timer self.console.clients.newClient(-1, name="WORLD", guid="WORLD", hide=True) def init(self, config_content=None): """ Load the config and starts the xlrstats plugin. If no config_content is provided, use the default config file :param config_content: optional XLRstats config :type config_content: str """ if config_content is None: self.conf.load(DEFAULT_XLRSTATS_CONFIG_FILE) else: self.conf.loadFromString(config_content) self.p.onLoadConfig() self.p.minlevel = 1 # tests in this module assume unregistered players aren't considered by Xlrstats self.p.onStartup()
def setUp(self): self.status_response = None # defaults to STATUS_RESPONSE module attribute self.conf = XmlConfigParser() self.conf.loadFromString("""<configuration></configuration>""") self.parser = EtproParser(self.conf) self.parser.output = Mock() self.parser.output.write = Mock(wraps=sys.stdout.write) adminPlugin_conf = CfgConfigParser() adminPlugin_conf.load(ADMIN_CONFIG_FILE) adminPlugin = AdminPlugin(self.parser, adminPlugin_conf) adminPlugin.onLoadConfig() adminPlugin.onStartup() when(self.parser).getPlugin('admin').thenReturn(adminPlugin) when(self.parser).getCvar('b_privatemessages').thenReturn(Cvar('b_privatemessages', value='1')) self.parser.startup()
def setUp(self): self.status_response = None # defaults to STATUS_RESPONSE module attribute self.conf = XmlConfigParser() self.conf.loadFromString("""<configuration></configuration>""") self.parser = RavagedParser(self.conf) self.parser.output = Mock() self.parser.output.write = Mock() ADMIN_CONFIG = CfgConfigParser() ADMIN_CONFIG.load(ADMIN_CONFIG_FILE) self.adminPlugin = AdminPlugin(self.parser, ADMIN_CONFIG) when(self.parser).getPlugin("admin").thenReturn(self.adminPlugin) self.adminPlugin.onLoadConfig() self.adminPlugin.onStartup() self.parser.startup()
def setUp(self): self.log = logging.getLogger('output') self.log.propagate = False B3TestCase.setUp(self) admin_conf = CfgConfigParser() admin_conf.load(b3.getAbsolutePath('@b3/conf/plugin_admin.ini')) self.adminPlugin = AdminPlugin(self.console, admin_conf) when(self.console).getPlugin("admin").thenReturn(self.adminPlugin) self.adminPlugin.onLoadConfig() self.adminPlugin.onStartup() self.console.gameName = "theGame" self.console.startup() self.log.propagate = True
class Welcome_functional_test(B3TestCase): def setUp(self): B3TestCase.setUp(self) with logging_disabled(): self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.ini') when(self.console).getPlugin("admin").thenReturn(self.adminPlugin) self.adminPlugin.onLoadConfig() self.adminPlugin.onStartup() self.conf = CfgConfigParser() self.p = WelcomePlugin(self.console, self.conf) self.joe = FakeClient(self.console, name="Joe", guid="joeguid", groupBits=1, team=b3.TEAM_RED) self.mike = FakeClient(self.console, name="Mike", guid="mikeguid", groupBits=1, team=b3.TEAM_RED) self.bill = FakeClient(self.console, name="Bill", guid="billguid", groupBits=1, team=b3.TEAM_RED) self.superadmin = FakeClient(self.console, name="SuperAdmin", guid="superadminguid", groupBits=128, team=b3.TEAM_RED) def load_config(self, config_content=None): """ load the given config content, or the default config if config_content is None. """ if config_content is None: self.conf.load( b3.functions.getAbsolutePath('@b3/conf/plugin_welcome.ini')) else: self.conf.loadFromString(config_content) self.p.onLoadConfig() self.p.onStartup()
class Admin_TestCase(B3TestCase): """ Tests from a class inherithing from Admin_TestCase must call self.init(). """ def setUp(self): B3TestCase.setUp(self) self.conf = CfgConfigParser() self.p = AdminPlugin(self.console, self.conf) def init(self, config_content=None): """ Optionally specify a config for the plugin. If called with no parameter, then the default config is loaded. """ if config_content is None: self.conf.load(b3.getAbsolutePath("@b3/conf/plugin_admin.ini")) else: self.conf.loadFromString(config_content) self.p.onLoadConfig() self.p.onStartup()
class LoginTestCase(B3TestCase): """ Ease testcases that need an working B3 console and need to control the censor plugin config. """ def setUp(self): self.log = logging.getLogger('output') self.log.propagate = False B3TestCase.setUp(self) admin_conf = CfgConfigParser() admin_conf.load(b3.getAbsolutePath('@b3/conf/plugin_admin.ini')) self.adminPlugin = AdminPlugin(self.console, admin_conf) when(self.console).getPlugin("admin").thenReturn(self.adminPlugin) self.adminPlugin.onLoadConfig() self.adminPlugin.onStartup() self.console.gameName = "theGame" self.console.startup() self.log.propagate = True def tearDown(self): B3TestCase.tearDown(self) def init_plugin(self, config_content=None): self.conf = CfgConfigParser() if config_content: self.conf.loadFromString(config_content) else: self.conf.load(b3.getAbsolutePath('@b3/conf/plugin_login.ini')) self.p = LoginPlugin(self.console, self.conf) self.log.setLevel(logging.DEBUG) self.log.info( "============================= Login plugin: loading config ============================" ) self.p.onLoadConfig() self.log.info( "============================= Login plugin: starting =================================" ) self.p.onStartup()
def setUp(self): self.status_response = None # defaults to STATUS_RESPONSE module attribute self.conf = XmlConfigParser() self.conf.loadFromString("""<configuration></configuration>""") self.parser = InsurgencyParser(self.conf) self.parser.output = Mock() self.parser.output.write = Mock(wraps=sys.stdout.write) when(self.parser).is_sourcemod_installed().thenReturn(True) adminPlugin_conf = CfgConfigParser() adminPlugin_conf.load(ADMIN_CONFIG_FILE) adminPlugin = AdminPlugin(self.parser, adminPlugin_conf) adminPlugin.onLoadConfig() adminPlugin.onStartup() when(self.parser).getPlugin('admin').thenReturn(adminPlugin) when(self.parser).getAllAvailableMaps().thenReturn([ 'buhriz', 'district', 'sinjar', 'siege', 'uprising', 'ministry', 'revolt', 'heights', 'contact', 'peak', 'panj', 'market' ]) when(self.parser).getMap().thenReturn('buhriz') self.parser.startup() self.parser.patch_b3_admin_plugin( ) # seems that without this the test module doesn't patch the admin plugin
class Test_config(ChatloggerTestCase): def setUp(self): ChatloggerTestCase.setUp(self) with logging_disabled(): self.console.startup() self.conf = CfgConfigParser() self.p = ChatloggerPlugin(self.console, self.conf) when(self.console.config).get('b3', 'time_zone').thenReturn('GMT') self.p.setup_fileLogger = Mock() def init(self, config_content=None): """ load plugin config and initialise the plugin """ if config_content: self.conf.loadFromString(config_content) else: if os.path.isfile( b3.getAbsolutePath('@b3/conf/plugin_chatlogger.ini')): self.conf.load( b3.getAbsolutePath('@b3/conf/plugin_chatlogger.ini')) else: raise unittest.SkipTest( "default config file '%s' does not exists" % b3.getAbsolutePath('@b3/conf/plugin_chatlogger.ini')) self.p.onLoadConfig() self.p.onStartup() def test_default_config(self): # GIVEN when(b3).getB3Path(decode=ANY).thenReturn("c:\\b3_folder") when(b3).getConfPath(decode=ANY).thenReturn("c:\\b3_conf_folder") # WHEN self.init() # THEN self.assertTrue(self.p._save2db) self.assertTrue(self.p._save2file) expected_log_file = 'c:\\b3_conf_folder\\chat.log' if sys.platform == 'win32' else 'c:\\b3_conf_folder/chat.log' self.assertEqual(expected_log_file, self.p._file_name) self.assertEqual("D", self.p._file_rotation_rate) self.assertEqual(0, self.p._max_age_in_days) self.assertEqual(0, self.p._max_age_cmd_in_days) self.assertEqual(0, self.p._hours) self.assertEqual(0, self.p._minutes) def test_empty_config(self): self.init(""" """) self.assertTrue(self.p._save2db) self.assertFalse(self.p._save2file) self.assertIsNone(self.p._file_name) self.assertIsNone(self.p._file_rotation_rate) self.assertEqual(0, self.p._max_age_in_days) self.assertEqual(0, self.p._max_age_cmd_in_days) self.assertEqual(0, self.p._hours) self.assertEqual(0, self.p._minutes) self.assertEqual("chatlog", self.p._db_table) self.assertEqual("cmdlog", self.p._db_table_cmdlog) def test_conf1(self): self.init( dedent(""" [purge] max_age:7d hour:4 min:0 """)) self.assertTrue(self.p._save2db) self.assertFalse(self.p._save2file) self.assertIsNone(self.p._file_name) self.assertIsNone(self.p._file_rotation_rate) self.assertEqual(7, self.p._max_age_in_days) self.assertEqual(0, self.p._max_age_cmd_in_days) self.assertEqual(4, self.p._hours) self.assertEqual(0, self.p._minutes)
class Test_Config(B3TestCase): def setUp(self): B3TestCase.setUp(self) with logging_disabled(): self.console.startup() logging.getLogger('output').setLevel(logging.DEBUG) self.conf = CfgConfigParser() self.p = ChatloggerPlugin(self.console, self.conf) when(self.console.config).get('b3', 'time_zone').thenReturn('GMT') self.p.setup_fileLogger = Mock() def init(self, config_content=None): """ load plugin config and initialise the plugin """ if config_content: self.conf.loadFromString(config_content) else: if os.path.isfile(CHATLOGGER_CONFIG_FILE): self.conf.load(CHATLOGGER_CONFIG_FILE) else: unittest.skip("default config file '%s' does not exists" % CHATLOGGER_CONFIG_FILE) return self.p.onLoadConfig() self.p.onStartup() def test_default_config(self): # GIVEN when(b3).getB3Path().thenReturn("c:\\b3_folder") when(b3).getConfPath().thenReturn("c:\\b3_conf_folder") # WHEN self.init() # THEN self.assertTrue(self.p._save2db) self.assertTrue(self.p._save2file) expected_log_file = 'c:\\b3_conf_folder\\chat.log' if sys.platform == 'win32' else 'c:\\b3_conf_folder/chat.log' self.assertEqual(expected_log_file, self.p._file_name) self.assertEqual("D", self.p._file_rotation_rate) self.assertEqual(0, self.p._max_age_in_days) self.assertEqual(0, self.p._max_age_cmd_in_days) self.assertEqual(0, self.p._hours) self.assertEqual(0, self.p._minutes) def test_empty_config(self): self.init(""" """) self.assertTrue(self.p._save2db) self.assertFalse(self.p._save2file) self.assertIsNone(self.p._file_name) self.assertIsNone(self.p._file_rotation_rate) self.assertEqual(0, self.p._max_age_in_days) self.assertEqual(0, self.p._max_age_cmd_in_days) self.assertEqual(0, self.p._hours) self.assertEqual(0, self.p._minutes) def test_conf1(self): self.init( dedent(""" [purge] max_age:7d hour:4 min:0 """)) self.assertTrue(self.p._save2db) self.assertFalse(self.p._save2file) self.assertIsNone(self.p._file_name) self.assertIsNone(self.p._file_rotation_rate) self.assertEqual(7, self.p._max_age_in_days) self.assertEqual(0, self.p._max_age_cmd_in_days) self.assertEqual(4, self.p._hours) self.assertEqual(0, self.p._minutes)
class Test_conf(Iourt42TestCase): def setUp(self): Iourt42TestCase.setUp(self) self.conf = CfgConfigParser() self.p = Weaponcontrolurt42Plugin(self.console, self.conf) logger = logging.getLogger('output') logger.setLevel(logging.INFO) self.info_patcher = patch.object(self.p, "info", wraps=self.p.info) self.info_mock = self.info_patcher.start() self.warning_patcher = patch.object(self.p, "warning", wraps=self.p.warning) self.warning_mock = self.warning_patcher.start() self.error_patcher = patch.object(self.p, "error", wraps=self.p.error) self.error_mock = self.error_patcher.start() def tearDown(self): Iourt42TestCase.tearDown(self) self.info_patcher.stop() self.warning_patcher.stop() self.error_patcher.stop() def test_empty_config(self): # GIVEN self.conf.loadFromString(""" [foo] """) # WHEN self.p.onLoadConfig() # THEN self.assertListEqual([], self.error_mock.mock_calls) self.assertSetEqual(set(), self.p._forbiddenWeaponsFromConfig) self.assertSetEqual(set(), self.p._forbiddenWeapons) self.assertNotIn('weaponcontrol', self.adminPlugin._commands) self.warning_mock.assert_has_calls( [call("could not find section 'commands' in the plugin config. No command can be made available."), call("The config has no section 'allowed weapons'.")]) def test_config_cmd_weaponcontrol(self): # GIVEN self.conf.loadFromString(""" [commands] weaponcontrol-wpctrl: 60 """) # WHEN self.p.onLoadConfig() # THEN self.assertListEqual([], self.error_mock.mock_calls) self.assertIn('weaponcontrol', self.adminPlugin._commands) self.assertSetEqual(set(), self.p._forbiddenWeaponsFromConfig) self.assertSetEqual(set(), self.p._forbiddenWeapons) self.warning_mock.assert_has_calls([call("The config has no section 'allowed weapons'.")]) @skipUnless(os.path.isfile(DEFAULT_CONFIG_FILE), "Default config file not found at " + DEFAULT_CONFIG_FILE) def test_default_config(self): # GIVEN self.conf.load(DEFAULT_CONFIG_FILE) # WHEN self.p.onLoadConfig() # THEN self.assertListEqual([], self.error_mock.mock_calls) self.assertIn('weaponcontrol', self.adminPlugin._commands) self.assertSetEqual(set(), self.p._forbiddenWeaponsFromConfig) self.assertSetEqual(set(), self.p._forbiddenWeapons) self.warning_mock.assert_has_calls([])