def test_cmdrevoke_with_client_reconnection(self): # GIVEN superadmin = FakeClient(self.console, name="superadmin", guid="superadminguid", groupBits=128) superadmin.connects("1") mike = FakeClient(self.console, id="10", name="mike", guid="mikeguid", groupBits=1) mike.connects("2") # WHEN superadmin.clearMessageHistory() superadmin.says("!cmdgrant mike cmdlevel") superadmin.says("!cmdrevoke mike cmdlevel") mike.disconnects() del mike # totally destroy the object mike = FakeClient(self.console, id="10", name="mike", guid="mikeguid", groupBits=1) mike.connects("2") # THEN grantlist = getattr(mike, GRANT_SET_ATTR, None) self.assertIsNotNone(grantlist) self.assertIsInstance(grantlist, set) self.assertEqual(0, len(grantlist))
def test_Banid(self): # GIVEN player = FakeClient(self.parser, name="courgette", guid="STEAM_1:0:1111111") player.connects("2") # WHEN player.disconnects() # ban self.clear_events() self.parser.parseLine('''L 08/28/2012 - 00:03:01: Banid: "courgette<91><STEAM_1:0:1111111><>" was banned "for 1.00 minutes" by "Console"''') # THEN self.assert_has_event("EVT_CLIENT_BAN_TEMP", {'reason': None, 'duration': '1.00 minutes', 'admin': 'Console'}, player)
def test_kick(self): # GIVEN player = FakeClient(self.parser, name="courgette", guid="STEAM_1:0:1111111") player.connects("2") # WHEN player.disconnects() # kick self.clear_events() self.parser.parseLine('''L 08/28/2012 - 00:12:07: [basecommands.smx] "Console<0><Console><Console>" kicked "courgette<91><STEAM_1:0:1111111><>" (reason "f00")''') # THEN self.assert_has_event("EVT_CLIENT_KICK", 'f00', player)
def test_checkdupes_with_player_reconnecting(self): # GIVEN p1 = FakeClient(self.console, name="sameName", guid="p1guid") p1.warn = Mock() p1.connects("1") # WHEN p1.disconnects() p1.connects("2") self.p.namecheck() # THEN self.assertFalse(p1.warn.called)
def test_banned_player_reconnects(self): # GIVEN player = FakeClient(self.parser, name="courgette", guid="STEAM_1:0:1111111") player.connects("2") self.assertEqual(0, player.numBans) player.ban(reason="test") self.assertEqual(1, player.numBans) player.disconnects() # WHEN with patch.object(player, "ban") as ban_mock: player.connects("3") # THEN ban_mock.assert_was_called_once()
class Test_events(unittest.TestCase): def setUp(self): with logging_disabled(): from b3.fake import FakeConsole, FakeClient self.console = FakeConsole('@b3/conf/b3.distribution.xml') self.conf = CfgConfigParser() self.p = GaPlugin(self.console, self.conf) self.conf.loadFromString( dedent(""" [google analytics] tracking ID: UA-000000-0 """)) self.p.onLoadConfig() self.p.onStartup() self.assertIsNotNone(self.p._ga_tracking_id) self.joe = FakeClient(self.console, name="Joe", guid="GUID_joe", ip="12.12.12.12") self.jack = FakeClient(self.console, name="Jack", guid="GUID_jack", ip="45.45.45.45") def test_auth(self, fire_mock): self.joe.connects("1") # THEN self.assertEqual(1, len(fire_mock.mock_calls)) def test_disconnect(self, fire_mock): self.joe.connects("1") self.joe.disconnects() # THEN self.assertEqual(2, len(fire_mock.mock_calls)) def test_new_round(self, fire_mock): # GIVEN self.joe.connects("1") self.jack.connects("2") # WHEN self.console.game.gameType = "The game type" self.console.game.mapName = "The map name" self.console.queueEvent( self.console.getEvent('EVT_GAME_ROUND_START', data=self.console.game)) # THEN self.assertEqual(4, len(fire_mock.mock_calls))
def test_cmdgrant_with_client_reconnection(self): # GIVEN superadmin = FakeClient(self.console, name="superadmin", guid="superadminguid", groupBits=128) superadmin.connects("1") mike = FakeClient(self.console, id="10", name="mike", guid="mikeguid", groupBits=1) mike.connects("2") # WHEN superadmin.clearMessageHistory() superadmin.says("!cmdgrant mike cmdlevel") mike.disconnects() del mike # totally destroy the object mike = FakeClient(self.console, id="10", name="mike", guid="mikeguid", groupBits=1) mike.connects("2") # THEN grantlist = getattr(mike, GRANT_SET_ATTR, None) self.assertIsNotNone(grantlist) self.assertIsInstance(grantlist, set) self.assertEqual(1, len(grantlist))
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 Test_events(unittest.TestCase): def setUp(self): with logging_disabled(): from b3.fake import FakeConsole, FakeClient self.console = FakeConsole('@b3/conf/b3.distribution.xml') self.conf = CfgConfigParser() self.p = GaPlugin(self.console, self.conf) self.conf.loadFromString(dedent(""" [google analytics] tracking ID: UA-000000-0 """)) self.p.onLoadConfig() self.p.onStartup() self.assertIsNotNone(self.p._ga_tracking_id) self.joe = FakeClient(self.console, name="Joe", guid="GUID_joe", ip="12.12.12.12") self.jack = FakeClient(self.console, name="Jack", guid="GUID_jack", ip="45.45.45.45") def test_auth(self, fire_mock): self.joe.connects("1") # THEN self.assertEqual(1, len(fire_mock.mock_calls)) def test_disconnect(self, fire_mock): self.joe.connects("1") self.joe.disconnects() # THEN self.assertEqual(2, len(fire_mock.mock_calls)) def test_new_round(self, fire_mock): # GIVEN self.joe.connects("1") self.jack.connects("2") # WHEN self.console.game.gameType = "The game type" self.console.game.mapName = "The map name" self.console.queueEvent(self.console.getEvent('EVT_GAME_ROUND_START', data=self.console.game)) # THEN self.assertEqual(4, len(fire_mock.mock_calls))
class Test_events(UrtauthTestCase): def setUp(self): UrtauthTestCase.setUp(self) with logging_disabled(): from b3.fake import FakeClient # create some clients self.mike = FakeClient(console=self.console, name="Mike", guid="mikeguid", groupBits=1) self.mark = FakeClient(console=self.console, name="Mark", guid="markguid", groupBits=1, pbid='markguid') self.bill = FakeClient(console=self.console, name="Bill", guid="billguid", groupBits=2) self.npc = FakeClient(console=self.console, name="NPC", guid="NPCGUID", groupBits=0, bot=True) def tearDown(self): self.mike.disconnects() self.bill.disconnects() self.mark.disconnects() UrtauthTestCase.tearDown(self) #################################################################################################################### ## ## ## TEST CASES ## ## ## #################################################################################################################### def test_client_connect_with_no_account_and_low_group(self): # GIVEN self.mike.kick = Mock() # WHEN self.mike.connects("1") # THEN self.assertEqual(1, self.mike.kick.call_count) self.mike.kick.assert_has_calls([call('^7you need an ^1auth ^7account to play here')]) def test_client_connect_with_account_and_low_group(self): # GIVEN self.mark.kick = Mock() # WHEN self.mike.connects("1") # THEN self.assertEqual(0, self.mark.kick.call_count) def test_client_connect_with_no_account_but_high_group(self): # GIVEN self.bill.kick = Mock() # WHEN self.bill.connects("1") # THEN self.assertEqual(0, self.bill.kick.call_count) def test_bot_connect(self): # GIVEN self.npc.kick = Mock() # WHEN self.npc.connects("1") # THEN self.assertEqual(0, self.npc.kick.call_count)
class Test_events(CallvoteTestCase): def setUp(self): CallvoteTestCase.setUp(self) 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 init(self, config_content=None): if config_content: self.conf.loadFromString(config_content) else: self.conf.loadFromString(dedent(r""" [callvoteminlevel] capturelimit: guest clientkick: guest clientkickreason: guest cyclemap: guest exec: guest fraglimit: guest kick: guest map: guest reload: guest restart: guest shuffleteams: guest swapteams: guest timelimit: guest g_bluewaverespawndelay: guest g_bombdefusetime: guest g_bombexplodetime: guest g_capturescoretime: guest g_friendlyfire: guest g_followstrict: guest g_gametype: guest g_gear: guest g_matchmode: guest g_maxrounds: guest g_nextmap: guest g_redwaverespawndelay: guest g_respawndelay: guest g_roundtime: guest g_timeouts: guest g_timeoutlength: guest g_swaproles: guest g_waverespawns: guest [callvotespecialmaplist] #ut4_abbey: guest #ut4_abbeyctf: guest [commands] lastvote: mod veto: mod """)) self.p.onLoadConfig() self.p.onStartup() # return a fixed timestamp when(self.p).getTime().thenReturn(1399725576) def tearDown(self): self.mike.disconnects() self.bill.disconnects() self.mark.disconnects() self.sara.disconnects() CallvoteTestCase.tearDown(self) #################################################################################################################### ## ## ## TEST CASES ## ## ## #################################################################################################################### def test_cmd_veto(self): # GIVEN self.init() self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.console.parseLine('''Callvote: 4 - "map ut4_dressingroom"''') self.mike.says('!veto') # THEN self.assertIsNone(self.p.callvote) def test_cmd_lastvote_legit(self): # GIVEN self.init() self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.console.parseLine('''Callvote: 4 - "map ut4_casa"''') self.p.callvote['time'] = self.p.getTime() - 10 # fake timestamp self.console.parseLine('''VotePassed: 3 - 0 - "map ut4_casa"''') self.mike.clearMessageHistory() self.mike.says('!lastvote') # THEN self.assertIsNotNone(self.p.callvote) self.assertListEqual(["Last vote issued by Sara 10 seconds ago", "Type: map - Data: ut4_casa", "Result: 3:0 on 4 clients"], self.mike.message_history)
class Test_commands(JumperTestCase): def setUp(self): JumperTestCase.setUp(self) with logging_disabled(): from b3.fake import FakeClient # create some clients self.mike = FakeClient(console=self.console, name="Mike", guid="mikeguid", team=TEAM_FREE, groupBits=128) self.bill = FakeClient(console=self.console, name="Bill", guid="billguid", team=TEAM_FREE, groupBits=1) self.mark = FakeClient(console=self.console, name="Mark", guid="markguid", team=TEAM_FREE, groupBits=1) self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.console.game.mapName = 'ut42_bstjumps_u2' self.console.parseLine('''ClientJumpRunStarted: 1 - way: 1''') self.console.parseLine('''ClientJumpRunStopped: 1 - way: 1 - time: 537000''') self.console.parseLine('''ClientJumpRunStarted: 2 - way: 1''') self.console.parseLine('''ClientJumpRunStopped: 2 - way: 1 - time: 349000''') self.console.parseLine('''ClientJumpRunStarted: 3 - way: 1''') self.console.parseLine('''ClientJumpRunStopped: 3 - way: 1 - time: 122000''') self.console.parseLine('''ClientJumpRunStarted: 1 - way: 2''') self.console.parseLine('''ClientJumpRunStopped: 1 - way: 2 - time: 84000''') self.console.parseLine('''ClientJumpRunStarted: 2 - way: 2''') self.console.parseLine('''ClientJumpRunStopped: 2 - way: 2 - time: 91000''') self.console.parseLine('''ClientJumpRunStarted: 3 - way: 2''') self.console.parseLine('''ClientJumpRunStopped: 3 - way: 2 - time: 177000''') self.console.game.mapName = 'ut42_jupiter' self.console.parseLine('''ClientJumpRunStarted: 1 - way: 1''') self.console.parseLine('''ClientJumpRunStopped: 1 - way: 1 - time: 123000''') self.console.parseLine('''ClientJumpRunStarted: 2 - way: 1''') self.console.parseLine('''ClientJumpRunStopped: 2 - way: 1 - time: 543000''') self.console.parseLine('''ClientJumpRunStarted: 1 - way: 2''') self.console.parseLine('''ClientJumpRunStopped: 1 - way: 2 - time: 79000''') when(self.console).getMaps().thenReturn(['ut4_abbey', 'ut4_abbeyctf', 'ut4_algiers', 'ut4_ambush', 'ut4_austria', 'ut42_bstjumps_u2', 'ut4_bohemia', 'ut4_casa', 'ut4_cascade', 'ut4_commune', 'ut4_company', 'ut4_crossing', 'ut4_docks', 'ut4_dressingroom', 'ut4_eagle', 'ut4_elgin', 'ut4_firingrange', 'ut4_ghosttown_rc4', 'ut4_harbortown', 'ut4_herring', 'ut4_horror', 'ut42_jupiter', 'ut4_kingdom', 'ut4_kingpin', 'ut4_mandolin', 'ut4_mars_b1', 'ut4_maya', 'ut4_oildepot', 'ut4_prague', 'ut4_prague_v2', 'ut4_raiders', 'ut4_ramelle', 'ut4_ricochet', 'ut4_riyadh', 'ut4_sanc', 'ut4_snoppis', 'ut4_suburbs', 'ut4_subway', 'ut4_swim', 'ut4_thingley', 'ut4_tombs', 'ut4_toxic', 'ut4_tunis', 'ut4_turnpike', 'ut4_uptown']) def tearDown(self): self.mike.disconnects() self.bill.disconnects() self.mark.disconnects() JumperTestCase.tearDown(self) #################################################################################################################### # # # CLIENT RECORD COMMAND # # # #################################################################################################################### def test_cmd_client_record_no_arguments(self): # GIVEN self.console.game.mapName = 'ut42_bstjumps_u2' # WHEN self.mike.clearMessageHistory() self.mike.says('!record') # THEN self.assertListEqual(['listing records for Mike (@%s) on ut42_bstjumps_u2:' % self.mike.id, '[1] 0:08:57.000 since %s' % JumperPlugin.getDateString(self.console.time()), '[2] 0:01:24.000 since %s' % JumperPlugin.getDateString(self.console.time())], self.mike.message_history) def test_cmd_client_record_single_argument(self): # GIVEN self.console.game.mapName = 'ut42_bstjumps_u2' # WHEN self.mike.clearMessageHistory() self.mike.says('!record bill') # THEN self.assertListEqual(['listing records for Bill (@%s) on ut42_bstjumps_u2:' % self.bill.id, '[1] 0:05:49.000 since %s' % JumperPlugin.getDateString(self.console.time()), '[2] 0:01:31.000 since %s' % JumperPlugin.getDateString(self.console.time())], self.mike.message_history) def test_cmd_client_record_double_arguments(self): # GIVEN self.console.game.mapName = 'ut42_bstjumps_u2' # WHEN self.mike.clearMessageHistory() self.mike.says('!record bill jupiter') # THEN self.assertListEqual(['[1] 0:09:03.000 since %s' % JumperPlugin.getDateString(self.console.time())], self.mike.message_history) def test_cmd_client_record_double_arguments_no_record(self): # GIVEN self.console.game.mapName = 'ut42_bstjumps_u2' # WHEN self.mike.clearMessageHistory() self.mike.says('!record mark jupiter') # THEN self.assertListEqual(['no record found for Mark (@%s) on ut42_jupiter' % self.mark.id], self.mike.message_history) #################################################################################################################### # # # MAP RECORD COMMAND # # # #################################################################################################################### def test_cmd_maprecord_no_arguments(self): # GIVEN self.console.game.mapName = 'ut42_bstjumps_u2' # WHEN self.mike.clearMessageHistory() self.mike.says('!maprecord') # THEN self.assertListEqual(['listing map records on ut42_bstjumps_u2:', '[1] Mark (@%s) with 0:02:02.000' % self.mark.id, '[2] Mike (@%s) with 0:01:24.000' % self.mike.id], self.mike.message_history) def test_cmd_maprecord_with_arguments(self): # GIVEN self.console.game.mapName = 'ut42_bstjumps_u2' # WHEN self.mike.clearMessageHistory() self.mike.says('!maprecord jupiter') # THEN self.assertListEqual(['listing map records on ut42_jupiter:', '[1] Mike (@%s) with 0:02:03.000' % self.mike.id, '[2] Mike (@%s) with 0:01:19.000' % self.mike.id], self.mike.message_history) def test_cmd_maprecord_with_arguments_no_record(self): # GIVEN self.console.game.mapName = 'ut42_bstjumps_u2' # WHEN self.mike.clearMessageHistory() self.mike.says('!maprecord mars') # THEN self.assertListEqual(['no record found on ut4_mars_b1'], self.mike.message_history) #################################################################################################################### # # # TOPRUNS COMMAND # # # #################################################################################################################### def test_cmd_topruns_no_arguments(self): # GIVEN self.console.game.mapName = 'ut42_bstjumps_u2' # WHEN self.mike.clearMessageHistory() self.mike.says('!topruns') # THEN self.assertListEqual(['listing top runs on ut42_bstjumps_u2:', '[1] #1 Mark (@%s) with 0:02:02.000' % self.mark.id, '[1] #2 Bill (@%s) with 0:05:49.000' % self.bill.id, '[1] #3 Mike (@%s) with 0:08:57.000' % self.mike.id, '[2] #1 Mike (@%s) with 0:01:24.000' % self.mike.id, '[2] #2 Bill (@%s) with 0:01:31.000' % self.bill.id, '[2] #3 Mark (@%s) with 0:02:57.000' % self.mark.id], self.mike.message_history) def test_cmd_topruns_with_arguments(self): # GIVEN self.console.game.mapName = 'ut42_bstjumps_u2' # WHEN self.mike.clearMessageHistory() self.mike.says('!topruns jupiter') # THEN self.assertListEqual(['listing top runs on ut42_jupiter:', '[1] #1 Mike (@%s) with 0:02:03.000' % self.mike.id, '[1] #2 Bill (@%s) with 0:09:03.000' % self.bill.id, '[2] #1 Mike (@%s) with 0:01:19.000' % self.mike.id], self.mike.message_history) def test_cmd_topruns_with_arguments_no_record(self): # GIVEN self.console.game.mapName = 'ut42_bstjumps_u2' # WHEN self.mike.clearMessageHistory() self.mike.says('!topruns mars') # THEN self.assertListEqual(['no record found on ut4_mars_b1'], self.mike.message_history) #################################################################################################################### # # # DELRECORD COMMAND # # # #################################################################################################################### def test_cmd_delrecord_no_arguments(self): # GIVEN self.console.game.mapName = 'ut42_bstjumps_u2' # WHEN self.mike.clearMessageHistory() self.mike.says('!delrecord') # THEN self.assertListEqual(['removed 2 records for Mike (@%s) on ut42_bstjumps_u2' % self.mike.id], self.mike.message_history) self.assertListEqual([], self.p.getClientRecords(self.mike, 'ut42_bstjumps_u2')) def test_cmd_delrecord_with_one_argument(self): # GIVEN self.console.game.mapName = 'ut42_bstjumps_u2' # WHEN self.mike.clearMessageHistory() self.mike.says('!delrecord bill') # THEN self.assertListEqual(['removed 2 records for Bill (@%s) on ut42_bstjumps_u2' % self.bill.id], self.mike.message_history) self.assertListEqual([], self.p.getClientRecords(self.bill, 'ut42_bstjumps_u2')) def test_cmd_delrecord_with_two_arguments(self): # GIVEN self.console.game.mapName = 'ut42_bstjumps_u2' # WHEN self.mike.clearMessageHistory() self.mike.says('!delrecord bill jupiter') # THEN self.assertListEqual(['removed 1 record for Bill (@%s) on ut42_jupiter' % self.bill.id], self.mike.message_history) self.assertListEqual([], self.p.getClientRecords(self.bill, 'ut42_jupiter')) def test_cmd_delrecord_with_two_arguments_no_record(self): # GIVEN self.console.game.mapName = 'ut42_bstjumps_u2' # WHEN self.mike.clearMessageHistory() self.mike.says('!delrecord mark jupiter') # THEN self.assertListEqual(['no record found for Mark (@%s) on ut42_jupiter' % self.mark.id], self.mike.message_history) #################################################################################################################### # # # MAPINFO COMMAND # # # #################################################################################################################### # def test_cmd_mapinfo_no_arguments(self): # # GIVEN # self.console.game.mapName = 'ut4_uranus_beta1a' # # WHEN # self.mike.clearMessageHistory() # self.mike.says('!mapinfo') # # THEN # self.assertListEqual([u'''Uranus has been created by Levant''', # u'''it has been released on Tue, 15 Jan 2013''', # u'''it's composed of 22 jumps and 1 way''', # u'''level: 50/100'''], self.mike.message_history) # def test_cmd_mapinfo_with_arguments(self): # # GIVEN # self.console.game.mapName = 'ut4_uranus_beta1a' # # WHEN # self.mike.clearMessageHistory() # self.mike.says('!mapinfo crouch') # # THEN # self.assertListEqual([u'''Crouch Training has been created by spidercochon''', # u'''it has been released on Thu, 30 Dec 2010''', # u'''it's composed of 11 jumps and 1 way''', # u'''level: 79/100'''], self.mike.message_history) # def test_cmd_mapinfo_no_result(self): # # GIVEN # self.console.game.mapName = 'ut4_turnpike' # # WHEN # self.mike.clearMessageHistory() # self.mike.says('!mapinfo') # # THEN # self.assertListEqual(['could not find info for map ut4_turnpike'], self.mike.message_history) #################################################################################################################### # # # SETWAY COMMAND # # # #################################################################################################################### def test_cmd_setway(self): # GIVEN self.console.game.mapName = 'ut42_bstjumps_u2' # WHEN self.mike.clearMessageHistory() self.mike.says('!setway 1 Rookie') self.mike.says('!setway 2 Explorer') self.mike.clearMessageHistory() self.mike.says('!maprecord') # THEN self.assertListEqual(['listing map records on ut42_bstjumps_u2:', '[Rookie] Mark (@%s) with 0:02:02.000' % self.mark.id, '[Explorer] Mike (@%s) with 0:01:24.000' % self.mike.id], self.mike.message_history) #################################################################################################################### # # # OTHER COMMANDS # # # #################################################################################################################### def test_cmd_maps(self): # WHEN self.mike.clearMessageHistory() self.mike.says('!maps') # THEN self.assertListEqual(['map rotation: ut42_bstjumps_u2, ut42_jupiter, ut4_mars_b1'], self.mike.message_history) def test_cmd_map_valid_name(self): # WHEN self.mike.clearMessageHistory() self.mike.says('!map jup') # THEN self.assertListEqual(['changing map to ut42_jupiter'], self.mike.message_history) def test_cmd_map_invalid_name(self): # WHEN self.mike.clearMessageHistory() self.mike.says('!map f00') # THEN self.assertListEqual(['do you mean: ut42_bstjumps_u2, ut4_mars_b1, ut42_jupiter?'], self.mike.message_history)
class FirstKillCase(unittest2.TestCase): @classmethod def setUpClass(cls): with logging_disabled(): from b3.parsers.q3a.abstractParser import AbstractParser from b3.fake import FakeConsole AbstractParser.__bases__ = (FakeConsole,) # Now parser inheritance hierarchy is : # Iourt41Parser -> abstractParser -> FakeConsole -> Parser 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") def tearDown(self): self.console.working = False self.mike.disconnects() self.bill.disconnects() self.mark.disconnects()
class Test_events(JumperTestCase): def setUp(self): JumperTestCase.setUp(self) with logging_disabled(): from b3.fake import FakeClient # prevent the test to query the api: we handle this somewhere else when(self.p).getMapsData().thenReturn(dict()) # create some clients self.mike = FakeClient(console=self.console, name="Mike", guid="mikeguid", team=TEAM_FREE, groupBits=1) self.bill = FakeClient(console=self.console, name="Bill", guid="billguid", team=TEAM_FREE, groupBits=1) self.mike.connects("1") self.bill.connects("2") # force fake mapname self.console.game.mapName = 'ut42_bstjumps_u2' def tearDown(self): self.mike.disconnects() self.bill.disconnects() JumperTestCase.tearDown(self) #################################################################################################################### ## ## ## JUMPRUN EVENTS ## ## ## #################################################################################################################### def test_event_client_jumprun_started(self): # WHEN event = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START') self.console.queueEvent(Event(event, client=self.mike, data={'way_id' : '1'})) # THEN self.assertEqual(True, self.mike.isvar(self.p, 'jumprun')) self.assertIsNone(self.mike.var(self.p, 'jumprun').value.demo) self.assertIsInstance(self.mike.var(self.p, 'jumprun').value, JumpRun) def test_event_client_jumprun_stopped(self): # WHEN event = self.console.getEventID('EVT_CLIENT_JUMP_RUN_STOP') self.console.queueEvent(Event(event, client=self.mike, data={'way_id' : '1', 'way_time' : '12345'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertListEqual([], self.p.getClientRecords(self.mike, self.console.game.mapName)) def test_event_client_jumprun_canceled(self): # WHEN event = self.console.getEventID('EVT_CLIENT_JUMP_RUN_CANCEL') self.console.queueEvent(Event(event, client=self.mike, data={'way_id' : '1'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertListEqual([], self.p.getClientRecords(self.mike, self.console.game.mapName)) def test_event_client_jumprun_started_stopped(self): # WHEN event1 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START') event2 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_STOP') self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(Event(event2, client=self.mike, data={'way_id' : '1', 'way_time' : '12345'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(1, len(self.p.getClientRecords(self.mike, self.console.game.mapName))) def test_event_client_jumprun_started_canceled(self): # WHEN event1 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START') event2 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_CANCEL') self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(Event(event2, client=self.mike, data={'way_id' : '1'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(0, len(self.p.getClientRecords(self.mike, self.console.game.mapName))) def test_event_client_jumprun_started_stopped_multiple_clients(self): # WHEN event1 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START') event2 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_STOP') self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(Event(event2, client=self.mike, data={'way_id' : '1', 'way_time' : '12345'})) self.console.queueEvent(Event(event1, client=self.bill, data={'way_id' : '1'})) self.console.queueEvent(Event(event2, client=self.bill, data={'way_id' : '1', 'way_time' : '12345'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(False, self.bill.isvar(self.p, 'jumprun')) self.assertEqual(1, len(self.p.getClientRecords(self.mike, self.console.game.mapName))) self.assertEqual(1, len(self.p.getClientRecords(self.bill, self.console.game.mapName))) def test_event_client_jumprun_started_stopped_multiple_ways(self): # WHEN event1 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START') event2 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_STOP') self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(Event(event2, client=self.mike, data={'way_id' : '1', 'way_time' : '12345'})) self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '2'})) self.console.queueEvent(Event(event2, client=self.mike, data={'way_id' : '2', 'way_time' : '12345'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(2, len(self.p.getClientRecords(self.mike, self.console.game.mapName))) def test_event_client_jumprun_started_stopped_multiple_clients_multiple_ways(self): # WHEN event1 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START') event2 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_STOP') self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(Event(event2, client=self.mike, data={'way_id' : '1', 'way_time' : '12345'})) self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '2'})) self.console.queueEvent(Event(event2, client=self.mike, data={'way_id' : '2', 'way_time' : '12345'})) self.console.queueEvent(Event(event1, client=self.bill, data={'way_id' : '1'})) self.console.queueEvent(Event(event2, client=self.bill, data={'way_id' : '1', 'way_time' : '12345'})) self.console.queueEvent(Event(event1, client=self.bill, data={'way_id' : '2'})) self.console.queueEvent(Event(event2, client=self.bill, data={'way_id' : '2', 'way_time' : '12345'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(False, self.bill.isvar(self.p, 'jumprun')) self.assertEqual(2, len(self.p.getClientRecords(self.mike, self.console.game.mapName))) self.assertEqual(2, len(self.p.getClientRecords(self.bill, self.console.game.mapName))) #################################################################################################################### ## ## ## OTHER EVENTS ## ## ## #################################################################################################################### def test_event_game_map_change(self): # WHEN event1 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START') event2 = self.console.getEventID('EVT_GAME_MAP_CHANGE') self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(Event(event1, client=self.bill, data={'way_id' : '1'})) self.console.queueEvent(Event(event2, data='\sv_allowdownload\0\g_matchmode\0\g_gametype\9\sv_maxclients\32\sv_floodprotect\1')) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(False, self.bill.isvar(self.p, 'jumprun')) self.assertListEqual([], self.p.getClientRecords(self.mike, self.console.game.mapName)) self.assertListEqual([], self.p.getClientRecords(self.bill, self.console.game.mapName)) def test_event_client_disconnect(self): # WHEN event1 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START') event2 = self.console.getEventID('EVT_CLIENT_DISCONNECT') self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(Event(event2, client=self.mike, data=None)) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) def test_event_client_team_change(self): # WHEN event = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START') self.console.queueEvent(Event(event, client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(Event(event, client=self.bill, data={'way_id' : '1'})) self.mike.team = TEAM_SPEC # will raise EVT_CLIENT_TEAM_CHANGE self.bill.team = TEAM_FREE # will not raise EVT_CLIENT_TEAM_CHANGE # THEN self.assertEqual(TEAM_SPEC, self.mike.team) self.assertEqual(TEAM_FREE, self.bill.team) self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(True, self.bill.isvar(self.p, 'jumprun')) self.assertIsInstance(self.bill.var(self.p, 'jumprun').value, JumpRun) #################################################################################################################### ## ## ## PLUGIN HOOKS ## ## ## #################################################################################################################### @unittest2.skipUnless(HAS_ENABLE_DISABLE_HOOKS, "B3 %s doesn't provide onDisable() plugin hook" % b3_version) def test_plugin_disable(self): # WHEN event = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START') self.console.queueEvent(Event(event, client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(Event(event, client=self.bill, data={'way_id' : '1'})) self.p.disable() # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(False, self.bill.isvar(self.p, 'jumprun')) @unittest2.skipUnless(HAS_ENABLE_DISABLE_HOOKS, "B3 %s doesn't provide onEnable() plugin hook" % b3_version) def test_plugin_enable(self): # GIVEN self.p.console.write = Mock() self.p.disable() self.p.settings['cycle_count'] = 0 self.console.game.mapName = 'ut4_casa' # WHEN self.p.enable() self.p.console.write.assert_called_with('cyclemap') self.assertEqual(1, self.p.settings['cycle_count'])
class Test_commands(CalladminTestCase): def setUp(self): CalladminTestCase.setUp(self) self.conf = CfgConfigParser() self.conf.loadFromString(dedent(r""" [teamspeak] ip: 127.0.0.1 port: 10011 serverid: 1 username: fakeusername password: fakepassword msg_groupid: -1 [settings] treshold: 3600 useirc: no [commands] calladmin: user """)) self.p = CalladminPlugin(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) #################################################################################################################### ## ## ## TEST EVT_CLIENT_CONNECT ## ## ## #################################################################################################################### def test_admin_connect_with_active_request(self): # GIVEN self.p.send_teamspeak_message = Mock() self.mike.connects('1') self.mike.says('!calladmin test reason') # WHEN self.mike.clearMessageHistory() self.bill.connects('2') self.bill.auth() # THEN self.p.send_teamspeak_message.assert_has_calls([call('[B][ADMIN REQUEST][/B] [B]Mike[/B] requested an admin on [B]Test Server[/B] : [B]test reason[/B]')]) self.p.send_teamspeak_message.assert_has_calls([call('[B][ADMIN REQUEST][/B] [B]Bill [40][/B] connected to [B]Test Server[/B]')]) self.assertListEqual(['[ADMIN ONLINE] Bill [40]'], self.mike.message_history) self.assertIsNone(self.p.adminRequest) #################################################################################################################### ## ## ## TEST EVT_CLIENT_DISCONNECT ## ## ## #################################################################################################################### def test_client_disconnect_with_active_request(self): # GIVEN self.p.send_teamspeak_message = Mock() self.mike.connects('1') self.mike.says('!calladmin test reason') # WHEN self.mike.disconnects() # THEN self.p.send_teamspeak_message.assert_has_calls([call('[B][ADMIN REQUEST][/B] [B]Mike[/B] disconnected from [B]Test Server[/B]')]) self.assertIsNone(self.p.adminRequest)
class SpawnkillTestCase(unittest.TestCase): @classmethod def setUpClass(cls): with logging_disabled(): from b3.parsers.q3a.abstractParser import AbstractParser from b3.fake import FakeConsole AbstractParser.__bases__ = (FakeConsole, ) # Now parser inheritance hierarchy is : # Iourt42Parser -> abstractParser -> FakeConsole -> Parser def setUp(self): # create a Iourt42 parser self.parser_conf = XmlConfigParser() self.parser_conf.loadFromString( dedent(r""" <configuration> <settings name="server"> <set name="game_log"></set> </settings> </configuration> """)) 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('g_maxGameClients', value='/fake/basepath')) when(self.console).getCvar('fs_homepath').thenReturn( Cvar('sv_maxclients', 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() self.admin_plugin_conf = CfgConfigParser() self.admin_plugin_conf.loadFromString( dedent(r""" [warn] pm_global: yes alert_kick_num: 3 instant_kick_num: 5 tempban_num: 6 tempban_duration: 1d max_duration: 1d message: ^1WARNING^7 [^3$warnings^7]: $reason warn_delay: 15 reason: ^7too many warnings: $reason duration_divider: 30 alert: ^1ALERT^7: $name^7 auto-kick from warnings if not cleared [^3$warnings^7] $reason warn_command_abusers: no""")) with logging_disabled(): self.adminPlugin = AdminPlugin(self.console, self.admin_plugin_conf) 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=1) self.bill = FakeClient(console=self.console, name="Bill", guid="billguid", team=TEAM_BLUE, groupBits=1) self.mark = FakeClient(console=self.console, name="Mark", guid="markguid", team=TEAM_BLUE, groupBits=128) self.conf = CfgConfigParser() self.p = SpawnkillPlugin(self.console, self.conf) def tearDown(self): self.console.working = False self.mike.disconnects() self.bill.disconnects() self.mark.disconnects() def init(self, config_content=None): if config_content: self.conf.loadFromString(config_content) else: self.conf.loadFromString( dedent(r""" [hit] maxlevel: admin delay: 2 penalty: warn duration: 3m reason: do not shoot to spawning players! [kill] maxlevel: admin delay: 3 penalty: warn duration: 5m reason: spawnkilling is not allowed on this server! """)) self.p.onLoadConfig() self.p.onStartup()
class CallvoteTestCase(unittest2.TestCase): @classmethod def setUpClass(cls): with logging_disabled(): from b3.parsers.q3a.abstractParser import AbstractParser from b3.fake import FakeConsole AbstractParser.__bases__ = (FakeConsole,) # Now parser inheritance hierarchy is : # Iourt41Parser -> abstractParser -> FakeConsole -> Parser 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 tearDown(self): self.console.working = False self.mike.disconnects() self.bill.disconnects() self.mark.disconnects() self.sara.disconnects() def init(self, config_content=None): if config_content: self.conf.loadFromString(config_content) else: self.conf.loadFromString(dedent(r""" [callvoteminlevel] capturelimit: guest clientkick: guest clientkickreason: guest cyclemap: guest exec: guest fraglimit: guest kick: guest map: guest reload: guest restart: guest shuffleteams: guest swapteams: guest timelimit: guest g_bluewaverespawndelay: guest g_bombdefusetime: guest g_bombexplodetime: guest g_capturescoretime: guest g_friendlyfire: guest g_followstrict: guest g_gametype: guest g_gear: guest g_matchmode: guest g_maxrounds: guest g_nextmap: guest g_redwaverespawndelay: guest g_respawndelay: guest g_roundtime: guest g_timeouts: guest g_timeoutlength: guest g_swaproles: guest g_waverespawns: guest [callvotespecialmaplist] #ut4_abbey: guest #ut4_abbeyctf: guest [commands] lastvote: mod veto: mod """)) self.p.onLoadConfig() self.p.onStartup() # return a fixed timestamp when(self.p).getTime().thenReturn(1399725576)
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 FirstKillCase(unittest2.TestCase): @classmethod def setUpClass(cls): with logging_disabled(): from b3.parsers.q3a.abstractParser import AbstractParser from b3.fake import FakeConsole AbstractParser.__bases__ = (FakeConsole, ) # Now parser inheritance hierarchy is : # Iourt41Parser -> abstractParser -> FakeConsole -> Parser 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") def tearDown(self): self.console.working = False self.mike.disconnects() self.bill.disconnects() self.mark.disconnects()
class Test_events(JumperTestCase): def setUp(self): JumperTestCase.setUp(self) with logging_disabled(): from b3.fake import FakeClient # create some clients self.mike = FakeClient(console=self.console, name="Mike", guid="mikeguid", team=TEAM_FREE, groupBits=1) self.bill = FakeClient(console=self.console, name="Bill", guid="billguid", team=TEAM_FREE, groupBits=1) self.mike.connects("1") self.bill.connects("2") # force fake mapname self.console.game.mapName = 'ut42_bstjumps_u2' def tearDown(self): self.mike.disconnects() self.bill.disconnects() JumperTestCase.tearDown(self) #################################################################################################################### # # # JUMPRUN EVENTS # # # #################################################################################################################### def test_event_client_jumprun_started(self): # WHEN self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id': '1'})) # THEN self.assertEqual(True, self.mike.isvar(self.p, 'jumprun')) self.assertIsNone(self.mike.var(self.p, 'jumprun').value.demo) self.assertIsInstance(self.mike.var(self.p, 'jumprun').value, JumpRun) def test_event_client_jumprun_stopped(self): # WHEN self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={ 'way_id': '1', 'way_time': '12345' })) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertListEqual([], self.p.getClientRecords( self.mike, self.console.game.mapName)) def test_event_client_jumprun_canceled(self): # WHEN self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_CANCEL', client=self.mike, data={'way_id': '1'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertListEqual([], self.p.getClientRecords( self.mike, self.console.game.mapName)) def test_event_client_jumprun_started_stopped(self): # WHEN self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id': '1'})) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={ 'way_id': '1', 'way_time': '12345' })) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual( 1, len(self.p.getClientRecords(self.mike, self.console.game.mapName))) def test_event_client_jumprun_started_canceled(self): # WHEN self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id': '1'})) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_CANCEL', client=self.mike, data={'way_id': '1'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual( 0, len(self.p.getClientRecords(self.mike, self.console.game.mapName))) def test_event_client_jumprun_started_stopped_multiple_clients(self): # WHEN self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id': '1'})) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={ 'way_id': '1', 'way_time': '12345' })) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id': '1'})) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.bill, data={ 'way_id': '1', 'way_time': '12345' })) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(False, self.bill.isvar(self.p, 'jumprun')) self.assertEqual( 1, len(self.p.getClientRecords(self.mike, self.console.game.mapName))) self.assertEqual( 1, len(self.p.getClientRecords(self.bill, self.console.game.mapName))) def test_event_client_jumprun_started_stopped_multiple_ways(self): # WHEN self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id': '1'})) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={ 'way_id': '1', 'way_time': '12345' })) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id': '2'})) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={ 'way_id': '2', 'way_time': '12345' })) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual( 2, len(self.p.getClientRecords(self.mike, self.console.game.mapName))) def test_event_client_jumprun_started_stopped_multiple_clients_multiple_ways( self): # WHEN self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id': '1'})) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={ 'way_id': '1', 'way_time': '12345' })) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id': '2'})) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={ 'way_id': '2', 'way_time': '12345' })) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id': '1'})) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.bill, data={ 'way_id': '1', 'way_time': '12345' })) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id': '2'})) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.bill, data={ 'way_id': '2', 'way_time': '12345' })) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(False, self.bill.isvar(self.p, 'jumprun')) self.assertEqual( 2, len(self.p.getClientRecords(self.mike, self.console.game.mapName))) self.assertEqual( 2, len(self.p.getClientRecords(self.bill, self.console.game.mapName))) #################################################################################################################### # # # OTHER EVENTS # # # #################################################################################################################### def test_event_game_map_change(self): # WHEN self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id': '1'})) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id': '1'})) self.console.queueEvent( self.console.getEvent( 'EVT_GAME_MAP_CHANGE', data= '\sv_allowdownload\0\g_matchmode\0\g_gametype\9\sv_maxclients\32\sv_floodprotect\1' )) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(False, self.bill.isvar(self.p, 'jumprun')) self.assertListEqual([], self.p.getClientRecords( self.mike, self.console.game.mapName)) self.assertListEqual([], self.p.getClientRecords( self.bill, self.console.game.mapName)) def test_event_client_disconnect(self): # WHEN self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id': '1'})) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_DISCONNECT', client=self.mike, data=None)) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) def test_event_client_team_change(self): # WHEN self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id': '1'})) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id': '1'})) self.mike.team = TEAM_SPEC # will raise EVT_CLIENT_TEAM_CHANGE self.bill.team = TEAM_FREE # will not raise EVT_CLIENT_TEAM_CHANGE # THEN self.assertEqual(TEAM_SPEC, self.mike.team) self.assertEqual(TEAM_FREE, self.bill.team) self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(True, self.bill.isvar(self.p, 'jumprun')) self.assertIsInstance(self.bill.var(self.p, 'jumprun').value, JumpRun) #################################################################################################################### # # # PLUGIN HOOKS # # # #################################################################################################################### def test_plugin_disable(self): # WHEN self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id': '1'})) self.console.queueEvent( self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id': '1'})) self.p.disable() # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(False, self.bill.isvar(self.p, 'jumprun')) def test_plugin_enable(self): # GIVEN self.p.console.write = Mock() self.p.disable() self.p._cycle_count = 0 self.console.game.mapName = 'ut4_casa' # WHEN self.p.enable() self.p.console.write.assert_called_with('cyclemap') self.assertEqual(1, self.p._cycle_count)
class Test_events(CallvoteTestCase): def setUp(self): CallvoteTestCase.setUp(self) 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 init(self, config_content=None): if config_content: self.conf.loadFromString(config_content) else: self.conf.loadFromString( dedent(r""" [callvoteminlevel] capturelimit: guest clientkick: guest clientkickreason: guest cyclemap: guest exec: guest fraglimit: guest kick: guest map: guest reload: guest restart: guest shuffleteams: guest swapteams: guest timelimit: guest g_bluewaverespawndelay: guest g_bombdefusetime: guest g_bombexplodetime: guest g_capturescoretime: guest g_friendlyfire: guest g_followstrict: guest g_gametype: guest g_gear: guest g_matchmode: guest g_maxrounds: guest g_nextmap: guest g_redwaverespawndelay: guest g_respawndelay: guest g_roundtime: guest g_timeouts: guest g_timeoutlength: guest g_swaproles: guest g_waverespawns: guest [callvotespecialmaplist] #ut4_abbey: guest #ut4_abbeyctf: guest [commands] lastvote: mod veto: mod """)) self.p.onLoadConfig() self.p.onStartup() # return a fixed timestamp when(self.p).getTime().thenReturn(1399725576) def tearDown(self): self.mike.disconnects() self.bill.disconnects() self.mark.disconnects() self.sara.disconnects() CallvoteTestCase.tearDown(self) #################################################################################################################### ## ## ## TEST CASES ## ## ## #################################################################################################################### def test_client_callvote_legit(self): # GIVEN self.init() self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.console.parseLine('''Callvote: 1 - "map ut4_dressingroom"''') # THEN self.assertIsNotNone(self.p.callvote) self.assertEqual(self.mike, self.p.callvote['client']) self.assertEqual('map', self.p.callvote['type']) self.assertEqual('ut4_dressingroom', self.p.callvote['args']) self.assertEqual(1399725576, self.p.callvote['time']) self.assertEqual(3, self.p.callvote['max_num']) def test_client_callvote_not_enough_level(self): # GIVEN self.init( dedent(r""" [callvoteminlevel] clientkick: admin clientkickreason: admin kick: admin """)) self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.sara.clearMessageHistory() self.console.parseLine('''Callvote: 4 - "kick bill"''') # THEN self.assertIsNone(self.p.callvote) self.assertListEqual( ["You can't issue this callvote. Required level: Admin"], self.sara.message_history) def test_client_callvote_map_not_enough_level(self): # GIVEN self.init( dedent(r""" [callvotespecialmaplist] ut4_abbey: admin ut4_abbeyctf: superadmin """)) self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.sara.clearMessageHistory() self.console.parseLine('''Callvote: 4 - "map ut4_abbey"''') # THEN self.assertIsNone(self.p.callvote) self.assertListEqual( ["You can't issue this callvote. Required level: Admin"], self.sara.message_history) def test_client_callvote_passed(self): # GIVEN self.init() self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.console.parseLine('''Callvote: 4 - "map ut4_casa"''') self.console.parseLine('''VotePassed: 3 - 0 - "map ut4_casa"''') # THEN self.assertIsNotNone(self.p.callvote) self.assertEqual(self.sara, self.p.callvote['client']) self.assertEqual('map', self.p.callvote['type']) self.assertEqual('ut4_casa', self.p.callvote['args']) self.assertEqual(1399725576, self.p.callvote['time']) self.assertEqual(4, self.p.callvote['max_num']) self.assertEqual(3, self.p.callvote['num_yes']) self.assertEqual(0, self.p.callvote['num_no']) def test_client_callvote_failed(self): # GIVEN self.init() self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.console.parseLine('''Callvote: 4 - "map ut4_casa"''') self.console.parseLine('''VotePassed: 1 - 3 - "map ut4_casa"''') # THEN self.assertIsNotNone(self.p.callvote) self.assertEqual(self.sara, self.p.callvote['client']) self.assertEqual('map', self.p.callvote['type']) self.assertEqual('ut4_casa', self.p.callvote['args']) self.assertEqual(1399725576, self.p.callvote['time']) self.assertEqual(4, self.p.callvote['max_num']) self.assertEqual(1, self.p.callvote['num_yes']) self.assertEqual(3, self.p.callvote['num_no']) def test_client_callvote_finish_with_none_callvote_object(self): # GIVEN self.init() self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.console.parseLine('''VotePassed: 3 - 0 - "map ut4_casa"''') # THEN self.assertIsNone(self.p.callvote) def test_client_callvote_finish_with_different_arguments(self): # GIVEN self.init() self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.console.parseLine('''Callvote: 4 - "map ut4_casa"''') self.console.parseLine('''VotePassed: 1 - 3 - "reload"''') # THEN self.assertIsNone(self.p.callvote)
class SpawnkillTestCase(unittest.TestCase): @classmethod def setUpClass(cls): with logging_disabled(): from b3.parsers.q3a.abstractParser import AbstractParser from b3.fake import FakeConsole AbstractParser.__bases__ = (FakeConsole,) # Now parser inheritance hierarchy is : # Iourt42Parser -> abstractParser -> FakeConsole -> Parser def setUp(self): # create a Iourt42 parser self.parser_conf = XmlConfigParser() self.parser_conf.loadFromString(dedent(r""" <configuration> <settings name="server"> <set name="game_log"></set> </settings> </configuration> """)) 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('g_maxGameClients', value='/fake/basepath')) when(self.console).getCvar('fs_homepath').thenReturn(Cvar('sv_maxclients', 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() self.admin_plugin_conf = CfgConfigParser() self.admin_plugin_conf.loadFromString(dedent(r""" [warn] pm_global: yes alert_kick_num: 3 instant_kick_num: 5 tempban_num: 6 tempban_duration: 1d max_duration: 1d message: ^1WARNING^7 [^3$warnings^7]: $reason warn_delay: 15 reason: ^7too many warnings: $reason duration_divider: 30 alert: ^1ALERT^7: $name^7 auto-kick from warnings if not cleared [^3$warnings^7] $reason warn_command_abusers: no""")) with logging_disabled(): self.adminPlugin = AdminPlugin(self.console, self.admin_plugin_conf) 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=1) self.bill = FakeClient(console=self.console, name="Bill", guid="billguid", team=TEAM_BLUE, groupBits=1) self.mark = FakeClient(console=self.console, name="Mark", guid="markguid", team=TEAM_BLUE, groupBits=128) self.conf = CfgConfigParser() self.p = SpawnkillPlugin(self.console, self.conf) def tearDown(self): self.console.working = False self.mike.disconnects() self.bill.disconnects() self.mark.disconnects() def init(self, config_content=None): if config_content: self.conf.loadFromString(config_content) else: self.conf.loadFromString(dedent(r""" [hit] maxlevel: admin delay: 2 penalty: warn duration: 3m reason: do not shoot to spawning players! [kill] maxlevel: admin delay: 3 penalty: warn duration: 5m reason: spawnkilling is not allowed on this server! """)) self.p.onLoadConfig() self.p.onStartup()
class Test_events(JumperTestCase): def setUp(self): JumperTestCase.setUp(self) with logging_disabled(): from b3.fake import FakeClient # create some clients self.mike = FakeClient(console=self.console, name="Mike", guid="mikeguid", team=TEAM_FREE, groupBits=1) self.bill = FakeClient(console=self.console, name="Bill", guid="billguid", team=TEAM_FREE, groupBits=1) self.mike.connects("1") self.bill.connects("2") # force fake mapname self.console.game.mapName = 'ut42_bstjumps_u2' def tearDown(self): self.mike.disconnects() self.bill.disconnects() JumperTestCase.tearDown(self) #################################################################################################################### # # # JUMPRUN EVENTS # # # #################################################################################################################### def test_event_client_jumprun_started(self): # WHEN self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'})) # THEN self.assertEqual(True, self.mike.isvar(self.p, 'jumprun')) self.assertIsNone(self.mike.var(self.p, 'jumprun').value.demo) self.assertIsInstance(self.mike.var(self.p, 'jumprun').value, JumpRun) def test_event_client_jumprun_stopped(self): # WHEN self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={'way_id' : '1', 'way_time' : '12345'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertListEqual([], self.p.getClientRecords(self.mike, self.console.game.mapName)) def test_event_client_jumprun_canceled(self): # WHEN self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_CANCEL', client=self.mike, data={'way_id' : '1'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertListEqual([], self.p.getClientRecords(self.mike, self.console.game.mapName)) def test_event_client_jumprun_started_stopped(self): # WHEN self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={'way_id' : '1', 'way_time' : '12345'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(1, len(self.p.getClientRecords(self.mike, self.console.game.mapName))) def test_event_client_jumprun_started_canceled(self): # WHEN self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_CANCEL', client=self.mike, data={'way_id' : '1'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(0, len(self.p.getClientRecords(self.mike, self.console.game.mapName))) def test_event_client_jumprun_started_stopped_multiple_clients(self): # WHEN self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={'way_id' : '1', 'way_time' : '12345'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id' : '1'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.bill, data={'way_id' : '1', 'way_time' : '12345'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(False, self.bill.isvar(self.p, 'jumprun')) self.assertEqual(1, len(self.p.getClientRecords(self.mike, self.console.game.mapName))) self.assertEqual(1, len(self.p.getClientRecords(self.bill, self.console.game.mapName))) def test_event_client_jumprun_started_stopped_multiple_ways(self): # WHEN self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={'way_id' : '1', 'way_time' : '12345'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '2'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={'way_id' : '2', 'way_time' : '12345'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(2, len(self.p.getClientRecords(self.mike, self.console.game.mapName))) def test_event_client_jumprun_started_stopped_multiple_clients_multiple_ways(self): # WHEN self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={'way_id' : '1', 'way_time' : '12345'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '2'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={'way_id' : '2', 'way_time' : '12345'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id' : '1'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.bill, data={'way_id' : '1', 'way_time' : '12345'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id' : '2'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.bill, data={'way_id' : '2', 'way_time' : '12345'})) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(False, self.bill.isvar(self.p, 'jumprun')) self.assertEqual(2, len(self.p.getClientRecords(self.mike, self.console.game.mapName))) self.assertEqual(2, len(self.p.getClientRecords(self.bill, self.console.game.mapName))) #################################################################################################################### # # # OTHER EVENTS # # # #################################################################################################################### def test_event_game_map_change(self): # WHEN self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id' : '1'})) self.console.queueEvent(self.console.getEvent('EVT_GAME_MAP_CHANGE', data='\sv_allowdownload\0\g_matchmode\0\g_gametype\9\sv_maxclients\32\sv_floodprotect\1')) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(False, self.bill.isvar(self.p, 'jumprun')) self.assertListEqual([], self.p.getClientRecords(self.mike, self.console.game.mapName)) self.assertListEqual([], self.p.getClientRecords(self.bill, self.console.game.mapName)) def test_event_client_disconnect(self): # WHEN self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_DISCONNECT', client=self.mike, data=None)) # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) def test_event_client_team_change(self): # WHEN self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id' : '1'})) self.mike.team = TEAM_SPEC # will raise EVT_CLIENT_TEAM_CHANGE self.bill.team = TEAM_FREE # will not raise EVT_CLIENT_TEAM_CHANGE # THEN self.assertEqual(TEAM_SPEC, self.mike.team) self.assertEqual(TEAM_FREE, self.bill.team) self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(True, self.bill.isvar(self.p, 'jumprun')) self.assertIsInstance(self.bill.var(self.p, 'jumprun').value, JumpRun) #################################################################################################################### # # # PLUGIN HOOKS # # # #################################################################################################################### def test_plugin_disable(self): # WHEN self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'})) self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id' : '1'})) self.p.disable() # THEN self.assertEqual(False, self.mike.isvar(self.p, 'jumprun')) self.assertEqual(False, self.bill.isvar(self.p, 'jumprun')) def test_plugin_enable(self): # GIVEN self.p.console.write = Mock() self.p.disable() self.p._cycle_count = 0 self.console.game.mapName = 'ut4_casa' # WHEN self.p.enable() self.p.console.write.assert_called_with('cyclemap') self.assertEqual(1, self.p._cycle_count)
class Test_events(CallvoteTestCase): def setUp(self): CallvoteTestCase.setUp(self) 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 init(self, config_content=None): if config_content: self.conf.loadFromString(config_content) else: self.conf.loadFromString(dedent(r""" [callvoteminlevel] capturelimit: guest clientkick: guest clientkickreason: guest cyclemap: guest exec: guest fraglimit: guest kick: guest map: guest reload: guest restart: guest shuffleteams: guest swapteams: guest timelimit: guest g_bluewaverespawndelay: guest g_bombdefusetime: guest g_bombexplodetime: guest g_capturescoretime: guest g_friendlyfire: guest g_followstrict: guest g_gametype: guest g_gear: guest g_matchmode: guest g_maxrounds: guest g_nextmap: guest g_redwaverespawndelay: guest g_respawndelay: guest g_roundtime: guest g_timeouts: guest g_timeoutlength: guest g_swaproles: guest g_waverespawns: guest [callvotespecialmaplist] #ut4_abbey: guest #ut4_abbeyctf: guest [commands] lastvote: mod veto: mod """)) self.p.onLoadConfig() self.p.onStartup() # return a fixed timestamp when(self.p).getTime().thenReturn(1399725576) def tearDown(self): self.mike.disconnects() self.bill.disconnects() self.mark.disconnects() self.sara.disconnects() CallvoteTestCase.tearDown(self) #################################################################################################################### ## ## ## TEST CASES ## ## ## #################################################################################################################### def test_client_callvote_legit(self): # GIVEN self.init() self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.console.parseLine('''Callvote: 1 - "map ut4_dressingroom"''') # THEN self.assertIsNotNone(self.p.callvote) self.assertEqual(self.mike, self.p.callvote['client']) self.assertEqual('map', self.p.callvote['type']) self.assertEqual('ut4_dressingroom', self.p.callvote['args']) self.assertEqual(1399725576, self.p.callvote['time']) self.assertEqual(3, self.p.callvote['max_num']) def test_client_callvote_not_enough_level(self): # GIVEN self.init(dedent(r""" [callvoteminlevel] clientkick: admin clientkickreason: admin kick: admin """)) self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.sara.clearMessageHistory() self.console.parseLine('''Callvote: 4 - "kick bill"''') # THEN self.assertIsNone(self.p.callvote) self.assertListEqual(["You can't issue this callvote. Required level: Admin"], self.sara.message_history) def test_client_callvote_map_not_enough_level(self): # GIVEN self.init(dedent(r""" [callvotespecialmaplist] ut4_abbey: admin ut4_abbeyctf: superadmin """)) self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.sara.clearMessageHistory() self.console.parseLine('''Callvote: 4 - "map ut4_abbey"''') # THEN self.assertIsNone(self.p.callvote) self.assertListEqual(["You can't issue this callvote. Required level: Admin"], self.sara.message_history) def test_client_callvote_passed(self): # GIVEN self.init() self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.console.parseLine('''Callvote: 4 - "map ut4_casa"''') self.console.parseLine('''VotePassed: 3 - 0 - "map ut4_casa"''') # THEN self.assertIsNotNone(self.p.callvote) self.assertEqual(self.sara, self.p.callvote['client']) self.assertEqual('map', self.p.callvote['type']) self.assertEqual('ut4_casa', self.p.callvote['args']) self.assertEqual(1399725576, self.p.callvote['time']) self.assertEqual(4, self.p.callvote['max_num']) self.assertEqual(3, self.p.callvote['num_yes']) self.assertEqual(0, self.p.callvote['num_no']) def test_client_callvote_failed(self): # GIVEN self.init() self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.console.parseLine('''Callvote: 4 - "map ut4_casa"''') self.console.parseLine('''VotePassed: 1 - 3 - "map ut4_casa"''') # THEN self.assertIsNotNone(self.p.callvote) self.assertEqual(self.sara, self.p.callvote['client']) self.assertEqual('map', self.p.callvote['type']) self.assertEqual('ut4_casa', self.p.callvote['args']) self.assertEqual(1399725576, self.p.callvote['time']) self.assertEqual(4, self.p.callvote['max_num']) self.assertEqual(1, self.p.callvote['num_yes']) self.assertEqual(3, self.p.callvote['num_no']) def test_client_callvote_finish_with_none_callvote_object(self): # GIVEN self.init() self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.console.parseLine('''VotePassed: 3 - 0 - "map ut4_casa"''') # THEN self.assertIsNone(self.p.callvote) def test_client_callvote_finish_with_different_arguments(self): # GIVEN self.init() self.mike.connects("1") self.bill.connects("2") self.mark.connects("3") self.sara.connects("4") # WHEN self.console.parseLine('''Callvote: 4 - "map ut4_casa"''') self.console.parseLine('''VotePassed: 1 - 3 - "reload"''') # THEN self.assertIsNone(self.p.callvote)
class IpbanTestCase(unittest2.TestCase): def setUp(self): if B3version(b3_version) >= B3version("1.10dev"): from b3.config import MainConfig self.parser_conf = MainConfig(CfgConfigParser(allow_no_value=True)) self.parser_conf.loadFromString(dedent(r"""""")) admin_plugin_conf_file = '@b3/conf/plugin_admin.ini' else: self.parser_conf = XmlConfigParser() self.parser_conf.loadFromString( dedent(r"""<configuration></configuration>""")) admin_plugin_conf_file = '@b3/conf/plugin_admin.xml' self.console = FakeConsole(self.parser_conf) self.console.gameName = 'f00' self.console.startup() with logging_disabled(): self.adminPlugin = AdminPlugin(self.console, admin_plugin_conf_file) 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() #################################################################################################################### # # # TEST CONFIG # # # #################################################################################################################### def test_with_config_content(self): # GIVEN self.init( dedent(r""" [settings] maxlevel: admin """)) # THEN self.assertEqual(self.p._maxLevel, 40) def test_with_empty_config(self): # GIVEN self.init(dedent(r""" [settings] """)) # THEN self.assertEqual(self.p._maxLevel, 1) #################################################################################################################### # # # TEST EVENTS # # # #################################################################################################################### def test_higher_group_level_client_connect(self): # GIVEN self.init() self.mike.kick = Mock() # WHEN self.mike.connects('1') # THEN self.assertGreater(self.mike.maxLevel, self.p._maxLevel) self.assertEqual(self.mike.kick.call_count, 0) def test_ip_banned_client_connect(self): # GIVEN self.init() self.paul.kick = Mock() # WHEN self.paul.connects('1') # THEN self.assertLessEqual(self.paul.maxLevel, self.p._maxLevel) self.assertIn(self.paul.ip, self.p.getBanIps()) self.paul.kick.assert_called_once_with( 'Netblocker: Client refused: 2.2.2.2 (Paul) has an active Ban') def test_ip_temp_banned_client_connect(self): # GIVEN self.init() self.john.kick = Mock() # WHEN self.john.connects('1') # THEN self.assertLessEqual(self.paul.maxLevel, self.p._maxLevel) self.assertIn(self.john.ip, self.p.getTempBanIps()) self.john.kick.assert_called_once_with( 'Netblocker: Client refused: 3.3.3.3 (John) has an active TempBan') def test_clean_client_connect(self): # GIVEN self.init() self.mary.kick = Mock() # WHEN self.mary.connects('1') # THEN self.assertLessEqual(self.paul.maxLevel, self.p._maxLevel) self.assertNotIn(self.mary.ip, self.p.getBanIps()) self.assertNotIn(self.mary.ip, self.p.getTempBanIps()) self.assertEqual(self.mary.kick.call_count, 0)