Esempio n. 1
0
    def setUp(self):
        # create a FakeConsole parser
        parser_ini_conf = CfgConfigParser()
        parser_ini_conf.loadFromString(r'''''')
        self.parser_main_conf = MainConfig(parser_ini_conf)

        with logging_disabled():
            from tests.fake import FakeConsole
            self.console = FakeConsole(self.parser_main_conf)

        with logging_disabled():
            self.adminPlugin = AdminPlugin(self.console,
                                           '@b3/conf/plugin_admin.ini')
            self.adminPlugin._commands = {}
            self.adminPlugin.onStartup()

        # make sure the admin plugin obtained by other plugins is our admin plugin
        when(self.console).getPlugin('admin').thenReturn(self.adminPlugin)

        # create our plugin instance
        self.p = SpreePlugin(self.console, CfgConfigParser())

        with logging_disabled():
            from tests.fake import FakeClient

        self.mike = FakeClient(console=self.console,
                               name="Mike",
                               guid="MIKEGUID",
                               groupBits=1)
        self.bill = FakeClient(console=self.console,
                               name="Bill",
                               guid="BILLGUID",
                               groupBits=1)
Esempio n. 2
0
    def setUp(self):
        SpamcontrolTestCase.setUp(self)

        with open(getAbsolutePath('@b3/conf/plugin_spamcontrol.ini')) as default_conf:
            self.init_plugin(default_conf.read())

        self.joe = FakeClient(self.console, name="Joe", exactName="Joe", guid="zaerezarezar", groupBits=1)
        self.joe.connects("1")

        # let's say our game has a new event : EVT_CLIENT_RADIO
        EVT_CLIENT_RADIO = self.console.Events.createEvent('EVT_CLIENT_RADIO', 'Event client radio')

        # teach the Spamcontrol plugin how to react on such events
        def onRadio(this, event):
            new_event = Event(type=event.type, client=event.client, target=event.target, data=event.data['text'])
            this.onChat(new_event)

        self.p.onRadio = MethodType(onRadio, self.p)
        self.p.registerEvent('EVT_CLIENT_RADIO', self.p.onRadio)

        # patch joe to make him able to send radio messages
        def radios(me, text):
            me.console.queueEvent(Event(type=EVT_CLIENT_RADIO, client=me, data={'text': text}))

        self.joe.radios = MethodType(radios, self.joe)
Esempio n. 3
0
    def setUp(self):

        B3TestCase.setUp(self)

        with logging_disabled():
            self.adminPlugin = AdminPlugin(self.console,
                                           '@b3/conf/plugin_admin.ini')
            when(self.console).getPlugin("admin").thenReturn(self.adminPlugin)
            self.adminPlugin.onLoadConfig()
            self.adminPlugin.onStartup()

            self.conf = CfgConfigParser()
            self.p = WelcomePlugin(self.console, self.conf)

            self.joe = FakeClient(self.console,
                                  name="Joe",
                                  guid="joeguid",
                                  groupBits=1,
                                  team=b3.TEAM_RED)
            self.mike = FakeClient(self.console,
                                   name="Mike",
                                   guid="mikeguid",
                                   groupBits=1,
                                   team=b3.TEAM_RED)
            self.bill = FakeClient(self.console,
                                   name="Bill",
                                   guid="billguid",
                                   groupBits=1,
                                   team=b3.TEAM_RED)
            self.superadmin = FakeClient(self.console,
                                         name="SuperAdmin",
                                         guid="superadminguid",
                                         groupBits=128,
                                         team=b3.TEAM_RED)
Esempio n. 4
0
 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, '')])
Esempio n. 5
0
 def test_cmd_spamins_uppercase(self):
     # GIVEN
     mike = FakeClient(self.console, name="Mike")
     mike.connects("3")
     # WHEN
     self.superadmin.clearMessageHistory()
     self.superadmin.says("!spamins MIKE")
     # THEN
     self.assertListEqual(['Mike currently has 0 spamins, peak was 0'], self.superadmin.message_history)
Esempio n. 6
0
 def test_regulars(self):
     # GIVEN
     joe = FakeClient(self.console, name="Joe", guid="joeguid", groupBits=2)
     joe.connects(0)
     when(self.p.admin_plugin).getRegulars().thenReturn([joe])
     with patch.object(self.console, "say") as say_mock:
         # WHEN
         self.p.print_ad('@regulars')
     # THEN
     say_mock.assert_has_calls([call('^7Regular players online: Joe^7')])
Esempio n. 7
0
 def test_nextmap(self):
     when(self.console).getNextMap().thenReturn("f00")
     joe = FakeClient(self.console,
                      name="Joe",
                      guid="joeguid",
                      groupBits=128)
     joe.connects(0)
     with patch.object(self.console, "say") as say_mock:
         # WHEN
         self.p.print_ad('@nextmap')
     # THEN
     say_mock.assert_has_calls([call('^2Next map: ^3f00')])
Esempio n. 8
0
 def test_time(self):
     when(self.console).formatTime(ANY()).thenReturn("f00")
     joe = FakeClient(self.console,
                      name="Joe",
                      guid="joeguid",
                      groupBits=128)
     joe.connects(0)
     with patch.object(self.console, "say") as say_mock:
         # WHEN
         self.p.print_ad('@time')
     # THEN
     say_mock.assert_has_calls([call('^2Time: ^3f00')])
Esempio n. 9
0
 def test_admins(self):
     # GIVEN
     joe = FakeClient(self.console,
                      name="Joe",
                      guid="joeguid",
                      groupBits=128)
     joe.connects(0)
     when(self.p.admin_plugin).getAdmins().thenReturn([joe])
     with patch.object(self.console, "say") as say_mock:
         # WHEN
         self.p.print_ad('@admins')
     # THEN
     say_mock.assert_has_calls([call('^7Admins online: Joe^7^7 [^3100^7]')])
Esempio n. 10
0
 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, '')])
Esempio n. 11
0
    def setUp(self):
        Welcome_functional_test.setUp(self)
        self.load_config()
        # disabled event handling (spawns threads and is of no use for that test)
        self.p.onEvent = lambda *args, **kwargs: None

        self.client = FakeClient(console=self.console, name="Jack", guid="JackGUID")
        self.client._connections = 0
        self.client.greeting = 'hi everyone :)'
        self.client.connects("0")
        self.superadmin.connects("1")

        self.say_patcher = patch.object(self.console, "say")
        self.say_mock = self.say_patcher.start()
Esempio n. 12
0
 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)
Esempio n. 13
0
 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)
Esempio n. 14
0
    def setUp(self):
        SpamcontrolTestCase.setUp(self)

        self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.ini')
        when(self.console).getPlugin("admin").thenReturn(self.adminPlugin)
        self.adminPlugin.onLoadConfig()
        self.adminPlugin.onStartup()

        with open(b3.functions.getAbsolutePath('@b3/conf/plugin_spamcontrol.ini')) as default_conf:
            self.init_plugin(default_conf.read())

        self.joe = FakeClient(self.console, name="Joe", guid="zaerezarezar", groupBits=1)
        self.joe.connects("1")

        self.superadmin = FakeClient(self.console, name="Superadmin", guid="superadmin_guid", groupBits=128)
        self.superadmin.connects("2")
Esempio n. 15
0
class StatPluginTestCase(B3TestCase):
    def setUp(self):
        B3TestCase.setUp(self)

        with logging_disabled():
            admin_conf = CfgConfigParser()
            admin_plugin = AdminPlugin(self.console, admin_conf)
            admin_plugin.onLoadConfig()
            admin_plugin.onStartup()
            when(self.console).getPlugin('admin').thenReturn(admin_plugin)

        conf = CfgConfigParser()
        conf.loadFromString(
            dedent(r"""
            [commands]
            mapstats-stats: 0
            testscore-ts: 0
            topstats-top: 0
            topxp: 0

            [settings]
            startPoints: 100
            resetscore: no
            resetxp: no
            show_awards: no
            show_awards_xp: no
        """))
        self.p = StatsPlugin(self.console, conf)
        self.p.onLoadConfig()
        self.p.onStartup()

        self.joe = FakeClient(self.console,
                              name="Joe",
                              guid="joeguid",
                              groupBits=1,
                              team=TEAM_RED)
        self.mike = FakeClient(self.console,
                               name="Mike",
                               guid="mikeguid",
                               groupBits=1,
                               team=TEAM_RED)
        self.joe.connects(1)
        self.mike.connects(2)
Esempio n. 16
0
    def init(self, config_content=None):
        """ optionally specify a config for the plugin. If called with no parameter, then the default config is loaded """
        if config_content is None:
            self.conf.load(
                b3.functions.getAbsolutePath("@b3/conf/plugin_admin.ini"))
        else:
            self.conf.loadFromString(config_content)

        self.p._commands = {}
        self.p.onLoadConfig()
        self.p.onStartup()

        self.joe = FakeClient(self.console,
                              name="Joe",
                              exactName="Joe",
                              guid="joeguid",
                              groupBits=128,
                              team=TEAM_RED)
        self.mike = FakeClient(self.console,
                               name="Mike",
                               exactName="Mike",
                               guid="mikeguid",
                               groupBits=1,
                               team=TEAM_BLUE)
Esempio n. 17
0
    def setUp(self):
        # create a Iourt43 parser
        self.parser_conf = XmlConfigParser()
        self.parser_conf.loadFromString(
            dedent(r"""
            <configuration>
                <settings name="server">
                    <set name="game_log"></set>
                </settings>
            </configuration>
        """))

        self.console = Iourt43Parser(self.parser_conf)

        # initialize some fixed cvars which will be used by both the plugin and the iourt42 parser
        when(self.console).getCvar('auth').thenReturn(Cvar('auth', value='0'))
        when(self.console).getCvar('fs_basepath').thenReturn(
            Cvar('g_maxGameClients', value='/fake/basepath'))
        when(self.console).getCvar('fs_homepath').thenReturn(
            Cvar('sv_maxclients', value='/fake/homepath'))
        when(self.console).getCvar('fs_game').thenReturn(
            Cvar('fs_game', value='q3ut4'))
        when(self.console).getCvar('gamename').thenReturn(
            Cvar('gamename', value='q3urt43'))

        # start the parser
        self.console.startup()

        self.admin_plugin_conf = CfgConfigParser()
        self.admin_plugin_conf.loadFromString(
            dedent(r"""
            [warn]
            pm_global: yes
            alert_kick_num: 3
            instant_kick_num: 5
            tempban_num: 6
            tempban_duration: 1d
            max_duration: 1d
            message: ^1WARNING^7 [^3$warnings^7]: $reason
            warn_delay: 15
            reason: ^7too many warnings: $reason
            duration_divider: 30
            alert: ^1ALERT^7: $name^7 auto-kick from warnings if not cleared [^3$warnings^7] $reason
            warn_command_abusers: no"""))

        with logging_disabled():
            self.adminPlugin = AdminPlugin(self.console,
                                           self.admin_plugin_conf)
            self.adminPlugin.onLoadConfig()
            self.adminPlugin.onStartup()

        # make sure the admin plugin obtained by other plugins is our admin plugin
        when(self.console).getPlugin('admin').thenReturn(self.adminPlugin)

        with logging_disabled():
            from tests.fake import FakeClient

        # create some clients
        self.mike = FakeClient(console=self.console,
                               name="Mike",
                               guid="mikeguid",
                               team=TEAM_RED,
                               groupBits=1)
        self.bill = FakeClient(console=self.console,
                               name="Bill",
                               guid="billguid",
                               team=TEAM_BLUE,
                               groupBits=1)
        self.mark = FakeClient(console=self.console,
                               name="Mark",
                               guid="markguid",
                               team=TEAM_BLUE,
                               groupBits=128)

        self.conf = CfgConfigParser()
        self.p = SpawnkillPlugin(self.console, self.conf)
Esempio n. 18
0
class SpawnkillTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        with logging_disabled():
            from b3.parsers.iourt43 import Iourt43Parser
            from tests.fake import FakeConsole
            Iourt43Parser.__bases__ = (FakeConsole, )
            # Now parser inheritance hierarchy is :
            # Iourt43Parser -> abstractParser -> FakeConsole -> Parser

    def setUp(self):
        # create a Iourt43 parser
        self.parser_conf = XmlConfigParser()
        self.parser_conf.loadFromString(
            dedent(r"""
            <configuration>
                <settings name="server">
                    <set name="game_log"></set>
                </settings>
            </configuration>
        """))

        self.console = Iourt43Parser(self.parser_conf)

        # initialize some fixed cvars which will be used by both the plugin and the iourt42 parser
        when(self.console).getCvar('auth').thenReturn(Cvar('auth', value='0'))
        when(self.console).getCvar('fs_basepath').thenReturn(
            Cvar('g_maxGameClients', value='/fake/basepath'))
        when(self.console).getCvar('fs_homepath').thenReturn(
            Cvar('sv_maxclients', value='/fake/homepath'))
        when(self.console).getCvar('fs_game').thenReturn(
            Cvar('fs_game', value='q3ut4'))
        when(self.console).getCvar('gamename').thenReturn(
            Cvar('gamename', value='q3urt43'))

        # start the parser
        self.console.startup()

        self.admin_plugin_conf = CfgConfigParser()
        self.admin_plugin_conf.loadFromString(
            dedent(r"""
            [warn]
            pm_global: yes
            alert_kick_num: 3
            instant_kick_num: 5
            tempban_num: 6
            tempban_duration: 1d
            max_duration: 1d
            message: ^1WARNING^7 [^3$warnings^7]: $reason
            warn_delay: 15
            reason: ^7too many warnings: $reason
            duration_divider: 30
            alert: ^1ALERT^7: $name^7 auto-kick from warnings if not cleared [^3$warnings^7] $reason
            warn_command_abusers: no"""))

        with logging_disabled():
            self.adminPlugin = AdminPlugin(self.console,
                                           self.admin_plugin_conf)
            self.adminPlugin.onLoadConfig()
            self.adminPlugin.onStartup()

        # make sure the admin plugin obtained by other plugins is our admin plugin
        when(self.console).getPlugin('admin').thenReturn(self.adminPlugin)

        with logging_disabled():
            from tests.fake import FakeClient

        # create some clients
        self.mike = FakeClient(console=self.console,
                               name="Mike",
                               guid="mikeguid",
                               team=TEAM_RED,
                               groupBits=1)
        self.bill = FakeClient(console=self.console,
                               name="Bill",
                               guid="billguid",
                               team=TEAM_BLUE,
                               groupBits=1)
        self.mark = FakeClient(console=self.console,
                               name="Mark",
                               guid="markguid",
                               team=TEAM_BLUE,
                               groupBits=128)

        self.conf = CfgConfigParser()
        self.p = SpawnkillPlugin(self.console, self.conf)

    def tearDown(self):
        self.console.shutdown()
        self.mike.disconnects()
        self.bill.disconnects()
        self.mark.disconnects()
        unstub()

    def init(self, config_content=None):
        if config_content:
            self.conf.loadFromString(config_content)
        else:
            self.conf.loadFromString(
                dedent(r"""
                [hit]
                maxlevel: admin
                delay: 2
                penalty: warn
                duration: 3m
                reason: do not shoot to spawning players!

                [kill]
                maxlevel: admin
                delay: 3
                penalty: warn
                duration: 5m
                reason: spawnkilling is not allowed on this server!
            """))

        self.p.onLoadConfig()
        self.p.onStartup()
Esempio n. 19
0
 def setUp(self):
     AdvTestCase.setUp(self)
     self.joe = FakeClient(self.console,
                           name="Joe",
                           guid="joeguid",
                           groupBits=128)
Esempio n. 20
0
class Test_commands(AdvTestCase):
    def setUp(self):
        AdvTestCase.setUp(self)
        self.joe = FakeClient(self.console,
                              name="Joe",
                              guid="joeguid",
                              groupBits=128)

    def test_advlist_empty(self):
        self.init_plugin("""
            [settings]
            rate: 3
        """)
        self.joe.clearMessageHistory()
        self.p.cmd_advlist(data=None, client=self.joe)
        self.assertEqual([], self.p.ad_list)
        self.assertEqual(['Adv: no ads loaded'], self.joe.message_history)

    def test_advlist_one_item(self):
        self.init_plugin("""
            [settings]
            rate: 3

            [messages]
            ad1: f00
        """)
        self.joe.clearMessageHistory()
        self.p.cmd_advlist(data=None, client=self.joe)
        self.assertEqual(['f00'], self.p.ad_list)
        self.assertEqual(['Adv: [1] f00'], self.joe.message_history)

    def test_advlist_many_items(self):
        self.init_plugin("""
            [settings]
            rate: 3

            [messages]
            ad1: f00
            ad2: bar
            ad3: test
        """)
        self.joe.clearMessageHistory()
        self.p.cmd_advlist(data=None, client=self.joe)
        self.assertEqual(['f00', 'bar', 'test'], self.p.ad_list)
        self.assertEqual(['Adv: [1] f00', 'Adv: [2] bar', 'Adv: [3] test'],
                         self.joe.message_history)

    def test_advrate_no_arg_3min(self):
        self.init_plugin("""
            [settings]
            rate: 3

            [messages]
            ad1: f00
            ad2: bar
            ad3: test
        """)
        self.joe.clearMessageHistory()
        self.p.cmd_advrate(data='', client=self.joe)
        self.assertEqual('3', self.p._rate)
        self.assertEqual(['Current rate is every 3 minutes'],
                         self.joe.message_history)

    def test_advrate_no_arg_2min(self):
        self.init_plugin("""
            [settings]
            rate: 2

            [messages]
            ad1: f00
            ad2: bar
            ad3: test
        """)
        self.joe.clearMessageHistory()
        self.p.cmd_advrate(data=None, client=self.joe)
        self.assertEqual('2', self.p._rate)
        self.assertEqual(['Current rate is every 2 minutes'],
                         self.joe.message_history)

    def test_advrate_set_2min(self):
        self.init_plugin("""
            [settings]
            rate: 4

            [messages]
            ad1: f00
            ad2: bar
            ad3: test
        """)
        self.assertEqual('4', self.p._rate)
        self.joe.clearMessageHistory()
        self.p.cmd_advrate(data="2", client=self.joe)
        self.assertEqual('2', self.p._rate)
        self.assertEqual(['Adv: rate set to 2 minutes'],
                         self.joe.message_history)

    def test_advrate_set_3min(self):
        self.init_plugin("""
            [settings]
            rate: 4

            [messages]
            ad1: f00
            ad2: bar
            ad3: test
        """)
        self.assertEqual('4', self.p._rate)
        self.joe.clearMessageHistory()
        self.p.cmd_advrate(data="3", client=self.joe)
        self.assertEqual('3', self.p._rate)
        self.assertEqual(['Adv: rate set to 3 minutes'],
                         self.joe.message_history)

    def test_advrem_nominal(self):
        self.init_plugin("""
            [settings]
            rate: 4

            [messages]
            ad1: f00
            ad2: bar
            ad3: test
        """)
        self.assertEqual(['f00', 'bar', 'test'], self.p.ad_list)
        self.joe.clearMessageHistory()
        self.p.cmd_advrem(data="2", client=self.joe)
        self.assertEqual(['f00', 'test'], self.p.ad_list)
        self.assertEqual(['Adv: removed item: bar'], self.joe.message_history)

    def test_advrem_no_arg(self):
        self.init_plugin("""
            [settings]
            rate: 4

            [messages]
            ad1: f00
            ad2: bar
            ad3: test
        """)
        self.assertEqual(['f00', 'bar', 'test'], self.p.ad_list)
        self.joe.clearMessageHistory()
        self.p.cmd_advrem(data=None, client=self.joe)
        self.assertEqual(['f00', 'bar', 'test'], self.p.ad_list)
        self.assertEqual(['Missing data, try !help advrem'],
                         self.joe.message_history)

    def test_advrem_junk(self):
        self.init_plugin("""
            [settings]
            rate: 4

            [messages]
            ad1: f00
            ad2: bar
            ad3: test
        """)
        self.assertEqual(['f00', 'bar', 'test'], self.p.ad_list)
        self.joe.clearMessageHistory()
        self.p.cmd_advrem(data='f00', client=self.joe)
        self.assertEqual(['f00', 'bar', 'test'], self.p.ad_list)
        self.assertEqual([
            'Invalid data, use the !advlist command to list valid items numbers'
        ], self.joe.message_history)

    def test_advrem_invalid_index(self):
        self.init_plugin("""
            [settings]
            rate: 4

            [messages]
            ad1: f00
            ad2: bar
            ad3: test
        """)
        self.assertEqual(['f00', 'bar', 'test'], self.p.ad_list)
        self.joe.clearMessageHistory()
        self.p.cmd_advrem(data='-18', client=self.joe)
        self.assertEqual(['f00', 'bar', 'test'], self.p.ad_list)
        self.assertEqual([
            'Invalid data, use the !advlist command to list valid items numbers'
        ], self.joe.message_history)

    def test_advadd_nominal(self):
        self.init_plugin("""
            [settings]
            rate: 4

            [messages]
            ad1: f00
        """)
        self.assertEqual(['f00'], self.p.ad_list)
        self.joe.clearMessageHistory()
        self.p.cmd_advadd(data="bar", client=self.joe)
        self.assertEqual(['f00', 'bar'], self.p.ad_list)
        self.assertEqual(['Adv: "bar" added'], self.joe.message_history)

    def test_advadd_no_arg(self):
        self.init_plugin("""
            [settings]
            rate: 4

            [messages]
            ad1: f00
        """)
        self.assertEqual(['f00'], self.p.ad_list)
        self.joe.clearMessageHistory()
        self.p.cmd_advadd(data=None, client=self.joe)
        self.assertEqual(['f00'], self.p.ad_list)
        self.assertEqual(['Missing data, try !help advadd'],
                         self.joe.message_history)
Esempio n. 21
0
    def setUp(self):
        # create a Iourt43 parser
        parser_conf = XmlConfigParser()
        parser_conf.loadFromString(
            dedent(r"""
            <configuration>
                <settings name="server">
                    <set name="game_log"></set>
                </settings>
            </configuration>
        """))

        self.parser_conf = MainConfig(parser_conf)
        self.console = Iourt43Parser(self.parser_conf)

        # initialize some fixed cvars which will be used by both the plugin and the iourt42 parser
        when(self.console).getCvar('auth').thenReturn(Cvar('auth', value='0'))
        when(self.console).getCvar('auth_owners').thenReturn(None)
        when(self.console).getCvar('fs_basepath').thenReturn(
            Cvar('fs_basepath', value='/fake/basepath'))
        when(self.console).getCvar('fs_homepath').thenReturn(
            Cvar('fs_homepath', value='/fake/homepath'))
        when(self.console).getCvar('fs_game').thenReturn(
            Cvar('fs_game', value='q3ut4'))
        when(self.console).getCvar('gamename').thenReturn(
            Cvar('gamename', value='q3urt43'))

        # start the parser
        self.console.startup()

        with logging_disabled():
            self.adminPlugin = AdminPlugin(self.console,
                                           '@b3/conf/plugin_admin.ini')
            self.adminPlugin.onLoadConfig()
            self.adminPlugin.onStartup()

        # make sure the admin plugin obtained by other plugins is our admin plugin
        when(self.console).getPlugin('admin').thenReturn(self.adminPlugin)

        self.conf = CfgConfigParser()
        self.conf.loadFromString(
            dedent(r"""
            [settings]
            enabled: on
            minlevel: 40
            nbtop: 5
            
            [messages]
            msg_5: ^3$name ^4$score ^7HE grenade kills!
        """))

        self.p = NaderPlugin(self.console, self.conf)
        self.p.onLoadConfig()
        self.p.onStartup()

        with logging_disabled():
            from tests.fake import FakeClient

        # create some clients
        self.mike = FakeClient(console=self.console,
                               name="Mike",
                               guid="mikeguid",
                               team=TEAM_BLUE,
                               groupBits=1)
        self.mark = FakeClient(console=self.console,
                               name="Mark",
                               guid="markguid",
                               team=TEAM_BLUE,
                               groupBits=1)
        self.bill = FakeClient(console=self.console,
                               name="Bill",
                               guid="billguid",
                               team=TEAM_RED,
                               groupBits=1)
        self.mike.connects("1")
        self.bill.connects("2")
        self.mark.connects("3")
Esempio n. 22
0
    def setUp(self):
        with logging_disabled():
            self.parser_conf = XmlConfigParser()
            self.parser_conf.loadFromString(
                """<configuration><settings name="server"><set name="game_log"></set></settings></configuration>"""
            )
            self.console = Iourt43Parser(self.parser_conf)
            self.console.startup()

            admin_plugin_conf_file = '@b3/conf/plugin_admin.ini'
            with logging_disabled():
                self.adminPlugin = AdminPlugin(self.console,
                                               admin_plugin_conf_file)
                self.adminPlugin.onLoadConfig()
                self.adminPlugin.onStartup()

            # make sure the admin plugin obtained by other plugins is our admin plugin
            when(self.console).getPlugin('admin').thenReturn(self.adminPlugin)
            when(self.console).queryClientFrozenSandAccount(...).thenReturn({})

            # prepare a few players
            from tests.fake import FakeClient
            self.joe = FakeClient(self.console,
                                  name="Joe",
                                  exactName="Joe",
                                  guid="zaerezarezar",
                                  groupBits=1,
                                  team=TEAM_UNKNOWN,
                                  teamId=0,
                                  squad=0)
            self.simon = FakeClient(self.console,
                                    name="Simon",
                                    exactName="Simon",
                                    guid="qsdfdsqfdsqf",
                                    groupBits=0,
                                    team=TEAM_UNKNOWN,
                                    teamId=0,
                                    squad=0)
            self.reg = FakeClient(self.console,
                                  name="Reg",
                                  exactName="Reg",
                                  guid="qsdfdsqfdsqf33",
                                  groupBits=4,
                                  team=TEAM_UNKNOWN,
                                  teamId=0,
                                  squad=0)
            self.moderator = FakeClient(self.console,
                                        name="Moderator",
                                        exactName="Moderator",
                                        guid="sdf455ezr",
                                        groupBits=8,
                                        team=TEAM_UNKNOWN,
                                        teamId=0,
                                        squad=0)
            self.admin = FakeClient(self.console,
                                    name="Level-40-Admin",
                                    exactName="Level-40-Admin",
                                    guid="875sasda",
                                    groupBits=16,
                                    team=TEAM_UNKNOWN,
                                    teamId=0,
                                    squad=0)
            self.superadmin = FakeClient(self.console,
                                         name="God",
                                         exactName="God",
                                         guid="f4qfer654r",
                                         groupBits=128,
                                         team=TEAM_UNKNOWN,
                                         teamId=0,
                                         squad=0)
Esempio n. 23
0
class Test_cmd_balance(Iourt43TestCase):
    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 tests.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)
Esempio n. 24
0
    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 tests.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
Esempio n. 25
0
class Test_commands(SpamcontrolTestCase):

    def setUp(self):
        SpamcontrolTestCase.setUp(self)

        self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.ini')
        when(self.console).getPlugin("admin").thenReturn(self.adminPlugin)
        self.adminPlugin.onLoadConfig()
        self.adminPlugin.onStartup()

        with open(b3.functions.getAbsolutePath('@b3/conf/plugin_spamcontrol.ini')) as default_conf:
            self.init_plugin(default_conf.read())

        self.joe = FakeClient(self.console, name="Joe", guid="zaerezarezar", groupBits=1)
        self.joe.connects("1")

        self.superadmin = FakeClient(self.console, name="Superadmin", guid="superadmin_guid", groupBits=128)
        self.superadmin.connects("2")

    def test_cmd_spamins(self):
        # GIVEN
        when(self.p).getTime().thenReturn(0).thenReturn(3).thenReturn(4).thenReturn(4).thenReturn(500)
        self.joe.says("doh")  # 0s
        self.joe.says("doh")  # 3s
        self.joe.says("doh")  # 4s
        # WHEN
        self.superadmin.clearMessageHistory()
        self.superadmin.says("!spamins joe")
        # THEN
        self.assertListEqual(['Joe currently has 9 spamins, peak was 9'], self.superadmin.message_history)  # 4s
        # WHEN
        self.superadmin.clearMessageHistory()
        self.superadmin.says("!spamins joe")
        self.assertListEqual(['Joe currently has 0 spamins, peak was 9'], self.superadmin.message_history)  # 500s

    def test_cmd_spamins_lowercase(self):
        # GIVEN
        mike = FakeClient(self.console, name="Mike")
        mike.connects("3")
        # WHEN
        self.superadmin.clearMessageHistory()
        self.superadmin.says("!spamins mike")
        # THEN
        self.assertListEqual(['Mike currently has 0 spamins, peak was 0'], self.superadmin.message_history)

    def test_cmd_spamins_uppercase(self):
        # GIVEN
        mike = FakeClient(self.console, name="Mike")
        mike.connects("3")
        # WHEN
        self.superadmin.clearMessageHistory()
        self.superadmin.says("!spamins MIKE")
        # THEN
        self.assertListEqual(['Mike currently has 0 spamins, peak was 0'], self.superadmin.message_history)

    def test_cmd_spamins_unknown_player(self):
        # WHEN
        self.superadmin.clearMessageHistory()
        self.superadmin.says("!spamins nobody")
        # THEN
        self.assertListEqual(['No players found matching nobody'], self.superadmin.message_history)

    def test_cmd_spamins_no_argument(self):
        # GIVEN
        self.console.getPlugin('admin')._warn_command_abusers = True
        # WHEN
        self.joe.clearMessageHistory()
        self.joe.says("!spamins")
        # THEN
        self.assertListEqual(['You need to be in group Moderator to use !spamins'], self.joe.message_history)
        # WHEN
        self.superadmin.says("!putgroup joe mod")
        self.joe.clearMessageHistory()
        self.joe.says("!spamins")
        # THEN
        self.assertListEqual(['Joe is too cool to spam'], self.joe.message_history)

    def test_joe_gets_warned(self):
        # GIVEN
        when(self.p).getTime().thenReturn(0)
        self.joe.warn = Mock()

        # WHEN
        self.joe.says("doh 1")
        self.joe.says("doh 2")
        self.joe.says("doh 3")
        self.joe.says("doh 4")
        self.joe.says("doh 5")

        # THEN
        self.assertEqual(1, self.joe.warn.call_count)
Esempio n. 26
0
class Test_plugin(SpamcontrolTestCase):

    def setUp(self):
        SpamcontrolTestCase.setUp(self)

        self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.ini')
        when(self.console).getPlugin("admin").thenReturn(self.adminPlugin)
        self.adminPlugin.onLoadConfig()
        self.adminPlugin.onStartup()

        with open(b3.functions.getAbsolutePath('@b3/conf/plugin_spamcontrol.ini')) as default_conf:
            self.init_plugin(default_conf.read())

        self.joe = FakeClient(self.console, name="Joe", guid="zaerezarezar", groupBits=1)
        self.joe.connects("1")

        self.superadmin = FakeClient(self.console, name="Superadmin", guid="superadmin_guid", groupBits=128)
        self.superadmin.connects("2")

    def assertSpaminsPoints(self, client, points):
        actual = client.var(self.p, 'spamins', 0).value
        self.assertEqual(points, actual, "expecting %s to have %s spamins points" % (client.name, points))

    def test_say(self):
        when(self.p).getTime().thenReturn(0).thenReturn(1).thenReturn(20).thenReturn(120)

        self.assertSpaminsPoints(self.joe, 0)

        self.joe.says("doh")  # 0s
        self.assertSpaminsPoints(self.joe, 2)

        self.joe.says("foo")  # 1s
        self.assertSpaminsPoints(self.joe, 4)

        self.joe.says("bar")  # 20s
        self.assertSpaminsPoints(self.joe, 3)

        self.joe.says("hi")  # 120s
        self.assertSpaminsPoints(self.joe, 0)
Esempio n. 27
0
class FlagstatsPluginTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        with logging_disabled():
            from b3.parsers.iourt43 import Iourt43Parser
            from tests.fake import FakeConsole
            Iourt43Parser.__bases__ = (FakeConsole, )
            # Now parser inheritance hierarchy is :
            # Iourt43Parser -> abstractParser -> FakeConsole -> Parser

    def setUp(self):
        # create a Iourt43 parser
        parser_conf = XmlConfigParser()
        parser_conf.loadFromString(
            dedent(r"""
            <configuration>
                <settings name="server">
                    <set name="game_log"></set>
                </settings>
            </configuration>
        """))

        self.parser_conf = MainConfig(parser_conf)
        self.console = Iourt43Parser(self.parser_conf)

        # initialize some fixed cvars which will be used by both the plugin and the iourt42 parser
        when(self.console).getCvar('auth').thenReturn(Cvar('auth', value='0'))
        when(self.console).getCvar('auth_owners').thenReturn(None)
        when(self.console).getCvar('fs_basepath').thenReturn(
            Cvar('fs_basepath', value='/fake/basepath'))
        when(self.console).getCvar('fs_homepath').thenReturn(
            Cvar('fs_homepath', value='/fake/homepath'))
        when(self.console).getCvar('fs_game').thenReturn(
            Cvar('fs_game', value='q3ut4'))
        when(self.console).getCvar('gamename').thenReturn(
            Cvar('gamename', value='q3urt43'))

        self.console.game.gameType = 'ctf'

        # start the parser
        self.console.startup()

        with logging_disabled():
            self.adminPlugin = AdminPlugin(self.console,
                                           '@b3/conf/plugin_admin.ini')
            self.adminPlugin.onLoadConfig()
            self.adminPlugin.onStartup()

        # make sure the admin plugin obtained by other plugins is our admin plugin
        when(self.console).getPlugin('admin').thenReturn(self.adminPlugin)

        self.conf = CfgConfigParser()
        self.conf.loadFromString(
            dedent(r"""
            [commands]
            flagstats: 1
            topstats: 40
            
            [settings]
        """))

        self.p = FlagstatsPlugin(self.console, self.conf)
        self.p.onLoadConfig()
        self.p.onStartup()

        with logging_disabled():
            from tests.fake import FakeClient

        # create some clients
        self.mike = FakeClient(console=self.console,
                               name="Mike",
                               guid="mikeguid",
                               team=TEAM_BLUE,
                               groupBits=1)
        self.mark = FakeClient(console=self.console,
                               name="Mark",
                               guid="markguid",
                               team=TEAM_BLUE,
                               groupBits=1)
        self.bill = FakeClient(console=self.console,
                               name="Bill",
                               guid="billguid",
                               team=TEAM_RED,
                               groupBits=1)
        self.mike.connects("1")
        self.bill.connects("2")
        self.mark.connects("3")

    def tearDown(self):
        self.console.shutdown()
        self.mike.disconnects()
        self.bill.disconnects()
        self.mark.disconnects()
Esempio n. 28
0
class Test_welcome(Welcome_functional_test):

    def setUp(self):
        Welcome_functional_test.setUp(self)
        self.load_config()
        # disabled event handling (spawns threads and is of no use for that test)
        self.p.onEvent = lambda *args, **kwargs: None

        self.client = FakeClient(console=self.console, name="Jack", guid="JackGUID")
        self.client._connections = 0
        self.client.greeting = 'hi everyone :)'
        self.client.connects("0")
        self.superadmin.connects("1")

        self.say_patcher = patch.object(self.console, "say")
        self.say_mock = self.say_patcher.start()

    def tearDown(self):
        Welcome_functional_test.tearDown(self)
        self.say_patcher.stop()

    def Test_get_client_info(self):
        self.parser_conf.add_section('b3')
        self.parser_conf.set('b3', 'time_zone', 'UTC')
        self.parser_conf.set('b3', 'time_format', '%I:%M%p %Z %m/%d/%y')
        self.assertDictEqual({'connections': '1',
                              'group': 'Super Admin',
                              'id': '2',
                              'lastVisit': 'Unknown',
                              'level': '100',
                              'name': u'SuperAdmin^7'}, self.p.get_client_info(self.superadmin))
        # WHEN
        self.superadmin.lastVisit = 1364821993
        self.superadmin._connections = 2
        # THEN
        self.assertDictEqual({'connections': '2',
                              'group': u'Super Admin',
                              'id': '2',
                              'lastVisit': '02:13PM CET 04/01/13',
                              'level': '100',
                              'name': 'SuperAdmin^7'}, self.p.get_client_info(self.superadmin))
        # WHEN
        self.superadmin.says("!mask mod")
        # THEN
        self.assertDictEqual({'connections': '2',
                              'group': u'Moderator',
                              'id': '2',
                              'lastVisit': '02:13PM CET 04/01/13',
                              'level': '20',
                              'name': 'SuperAdmin^7'}, self.p.get_client_info(self.superadmin))

    def test_0(self):
        # GIVEN
        self.p._welcomeFlags = 0
        # WHEN
        self.p.welcome(self.superadmin)
        # THEN
        self.assertListEqual([], self.say_mock.mock_calls)
        self.assertListEqual([], self.superadmin.message_history)

    def test_first(self):
        # GIVEN
        self.client._connections = 0
        self.p._welcomeFlags = F_FIRST
        # WHEN
        self.p.welcome(self.client)
        # THEN
        self.assertListEqual([], self.say_mock.mock_calls)
        self.assertListEqual(['Welcome Jack, this must be your first visit, you are player #1. Type !help for '
                              'help'], self.client.message_history)

    def test_newb(self):
        # GIVEN
        self.client._connections = 2
        self.p._welcomeFlags = F_NEWB
        # WHEN
        self.p.welcome(self.client)
        # THEN
        self.assertListEqual([], self.say_mock.mock_calls)
        self.assertListEqual(['[Authed] Welcome back Jack [@1], last visit Unknown. Type !register in chat to register.'
                              ' Type !help for help'], self.client.message_history)

    def test_user(self):
        # GIVEN
        self.client._connections = 2
        self.p._welcomeFlags = F_USER
        self.client.says("!register")
        self.client.clearMessageHistory()
        # WHEN
        self.p.welcome(self.client)
        # THEN
        self.assertListEqual([call(u'^7Jack^7 ^7put in group User')], self.say_mock.mock_calls)
        self.assertListEqual(["[Authed] Welcome back Jack [@1], last visit Unknown, you're a User, played 2 times"],
                             self.client.message_history)

    def test_announce_first(self):
        # GIVEN
        self.client._connections = 0
        self.p._welcomeFlags = F_ANNOUNCE_FIRST
        # WHEN
        self.p.welcome(self.client)
        # THEN
        self.assertListEqual([call('^7Everyone welcome Jack^7^7, player number ^3#1^7, to the server')],
                             self.say_mock.mock_calls)
        self.assertListEqual([], self.client.message_history)

    def test_announce_user(self):
        # GIVEN
        self.client._connections = 2
        self.p._welcomeFlags = F_ANNOUNCE_USER
        self.client.says("!register")
        self.client.clearMessageHistory()
        # WHEN
        self.p.welcome(self.client)
        # THEN
        self.assertListEqual([call(u'^7Jack^7 ^7put in group User'),
                              call('^7Everyone welcome back Jack^7^7, player number ^3#1^7, to the server, played 2 '
                                   'times')], self.say_mock.mock_calls)
        self.assertListEqual([], self.client.message_history)

    def test_custom_greeting(self):
        # GIVEN
        self.client._connections = 2
        self.p._welcomeFlags = F_CUSTOM_GREETING
        self.client.says("!register")
        self.client.clearMessageHistory()
        # WHEN
        self.p.welcome(self.client)
        # THEN
        self.assertListEqual([call(u'^7Jack^7 ^7put in group User'), call('^7Jack^7^7 joined: hi everyone :)')],
                             self.say_mock.mock_calls)
        self.assertListEqual([], self.client.message_history)