class Test_validate_cmd_name(TestCase):

    def setUp(self):
        self.conf = CfgConfigParser()
        self.p = CustomcommandsPlugin(Mock(), self.conf)

    def test_None(self):
        self.assertRaises(AssertionError, self.p._validate_cmd_name, None)

    def test_blank(self):
        self.assertRaises(AssertionError, self.p._validate_cmd_name, "  ")

    def test_does_not_start_with_a_letter(self):
        self.assertRaises(ValueError, self.p._validate_cmd_name, "!f00")
        self.assertRaises(ValueError, self.p._validate_cmd_name, "1f00")
        self.assertRaises(ValueError, self.p._validate_cmd_name, "*f00")

    def test_too_short(self):
        self.assertRaises(ValueError, self.p._validate_cmd_name, "f")

    def test_have_blank(self):
        self.assertRaises(ValueError, self.p._validate_cmd_name, "ab cd")

    def test_nominal(self):
        try:
            self.p._validate_cmd_name("cookie")
        except (AssertionError, ValueError), err:
            self.fail("expecting no error, got %r" % err)
Example #2
0
class Test_validate_cmd_name(TestCase):
    def setUp(self):
        self.conf = CfgConfigParser()
        self.p = CustomcommandsPlugin(Mock(), self.conf)

    def test_None(self):
        self.assertRaises(AssertionError, self.p._validate_cmd_name, None)

    def test_blank(self):
        self.assertRaises(AssertionError, self.p._validate_cmd_name, "  ")

    def test_does_not_start_with_a_letter(self):
        self.assertRaises(ValueError, self.p._validate_cmd_name, "!f00")
        self.assertRaises(ValueError, self.p._validate_cmd_name, "1f00")
        self.assertRaises(ValueError, self.p._validate_cmd_name, "*f00")

    def test_too_short(self):
        self.assertRaises(ValueError, self.p._validate_cmd_name, "f")

    def test_have_blank(self):
        self.assertRaises(ValueError, self.p._validate_cmd_name, "ab cd")

    def test_nominal(self):
        try:
            self.p._validate_cmd_name("cookie")
        except (AssertionError, ValueError), err:
            self.fail("expecting no error, got %r" % err)
class Test_validate_cmd_template(TestCase):
    def setUp(self):
        self.conf = CfgConfigParser()
        self.p = CustomcommandsPlugin(Mock(), self.conf)

    def test_nominal(self):
        try:
            self.p._validate_cmd_template("cookie")
        except (AssertionError, ValueError), err:
            self.fail("expecting no error, got %r" % err)
class Test_validate_cmd_template(TestCase):

    def setUp(self):
        self.conf = CfgConfigParser()
        self.p = CustomcommandsPlugin(Mock(), self.conf)

    def test_nominal(self):
        try:
            self.p._validate_cmd_template("cookie")
        except (AssertionError, ValueError), err:
            self.fail("expecting no error, got %r" % err)
Example #5
0
 def setUp(self):
     CustomcommandsTestCase.setUp(self)
     self.conf = CfgConfigParser()
     self.p = CustomcommandsPlugin(self.console, self.conf)
     self.p.onLoadConfig()
     self.p.onStartup()
     self.player1 = FakeClient(console=self.console,
                               name="Player1",
                               guid="player1GUID",
                               pbid="player1PBID")
     self.player2 = FakeClient(console=self.console,
                               name="Player2",
                               guid="player2GUID",
                               pbid="player2PBID")
     self.player1.connects(cid="slot1")
     self.player2.connects(cid="slot2")
class Test_commands(CustomcommandsTestCase):
    def setUp(self):
        CustomcommandsTestCase.setUp(self)
        self.conf = CfgConfigParser()
        self.p = CustomcommandsPlugin(self.console, self.conf)
        with logging_disabled():
            from b3.fake import FakeClient
        self.guest = FakeClient(console=self.console, name="Guest", guid="GuestGUID", pbid="GuestPBID", group_bits=0)
        self.user = FakeClient(console=self.console, name="User", guid="UserGUID", pbid="UserPBID", group_bits=1)
        self.regular = FakeClient(console=self.console, name="Regular", guid="RegularGUID", pbid="RegularPBID", group_bits=2)
        self.mod = FakeClient(console=self.console, name="Moderator", guid="ModeratorGUID", pbid="ModeratorPBID", group_bits=8)
        self.admin = FakeClient(console=self.console, name="Admin", guid="AdminGUID", pbid="AdminPBID", group_bits=16)
        self.fulladmin = FakeClient(console=self.console, name="FullAdmin", guid="FullAdminGUID", pbid="FullAdminPBID", group_bits=32)
        self.senioradmin = FakeClient(console=self.console, name="SeniorAdmin", guid="SeniorAdminGUID", pbid="SeniorAdminPBID", group_bits=64)
        self.superadmin = FakeClient(console=self.console, name="SuperAdmin", guid="SuperAdminGUID", pbid="SuperAdminPBID", group_bits=128)

    def test_guest(self):
        # GIVEN
        self.conf.loadFromString("""
[guest commands]
# define in this section commands that will be available to all players
f00 = f00 rcon command
        """)
        # WHEN
        self.p.onLoadConfig()
        self.p.onStartup()
        # THEN
        self.assertIn("f00", self.p._adminPlugin._commands)
        # WHEN
        self.guest.connects(cid="guestCID")
        with patch.object(self.console, "write") as write_mock:
            self.guest.says("!f00")
        # THEN
        self.assertListEqual([call("f00 rcon command")], write_mock.mock_calls)
        self.assertListEqual([], self.guest.message_history)

    def test_user(self):
        # GIVEN
        self.console.getPlugin('admin')._warn_command_abusers = True
        self.conf.loadFromString("""
[user commands]
# define in this section commands that will be available to all players
f00 = f00 rcon command
        """)
        self.p.onLoadConfig()
        self.p.onStartup()
        # WHEN
        self.guest.connects(cid="guestCID")
        with patch.object(self.console, "write") as write_mock:
            self.guest.says("!f00")
        # THEN
        self.assertEqual(1, len(self.guest.message_history))
        self.assertIn(self.guest.message_history[0], [
            'You do not have sufficient access to use !f00',
            'You need to be in group User to use !f00'
        ])
        self.assertListEqual([], write_mock.mock_calls)
 def setUp(self):
     CustomcommandsTestCase.setUp(self)
     self.conf = CfgConfigParser()
     self.p = CustomcommandsPlugin(self.console, self.conf)
     self.p.onLoadConfig()
     self.p.onStartup()
     self.player1 = FakeClient(console=self.console, name="Player1", guid="player1GUID", pbid="player1PBID")
     self.player2 = FakeClient(console=self.console, name="Player2", guid="player2GUID", pbid="player2PBID")
     self.player1.connects(cid="slot1")
     self.player2.connects(cid="slot2")
 def setUp(self):
     with logging_disabled():
         CustomcommandsTestCase.setUp(self)
         self.conf = CfgConfigParser()
         self.p = CustomcommandsPlugin(self.console, self.conf)
         self.guest = FakeClient(console=self.console, name="Guest", guid="GuestGUID", pbid="GuestPBID", group_bits=0)
         self.player1 = FakeClient(console=self.console, name="player1", guid="player1GUID", pbid="player1PBID", group_bits=1)
         self.player1.connects(cid="CID1")
         self.player2 = FakeClient(console=self.console, name="player2", guid="player2GUID", pbid="player2PBID", group_bits=1)
         self.player2.connects(cid="CID2")
         self.guest.connects(cid="guestCID")
 def setUp(self):
     CustomcommandsTestCase.setUp(self)
     self.conf = CfgConfigParser()
     self.p = CustomcommandsPlugin(self.console, self.conf)
     with logging_disabled():
         from b3.fake import FakeClient
     self.guest = FakeClient(console=self.console, name="Guest", guid="GuestGUID", pbid="GuestPBID", group_bits=0)
     self.user = FakeClient(console=self.console, name="User", guid="UserGUID", pbid="UserPBID", group_bits=1)
     self.regular = FakeClient(console=self.console, name="Regular", guid="RegularGUID", pbid="RegularPBID", group_bits=2)
     self.mod = FakeClient(console=self.console, name="Moderator", guid="ModeratorGUID", pbid="ModeratorPBID", group_bits=8)
     self.admin = FakeClient(console=self.console, name="Admin", guid="AdminGUID", pbid="AdminPBID", group_bits=16)
     self.fulladmin = FakeClient(console=self.console, name="FullAdmin", guid="FullAdminGUID", pbid="FullAdminPBID", group_bits=32)
     self.senioradmin = FakeClient(console=self.console, name="SeniorAdmin", guid="SeniorAdminGUID", pbid="SeniorAdminPBID", group_bits=64)
     self.superadmin = FakeClient(console=self.console, name="SuperAdmin", guid="SuperAdminGUID", pbid="SuperAdminPBID", group_bits=128)
Example #10
0
 def setUp(self):
     with logging_disabled():
         CustomcommandsTestCase.setUp(self)
         self.conf = CfgConfigParser()
         self.p = CustomcommandsPlugin(self.console, self.conf)
         self.guest = FakeClient(console=self.console,
                                 name="Guest",
                                 guid="GuestGUID",
                                 pbid="GuestPBID",
                                 group_bits=0)
         self.player1 = FakeClient(console=self.console,
                                   name="player1",
                                   guid="player1GUID",
                                   pbid="player1PBID",
                                   group_bits=1)
         self.player1.connects(cid="CID1")
         self.player2 = FakeClient(console=self.console,
                                   name="player2",
                                   guid="player2GUID",
                                   pbid="player2PBID",
                                   group_bits=1)
         self.player2.connects(cid="CID2")
         self.guest.connects(cid="guestCID")
Example #11
0
 def setUp(self):
     CustomcommandsTestCase.setUp(self)
     self.conf = CfgConfigParser()
     self.p = CustomcommandsPlugin(self.console, self.conf)
     self.player1 = FakeClient(console=self.console,
                               name="Player1",
                               guid="player1GUID",
                               pbid="player1PBID")
     self.player2 = FakeClient(console=self.console,
                               name="Player2",
                               guid="player2GUID",
                               pbid="player2PBID")
     self.player1.connects(cid="slot1")
     self.player2.connects(cid="slot2")
     self.write_patcher = patch.object(self.console,
                                       "write",
                                       wrap=self.console.write)
     self.write_mock = self.write_patcher.start()
Example #12
0
class Test_render_cmd_template(CustomcommandsTestCase):
    def setUp(self):
        CustomcommandsTestCase.setUp(self)
        self.conf = CfgConfigParser()
        self.p = CustomcommandsPlugin(self.console, self.conf)
        self.p.onLoadConfig()
        self.p.onStartup()
        self.player1 = FakeClient(console=self.console,
                                  name="Player1",
                                  guid="player1GUID",
                                  pbid="player1PBID")
        self.player2 = FakeClient(console=self.console,
                                  name="Player2",
                                  guid="player2GUID",
                                  pbid="player2PBID")
        self.player1.connects(cid="slot1")
        self.player2.connects(cid="slot2")

    def test_no_placeholder(self):
        self.assertEqual(
            "f00",
            self.p._render_cmd_template("f00", data="", client=self.player1))

    def test_ARG(self):
        self.assertEqual(
            "f00 bill bar",
            self.p._render_cmd_template("f00 <ARG> bar",
                                        data="bill",
                                        client=self.player1))
        self.assertRaises(ValueError,
                          self.p._render_cmd_template,
                          "f00 <ARG> bar",
                          data="",
                          client=self.player1)

    def test_ARG_OPT(self):
        self.assertEqual(
            "hi",
            self.p._render_cmd_template("hi <ARG:OPT:>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "hi f00",
            self.p._render_cmd_template("hi <ARG:OPT:f00>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "hi bar",
            self.p._render_cmd_template("hi <ARG:OPT:f00>",
                                        data="bar",
                                        client=self.player1))
        self.assertEqual(
            "hi foo bar",
            self.p._render_cmd_template("hi <ARG:OPT:f00>",
                                        data="foo bar",
                                        client=self.player1))

    def test_ARG_FIND_MAP_errors(self):
        # GIVEN
        when(self.p.console).getMaps().thenReturn(
            ["map1", "map2", "map3", "ut4_turnpike", "ut4_casa"])
        # THEN
        self.assertRaises(ValueError,
                          self.p._render_cmd_template,
                          "map <ARG:FIND_MAP>",
                          data="",
                          client=self.player1)
        self.assertRaisesRegexp(ValueError,
                                "do you mean : map2, map3, map1 ?",
                                self.p._render_cmd_template,
                                "map <ARG:FIND_MAP>",
                                data="map",
                                client=self.player1)

    def test_ARG_FIND_MAP_nominal(self):
        def assertFoundMap(expected_map, cmd_parameter):
            self.assertEqual(
                "map " + expected_map,
                self.p._render_cmd_template("map <ARG:FIND_MAP>",
                                            data=cmd_parameter,
                                            client=self.player1))

        # GIVEN
        when(self.p.console).getMaps().thenReturn(
            ["map1", "map2", "map3", "ut4_turnpike", "ut4_casa"])
        # THEN
        assertFoundMap("map1", "map1")
        assertFoundMap("map1", " map1 ")
        assertFoundMap("ut4_casa", "ut4_casa")
        assertFoundMap("ut4_casa", "casa")

    def test_ARG_FIND_PLAYER_nominal(self):
        # GIVEN
        when(self.p._adminPlugin).findClientPrompt(
            "f00", self.player1).thenReturn(self.player2)
        # THEN
        self.assertEqual(
            "kick slot2",
            self.p._render_cmd_template("kick <ARG:FIND_PLAYER:PID>",
                                        data="f00",
                                        client=self.player1))
        self.assertEqual(
            "kick player2PBID",
            self.p._render_cmd_template("kick <ARG:FIND_PLAYER:PBID>",
                                        data="f00",
                                        client=self.player1))
        self.assertEqual(
            "kick player2GUID",
            self.p._render_cmd_template("kick <ARG:FIND_PLAYER:GUID>",
                                        data="f00",
                                        client=self.player1))
        self.assertEqual(
            "kick Player2",
            self.p._render_cmd_template("kick <ARG:FIND_PLAYER:NAME>",
                                        data="f00",
                                        client=self.player1))
        self.assertEqual(
            "kick Player2^7",
            self.p._render_cmd_template("kick <ARG:FIND_PLAYER:EXACTNAME>",
                                        data="f00",
                                        client=self.player1))
        self.assertEqual(
            "kick @%s" % self.player2.id,
            self.p._render_cmd_template("kick <ARG:FIND_PLAYER:B3ID>",
                                        data="f00",
                                        client=self.player1))

    def test_PLAYER(self):
        self.assertEqual(
            "f00 slot1",
            self.p._render_cmd_template("f00 <PLAYER:PID>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "f00 player1PBID",
            self.p._render_cmd_template("f00 <PLAYER:PBID>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "f00 player1GUID",
            self.p._render_cmd_template("f00 <PLAYER:GUID>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "f00 Player1",
            self.p._render_cmd_template("f00 <PLAYER:NAME>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "f00 Player1^7",
            self.p._render_cmd_template("f00 <PLAYER:EXACTNAME>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "f00 @1",
            self.p._render_cmd_template("f00 <PLAYER:B3ID>",
                                        data="",
                                        client=self.player1))

    def test_LAST_KILLER(self):
        self.assertRaisesRegexp(ValueError,
                                "Your last killer is unknown",
                                self.p._render_cmd_template,
                                "f00 <LAST_KILLER:PID>",
                                data="",
                                client=self.player1)
        self.assertRaisesRegexp(ValueError,
                                "Your last killer is unknown",
                                self.p._render_cmd_template,
                                "f00 <LAST_KILLER:PBID>",
                                data="",
                                client=self.player1)
        self.assertRaisesRegexp(ValueError,
                                "Your last killer is unknown",
                                self.p._render_cmd_template,
                                "f00 <LAST_KILLER:GUID>",
                                data="",
                                client=self.player1)
        self.assertRaisesRegexp(ValueError,
                                "Your last killer is unknown",
                                self.p._render_cmd_template,
                                "f00 <LAST_KILLER:NAME>",
                                data="",
                                client=self.player1)
        self.assertRaisesRegexp(ValueError,
                                "Your last killer is unknown",
                                self.p._render_cmd_template,
                                "f00 <LAST_KILLER:EXACTNAME>",
                                data="",
                                client=self.player1)
        self.assertRaisesRegexp(ValueError,
                                "Your last killer is unknown",
                                self.p._render_cmd_template,
                                "f00 <LAST_KILLER:B3ID>",
                                data="",
                                client=self.player1)
        # WHEN
        self.player2.kills(self.player1)
        # THEN
        self.assertEqual(
            "f00 slot2",
            self.p._render_cmd_template("f00 <LAST_KILLER:PID>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "f00 player2PBID",
            self.p._render_cmd_template("f00 <LAST_KILLER:PBID>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "f00 player2GUID",
            self.p._render_cmd_template("f00 <LAST_KILLER:GUID>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "f00 Player2",
            self.p._render_cmd_template("f00 <LAST_KILLER:NAME>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "f00 Player2^7",
            self.p._render_cmd_template("f00 <LAST_KILLER:EXACTNAME>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "f00 @2",
            self.p._render_cmd_template("f00 <LAST_KILLER:B3ID>",
                                        data="",
                                        client=self.player1))

    def test_LAST_VICTIM(self):
        self.assertRaisesRegexp(ValueError,
                                "Your last victim is unknown",
                                self.p._render_cmd_template,
                                "f00 <LAST_VICTIM:PID>",
                                data="",
                                client=self.player1)
        self.assertRaisesRegexp(ValueError,
                                "Your last victim is unknown",
                                self.p._render_cmd_template,
                                "f00 <LAST_VICTIM:PBID>",
                                data="",
                                client=self.player1)
        self.assertRaisesRegexp(ValueError,
                                "Your last victim is unknown",
                                self.p._render_cmd_template,
                                "f00 <LAST_VICTIM:GUID>",
                                data="",
                                client=self.player1)
        self.assertRaisesRegexp(ValueError,
                                "Your last victim is unknown",
                                self.p._render_cmd_template,
                                "f00 <LAST_VICTIM:NAME>",
                                data="",
                                client=self.player1)
        self.assertRaisesRegexp(ValueError,
                                "Your last victim is unknown",
                                self.p._render_cmd_template,
                                "f00 <LAST_VICTIM:EXACTNAME>",
                                data="",
                                client=self.player1)
        self.assertRaisesRegexp(ValueError,
                                "Your last victim is unknown",
                                self.p._render_cmd_template,
                                "f00 <LAST_VICTIM:B3ID>",
                                data="",
                                client=self.player1)
        # WHEN
        self.player1.kills(self.player2)
        # THEN
        self.assertEqual(
            "f00 slot2",
            self.p._render_cmd_template("f00 <LAST_VICTIM:PID>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "f00 player2PBID",
            self.p._render_cmd_template("f00 <LAST_VICTIM:PBID>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "f00 player2GUID",
            self.p._render_cmd_template("f00 <LAST_VICTIM:GUID>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "f00 Player2",
            self.p._render_cmd_template("f00 <LAST_VICTIM:NAME>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "f00 Player2^7",
            self.p._render_cmd_template("f00 <LAST_VICTIM:EXACTNAME>",
                                        data="",
                                        client=self.player1))
        self.assertEqual(
            "f00 @2",
            self.p._render_cmd_template("f00 <LAST_VICTIM:B3ID>",
                                        data="",
                                        client=self.player1))

    def test_ADMINGROUP_SHORT(self):
        groups = {
            0: 'guest',
            1: 'user',
            2: 'reg',
            8: 'mod',
            16: 'admin',
            32: 'fulladmin',
            64: 'senioradmin',
            128: 'superadmin'
        }
        for groupBits, group_keyword in groups.items():
            # WHEN
            self.player1.groupBits = groupBits
            # THEN
            self.assertEqual(
                "f00 %s" % group_keyword,
                self.p._render_cmd_template("f00 <PLAYER:ADMINGROUP_SHORT>",
                                            data="",
                                            client=self.player1),
                "failed with group %s" % group_keyword)

    def test_ADMINGROUP_LONG(self):
        groups = {
            0: 'Guest',
            1: 'User',
            2: 'Regular',
            8: 'Moderator',
            16: 'Admin',
            32: 'Full Admin',
            64: 'Senior Admin',
            128: 'Super Admin'
        }
        for groupBits, group_name in groups.items():
            # WHEN
            self.player1.groupBits = groupBits
            # THEN
            self.assertEqual(
                "f00 %s" % group_name,
                self.p._render_cmd_template("f00 <PLAYER:ADMINGROUP_LONG>",
                                            data="",
                                            client=self.player1),
                "failed with group %s" % group_name)

    def test_ADMINGROUP_LEVEL(self):
        groups = {0: 0, 1: 1, 2: 2, 8: 20, 16: 40, 32: 60, 64: 80, 128: 100}
        for groupBits, group_level in groups.items():
            # WHEN
            self.player1.groupBits = groupBits
            # THEN
            self.assertEqual(
                "f00 %s" % group_level,
                self.p._render_cmd_template("f00 <PLAYER:ADMINGROUP_LEVEL>",
                                            data="",
                                            client=self.player1),
                "failed with group %s" % group_level)
Example #13
0
class Test_find_player(CustomcommandsTestCase):
    def setUp(self):
        with logging_disabled():
            CustomcommandsTestCase.setUp(self)
            self.conf = CfgConfigParser()
            self.p = CustomcommandsPlugin(self.console, self.conf)
            self.guest = FakeClient(console=self.console,
                                    name="Guest",
                                    guid="GuestGUID",
                                    pbid="GuestPBID",
                                    group_bits=0)
            self.player1 = FakeClient(console=self.console,
                                      name="player1",
                                      guid="player1GUID",
                                      pbid="player1PBID",
                                      group_bits=1)
            self.player1.connects(cid="CID1")
            self.player2 = FakeClient(console=self.console,
                                      name="player2",
                                      guid="player2GUID",
                                      pbid="player2PBID",
                                      group_bits=1)
            self.player2.connects(cid="CID2")
            self.guest.connects(cid="guestCID")

    def init(self, conf_content):
        with logging_disabled():
            self.conf.loadFromString(conf_content)
            self.p.onLoadConfig()
            self.p.onStartup()

    def test_ARG_FIND_PLAYER_NAME_no_parameter(self):
        # GIVEN
        self.init("""
[guest commands]
f00 = f00 #<ARG:FIND_PLAYER:NAME>#
        """)

        # WHEN
        with patch.object(self.console, "write") as write_mock:
            self.guest.says("!f00")

        # THEN
        self.assertListEqual(['Error: missing parameter'],
                             self.guest.message_history)
        self.assertListEqual([], write_mock.mock_calls)

    def test_ARG_FIND_PLAYER_NAME_no_match(self):
        # GIVEN
        self.init("""
[guest commands]
f00 = f00 #<ARG:FIND_PLAYER:NAME>#
        """)

        # WHEN
        with patch.object(self.console, "write") as write_mock:
            self.guest.says("!f00 bar")

        # THEN
        self.assertListEqual(['No players found matching bar'],
                             self.guest.message_history)
        self.assertListEqual([], write_mock.mock_calls)

    def test_ARG_FIND_PLAYER_NAME_with_multiple_matches(self):
        # GIVEN
        self.init("""
[guest commands]
f00 = f00 #<ARG:FIND_PLAYER:NAME>#
        """)

        # WHEN
        with patch.object(self.console, "write") as write_mock:
            self.guest.says("!f00 player")

        # THEN
        self.assertListEqual(
            ['Players matching player player1 [CID1], player2 [CID2]'],
            self.guest.message_history)
        self.assertListEqual([], write_mock.mock_calls)
class Test_find_player(CustomcommandsTestCase):
    def setUp(self):
        with logging_disabled():
            CustomcommandsTestCase.setUp(self)
            self.conf = CfgConfigParser()
            self.p = CustomcommandsPlugin(self.console, self.conf)
            self.guest = FakeClient(console=self.console, name="Guest", guid="GuestGUID", pbid="GuestPBID", group_bits=0)
            self.player1 = FakeClient(console=self.console, name="player1", guid="player1GUID", pbid="player1PBID", group_bits=1)
            self.player1.connects(cid="CID1")
            self.player2 = FakeClient(console=self.console, name="player2", guid="player2GUID", pbid="player2PBID", group_bits=1)
            self.player2.connects(cid="CID2")
            self.guest.connects(cid="guestCID")
            
    def init(self, conf_content):
        with logging_disabled():
            self.conf.loadFromString(conf_content)
            self.p.onLoadConfig()
            self.p.onStartup()

    def test_ARG_FIND_PLAYER_NAME_no_parameter(self):
        # GIVEN
        self.init("""
[guest commands]
f00 = f00 #<ARG:FIND_PLAYER:NAME>#
        """)
        
        # WHEN
        with patch.object(self.console, "write") as write_mock:
            self.guest.says("!f00")
        
        # THEN
        self.assertListEqual(['Error: missing parameter'], self.guest.message_history)
        self.assertListEqual([], write_mock.mock_calls)

    def test_ARG_FIND_PLAYER_NAME_no_match(self):
        # GIVEN
        self.init("""
[guest commands]
f00 = f00 #<ARG:FIND_PLAYER:NAME>#
        """)
        
        # WHEN
        with patch.object(self.console, "write") as write_mock:
            self.guest.says("!f00 bar")
        
        # THEN
        self.assertListEqual(['No players found matching bar'], self.guest.message_history)
        self.assertListEqual([], write_mock.mock_calls)

    def test_ARG_FIND_PLAYER_NAME_with_multiple_matches(self):
        # GIVEN
        self.init("""
[guest commands]
f00 = f00 #<ARG:FIND_PLAYER:NAME>#
        """)
        
        # WHEN
        with patch.object(self.console, "write") as write_mock:
            self.guest.says("!f00 player")
        
        # THEN
        self.assertListEqual(['Players matching player player1 [CID1], player2 [CID2]'], self.guest.message_history)
        self.assertListEqual([], write_mock.mock_calls)
 def setUp(self):
     self.conf = CfgConfigParser()
     self.p = CustomcommandsPlugin(Mock(), self.conf)
 def setUp(self):
     self.conf = CfgConfigParser()
     self.p = CustomcommandsPlugin(Mock(), self.conf)
class Test_render_cmd_template(CustomcommandsTestCase):

    def setUp(self):
        CustomcommandsTestCase.setUp(self)
        self.conf = CfgConfigParser()
        self.p = CustomcommandsPlugin(self.console, self.conf)
        self.p.onLoadConfig()
        self.p.onStartup()
        self.player1 = FakeClient(console=self.console, name="Player1", guid="player1GUID", pbid="player1PBID")
        self.player2 = FakeClient(console=self.console, name="Player2", guid="player2GUID", pbid="player2PBID")
        self.player1.connects(cid="slot1")
        self.player2.connects(cid="slot2")

    def test_no_placeholder(self):
        self.assertEqual("f00", self.p._render_cmd_template("f00", data="", client=self.player1))

    def test_ARG(self):
        self.assertEqual("f00 bill bar", self.p._render_cmd_template("f00 <ARG> bar", data="bill", client=self.player1))
        self.assertRaises(ValueError, self.p._render_cmd_template, "f00 <ARG> bar", data="", client=self.player1)

    def test_ARG_OPT(self):
        self.assertEqual("hi", self.p._render_cmd_template("hi <ARG:OPT:>", data="", client=self.player1))
        self.assertEqual("hi f00", self.p._render_cmd_template("hi <ARG:OPT:f00>", data="", client=self.player1))
        self.assertEqual("hi bar", self.p._render_cmd_template("hi <ARG:OPT:f00>", data="bar", client=self.player1))
        self.assertEqual("hi foo bar", self.p._render_cmd_template("hi <ARG:OPT:f00>", data="foo bar", client=self.player1))

    def test_ARG_FIND_MAP_errors(self):
        # GIVEN
        when(self.p.console).getMaps().thenReturn(["map1", "map2", "map3", "ut4_turnpike", "ut4_casa"])
        # THEN
        self.assertRaises(ValueError, self.p._render_cmd_template, "map <ARG:FIND_MAP>", data="", client=self.player1)
        self.assertRaisesRegexp(ValueError, "do you mean : map2, map3, map1 ?",
                                self.p._render_cmd_template, "map <ARG:FIND_MAP>", data="map", client=self.player1)

    def test_ARG_FIND_MAP_nominal(self):
        def assertFoundMap(expected_map, cmd_parameter):
            self.assertEqual("map " + expected_map, self.p._render_cmd_template("map <ARG:FIND_MAP>",
                                                                                data=cmd_parameter, client=self.player1))
        # GIVEN
        when(self.p.console).getMaps().thenReturn(["map1", "map2", "map3", "ut4_turnpike", "ut4_casa"])
        # THEN
        assertFoundMap("map1", "map1")
        assertFoundMap("map1", " map1 ")
        assertFoundMap("ut4_casa", "ut4_casa")
        assertFoundMap("ut4_casa", "casa")

    def test_ARG_FIND_PLAYER_nominal(self):
        # GIVEN
        when(self.p._adminPlugin).findClientPrompt("f00", self.player1).thenReturn(self.player2)
        # THEN
        self.assertEqual("kick slot2", self.p._render_cmd_template("kick <ARG:FIND_PLAYER:PID>", data="f00", client=self.player1))
        self.assertEqual("kick player2PBID", self.p._render_cmd_template("kick <ARG:FIND_PLAYER:PBID>", data="f00", client=self.player1))
        self.assertEqual("kick player2GUID", self.p._render_cmd_template("kick <ARG:FIND_PLAYER:GUID>", data="f00", client=self.player1))
        self.assertEqual("kick Player2", self.p._render_cmd_template("kick <ARG:FIND_PLAYER:NAME>", data="f00", client=self.player1))
        self.assertEqual("kick Player2^7", self.p._render_cmd_template("kick <ARG:FIND_PLAYER:EXACTNAME>", data="f00", client=self.player1))
        self.assertEqual("kick @%s" % self.player2.id, self.p._render_cmd_template("kick <ARG:FIND_PLAYER:B3ID>", data="f00", client=self.player1))

    def test_PLAYER(self):
        self.assertEqual("f00 slot1", self.p._render_cmd_template("f00 <PLAYER:PID>", data="", client=self.player1))
        self.assertEqual("f00 player1PBID", self.p._render_cmd_template("f00 <PLAYER:PBID>", data="", client=self.player1))
        self.assertEqual("f00 player1GUID", self.p._render_cmd_template("f00 <PLAYER:GUID>", data="", client=self.player1))
        self.assertEqual("f00 Player1", self.p._render_cmd_template("f00 <PLAYER:NAME>", data="", client=self.player1))
        self.assertEqual("f00 Player1^7", self.p._render_cmd_template("f00 <PLAYER:EXACTNAME>", data="", client=self.player1))
        self.assertEqual("f00 @1", self.p._render_cmd_template("f00 <PLAYER:B3ID>", data="", client=self.player1))

    def test_LAST_KILLER(self):
        self.assertRaisesRegexp(ValueError, "Your last killer is unknown", self.p._render_cmd_template, "f00 <LAST_KILLER:PID>", data="", client=self.player1)
        self.assertRaisesRegexp(ValueError, "Your last killer is unknown", self.p._render_cmd_template, "f00 <LAST_KILLER:PBID>", data="", client=self.player1)
        self.assertRaisesRegexp(ValueError, "Your last killer is unknown", self.p._render_cmd_template, "f00 <LAST_KILLER:GUID>", data="", client=self.player1)
        self.assertRaisesRegexp(ValueError, "Your last killer is unknown", self.p._render_cmd_template, "f00 <LAST_KILLER:NAME>", data="", client=self.player1)
        self.assertRaisesRegexp(ValueError, "Your last killer is unknown", self.p._render_cmd_template, "f00 <LAST_KILLER:EXACTNAME>", data="", client=self.player1)
        self.assertRaisesRegexp(ValueError, "Your last killer is unknown", self.p._render_cmd_template, "f00 <LAST_KILLER:B3ID>", data="", client=self.player1)
        # WHEN
        self.player2.kills(self.player1)
        # THEN
        self.assertEqual("f00 slot2", self.p._render_cmd_template("f00 <LAST_KILLER:PID>", data="", client=self.player1))
        self.assertEqual("f00 player2PBID", self.p._render_cmd_template("f00 <LAST_KILLER:PBID>", data="", client=self.player1))
        self.assertEqual("f00 player2GUID", self.p._render_cmd_template("f00 <LAST_KILLER:GUID>", data="", client=self.player1))
        self.assertEqual("f00 Player2", self.p._render_cmd_template("f00 <LAST_KILLER:NAME>", data="", client=self.player1))
        self.assertEqual("f00 Player2^7", self.p._render_cmd_template("f00 <LAST_KILLER:EXACTNAME>", data="", client=self.player1))
        self.assertEqual("f00 @2", self.p._render_cmd_template("f00 <LAST_KILLER:B3ID>", data="", client=self.player1))

    def test_LAST_VICTIM(self):
        self.assertRaisesRegexp(ValueError, "Your last victim is unknown", self.p._render_cmd_template, "f00 <LAST_VICTIM:PID>", data="", client=self.player1)
        self.assertRaisesRegexp(ValueError, "Your last victim is unknown", self.p._render_cmd_template, "f00 <LAST_VICTIM:PBID>", data="", client=self.player1)
        self.assertRaisesRegexp(ValueError, "Your last victim is unknown", self.p._render_cmd_template, "f00 <LAST_VICTIM:GUID>", data="", client=self.player1)
        self.assertRaisesRegexp(ValueError, "Your last victim is unknown", self.p._render_cmd_template, "f00 <LAST_VICTIM:NAME>", data="", client=self.player1)
        self.assertRaisesRegexp(ValueError, "Your last victim is unknown", self.p._render_cmd_template, "f00 <LAST_VICTIM:EXACTNAME>", data="", client=self.player1)
        self.assertRaisesRegexp(ValueError, "Your last victim is unknown", self.p._render_cmd_template, "f00 <LAST_VICTIM:B3ID>", data="", client=self.player1)
        # WHEN
        self.player1.kills(self.player2)
        # THEN
        self.assertEqual("f00 slot2", self.p._render_cmd_template("f00 <LAST_VICTIM:PID>", data="", client=self.player1))
        self.assertEqual("f00 player2PBID", self.p._render_cmd_template("f00 <LAST_VICTIM:PBID>", data="", client=self.player1))
        self.assertEqual("f00 player2GUID", self.p._render_cmd_template("f00 <LAST_VICTIM:GUID>", data="", client=self.player1))
        self.assertEqual("f00 Player2", self.p._render_cmd_template("f00 <LAST_VICTIM:NAME>", data="", client=self.player1))
        self.assertEqual("f00 Player2^7", self.p._render_cmd_template("f00 <LAST_VICTIM:EXACTNAME>", data="", client=self.player1))
        self.assertEqual("f00 @2", self.p._render_cmd_template("f00 <LAST_VICTIM:B3ID>", data="", client=self.player1))

    def test_ADMINGROUP_SHORT(self):
        groups = {0: 'guest', 1: 'user', 2: 'reg', 8: 'mod', 16: 'admin', 32: 'fulladmin', 64: 'senioradmin', 128: 'superadmin'}
        for groupBits, group_keyword in groups.items():
            # WHEN
            self.player1.groupBits = groupBits
            # THEN
            self.assertEqual("f00 %s" % group_keyword,
                             self.p._render_cmd_template("f00 <PLAYER:ADMINGROUP_SHORT>", data="", client=self.player1),
                             "failed with group %s" % group_keyword)

    def test_ADMINGROUP_LONG(self):
        groups = {0: 'Guest', 1: 'User', 2: 'Regular', 8: 'Moderator', 16: 'Admin', 32: 'Full Admin', 64: 'Senior Admin', 128: 'Super Admin'}
        for groupBits, group_name in groups.items():
            # WHEN
            self.player1.groupBits = groupBits
            # THEN
            self.assertEqual("f00 %s" % group_name,
                             self.p._render_cmd_template("f00 <PLAYER:ADMINGROUP_LONG>", data="", client=self.player1),
                             "failed with group %s" % group_name)

    def test_ADMINGROUP_LEVEL(self):
        groups = {0: 0, 1: 1, 2: 2, 8: 20, 16: 40, 32: 60, 64: 80, 128: 100}
        for groupBits, group_level in groups.items():
            # WHEN
            self.player1.groupBits = groupBits
            # THEN
            self.assertEqual("f00 %s" % group_level,
                             self.p._render_cmd_template("f00 <PLAYER:ADMINGROUP_LEVEL>", data="", client=self.player1),
                             "failed with group %s" % group_level)