class mixin_cmd_version(object):
    def setUp(self):
        super(mixin_cmd_version, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
paversion-version: 20
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.sleep_patcher = patch.object(time, 'sleep')
        self.sleep_patcher.start()

        self.console.say = Mock()
        self.console.saybig = Mock()
        self.console.write = Mock()

        self.moderator.connects("2")


    def tearDown(self):
        super(mixin_cmd_version, self).tearDown()
        self.sleep_patcher.stop()


    def test_nominal(self):
        self.moderator.message_history = []
        self.moderator.says("!version")
        self.assertEqual(['I am PowerAdminUrt version %s by %s' % (plugin_version, plugin_author)], self.moderator.message_history)
Exemple #2
0
class Test_cmd_instagib(Iourt43TestCase):
    def setUp(self):
        super(Test_cmd_instagib, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
painstagib-instagib: 20           ; change game mode to Instagib
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.console.say = Mock()
        self.console.write = Mock()

        self.moderator.connects("2")

    def test_nominal(self):
        self.moderator.message_history = []
        self.moderator.says("!instagib on")
        self.console.write.assert_has_calls([call('set g_instagib "1"')])

        self.moderator.says("!instagib off")
        self.console.write.assert_has_calls([call('set g_instagib "0"')])
Exemple #3
0
class Test_cmd_jump(Iourt42TestCase):
    def setUp(self):
        super(Test_cmd_jump, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString(
            """
[commands]
pajump-jump: 20           ; change game type to Jump
        """
        )
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.console.say = Mock()
        self.console.write = Mock()

        self.moderator.connects("2")

    def test_nominal(self):
        self.moderator.message_history = []
        self.moderator.says("!jump")
        self.console.write.assert_has_calls([call('set g_gametype "9"')])
        self.assertEqual(["game type changed to Jump"], self.moderator.message_history)
Exemple #4
0
class mixin_cmd_version:
    def setUp(self):
        super(mixin_cmd_version, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
paversion-version: 20
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.sleep_patcher = patch.object(time, 'sleep')
        self.sleep_patcher.start()

        self.console.say = Mock()
        self.console.saybig = Mock()
        self.console.write = Mock()

        self.moderator.connects("2")

    def tearDown(self):
        super(mixin_cmd_version, self).tearDown()
        self.sleep_patcher.stop()

    def test_nominal(self):
        self.moderator.message_history = []
        self.moderator.says("!version")
        self.assertEqual(['I am PowerAdminUrt version %s by %s' % (plugin_version, plugin_author)],
                         self.moderator.message_history)
class Test_cmd_skins(Iourt42TestCase):
    def setUp(self):
        super(Test_cmd_skins, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
pagoto-goto: 20         ; set the goto <on/off>
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.console.say = Mock()
        self.console.write = Mock()

        self.moderator.connects("2")

    def test_missing_parameter(self):
        self.moderator.message_history = []
        self.moderator.says("!goto")
        self.assertListEqual(["invalid or missing data, try !help pagoto"], self.moderator.message_history)

    def test_junk(self):
        self.moderator.message_history = []
        self.moderator.says("!goto qsdf")
        self.assertListEqual(["invalid or missing data, try !help pagoto"], self.moderator.message_history)

    def test_on(self):
        self.moderator.says("!goto on")
        self.console.write.assert_has_calls([call('set g_allowgoto "1"')])

    def test_off(self):
        self.moderator.says("!goto off")
        self.console.write.assert_has_calls([call('set g_allowgoto "0"')])
Exemple #6
0
class mixin_cmd_nuke(object):

    def setUp(self):
        super(mixin_cmd_nuke, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
panuke-nuke: 20
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.sleep_patcher = patch.object(time, 'sleep')
        self.sleep_patcher.start()

        self.console.say = Mock()
        self.console.saybig = Mock()
        self.console.write = Mock()

        self.moderator.connects("2")

    def tearDown(self):
        super(mixin_cmd_nuke, self).tearDown()
        self.sleep_patcher.stop()

    def test_no_argument(self):
        self.moderator.message_history = []
        self.moderator.says("!nuke")
        self.assertEqual(['Invalid data, try !help panuke'], self.moderator.message_history)
        self.console.write.assert_has_calls([])

    def test_unknown_player(self):
        self.moderator.message_history = []
        self.moderator.says("!nuke f00")
        self.assertEqual(['No players found matching f00'], self.moderator.message_history)
        self.console.write.assert_has_calls([])

    def test_joe(self):
        self.joe.connects('3')
        self.moderator.message_history = []
        self.moderator.says("!nuke joe")
        self.assertEqual([], self.moderator.message_history)
        self.assertEqual([], self.joe.message_history)
        self.console.write.assert_has_calls([call('nuke 3')])

    def test_joe_multi(self):

        def _start_new_thread(function, args):
            function(*args)

        with patch.object(thread, 'start_new_thread', wraps=_start_new_thread):
            self.joe.connects('3')
            self.moderator.message_history = []
            self.moderator.says("!nuke joe 3")
            self.assertEqual([], self.moderator.message_history)
            self.assertEqual([], self.joe.message_history)
            self.console.write.assert_has_calls([call('nuke 3'), call('nuke 3'), call('nuke 3')])
class mixin_cmd_nuke(object):

    def setUp(self):
        super(mixin_cmd_nuke, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
panuke-nuke: 20
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.sleep_patcher = patch.object(time, 'sleep')
        self.sleep_patcher.start()

        self.console.say = Mock()
        self.console.saybig = Mock()
        self.console.write = Mock()

        self.moderator.connects("2")

    def tearDown(self):
        super(mixin_cmd_nuke, self).tearDown()
        self.sleep_patcher.stop()

    def test_no_argument(self):
        self.moderator.message_history = []
        self.moderator.says("!nuke")
        self.assertEqual(['Invalid data, try !help panuke'], self.moderator.message_history)
        self.console.write.assert_has_calls([])

    def test_unknown_player(self):
        self.moderator.message_history = []
        self.moderator.says("!nuke f00")
        self.assertEqual(['No players found matching f00'], self.moderator.message_history)
        self.console.write.assert_has_calls([])

    def test_joe(self):
        self.joe.connects('3')
        self.moderator.message_history = []
        self.moderator.says("!nuke joe")
        self.assertEqual([], self.moderator.message_history)
        self.assertEqual([], self.joe.message_history)
        self.console.write.assert_has_calls([call('nuke 3')])

    def test_joe_multi(self):

        def _start_new_thread(function, args):
            function(*args)

        with patch.object(thread, 'start_new_thread', wraps=_start_new_thread):
            self.joe.connects('3')
            self.moderator.message_history = []
            self.moderator.says("!nuke joe 3")
            self.assertEqual([], self.moderator.message_history)
            self.assertEqual([], self.joe.message_history)
            self.console.write.assert_has_calls([call('nuke 3'), call('nuke 3'), call('nuke 3')])
Exemple #8
0
class Test_cmd_ident(Iourt43TestCase):
    def setUp(self):
        super(Test_cmd_ident, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
paident-id: 20

[special]
paident_full_level: 40
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.parser_conf._settings.update({'b3': {"time_zone": "UTC", "time_format": "%I:%M%p %Z %m/%d/%y"}})
        self.p.onLoadConfig()
        self.p.onStartup()

        self.console.say = Mock()
        self.console.write = Mock()

        self.moderator.connects("2")
        self.moderator.message_history = []

    def test_no_parameter(self):
        # WHEN
        self.moderator.says("!id")
        # THEN
        self.assertListEqual(["Your id is @2"], self.moderator.message_history)

    def test_junk(self):
        # WHEN
        self.moderator.says("!id qsdfsqdq sqfd qf")
        # THEN
        self.assertListEqual(["No players found matching qsdfsqdq"], self.moderator.message_history)

    def test_nominal_under_full_level(self):
        # GIVEN
        self.joe.pbid = "joe_pbid"
        self.joe.connects('3')
        # WHEN
        with patch('time.time', return_value=0.0) as time_mock:
            self.moderator.says("!id joe")
        # THEN
        self.assertListEqual(['12:00AM UTC 01/01/70 @3 Joe'], self.moderator.message_history)

    def test_nominal_above_full_level(self):
        # GIVEN
        self.joe.pbid = "joe_pbid"
        self.joe.connects('3')
        self.joe.timeAdd = 90 * 60.0
        self.superadmin.connects('1')
        # WHEN
        with patch('time.time', return_value=180 * 60.0):
            self.superadmin.says("!id joe")
        # THEN
        self.assertListEqual(['03:00AM UTC 01/01/70 @3 Joe  [joe_pbid] since 01:30AM UTC 01/01/70'],
                             self.superadmin.message_history)
class Test_cmd_ident(Iourt41TestCase):
    def setUp(self):
        super(Test_cmd_ident, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
paident-id: 20

[special]
paident_full_level: 40
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.parser_conf._settings.update({'b3': {"time_zone": "GMT", "time_format": "%I:%M%p %Z %m/%d/%y"}})
        self.p.onLoadConfig()
        self.p.onStartup()

        self.console.say = Mock()
        self.console.write = Mock()

        self.moderator.connects("2")
        self.moderator.message_history = []

    def test_no_parameter(self):
        # WHEN
        self.moderator.says("!id")
        # THEN
        self.assertListEqual(["Your id is @2"], self.moderator.message_history)

    def test_junk(self):
        # WHEN
        self.moderator.says("!id qsdfsqdq sqfd qf")
        # THEN
        self.assertListEqual(["No players found matching qsdfsqdq"], self.moderator.message_history)

    def test_nominal_under_full_level(self):
        # GIVEN
        self.joe.pbid = "joe_pbid"
        self.joe.connects('3')
        # WHEN
        with patch('time.time', return_value=0.0) as time_mock:
            self.moderator.says("!id joe")
        # THEN
        self.assertListEqual(['12:00AM GMT 01/01/70 @3 Joe'], self.moderator.message_history)

    def test_nominal_above_full_level(self):
        # GIVEN
        self.joe.pbid = "joe_pbid"
        self.joe.connects('3')
        self.joe.timeAdd = 90*60.0
        self.superadmin.connects('1')
        # WHEN
        with patch('time.time', return_value=180*60.0):
            self.superadmin.says("!id joe")
        # THEN
        self.assertListEqual(['03:00AM GMT 01/01/70 @3 Joe  01:30AM GMT 01/01/70'], self.superadmin.message_history)
class mixin_cmd_pasetnextmap(object):
    def setUp(self):
        super(mixin_cmd_pasetnextmap, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
pasetnextmap-snmap: 20
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.sleep_patcher = patch.object(time, 'sleep')
        self.sleep_patcher.start()

        self.console.say = Mock()
        self.console.saybig = Mock()
        self.console.write = Mock()

        self.moderator.connects("2")


    def tearDown(self):
        super(mixin_cmd_pasetnextmap, self).tearDown()
        self.sleep_patcher.stop()


    def test_missing_parameter(self):
        self.moderator.clearMessageHistory()
        self.moderator.says("!snmap")
        self.assertEqual(['Invalid or missing data, try !help pasetnextmap'], self.moderator.message_history)


    def test_existing_map(self):
        # GIVEN
        when(self.console).getMapsSoundingLike('f00').thenReturn('f00')
        # WHEN
        self.moderator.clearMessageHistory()
        self.moderator.says("!snmap f00")
        # THEN
        verify(self.console).getMapsSoundingLike('f00')
        self.assertEqual(['nextmap set to f00'], self.moderator.message_history)


    def test_suggestions(self):
        # GIVEN
        when(self.console).getMapsSoundingLike('f00').thenReturn(['f001', 'foo2'])
        # WHEN
        self.moderator.clearMessageHistory()
        self.moderator.says("!snmap f00")
        # THEN
        verify(self.console).getMapsSoundingLike('f00')
        self.assertEqual(['do you mean : f001, foo2 ?'], self.moderator.message_history)
Exemple #11
0
class Test_cmd_captain(Iourt43TestCase):
    def setUp(self):
        super(Test_cmd_captain, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
pacaptain-captain: 40   ; set the the given client as the captain for its team
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()
        self.console.say = Mock()
        self.console.write = Mock()
        self.admin.connects("2")
        self.moderator.connects("3")

    def test_match_mode_deactivated(self):
        self.p._matchmode = False
        self.admin.message_history = []
        self.admin.says("!captain")
        self.assertListEqual(
            ["!pacaptain command is available only in match mode"],
            self.admin.message_history)

    def test_client_spectator(self):
        self.p._matchmode = True
        self.admin.message_history = []
        self.admin.team = TEAM_SPEC
        self.admin.says("!captain")
        self.assertListEqual(
            ["Level-40-Admin is a spectator! - Can't set captain status"],
            self.admin.message_history)

    def test_client_with_no_parameters(self):
        self.p._matchmode = True
        self.admin.message_history = []
        self.admin.team = TEAM_RED
        self.admin.says("!captain")
        self.console.write.assert_has_calls(
            [call('forcecaptain %s' % self.admin.cid)])

    def test_client_with_parameters(self):
        self.p._matchmode = True
        self.admin.message_history = []
        self.admin.team = TEAM_RED
        self.moderator.message_history = []
        self.moderator.team = TEAM_BLUE
        self.admin.says("!captain 3")
        self.console.write.assert_has_calls(
            [call('forcecaptain %s' % self.moderator.cid)])
        self.assertListEqual(
            ["You were set as captain for the BLUE team by the Admin"],
            self.moderator.message_history)
class mixin_cmd_pasetnextmap(object):
    def setUp(self):
        super(mixin_cmd_pasetnextmap, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
pasetnextmap-snmap: 20
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.sleep_patcher = patch.object(time, 'sleep')
        self.sleep_patcher.start()

        self.console.say = Mock()
        self.console.saybig = Mock()
        self.console.write = Mock()

        self.moderator.connects("2")

    def tearDown(self):
        super(mixin_cmd_pasetnextmap, self).tearDown()
        self.sleep_patcher.stop()

    def test_missing_parameter(self):
        self.moderator.clearMessageHistory()
        self.moderator.says("!snmap")
        self.assertEqual(['Invalid or missing data, try !help pasetnextmap'],
                         self.moderator.message_history)

    def test_existing_map(self):
        # GIVEN
        when(self.console).getMapsSoundingLike('f00').thenReturn('f00')
        # WHEN
        self.moderator.clearMessageHistory()
        self.moderator.says("!snmap f00")
        # THEN
        verify(self.console).getMapsSoundingLike('f00')
        self.assertEqual(['nextmap set to f00'],
                         self.moderator.message_history)

    def test_suggestions(self):
        # GIVEN
        when(self.console).getMapsSoundingLike('f00').thenReturn(
            ['f001', 'foo2'])
        # WHEN
        self.moderator.clearMessageHistory()
        self.moderator.says("!snmap f00")
        # THEN
        verify(self.console).getMapsSoundingLike('f00')
        self.assertEqual(['do you mean : f001, foo2 ?'],
                         self.moderator.message_history)
class mixin_cmd_paset(object):
    def setUp(self):
        super(mixin_cmd_paset, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
paset: 20
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.sleep_patcher = patch.object(time, 'sleep')
        self.sleep_patcher.start()
        self.setCvar_patcher = patch.object(self.console, 'setCvar')
        self.setCvar_mock = self.setCvar_patcher.start()

        self.moderator.connects("2")

    def assert_setCvar_calls(self, expected_calls):
        self.assertListEqual(expected_calls, self.setCvar_mock.mock_calls)

    def tearDown(self):
        super(mixin_cmd_paset, self).tearDown()
        self.sleep_patcher.stop()
        self.setCvar_patcher.stop()


    def test_nominal(self):
        # WHEN
        self.moderator.says('!paset sv_foo bar')
        # THEN
        self.assert_setCvar_calls([call('sv_foo', 'bar')])
        self.assertListEqual([], self.moderator.message_history)

    def test_no_parameter(self):
        # WHEN
        self.moderator.says('!paset')
        # THEN
        self.assert_setCvar_calls([])
        self.assertListEqual(['Invalid or missing data, try !help paset'], self.moderator.message_history)

    def test_no_value(self):
        # WHEN
        self.moderator.says('!paset sv_foo')
        # THEN
        self.assert_setCvar_calls([call('sv_foo', '')])
        self.assertListEqual([], self.moderator.message_history)
Exemple #14
0
class mixin_cmd_paset(object):
    def setUp(self):
        super(mixin_cmd_paset, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
paset: 20
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.sleep_patcher = patch.object(time, 'sleep')
        self.sleep_patcher.start()
        self.setCvar_patcher = patch.object(self.console, 'setCvar')
        self.setCvar_mock = self.setCvar_patcher.start()

        self.moderator.connects("2")

    def assert_setCvar_calls(self, expected_calls):
        self.assertListEqual(expected_calls, self.setCvar_mock.mock_calls)

    def tearDown(self):
        super(mixin_cmd_paset, self).tearDown()
        self.sleep_patcher.stop()
        self.setCvar_patcher.stop()

    def test_nominal(self):
        # WHEN
        self.moderator.says('!paset sv_foo bar')
        # THEN
        self.assert_setCvar_calls([call('sv_foo', 'bar')])
        self.assertListEqual([], self.moderator.message_history)

    def test_no_parameter(self):
        # WHEN
        self.moderator.says('!paset')
        # THEN
        self.assert_setCvar_calls([])
        self.assertListEqual(['Invalid or missing data, try !help paset'],
                             self.moderator.message_history)

    def test_no_value(self):
        # WHEN
        self.moderator.says('!paset sv_foo')
        # THEN
        self.assert_setCvar_calls([call('sv_foo', '')])
        self.assertListEqual([], self.moderator.message_history)
class Test_cmd_captain(Iourt42TestCase):
    def setUp(self):
        super(Test_cmd_captain, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
pacaptain-captain: 40   ; set the the given client as the captain for its team
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()
        self.console.say = Mock()
        self.console.write = Mock()
        self.admin.connects("2")
        self.moderator.connects("3")

    def test_match_mode_deactivated(self):
        self.p._matchmode = False
        self.admin.message_history = []
        self.admin.says("!captain")
        self.assertListEqual(["!pacaptain command is available only in match mode"], self.admin.message_history)

    def test_client_spectator(self):
        self.p._matchmode = True
        self.admin.message_history = []
        self.admin.team = TEAM_SPEC
        self.admin.says("!captain")
        self.assertListEqual(["Level-40-Admin is a spectator! - Can't set captain status"], self.admin.message_history)

    def test_client_with_no_parameters(self):
        self.p._matchmode = True
        self.admin.message_history = []
        self.admin.team = TEAM_RED
        self.admin.says("!captain")
        self.console.write.assert_has_calls([call('forcecaptain %s' % self.admin.cid)])

    def test_client_with_parameters(self):
        self.p._matchmode = True
        self.admin.message_history = []
        self.admin.team = TEAM_RED
        self.moderator.message_history = []
        self.moderator.team = TEAM_BLUE
        self.admin.says("!captain 3")
        self.console.write.assert_has_calls([call('forcecaptain %s' % self.moderator.cid)])
        self.assertListEqual(["You were set as captain for the BLUE team by the Admin"], self.moderator.message_history)
class Test_cmd_kill(Iourt42TestCase):
    def setUp(self):
        super(Test_cmd_kill, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
pakill-kill: 20
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.console.say = Mock()
        self.console.write = Mock()

        self.moderator.connects("2")


    def tearDown(self):
        super(Test_cmd_kill, self).tearDown()


    def test_no_argument(self):
        self.moderator.message_history = []
        self.moderator.says("!kill")
        self.assertEqual(['invalid data, try !help pakill'], self.moderator.message_history)
        self.console.write.assert_has_calls([])


    def test_unknown_player(self):
        self.moderator.message_history = []
        self.moderator.says("!kill f00")
        self.assertEqual(['No players found matching f00'], self.moderator.message_history)
        self.console.write.assert_has_calls([])

    def test_joe(self):
        self.joe.connects('3')
        self.moderator.message_history = []
        self.moderator.says("!kill joe")
        self.assertEqual([], self.moderator.message_history)
        self.assertEqual([], self.joe.message_history)
        self.console.write.assert_has_calls([call('smite 3')])
class Test_cmd_funstuff(Iourt42TestCase):
    def setUp(self):
        super(Test_cmd_funstuff, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
pastamina-stamina: 20   ; set the stamina behavior <default/regain/infinite>
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.console.say = Mock()
        self.console.write = Mock()

        self.moderator.connects("2")

    def test_missing_parameter(self):
        self.moderator.message_history = []
        self.moderator.says("!stamina")
        self.assertListEqual(["invalid or missing data, try !help pastamina"],
                             self.moderator.message_history)

    def test_junk(self):
        self.moderator.message_history = []
        self.moderator.says("!stamina qsdf")
        self.assertListEqual(["invalid or missing data, try !help pastamina"],
                             self.moderator.message_history)

    def test_default(self):
        self.moderator.says("!stamina default")
        self.console.write.assert_has_calls([call('set g_stamina "0"')])

    def test_regain(self):
        self.moderator.says("!stamina regain")
        self.console.write.assert_has_calls([call('set g_stamina "1"')])

    def test_infinite(self):
        self.moderator.says("!stamina infinite")
        self.console.write.assert_has_calls([call('set g_stamina "2"')])
Exemple #18
0
class Test_cmd_swap(Iourt43TestCase):
    def setUp(self):
        super(Test_cmd_swap, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
paswap-swap: 20
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()
        self.console.say = Mock()
        self.console.write = Mock()
        self.admin.connects("2")
        self.moderator.connects("3")

    def test_plugin_using_overridden_command_method(self):
        self.admin.team = TEAM_RED
        self.moderator.team = TEAM_BLUE
        self.admin.says("!swap 2 3")
        self.console.write.assert_has_calls([call('swap %s %s' % (self.admin.cid, self.moderator.cid))])
class Test_cmd_funstuff(Iourt42TestCase):
    def setUp(self):
        super(Test_cmd_funstuff, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
pastamina-stamina: 20   ; set the stamina behavior <default/regain/infinite>
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.console.say = Mock()
        self.console.write = Mock()

        self.moderator.connects("2")

    def test_missing_parameter(self):
        self.moderator.message_history = []
        self.moderator.says("!stamina")
        self.assertListEqual(["invalid or missing data, try !help pastamina"], self.moderator.message_history)

    def test_junk(self):
        self.moderator.message_history = []
        self.moderator.says("!stamina qsdf")
        self.assertListEqual(["invalid or missing data, try !help pastamina"], self.moderator.message_history)

    def test_default(self):
        self.moderator.says("!stamina default")
        self.console.write.assert_has_calls([call('set g_stamina "0"')])

    def test_regain(self):
        self.moderator.says("!stamina regain")
        self.console.write.assert_has_calls([call('set g_stamina "1"')])

    def test_infinite(self):
        self.moderator.says("!stamina infinite")
        self.console.write.assert_has_calls([call('set g_stamina "2"')])
Exemple #20
0
class Test_cmd_gungame(Iourt43TestCase):

    def setUp(self):
        super(Test_cmd_gungame, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
pagungame-gungame: 20           ; change game type to Gun Game
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.console.say = Mock()
        self.console.write = Mock()

        self.moderator.connects("2")

    def test_nominal(self):
        self.moderator.message_history = []
        self.moderator.says("!gungame")
        self.console.write.assert_has_calls([call('set g_gametype "11"')])
        self.assertEqual(['game type changed to Gun Game'], self.moderator.message_history)
class Test_cmd_pagear(Iourt42TestCase):

    def setUp(self):
        super(Test_cmd_pagear, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
pagear-gear: 20
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)

        when(self.console).getCvar('timelimit').thenReturn(Cvar('timelimit', value=20))
        when(self.console).getCvar('g_maxGameClients').thenReturn(Cvar('g_maxGameClients', value=16))
        when(self.console).getCvar('sv_maxclients').thenReturn(Cvar('sv_maxclients', value=16))
        when(self.console).getCvar('sv_privateClients').thenReturn(Cvar('sv_privateClients', value=0))
        when(self.console).getCvar('g_allowvote').thenReturn(Cvar('g_allowvote', value=0))
        when(self.console).getCvar('g_modversion').thenReturn(Cvar('g_modversion', value="4.2.018"))
        self.given_forbidden_weapon_are(G_NONE)
        self.p.onLoadConfig()
        self.p.onStartup()

        self.sleep_patcher = patch.object(time, 'sleep')
        self.sleep_patcher.start()
        self.setCvar_patcher = patch.object(self.console, 'setCvar')
        self.setCvar_mock = self.setCvar_patcher.start()

        self.superadmin.connects("2")

    def tearDown(self):
        super(Test_cmd_pagear, self).tearDown()
        self.sleep_patcher.stop()
        self.setCvar_patcher.stop()

    def given_forbidden_weapon_are(self, g_gear_value):
        when(self.console).getCvar('g_gear').thenReturn(Cvar('g_gear', value=str(g_gear_value)))

    def assert_set_gear(self, expected_gear_value, fail_message=None):
        self.assertListEqual([call('g_gear', str(expected_gear_value))], self.setCvar_mock.mock_calls, fail_message)

    def test_no_parameter(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        self.p.onStartup()
        # WHEN
        self.superadmin.message_history = []
        self.superadmin.says("!gear")
        # THEN
        self.assertListEqual(["current gear: med:ON, vest:ON, ak:ON, de:ON, psg:ON, nvg:ON, hk:ON, mac:ON, mp5:ON, las:ON, ber:ON, ump:ON, g36:ON, neg:ON, glo:ON, smo:ON, m4:ON, lr:ON, hel:ON, spas:ON, sr8:ON, he:ON, colt:ON, ammo:ON, sil:ON",
                              "Usage: !pagear [+/-][med|vest|ak|de|psg|nvg|hk|mac|mp5|las|ber|ump|g36|neg|glo|smo|m4|lr|hel|spas|sr8|he|colt|ammo|sil]",
                              "Load weapon groups: !pagear [+/-][all_nades|all_auto|all_pistols|all_snipers]",
                              "Load defaults: !pagear [reset|all|none]"],
                              self.superadmin.message_history)

    def test_reset(self):
        # GIVEN
        self.given_forbidden_weapon_are("1234")
        self.p.onStartup()
        # WHEN
        self.superadmin.says("!gear reset")
        # THEN
        self.assert_set_gear('1234')

    def test_all(self):
        # WHEN
        self.superadmin.says("!gear all")
        # THEN
        self.assert_set_gear(G_NONE)

    def test_none(self):
        # WHEN
        self.superadmin.says("!gear none")
        # THEN
        self.assert_set_gear(G_ALL)

    def test_unknown_weapon(self):
        # WHEN
        self.superadmin.says("!gear +f00")
        # THEN
        self.assertListEqual([], self.setCvar_mock.mock_calls)
        self.assertListEqual([
            "could not recognize weapon/item 'f00'",
            "gear not changed"
        ], self.superadmin.message_history)

    def test_disallow_negev(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -neg")
        # THEN
        self.assert_set_gear(G_NEGEV_LMG)

    def test_allow_negev_short(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +neg")
        # THEN
        self.assert_set_gear(all_gear_but(G_NEGEV_LMG))

    def test_allow_negev_long(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +negev")
        # THEN
        self.assert_set_gear(all_gear_but(G_NEGEV_LMG))

    def test_allow_negev_long_spaced(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear + negev")
        # THEN
        self.assert_set_gear(all_gear_but(G_NEGEV_LMG))

    def test_disallow(self):
        for weapon_name, weapon_code in weapon_codes:
            # GIVEN
            self.setCvar_mock.reset_mock()
            self.given_forbidden_weapon_are(G_NONE)
            # WHEN
            self.superadmin.says("!gear -%s" % weapon_name)
            # THEN
            self.assert_set_gear(weapon_code, weapon_name)

    def test_allow(self):
        for weapon_name, weapon_code in weapon_codes:
            # GIVEN
            self.setCvar_mock.reset_mock()
            self.given_forbidden_weapon_are(G_ALL)
            # WHEN
            self.superadmin.says("!gear +%s" % weapon_name)
            # THEN
            self.assert_set_gear(all_gear_but(weapon_code), weapon_name)

    def test_allow_group_nades(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +all_nades")
        # THEN
        self.assert_set_gear(all_gear_but(G_SMOKE_GRENADE, G_HE_GRENADE))

    def test_disallow_group_nades(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -all_nades")
        # THEN
        self.assert_set_gear(only_gear(G_SMOKE_GRENADE, G_HE_GRENADE))

    def test_disallow_group_nades_spaced(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear - all_nades")
        # THEN
        self.assert_set_gear(only_gear(G_SMOKE_GRENADE, G_HE_GRENADE))

    def test_allow_all_snipers(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +all_snipers")
        # THEN
        self.assert_set_gear(all_gear_but(G_SR8, G_PSG1))

    def test_disallow_all_snipers(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -all_snipers")
        # THEN
        self.assert_set_gear(only_gear(G_SR8, G_PSG1))

    def test_allow_all_pistols(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +all_pistols")
        # THEN
        self.assert_set_gear(all_gear_but(G_BERETTA_92FS, G_50_DESERT_EAGLE, G_GLOCK, G_COLT1911))

    def test_disallow_all_pistols(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -all_pistols")
        # THEN
        self.assert_set_gear(only_gear(G_BERETTA_92FS, G_50_DESERT_EAGLE, G_GLOCK, G_COLT1911))

    def test_allow_all_auto(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +all_auto")
        # THEN
        self.assert_set_gear(all_gear_but(G_MP5K, G_LR300ML, G_COLT_M4, G_MAC11, G_UMP45, G_G36, G_AK103, G_NEGEV_LMG))

    def test_disallow_all_auto(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -all_auto")
        # THEN
        self.assert_set_gear(only_gear(G_MP5K, G_LR300ML, G_COLT_M4, G_MAC11, G_UMP45, G_G36, G_AK103, G_NEGEV_LMG))

    def test_disallow_all_auto_and_no_smoke(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear all -all_auto -smoke")
        # THEN
        self.assert_set_gear(only_gear(G_MP5K, G_LR300ML, G_COLT_M4, G_MAC11, G_UMP45, G_G36, G_AK103, G_NEGEV_LMG, G_SMOKE_GRENADE))

    def test_disallow_all_auto_and_no_smoke_spaced(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear all - all_auto - smoke")
        # THEN
        self.assert_set_gear(only_gear(G_MP5K, G_LR300ML, G_COLT_M4, G_MAC11, G_UMP45, G_G36, G_AK103, G_NEGEV_LMG, G_SMOKE_GRENADE))

    def test_disallow_all_auto_but_lr300(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear all -all_auto +lr")
        # THEN
        self.assert_set_gear(only_gear(G_MP5K, G_COLT_M4, G_MAC11, G_UMP45, G_G36, G_AK103, G_NEGEV_LMG))

    def test_disallow_all_auto_but_lr300_spaced(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear all - all_auto + lr")
        # THEN
        self.assert_set_gear(only_gear(G_MP5K, G_COLT_M4, G_MAC11, G_UMP45, G_G36, G_AK103, G_NEGEV_LMG))
Exemple #22
0
class Test_cmd_pagear(Iourt43TestCase):
    def setUp(self):
        super(Test_cmd_pagear, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
pagear-gear: 20
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)

        when(self.console).getCvar('timelimit').thenReturn(
            Cvar('timelimit', value=20))
        when(self.console).getCvar('g_maxGameClients').thenReturn(
            Cvar('g_maxGameClients', value=16))
        when(self.console).getCvar('sv_maxclients').thenReturn(
            Cvar('sv_maxclients', value=16))
        when(self.console).getCvar('sv_privateClients').thenReturn(
            Cvar('sv_privateClients', value=0))
        when(self.console).getCvar('g_allowvote').thenReturn(
            Cvar('g_allowvote', value=0))
        when(self.console).getCvar('g_modversion').thenReturn(
            Cvar('g_modversion', value="4.3.4"))
        self.given_forbidden_weapon_are(G_NONE)
        self.p.onLoadConfig()
        self.p.onStartup()

        self.sleep_patcher = patch.object(time, 'sleep')
        self.sleep_patcher.start()
        self.setCvar_patcher = patch.object(self.console, 'setCvar')
        self.setCvar_mock = self.setCvar_patcher.start()

        self.superadmin.connects("2")

    def tearDown(self):
        super(Test_cmd_pagear, self).tearDown()
        self.sleep_patcher.stop()
        self.setCvar_patcher.stop()

    def given_forbidden_weapon_are(self, g_gear_value):
        when(self.console).getCvar('g_gear').thenReturn(
            Cvar('g_gear', value=str(g_gear_value)))

    def assert_set_gear(self, expected_gear_value, fail_message=None):
        self.assertListEqual([call('g_gear', str(expected_gear_value))],
                             self.setCvar_mock.mock_calls, fail_message)

    def test_no_parameter(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        self.p.onStartup()
        # WHEN
        self.superadmin.message_history = []
        self.superadmin.says("!gear")
        # THEN
        current_gear = ":ON, ".join(sorted(self.p._weapons.keys()))
        weapon_groups = "|".join(sorted(self.p._weapon_groups))
        weapon_usage = "|".join(sorted(self.p._weapons.keys()))
        self.maxDiff = None
        self.assertListEqual([
            "current gear: %s:ON" % (current_gear, ),
            "Usage: !pagear [+/-][%s]" % (weapon_usage, ),
            "Load weapon groups: !pagear [+/-][%s]" %
            (weapon_groups, ), "Load defaults: !pagear [all|none|reset]"
        ], self.superadmin.message_history)

    def test_reset(self):
        # GIVEN
        self.given_forbidden_weapon_are("1234")
        self.p.onStartup()
        # WHEN
        self.given_forbidden_weapon_are("12345")
        self.superadmin.says("!gear reset")
        # THEN
        self.assert_set_gear('1234')

    def test_all(self):
        # GIVEN
        self.given_forbidden_weapon_are("1234")
        # WHEN
        self.superadmin.says("!gear all")
        # THEN
        self.assert_set_gear(G_NONE)

    def test_none(self):
        # WHEN
        self.superadmin.says("!gear none")
        # THEN
        self.assert_set_gear(G_ALL)

    def test_unknown_weapon(self):
        # WHEN
        self.superadmin.says("!gear +f00")
        # THEN
        self.assertListEqual([], self.setCvar_mock.mock_calls)
        self.assertListEqual(
            ["could not recognize weapon/item 'f00'", "gear not changed"],
            self.superadmin.message_history)

    def test_disallow_negev(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -neg")
        # THEN
        self.assert_set_gear(G_NEGEV_LMG)

    def test_allow_negev_short(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +neg")
        # THEN
        self.assert_set_gear(all_gear_but(G_NEGEV_LMG))

    def test_allow_negev_long(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +negev")
        # THEN
        self.assert_set_gear(all_gear_but(G_NEGEV_LMG))

    def test_allow_negev_long_spaced(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear + negev")
        # THEN
        self.assert_set_gear(all_gear_but(G_NEGEV_LMG))

    def test_disallow(self):
        for weapon_name, weapon_code in weapon_codes:
            # GIVEN
            self.setCvar_mock.reset_mock()
            self.given_forbidden_weapon_are(G_NONE)
            # WHEN
            self.superadmin.says("!gear -%s" % weapon_name)
            # THEN
            self.assert_set_gear(weapon_code, weapon_name)

    def test_allow(self):
        for weapon_name, weapon_code in weapon_codes:
            # GIVEN
            self.setCvar_mock.reset_mock()
            self.given_forbidden_weapon_are(G_ALL)
            # WHEN
            self.superadmin.says("!gear +%s" % weapon_name)
            # THEN
            self.assert_set_gear(all_gear_but(weapon_code), weapon_name)

    def test_allow_group_nades(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +all_nades")
        # THEN
        self.assert_set_gear(all_gear_but(G_HK69, G_HE_GRENADE))

    def test_disallow_group_nades(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -all_nades")
        # THEN
        self.assert_set_gear(only_gear(G_HK69, G_HE_GRENADE))

    def test_disallow_group_nades_spaced(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear - all_nades")
        # THEN
        self.assert_set_gear(only_gear(G_HK69, G_HE_GRENADE))

    def test_allow_all_snipers(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +all_snipers")
        # THEN
        self.assert_set_gear(all_gear_but(G_SR8, G_PSG1, G_FRF1))

    def test_disallow_all_snipers(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -all_snipers")
        # THEN
        self.assert_set_gear(only_gear(G_SR8, G_PSG1, G_FRF1))

    def test_allow_all_pistols(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +all_pistols")
        # THEN
        self.assert_set_gear(
            all_gear_but(G_BERETTA_92FS, G_50_DESERT_EAGLE, G_GLOCK,
                         G_COLT1911, G_MAGNUM))

    def test_disallow_all_pistols(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -all_pistols")
        # THEN
        self.assert_set_gear(
            only_gear(G_BERETTA_92FS, G_50_DESERT_EAGLE, G_GLOCK, G_COLT1911,
                      G_MAGNUM))

    def test_allow_all_auto(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +all_autos")
        # THEN
        self.assert_set_gear(
            all_gear_but(G_MP5K, G_LR300ML, G_COLT_M4, G_MAC11, G_UMP45, G_G36,
                         G_AK103, G_NEGEV_LMG, G_P90))

    def test_disallow_all_auto(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -all_autos")
        # THEN
        self.assert_set_gear(
            only_gear(G_MP5K, G_LR300ML, G_COLT_M4, G_MAC11, G_UMP45, G_G36,
                      G_AK103, G_NEGEV_LMG, G_P90))

    def test_disallow_all_auto_and_no_smoke(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear all -all_autos -smoke")
        # THEN
        self.assert_set_gear(
            only_gear(G_MP5K, G_LR300ML, G_COLT_M4, G_MAC11, G_UMP45, G_G36,
                      G_AK103, G_NEGEV_LMG, G_SMOKE_GRENADE, G_P90))

    def test_disallow_all_auto_and_no_smoke_spaced(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear all - all_autos - smoke")
        # THEN
        self.assert_set_gear(
            only_gear(G_MP5K, G_LR300ML, G_COLT_M4, G_MAC11, G_UMP45, G_G36,
                      G_AK103, G_NEGEV_LMG, G_SMOKE_GRENADE, G_P90))

    def test_disallow_all_auto_but_lr300(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear all -all_autos +lr")
        # THEN
        self.assert_set_gear(
            only_gear(G_MP5K, G_COLT_M4, G_MAC11, G_UMP45, G_G36, G_AK103,
                      G_NEGEV_LMG, G_P90))

    def test_disallow_all_auto_but_lr300_spaced(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear all - all_autos + lr")
        # THEN
        self.assert_set_gear(
            only_gear(G_MP5K, G_COLT_M4, G_MAC11, G_UMP45, G_G36, G_AK103,
                      G_NEGEV_LMG, G_P90))
class Test_cmd_teams(Iourt42TestCase):

    def setUp(self):
        super(Test_cmd_teams, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString(dedent("""
        [commands]
        pateams-teams: 1

        [teambalancer]
        tinterval: 0
        teamdifference: 1
        maxlevel: 60
        announce: 2
        team_change_force_balance_enable: True
        autobalance_gametypes: tdm,ctf,cah,ftl,ts,bm,freeze
        teamLocksPermanent: False
        timedelay: 60
        """))
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.console.say = Mock()
        self.console.write = Mock()

        with logging_disabled():
            from b3.fake import FakeClient
            self.blue1 = FakeClient(self.console, name="Blue1", guid="zaerezarezar", groupBits=1, team=TEAM_BLUE)
            self.blue2 = FakeClient(self.console, name="Blue2", guid="qsdfdsqfdsqf", groupBits=1, team=TEAM_BLUE)
            self.blue3 = FakeClient(self.console, name="Blue3", guid="qsdfdsqfdsqf33", groupBits=1, team=TEAM_BLUE)
            self.blue4 = FakeClient(self.console, name="Blue4", guid="sdf455ezr", groupBits=1, team=TEAM_BLUE)
            self.red1 = FakeClient(self.console, name="Red1", guid="875sasda", groupBits=1, team=TEAM_RED)
            self.red2 = FakeClient(self.console, name="Red2", guid="f4qfer654r", groupBits=1, team=TEAM_RED)

        # connect clients
        self.blue1.connects('1')
        self.blue2.connects('2')
        self.blue3.connects('3')
        self.blue4.connects('4')
        self.red1.connects('5')
        self.red2.connects('6')

        self.p.countteams = Mock(return_value=True)
        self.p._teamred = 2
        self.p._teamblue = 4

    def test_non_round_based_gametype(self):
        # GIVEN
        self.blue1.clearMessageHistory()
        self.console.game.gameType = 'tdm'
        # WHEN
        self.blue1.says('!teams')
        # THEN
        self.console.write.assert_has_calls([call('bigtext "Autobalancing Teams!"')])
        self.assertEqual(self.blue1.message_history, ['Teams are now balanced'])

    def test_round_based_gametype_delayed_announce_only(self):
        # GIVEN
        self.blue1.clearMessageHistory()
        self.console.game.gameType = 'bm'
        # WHEN
        self.blue1.says('!teams')
        # THEN
        self.assertEqual(self.blue1.message_history, ['Teams will be balanced at the end of this round'])
        self.assertTrue(self.p._pending_teambalance)
        self.assertFalse(self.p._pending_skillbalance)

    def test_round_based_gametype_delayed_execution(self):
        # GIVEN
        self.blue1.clearMessageHistory()
        self.console.game.gameType = 'bm'
        self.blue1.says('!teams')
        self.assertEqual(self.blue1.message_history, ['Teams will be balanced at the end of this round'])
        self.assertTrue(self.p._pending_teambalance)
        self.assertFalse(self.p._pending_skillbalance)
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_GAME_ROUND_END'))
        # THEN
        self.console.write.assert_has_calls([call('bigtext "Autobalancing Teams!"')])
        self.assertFalse(self.p._pending_teambalance)
        self.assertFalse(self.p._pending_skillbalance)
class Test_cmd_pagear(Iourt41TestCase):

    def setUp(self):
        super(Test_cmd_pagear, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
pagear-gear: 20
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)

        when(self.console).getCvar('timelimit').thenReturn(Cvar('timelimit', value=20))
        when(self.console).getCvar('g_maxGameClients').thenReturn(Cvar('g_maxGameClients', value=16))
        when(self.console).getCvar('sv_maxclients').thenReturn(Cvar('sv_maxclients', value=16))
        when(self.console).getCvar('sv_privateClients').thenReturn(Cvar('sv_privateClients', value=0))
        when(self.console).getCvar('g_allowvote').thenReturn(Cvar('g_allowvote', value=0))
        when(self.console).getCvar('g_modversion').thenReturn(Cvar('g_modversion', value="4.1"))
        self.given_forbidden_weapon_are(G_NONE)
        self.p.onLoadConfig()
        self.p.onStartup()

        self.sleep_patcher = patch.object(time, 'sleep')
        self.sleep_patcher.start()
        self.setCvar_patcher = patch.object(self.console, 'setCvar')
        self.setCvar_mock = self.setCvar_patcher.start()

        self.superadmin.connects("2")

    def tearDown(self):
        super(Test_cmd_pagear, self).tearDown()
        self.sleep_patcher.stop()
        self.setCvar_patcher.stop()

    def given_forbidden_weapon_are(self, g_gear_value):
        when(self.console).getCvar('g_gear').thenReturn(Cvar('g_gear', value=str(g_gear_value)))

    def assert_set_gear(self, expected_gear_value):
        self.assertListEqual([call('g_gear', str(expected_gear_value))], self.setCvar_mock.mock_calls)

    def test_reset(self):
        # GIVEN
        self.given_forbidden_weapon_are("1234")
        self.p.onStartup()
        # WHEN
        self.superadmin.says("!gear reset")
        # THEN
        self.assert_set_gear('1234')

    def test_all(self):
        # WHEN
        self.superadmin.says("!gear all")
        # THEN
        self.assert_set_gear(G_NONE)

    def test_none(self):
        # WHEN
        self.superadmin.says("!gear none")
        # THEN
        self.assert_set_gear(G_ALL)

    def test_forbid_nades(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -nade")
        # THEN
        self.assert_set_gear(G_NADES)

    def test_allow_nades(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +nade")
        # THEN
        self.assert_set_gear(G_ALL - G_NADES)

    def test_forbid_snipers(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -snip")
        # THEN
        self.assert_set_gear(G_SNIPERS)

    def test_allow_snipers(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +snip")
        # THEN
        self.assert_set_gear(G_ALL - G_SNIPERS)

    def test_forbid_spas(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -spas")
        # THEN
        self.assert_set_gear(G_SPAS)

    def test_allow_spas(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +spas")
        # THEN
        self.assert_set_gear(G_ALL - G_SPAS)

    def test_forbid_pistols(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -pist")
        # THEN
        self.assert_set_gear(G_PISTOLS)

    def test_allow_pistols(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +pist")
        # THEN
        self.assert_set_gear(G_ALL - G_PISTOLS)


    def test_forbid_auto(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -auto")
        # THEN
        self.assert_set_gear(G_RIFLES)

    def test_allow_auto(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +auto")
        # THEN
        self.assert_set_gear(G_ALL - G_RIFLES)

    def test_forbid_negev(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -negev")
        # THEN
        self.assert_set_gear(G_NEGEV)

    def test_allow_negev(self):
       # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +negev")
        # THEN
        self.assert_set_gear(G_ALL - G_NEGEV)
Exemple #25
0
class mixin_name_checker:
    def setUp(self):
        super(mixin_name_checker, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[namechecker]
checkdupes: True
checkunknown: True
checkbadnames: True
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.sleep_patcher = patch.object(time, 'sleep')
        self.sleep_patcher.start()

        self.console.say = Mock()
        self.console.write = Mock()

        self.p._ignoreTill = 0

    def tearDown(self):
        super(mixin_name_checker, self).tearDown()
        self.sleep_patcher.stop()

    def test_checkdupes_no_dup(self):
        # GIVEN
        p1 = FakeClient(self.console, name="theName", guid="p1guid")
        p1.warn = Mock()
        p1.connects("1")
        p2 = FakeClient(self.console, name="anotherName", guid="p2guid")
        p2.warn = Mock()
        p2.connects("2")
        # WHEN
        self.assertFalse(p1.name == p2.name)
        self.p.namecheck()
        # THEN
        self.assertFalse(p1.warn.called)
        self.assertFalse(p2.warn.called)

    def test_checkdupes_with_dup(self):
        # GIVEN
        p1 = FakeClient(self.console, name="sameName", guid="p1guid")
        p1.warn = Mock()
        p1.connects("1")
        p2 = FakeClient(self.console, name="sameName", guid="p2guid")
        p2.warn = Mock()
        p2.connects("2")
        # WHEN
        self.assertTrue(p1.name == p2.name)
        self.p.namecheck()
        # THEN
        p1.warn.assert_has_calls([call(ANY, ANY, 'badname', None, '')])
        p2.warn.assert_has_calls([call(ANY, ANY, 'badname', None, '')])

    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_checkunknown(self):
        # GIVEN
        p1 = FakeClient(self.console, name="New UrT Player", guid="p1guid")
        p1.warn = Mock()
        p1.connects("1")
        # WHEN
        self.p.namecheck()
        # THEN
        p1.warn.assert_has_calls([call(ANY, ANY, 'badname', None, '')])

    def test_checkbadnames(self):
        # GIVEN
        p1 = FakeClient(self.console, name="all", guid="p1guid")
        p1.warn = Mock()
        p1.connects("1")
        # WHEN
        self.p.namecheck()
        # THEN
        p1.warn.assert_has_calls([call(ANY, ANY, 'badname', None, '')])
Exemple #26
0
class Test_cmd_balance(Iourt42TestCase):
    def setUp(self):
        super(Test_cmd_balance, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString(
            dedent("""
        [commands]
        pabalance-balance: 1

        [skillbalancer]
        min_bal_interval: 1
        interval: 0
        difference: 0.5
        mode: 2
        """))
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.console.say = Mock()
        self.console.write = Mock()

        with logging_disabled():
            from b3.fake import FakeClient
            self.blue1 = FakeClient(self.console,
                                    name="Blue1",
                                    guid="zaerezarezar",
                                    groupBits=1,
                                    team=TEAM_BLUE)
            self.blue2 = FakeClient(self.console,
                                    name="Blue2",
                                    guid="qsdfdsqfdsqf",
                                    groupBits=1,
                                    team=TEAM_BLUE)
            self.blue3 = FakeClient(self.console,
                                    name="Blue3",
                                    guid="qsdfdsqfdsqf33",
                                    groupBits=1,
                                    team=TEAM_BLUE)
            self.blue4 = FakeClient(self.console,
                                    name="Blue4",
                                    guid="sdf455ezr",
                                    groupBits=1,
                                    team=TEAM_BLUE)
            self.red1 = FakeClient(self.console,
                                   name="Red1",
                                   guid="875sasda",
                                   groupBits=1,
                                   team=TEAM_RED)
            self.red2 = FakeClient(self.console,
                                   name="Red2",
                                   guid="f4qfer654r",
                                   groupBits=1,
                                   team=TEAM_RED)

        # connect clients
        self.blue1.connects('1')
        self.blue2.connects('2')
        self.blue3.connects('3')
        self.blue4.connects('4')
        self.red1.connects('5')
        self.red2.connects('6')

        self.p.countteams = Mock(return_value=True)
        self.p._teamred = 2
        self.p._teamblue = 4

    def test_balancing_already_done(self):
        # GIVEN
        self.blue1.clearMessageHistory()
        self.p._lastbal = 0
        self.p.ignoreCheck = Mock(return_value=True)
        self.console.time = Mock(return_value=10)
        # WHEN
        self.blue1.says('!balance')
        # THEN
        self.assertEqual(self.blue1.message_history,
                         ['Teams changed recently, please wait a while'])

    def test_non_round_based_gametype(self):
        # GIVEN
        self.blue1.clearMessageHistory()
        self.p._lastbal = 0
        self.p.ignoreCheck = Mock(return_value=False)
        self.console.time = Mock(return_value=120)
        self.console.game.gameType = 'tdm'
        # WHEN
        self.blue1.says('!balance')
        # THEN
        self.console.write.assert_has_calls(
            [call('bigtext "Balancing teams!"')])

    def test_round_based_gametype_delayed_announce_only(self):
        # GIVEN
        self.blue1.clearMessageHistory()
        self.p._lastbal = 0
        self.p.ignoreCheck = Mock(return_value=False)
        self.console.time = Mock(return_value=120)
        self.console.game.gameType = 'bm'
        # WHEN
        self.blue1.says('!balance')
        # THEN
        self.assertEqual(self.blue1.message_history,
                         ['Teams will be balanced at the end of this round'])
        self.assertFalse(self.p._pending_teambalance)
        self.assertTrue(self.p._pending_skillbalance)
        self.assertEqual(self.p.cmd_pabalance, self.p._skillbalance_func)

    def test_round_based_gametype_delayed_execution(self):
        # GIVEN
        self.blue1.clearMessageHistory()
        self.p._lastbal = 0
        self.p.ignoreCheck = Mock(return_value=False)
        self.console.time = Mock(return_value=120)
        self.console.game.gameType = 'bm'
        self.blue1.says('!balance')
        self.assertEqual(self.blue1.message_history,
                         ['Teams will be balanced at the end of this round'])
        self.assertFalse(self.p._pending_teambalance)
        self.assertTrue(self.p._pending_skillbalance)
        self.assertEqual(self.p.cmd_pabalance, self.p._skillbalance_func)
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_GAME_ROUND_END'))
        # THEN
        self.console.write.assert_has_calls(
            [call('bigtext "Balancing teams!"')])
        self.assertFalse(self.p._pending_teambalance)
        self.assertFalse(self.p._pending_skillbalance)
        self.assertIsNone(self.p._skillbalance_func)
Exemple #27
0
class Test_headshotcounter(Iourt41TestCase):
    def setUp(self):
        super(Test_headshotcounter, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[headshotcounter]
# enable the headshot counter?
hs_enable: True
# reset counts? Options: no / map / round
reset_vars: no
# set broadcast to True if you want the counter to appear in the upper left, False is in chatarea
broadcast: True
# Announce every single headshot?
announce_all: True
# Announce percentages (after 5 headshots)
announce_percentages: True
# Only show percentages larger than next threshold
percent_min: 10
# Advise victims to wear a helmet?
warn_helmet: True
# After how many headshots?
warn_helmet_nr: 7
# Advise victims to wear kevlar?
warn_kevlar: True
# After how many torso hits?
warn_kevlar_nr: 50
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.console.say = Mock()
        self.console.write = Mock()

    def test_hitlocation(self):
        def joe_hits_simon(hitloc):
            #Hit: 13 10 0 8: Grover hit jacobdk92 in the Head
            #Hit: cid acid hitloc aweap: text
            self.console.parseLine(
                "Hit: 7 6 %s 8: Grover hit jacobdk92 in the Head" % hitloc)

        def assertCounts(head, helmet, torso):
            self.assertEqual(
                head,
                self.joe.var(self.p, 'headhits', default=0.0).value)
            self.assertEqual(
                helmet,
                self.joe.var(self.p, 'helmethits', default=0.0).value)
            self.assertEqual(
                torso,
                self.simon.var(self.p, 'torsohitted', default=0.0).value)

        # GIVEN
        self.joe.connects("6")
        self.simon.connects("7")

        # WHEN
        joe_hits_simon('4')
        # THEN
        assertCounts(head=0.0, helmet=0.0, torso=0.0)

        # WHEN
        joe_hits_simon('0')
        # THEN
        assertCounts(head=1.0, helmet=0.0, torso=0.0)

        # WHEN
        joe_hits_simon('1')
        # THEN
        assertCounts(head=1.0, helmet=1.0, torso=0.0)

        # WHEN
        joe_hits_simon('2')
        # THEN
        assertCounts(head=1.0, helmet=1.0, torso=1.0)
class Test_cmd_teams(Iourt42TestCase):
    def setUp(self):
        super(Test_cmd_teams, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString(
            dedent("""
        [commands]
        pateams-teams: 1

        [teambalancer]
        tinterval: 0
        teamdifference: 1
        maxlevel: 60
        announce: 2
        team_change_force_balance_enable: True
        autobalance_gametypes: tdm,ctf,cah,ftl,ts,bm,freeze
        teamLocksPermanent: False
        timedelay: 60
        """))
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.console.say = Mock()
        self.console.write = Mock()

        with logging_disabled():
            from b3.fake import FakeClient
            self.blue1 = FakeClient(self.console,
                                    name="Blue1",
                                    guid="zaerezarezar",
                                    groupBits=1,
                                    team=TEAM_BLUE)
            self.blue2 = FakeClient(self.console,
                                    name="Blue2",
                                    guid="qsdfdsqfdsqf",
                                    groupBits=1,
                                    team=TEAM_BLUE)
            self.blue3 = FakeClient(self.console,
                                    name="Blue3",
                                    guid="qsdfdsqfdsqf33",
                                    groupBits=1,
                                    team=TEAM_BLUE)
            self.blue4 = FakeClient(self.console,
                                    name="Blue4",
                                    guid="sdf455ezr",
                                    groupBits=1,
                                    team=TEAM_BLUE)
            self.red1 = FakeClient(self.console,
                                   name="Red1",
                                   guid="875sasda",
                                   groupBits=1,
                                   team=TEAM_RED)
            self.red2 = FakeClient(self.console,
                                   name="Red2",
                                   guid="f4qfer654r",
                                   groupBits=1,
                                   team=TEAM_RED)

        # connect clients
        self.blue1.connects('1')
        self.blue2.connects('2')
        self.blue3.connects('3')
        self.blue4.connects('4')
        self.red1.connects('5')
        self.red2.connects('6')

        self.p.countteams = Mock(return_value=True)
        self.p._teamred = 2
        self.p._teamblue = 4

    def test_non_round_based_gametype(self):
        # GIVEN
        self.blue1.clearMessageHistory()
        self.console.game.gameType = 'tdm'
        # WHEN
        self.blue1.says('!teams')
        # THEN
        self.console.write.assert_has_calls(
            [call('bigtext "Autobalancing Teams!"')])
        self.assertEqual(self.blue1.message_history,
                         ['Teams are now balanced'])

    def test_round_based_gametype_delayed_announce_only(self):
        # GIVEN
        self.blue1.clearMessageHistory()
        self.console.game.gameType = 'bm'
        # WHEN
        self.blue1.says('!teams')
        # THEN
        self.assertEqual(self.blue1.message_history,
                         ['Teams will be balanced at the end of this round'])
        self.assertTrue(self.p._pending_teambalance)
        self.assertFalse(self.p._pending_skillbalance)

    def test_round_based_gametype_delayed_execution(self):
        # GIVEN
        self.blue1.clearMessageHistory()
        self.console.game.gameType = 'bm'
        self.blue1.says('!teams')
        self.assertEqual(self.blue1.message_history,
                         ['Teams will be balanced at the end of this round'])
        self.assertTrue(self.p._pending_teambalance)
        self.assertFalse(self.p._pending_skillbalance)
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_GAME_ROUND_END'))
        # THEN
        self.console.write.assert_has_calls(
            [call('bigtext "Autobalancing Teams!"')])
        self.assertFalse(self.p._pending_teambalance)
        self.assertFalse(self.p._pending_skillbalance)
class mixin_name_checker(object):

    def setUp(self):
        super(mixin_name_checker, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[namechecker]
checkdupes: True
checkunknown: True
checkbadnames: True
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.sleep_patcher = patch.object(time, 'sleep')
        self.sleep_patcher.start()

        self.console.say = Mock()
        self.console.write = Mock()

        self.p._ignoreTill = 0


    def tearDown(self):
        super(mixin_name_checker, self).tearDown()
        self.sleep_patcher.stop()


    def test_checkdupes_no_dup(self):
        # GIVEN
        p1 = FakeClient(self.console, name="theName", guid="p1guid")
        p1.warn = Mock()
        p1.connects("1")
        p2 = FakeClient(self.console, name="anotherName", guid="p2guid")
        p2.warn = Mock()
        p2.connects("2")
        # WHEN
        self.assertFalse(p1.name == p2.name)
        self.p.namecheck()
        # THEN
        self.assertFalse(p1.warn.called)
        self.assertFalse(p2.warn.called)


    def test_checkdupes_with_dup(self):
        # GIVEN
        p1 = FakeClient(self.console, name="sameName", guid="p1guid")
        p1.warn = Mock()
        p1.connects("1")
        p2 = FakeClient(self.console, name="sameName", guid="p2guid")
        p2.warn = Mock()
        p2.connects("2")
        # WHEN
        self.assertTrue(p1.name == p2.name)
        self.p.namecheck()
        # THEN
        p1.warn.assert_has_calls([call(ANY, ANY, 'badname', None, '')])
        p2.warn.assert_has_calls([call(ANY, ANY, 'badname', None, '')])


    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_checkunknown(self):
        # GIVEN
        p1 = FakeClient(self.console, name="New UrT Player", guid="p1guid")
        p1.warn = Mock()
        p1.connects("1")
        # WHEN
        self.p.namecheck()
        # THEN
        p1.warn.assert_has_calls([call(ANY, ANY, 'badname', None, '')])


    def test_checkbadnames(self):
        # GIVEN
        p1 = FakeClient(self.console, name="all", guid="p1guid")
        p1.warn = Mock()
        p1.connects("1")
        # WHEN
        self.p.namecheck()
        # THEN
        p1.warn.assert_has_calls([call(ANY, ANY, 'badname', None, '')])
class Test_radio_spam_protection(Iourt42TestCase):
    def setUp(self):
        super(Test_radio_spam_protection, self).setUp()
        self.conf = CfgConfigParser()
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()

    def init(self, config_content=None):
        if config_content:
            self.conf.loadFromString(config_content)
        else:
            self.conf.loadFromString("""
[radio_spam_protection]
enable: True
mute_duration: 2
        """)
        self.p.onLoadConfig()
        self.p.onStartup()

    def test_conf_nominal(self):
        self.init("""
[radio_spam_protection]
enable: True
mute_duration: 2
        """)
        self.assertTrue(self.p._rsp_enable)
        self.assertEqual(2, self.p._rsp_mute_duration)

    def test_conf_nominal_2(self):
        self.init("""
[radio_spam_protection]
enable: no
mute_duration: 1
        """)
        self.assertFalse(self.p._rsp_enable)
        self.assertEqual(1, self.p._rsp_mute_duration)

    def test_conf_broken(self):
        self.init("""
[radio_spam_protection]
enable: f00
mute_duration: 0
        """)
        self.assertFalse(self.p._rsp_enable)
        self.assertEqual(1, self.p._rsp_mute_duration)

    def test_spam(self):
        # GIVEN
        self.init("""
[radio_spam_protection]
enable: True
mute_duration: 2
""")
        self.joe.connects("0")
        self.console.write = Mock(wraps=lambda x: sys.stderr.write("%s\n" % x))
        self.joe.warn = Mock()

        def joe_radio(msg_group, msg_id, location, text):
            self.console.parseLine('''Radio: 0 - %s - %s - "%s" - "%s"''' %
                                   (msg_group, msg_id, location, text))

        def assertSpampoints(points):
            self.assertEqual(points,
                             self.joe.var(self.p, 'radio_spamins', 0).value)

        assertSpampoints(0)

        # WHEN
        when(self.p).getTime().thenReturn(0)
        joe_radio(3, 3, "Patio Courtyard", "Requesting medic. Status: healthy")
        # THEN
        assertSpampoints(0)
        self.assertEqual(0, self.joe.warn.call_count)
        self.assertEqual(0, self.console.write.call_count)

        # WHEN
        when(self.p).getTime().thenReturn(0)
        joe_radio(3, 3, "Patio Courtyard", "Requesting medic. Status: healthy")
        # THEN
        assertSpampoints(8)
        self.assertEqual(0, self.joe.warn.call_count)
        self.assertEqual(0, self.console.write.call_count)

        # WHEN
        when(self.p).getTime().thenReturn(1)
        joe_radio(3, 1, "Patio Courtyard", "f00")
        # THEN
        assertSpampoints(5)
        self.assertEqual(0, self.joe.warn.call_count)
        self.console.write.assert_has_calls([call("mute 0 2")])
class Test_cmd_balance(Iourt42TestCase):

    def setUp(self):
        super(Test_cmd_balance, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString(dedent("""
        [commands]
        pabalance-balance: 1

        [skillbalancer]
        min_bal_interval: 1
        interval: 0
        difference: 0.5
        mode: 2
        """))
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.console.say = Mock()
        self.console.write = Mock()

        with logging_disabled():
            from b3.fake import FakeClient
            self.blue1 = FakeClient(self.console, name="Blue1", guid="zaerezarezar", groupBits=1, team=TEAM_BLUE)
            self.blue2 = FakeClient(self.console, name="Blue2", guid="qsdfdsqfdsqf", groupBits=1, team=TEAM_BLUE)
            self.blue3 = FakeClient(self.console, name="Blue3", guid="qsdfdsqfdsqf33", groupBits=1, team=TEAM_BLUE)
            self.blue4 = FakeClient(self.console, name="Blue4", guid="sdf455ezr", groupBits=1, team=TEAM_BLUE)
            self.red1 = FakeClient(self.console, name="Red1", guid="875sasda", groupBits=1, team=TEAM_RED)
            self.red2 = FakeClient(self.console, name="Red2", guid="f4qfer654r", groupBits=1, team=TEAM_RED)

        # connect clients
        self.blue1.connects('1')
        self.blue2.connects('2')
        self.blue3.connects('3')
        self.blue4.connects('4')
        self.red1.connects('5')
        self.red2.connects('6')

        self.p.countteams = Mock(return_value=True)
        self.p._teamred = 2
        self.p._teamblue = 4

    def test_balancing_already_done(self):
        # GIVEN
        self.blue1.clearMessageHistory()
        self.p._lastbal = 0
        self.p.ignoreCheck = Mock(return_value=True)
        self.console.time = Mock(return_value=10)
        # WHEN
        self.blue1.says('!balance')
        # THEN
        self.assertEqual(self.blue1.message_history, ['Teams changed recently, please wait a while'])

    def test_non_round_based_gametype(self):
        # GIVEN
        self.blue1.clearMessageHistory()
        self.p._lastbal = 0
        self.p.ignoreCheck = Mock(return_value=False)
        self.console.time = Mock(return_value=120)
        self.console.game.gameType = 'tdm'
        # WHEN
        self.blue1.says('!balance')
        # THEN
        self.console.write.assert_has_calls([call('bigtext "Balancing teams!"')])

    def test_round_based_gametype_delayed_announce_only(self):
        # GIVEN
        self.blue1.clearMessageHistory()
        self.p._lastbal = 0
        self.p.ignoreCheck = Mock(return_value=False)
        self.console.time = Mock(return_value=120)
        self.console.game.gameType = 'bm'
        # WHEN
        self.blue1.says('!balance')
        # THEN
        self.assertEqual(self.blue1.message_history, ['Teams will be balanced at the end of this round'])
        self.assertFalse(self.p._pending_teambalance)
        self.assertTrue(self.p._pending_skillbalance)
        self.assertEqual(self.p.cmd_pabalance, self.p._skillbalance_func)

    def test_round_based_gametype_delayed_execution(self):
        # GIVEN
        self.blue1.clearMessageHistory()
        self.p._lastbal = 0
        self.p.ignoreCheck = Mock(return_value=False)
        self.console.time = Mock(return_value=120)
        self.console.game.gameType = 'bm'
        self.blue1.says('!balance')
        self.assertEqual(self.blue1.message_history, ['Teams will be balanced at the end of this round'])
        self.assertFalse(self.p._pending_teambalance)
        self.assertTrue(self.p._pending_skillbalance)
        self.assertEqual(self.p.cmd_pabalance, self.p._skillbalance_func)
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_GAME_ROUND_END'))
        # THEN
        self.console.write.assert_has_calls([call('bigtext "Balancing teams!"')])
        self.assertFalse(self.p._pending_teambalance)
        self.assertFalse(self.p._pending_skillbalance)
        self.assertIsNone(self.p._skillbalance_func)
class Test_headshotcounter(Iourt41TestCase):
    def setUp(self):
        super(Test_headshotcounter, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[headshotcounter]
# enable the headshot counter?
hs_enable: True
# reset counts? Options: no / map / round
reset_vars: no
# set broadcast to True if you want the counter to appear in the upper left, False is in chatarea
broadcast: True
# Announce every single headshot?
announce_all: True
# Announce percentages (after 5 headshots)
announce_percentages: True
# Only show percentages larger than next threshold
percent_min: 10
# Advise victims to wear a helmet?
warn_helmet: True
# After how many headshots?
warn_helmet_nr: 7
# Advise victims to wear kevlar?
warn_kevlar: True
# After how many torso hits?
warn_kevlar_nr: 50
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()
        self.p.onLoadConfig()
        self.p.onStartup()

        self.console.say = Mock()
        self.console.write = Mock()


    def test_hitlocation(self):

        def joe_hits_simon(hitloc):
            #Hit: 13 10 0 8: Grover hit jacobdk92 in the Head
            #Hit: cid acid hitloc aweap: text
            self.console.parseLine("Hit: 7 6 %s 8: Grover hit jacobdk92 in the Head" % hitloc)

        def assertCounts(head, helmet, torso):
            self.assertEqual(head, self.joe.var(self.p, 'headhits', default=0.0).value)
            self.assertEqual(helmet, self.joe.var(self.p, 'helmethits', default=0.0).value)
            self.assertEqual(torso, self.simon.var(self.p, 'torsohitted', default=0.0).value)


        # GIVEN
        self.joe.connects("6")
        self.simon.connects("7")

        # WHEN
        joe_hits_simon('4')
        # THEN
        assertCounts(head=0.0, helmet=0.0, torso=0.0)

        # WHEN
        joe_hits_simon('0')
        # THEN
        assertCounts(head=1.0, helmet=0.0, torso=0.0)

        # WHEN
        joe_hits_simon('1')
        # THEN
        assertCounts(head=1.0, helmet=1.0, torso=0.0)

        # WHEN
        joe_hits_simon('2')
        # THEN
        assertCounts(head=1.0, helmet=1.0, torso=1.0)
class Test_radio_spam_protection(Iourt42TestCase):
    def setUp(self):
        super(Test_radio_spam_protection, self).setUp()
        self.conf = CfgConfigParser()
        self.p = PoweradminurtPlugin(self.console, self.conf)
        self.init_default_cvar()


    def init(self, config_content=None):
        if config_content:
            self.conf.loadFromString(config_content)
        else:
            self.conf.loadFromString("""
[radio_spam_protection]
enable: True
mute_duration: 2
        """)
        self.p.onLoadConfig()
        self.p.onStartup()



    def test_conf_nominal(self):
        self.init("""
[radio_spam_protection]
enable: True
mute_duration: 2
        """)
        self.assertTrue(self.p._rsp_enable)
        self.assertEqual(2, self.p._rsp_mute_duration)


    def test_conf_nominal_2(self):
        self.init("""
[radio_spam_protection]
enable: no
mute_duration: 1
        """)
        self.assertFalse(self.p._rsp_enable)
        self.assertEqual(1, self.p._rsp_mute_duration)


    def test_conf_broken(self):
        self.init("""
[radio_spam_protection]
enable: f00
mute_duration: 0
        """)
        self.assertFalse(self.p._rsp_enable)
        self.assertEqual(1, self.p._rsp_mute_duration)



    def test_spam(self):
        # GIVEN
        self.init("""
[radio_spam_protection]
enable: True
mute_duration: 2
""")
        self.joe.connects("0")
        self.console.write = Mock(wraps=lambda x: sys.stderr.write("%s\n" % x))
        self.joe.warn = Mock()

        def joe_radio(msg_group, msg_id, location, text):
            self.console.parseLine('''Radio: 0 - %s - %s - "%s" - "%s"''' % (msg_group, msg_id, location, text))

        def assertSpampoints(points):
            self.assertEqual(points, self.joe.var(self.p, 'radio_spamins', 0).value)

        assertSpampoints(0)

        # WHEN
        when(self.p).getTime().thenReturn(0)
        joe_radio(3, 3, "Patio Courtyard", "Requesting medic. Status: healthy")
        # THEN
        assertSpampoints(0)
        self.assertEqual(0, self.joe.warn.call_count)
        self.assertEqual(0, self.console.write.call_count)

        # WHEN
        when(self.p).getTime().thenReturn(0)
        joe_radio(3, 3, "Patio Courtyard", "Requesting medic. Status: healthy")
        # THEN
        assertSpampoints(8)
        self.assertEqual(0, self.joe.warn.call_count)
        self.assertEqual(0, self.console.write.call_count)

        # WHEN
        when(self.p).getTime().thenReturn(1)
        joe_radio(3, 1, "Patio Courtyard", "f00")
        # THEN
        assertSpampoints(5)
        self.assertEqual(0, self.joe.warn.call_count)
        self.console.write.assert_has_calls([call("mute 0 2")])
class Test_cmd_pagear(Iourt42TestCase):
    def setUp(self):
        super(Test_cmd_pagear, self).setUp()
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""
[commands]
pagear-gear: 20
        """)
        self.p = PoweradminurtPlugin(self.console, self.conf)

        when(self.console).getCvar('timelimit').thenReturn(
            Cvar('timelimit', value=20))
        when(self.console).getCvar('g_maxGameClients').thenReturn(
            Cvar('g_maxGameClients', value=16))
        when(self.console).getCvar('sv_maxclients').thenReturn(
            Cvar('sv_maxclients', value=16))
        when(self.console).getCvar('sv_privateClients').thenReturn(
            Cvar('sv_privateClients', value=0))
        when(self.console).getCvar('g_allowvote').thenReturn(
            Cvar('g_allowvote', value=0))
        when(self.console).getCvar('g_modversion').thenReturn(
            Cvar('g_modversion', value="4.2.018"))
        self.given_forbidden_weapon_are(G_NONE)
        self.p.onLoadConfig()
        self.p.onStartup()

        self.sleep_patcher = patch.object(time, 'sleep')
        self.sleep_patcher.start()
        self.setCvar_patcher = patch.object(self.console, 'setCvar')
        self.setCvar_mock = self.setCvar_patcher.start()

        self.superadmin.connects("2")

    def tearDown(self):
        super(Test_cmd_pagear, self).tearDown()
        self.sleep_patcher.stop()
        self.setCvar_patcher.stop()

    def given_forbidden_weapon_are(self, g_gear_value):
        when(self.console).getCvar('g_gear').thenReturn(
            Cvar('g_gear', value=str(g_gear_value)))

    def assert_set_gear(self, expected_gear_value, fail_message=None):
        self.assertListEqual([call('g_gear', str(expected_gear_value))],
                             self.setCvar_mock.mock_calls, fail_message)

    def test_no_parameter(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        self.p.onStartup()
        # WHEN
        self.superadmin.message_history = []
        self.superadmin.says("!gear")
        # THEN
        self.assertListEqual([
            "current gear: med:ON, vest:ON, ak:ON, de:ON, psg:ON, nvg:ON, hk:ON, mac:ON, mp5:ON, las:ON, ber:ON, ump:ON, g36:ON, neg:ON, glo:ON, smo:ON, m4:ON, lr:ON, hel:ON, spas:ON, sr8:ON, he:ON, colt:ON, ammo:ON, sil:ON",
            "Usage: !pagear [+/-][med|vest|ak|de|psg|nvg|hk|mac|mp5|las|ber|ump|g36|neg|glo|smo|m4|lr|hel|spas|sr8|he|colt|ammo|sil]",
            "Load weapon groups: !pagear [+/-][all_nades|all_auto|all_pistols|all_snipers]",
            "Load defaults: !pagear [reset|all|none]"
        ], self.superadmin.message_history)

    def test_reset(self):
        # GIVEN
        self.given_forbidden_weapon_are("1234")
        self.p.onStartup()
        # WHEN
        self.superadmin.says("!gear reset")
        # THEN
        self.assert_set_gear('1234')

    def test_all(self):
        # WHEN
        self.superadmin.says("!gear all")
        # THEN
        self.assert_set_gear(G_NONE)

    def test_none(self):
        # WHEN
        self.superadmin.says("!gear none")
        # THEN
        self.assert_set_gear(G_ALL)

    def test_unknown_weapon(self):
        # WHEN
        self.superadmin.says("!gear +f00")
        # THEN
        self.assertListEqual([], self.setCvar_mock.mock_calls)
        self.assertListEqual(
            ["could not recognize weapon/item 'f00'", "gear not changed"],
            self.superadmin.message_history)

    def test_disallow_negev(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -neg")
        # THEN
        self.assert_set_gear(G_NEGEV_LMG)

    def test_allow_negev_short(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +neg")
        # THEN
        self.assert_set_gear(all_gear_but(G_NEGEV_LMG))

    def test_allow_negev_long(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +negev")
        # THEN
        self.assert_set_gear(all_gear_but(G_NEGEV_LMG))

    def test_allow_negev_long_spaced(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear + negev")
        # THEN
        self.assert_set_gear(all_gear_but(G_NEGEV_LMG))

    def test_disallow(self):
        for weapon_name, weapon_code in weapon_codes:
            # GIVEN
            self.setCvar_mock.reset_mock()
            self.given_forbidden_weapon_are(G_NONE)
            # WHEN
            self.superadmin.says("!gear -%s" % weapon_name)
            # THEN
            self.assert_set_gear(weapon_code, weapon_name)

    def test_allow(self):
        for weapon_name, weapon_code in weapon_codes:
            # GIVEN
            self.setCvar_mock.reset_mock()
            self.given_forbidden_weapon_are(G_ALL)
            # WHEN
            self.superadmin.says("!gear +%s" % weapon_name)
            # THEN
            self.assert_set_gear(all_gear_but(weapon_code), weapon_name)

    def test_allow_group_nades(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +all_nades")
        # THEN
        self.assert_set_gear(all_gear_but(G_SMOKE_GRENADE, G_HE_GRENADE))

    def test_disallow_group_nades(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -all_nades")
        # THEN
        self.assert_set_gear(only_gear(G_SMOKE_GRENADE, G_HE_GRENADE))

    def test_disallow_group_nades_spaced(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear - all_nades")
        # THEN
        self.assert_set_gear(only_gear(G_SMOKE_GRENADE, G_HE_GRENADE))

    def test_allow_all_snipers(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +all_snipers")
        # THEN
        self.assert_set_gear(all_gear_but(G_SR8, G_PSG1))

    def test_disallow_all_snipers(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -all_snipers")
        # THEN
        self.assert_set_gear(only_gear(G_SR8, G_PSG1))

    def test_allow_all_pistols(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +all_pistols")
        # THEN
        self.assert_set_gear(
            all_gear_but(G_BERETTA_92FS, G_50_DESERT_EAGLE, G_GLOCK,
                         G_COLT1911))

    def test_disallow_all_pistols(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -all_pistols")
        # THEN
        self.assert_set_gear(
            only_gear(G_BERETTA_92FS, G_50_DESERT_EAGLE, G_GLOCK, G_COLT1911))

    def test_allow_all_auto(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear +all_auto")
        # THEN
        self.assert_set_gear(
            all_gear_but(G_MP5K, G_LR300ML, G_COLT_M4, G_MAC11, G_UMP45, G_G36,
                         G_AK103, G_NEGEV_LMG))

    def test_disallow_all_auto(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_NONE)
        # WHEN
        self.superadmin.says("!gear -all_auto")
        # THEN
        self.assert_set_gear(
            only_gear(G_MP5K, G_LR300ML, G_COLT_M4, G_MAC11, G_UMP45, G_G36,
                      G_AK103, G_NEGEV_LMG))

    def test_disallow_all_auto_and_no_smoke(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear all -all_auto -smoke")
        # THEN
        self.assert_set_gear(
            only_gear(G_MP5K, G_LR300ML, G_COLT_M4, G_MAC11, G_UMP45, G_G36,
                      G_AK103, G_NEGEV_LMG, G_SMOKE_GRENADE))

    def test_disallow_all_auto_and_no_smoke_spaced(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear all - all_auto - smoke")
        # THEN
        self.assert_set_gear(
            only_gear(G_MP5K, G_LR300ML, G_COLT_M4, G_MAC11, G_UMP45, G_G36,
                      G_AK103, G_NEGEV_LMG, G_SMOKE_GRENADE))

    def test_disallow_all_auto_but_lr300(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear all -all_auto +lr")
        # THEN
        self.assert_set_gear(
            only_gear(G_MP5K, G_COLT_M4, G_MAC11, G_UMP45, G_G36, G_AK103,
                      G_NEGEV_LMG))

    def test_disallow_all_auto_but_lr300_spaced(self):
        # GIVEN
        self.given_forbidden_weapon_are(G_ALL)
        # WHEN
        self.superadmin.says("!gear all - all_auto + lr")
        # THEN
        self.assert_set_gear(
            only_gear(G_MP5K, G_COLT_M4, G_MAC11, G_UMP45, G_G36, G_AK103,
                      G_NEGEV_LMG))