コード例 #1
0
ファイル: test_commands.py プロジェクト: urt30plus/b3
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)
コード例 #2
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)
コード例 #3
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)
コード例 #4
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)