Exemplo n.º 1
0
    def test_auth_client_without_guid_but_with_known_pbid(self):
        # GIVEN

        # known superadmin named Snoopy
        superadmin = Client(console=self.parser, name='Snoopy', guid='EA_AAAAAAAABBBBBBBBBBBBBB00000000000012222', pbid='300000aaaaaabbbbbbccccc111223300', group_bits=128, connections=21)
        superadmin.save()

        # bf3 server failing to provide guid
        def write(data):
            if data == ('admin.listPlayers', 'player', 'Snoopy'):
                return ['7', 'name', 'guid', 'teamId', 'squadId', 'kills', 'deaths', 'score', '1', 'Snoopy', '', '2', '8', '0', '0', '0']
            else:
                return DEFAULT
        self.write_mock.side_effect = write

        # WHEN
        self.assertFalse('Snoopy' in self.parser.clients)
        self.parser.OnPBPlayerGuid(match=re.match(self.regex_for_OnPBPlistItem, self.event_raw_data), data=self.event_raw_data)

        # THEN
        # B3 should have authed Snoopy
        self.assertTrue('Snoopy' in self.parser.clients)
        snoopy = self.parser.clients['Snoopy']
        self.assertTrue(snoopy.authed)
        for attb in ('name', 'pbid', 'guid', 'groupBits'):
            self.assertEqual(getattr(superadmin, attb), getattr(snoopy, attb))
    def test_getClientsMatching(self):
        self.storage.setClient(Client(ip="1.2.3.4", connections=2, guid="mlkjmlkjqsdf", pbid="123546abcdef", name="bill"))
        self.storage.setClient(Client(ip="4.2.3.5", connections=3, guid="azerazerazer", pbid="wxcvwxvcxwcv", name="john"))
        self.storage.setClient(Client(ip="4.2.3.5", connections=45, guid="rtyrtyrty", pbid="rtyrtyrrtyr", name="jack"))

        result = self.storage.getClientsMatching({'guid': "xxxxxxxxxx"})
        self.assertEqual(0, len(result))

        result = self.storage.getClientsMatching({'ip': "1.2.3.4"})
        self.assertEqual(1, len(result))
        self.assertEqual('bill', result[0].name)

        result = self.storage.getClientsMatching({'ip': "4.2.3.5"})
        self.assertEqual(2, len(result))

        result = self.storage.getClientsMatching({'pbid': "rtyrtyrrtyr"})
        self.assertEqual(1, len(result))
        self.assertEqual('jack', result[0].name)

        result = self.storage.getClientsMatching({'name': "jack"})
        self.assertEqual(1, len(result))
        self.assertEqual('jack', result[0].name)

        result = self.storage.getClientsMatching({'ip': "4.2.3.5", 'name': 'jack'})
        self.assertEqual(1, len(result))
        self.assertEqual('jack', result[0].name)

        result = self.storage.getClientsMatching({'ip': "4.2.3.5", 'connections >': 10})
        self.assertEqual(1, len(result))
        self.assertEqual('jack', result[0].name)
 def test_getClient_guid(self):
     c1 = Client(ip="1.2.3.4", connections=3, guid="mlkjmlkjqsdf", pbid="123546abcdef", name="some dude", greeting="hi!")
     c1_id = self.storage.setClient(c1)
     self.assertEqual(1, c1_id)
     c2 = self.storage.getClient(Client(guid="mlkjmlkjqsdf"))
     self.assertIsInstance(c2, Client)
     self.assertRaises(KeyError, self.storage.getClient, Client(guid='god'))
 def test_message_with_color_codes(self):
     self.parser.msgPrefix = "[Pre]"
     player = Client(console=self.parser, guid="theGuid")
     with patch.object(self.parser, 'writelines') as write_mock:
         player.message("^7message ^1with ^2color ^8codes")
         write_mock.assert_has_calls(
             [call(['m  [Pre] ^8[pm]^7 ^7message ^1with ^2color ^8codes'])])
Exemplo n.º 5
0
 def test_setClient(self):
     c1 = Client(ip="1.2.3.4", connections=2, guid="abcdefghijkl", pbid="123546abcdef", name="some dude",
                 greeting="hi!", mask_level=20, group_bits=8, login="******", password="******")
     c1_id = self.storage.setClient(c1)
     self.assertEqual(1, c1_id)
     c2 = self.storage.getClient(Client(id=c1_id))
     self.assertIsInstance(c2, Client)
     self.assertEqual("1.2.3.4", c2.ip)
     self.assertEqual(2, c2.connections)
     self.assertEqual("abcdefghijkl", c2.guid)
     self.assertEqual("123546abcdef", c2.pbid)
     self.assertEqual("some dude", c2.name)
     self.assertEqual("hi!", c2.greeting)
     self.assertEqual("test login", c2.login)
     self.assertEqual("test password", c2.password)
     c3 = Client(id=c1_id, ip="5.6.7.8", connections=1, guid="ddddddddd", pbid="zzzzzzzzzzz", name="milka",
                 greeting="hello")
     self.storage.setClient(c3)
     c4 = self.storage.getClient(Client(id=c1_id))
     self.assertIsInstance(c4, Client)
     self.assertEqual("5.6.7.8", c4.ip)
     self.assertEqual(1, c4.connections)
     self.assertEqual("ddddddddd", c4.guid)
     self.assertEqual("zzzzzzzzzzz", c4.pbid)
     self.assertEqual("milka", c4.name)
     self.assertEqual("hello", c4.greeting)
Exemplo n.º 6
0
    def test_auth_client_without_guid_but_with_known_pbid(self):
        # GIVEN

        # known superadmin named Snoopy
        superadmin = Client(console=self.parser, name='Snoopy', guid='EA_AAAAAAAABBBBBBBBBBBBBB00000000000012222', pbid='300000aaaaaabbbbbbccccc111223300', group_bits=128, connections=21)
        superadmin.save()

        # bf3 server failing to provide guid
        def write(data):
            if data == ('admin.listPlayers', 'player', 'Snoopy'):
                return ['7', 'name', 'guid', 'teamId', 'squadId', 'kills', 'deaths', 'score', '1', 'Snoopy', '', '2', '8', '0', '0', '0']
            else:
                return DEFAULT
        self.write_mock.side_effect = write

        # WHEN
        self.assertFalse('Snoopy' in self.parser.clients)
        self.parser.OnPBPlayerGuid(match=re.match(self.regex_for_OnPBPlistItem, self.event_raw_data), data=self.event_raw_data)

        # THEN
        # B3 should have authed Snoopy
        self.assertTrue('Snoopy' in self.parser.clients)
        snoopy = self.parser.clients['Snoopy']
        self.assertTrue(snoopy.authed)
        for attb in ('name', 'pbid', 'guid', 'groupBits'):
            self.assertEqual(getattr(superadmin, attb), getattr(snoopy, attb))
Exemplo n.º 7
0
 def test_known_client__guid(self):
     # GIVEN
     known_client = Client(console=self.parser,
                           guid="12312312312312312",
                           name="courgette",
                           connections=15)
     known_client.save()
     self.assertEqual(1, len(self.parser.clients))
     self.assertDictContainsSubset({'clients': 2},
                                   self.parser.storage.getCounts())
     # WHEN
     client = self.parser.getClientOrCreate(guid="12312312312312312",
                                            name="newName",
                                            team="0")
     # THEN
     self.assertIsInstance(client, Client)
     self.assertEqual(known_client.id, client.id)
     self.assertEqual("12312312312312312", client.cid)
     self.assertEqual("12312312312312312", client.guid)
     self.assertEqual("newName", client.name)
     self.assertEqual(TEAM_SCAVENGERS, client.team)
     self.assertEqual(16, client.connections)
     self.assertTrue(client.authed)
     self.assertEqual(2, len(self.parser.clients))
     self.assertDictContainsSubset({'clients': 2},
                                   self.parser.storage.getCounts())
Exemplo n.º 8
0
 def test_message_with_color_codes(self):
     self.parser.msgPrefix = "[Pre]"
     player = Client(console=self.parser, guid="theGuid")
     with patch.object(self.parser.output, 'write') as write_mock:
         player.message("^7message ^1with ^2color ^8codes")
         write_mock.assert_has_calls(
             [call('sm_psay #theGuid "[Pre] message with color codes"')])
Exemplo n.º 9
0
 def setUp(self):
     B3TestCase.setUp(self)
     self.queueEvent_patcher = patch.object(self.console, 'queueEvent')
     self.queueEvent_mock = self.queueEvent_patcher.start()
     
     self.admin = Client(console=self.console)
     self.client = Client(console=self.console)
     self.client.save()
Exemplo n.º 10
0
 def test_truncateTables_single_table(self):
     self.storage.setClient(Client(guid="aaaaaaaaa"))
     self.storage.setClient(Client(guid="bbbbbbbbb"))
     cursor = self.storage.query("SELECT * FROM clients")
     self.assertFalse(cursor.EOF)
     self.storage.truncateTable('clients')
     cursor = self.storage.query("SELECT * FROM clients")
     self.assertTrue(cursor.EOF)
Exemplo n.º 11
0
 def test_name_change(self):
     c = Client(console=b3.console, authed=True)
     c.name = "cucurb"
     self.assertEqual(c.name, "cucurb")
     b3.console.queueEvent.assert_called()
     args = b3.console.queueEvent.call_args
     eventraised = args[0][0]
     self.assertEquals(eventraised.type, b3.events.EVT_CLIENT_NAME_CHANGE)
     self.assertEquals(eventraised.data, 'cucurb')
Exemplo n.º 12
0
    def test_connected_client_with_verified_guid(self):
        # GIVEN
        self.parser.routeBattleyeEvent(
            u'Player #8 Max (111.222.200.50:2304) connected')
        self.parser.routeBattleyeEvent(
            u'Player #8 Max - GUID: 73c5e50a7860475f0000000000000000 (unverified)'
        )
        self.parser.routeBattleyeEvent(
            u'Verified GUID (73c5e50a7860475f0000000000000000) of player #8 Max'
        )
        self.assertDictContainsSubset({'clients': 2},
                                      self.parser.storage.getCounts())
        self.assertIn('8', self.parser.clients)
        # GIVEN that the player exists in database
        client_from_db = self.parser.storage.getClient(
            Client(guid="73c5e50a7860475f0000000000000000"))
        self.assertIsNotNone(client_from_db)
        self.assertEqual("Max", client_from_db.name)
        self.assertEqual("73c5e50a7860475f0000000000000000",
                         client_from_db.guid)
        self.assertEqual("111.222.200.50", client_from_db.ip)
        self.clear_events()
        # WHEN
        when(self.parser.output).write('players').thenReturn('''\
Players on server:
[#] [IP Address]:[Port] [Ping] [GUID] [Name]
--------------------------------------------------
8   111.222.200.50:2304   62   73c5e50a7860475f0000000000000000(OK) Max (Lobby)
(1 players in total)
''')
        rv = self.parser.sync()
        # THEN no new client is saved to database
        self.assertDictContainsSubset({'clients': 2},
                                      self.parser.storage.getCounts())
        # THEN sync return correct info
        self.assertIn('8', rv)
        client = rv["8"]
        self.assertEqual("Max", client.name)
        self.assertEqual("8", client.cid)
        self.assertEqual("111.222.200.50", client.ip)
        self.assertEqual('73c5e50a7860475f0000000000000000', client.guid)
        self.assertTrue(client.authed)
        # THEN connected client list is updated
        self.assertIn('8', self.parser.clients)
        client = self.parser.clients['8']
        self.assertEqual("Max", client.name)
        self.assertEqual("111.222.200.50", client.ip)
        self.assertEqual('73c5e50a7860475f0000000000000000', client.guid)
        self.assertTrue(client.authed)
        # THEN player info in database is correct
        client_from_db = self.parser.storage.getClient(
            Client(guid="73c5e50a7860475f0000000000000000"))
        self.assertIsNotNone(client_from_db)
        self.assertEqual("Max", client_from_db.name)
        self.assertEqual("73c5e50a7860475f0000000000000000",
                         client_from_db.guid)
        self.assertEqual("111.222.200.50", client_from_db.ip)
Exemplo n.º 13
0
 def test_team_change(self):
     b3.console = Mock()
     c = Client(console=b3.console)
     c.team = 24
     self.assertEqual(c.team, 24)
     b3.console.queueEvent.assert_called()
     args = b3.console.queueEvent.call_args
     eventraised = args[0][0]
     self.assertEquals(eventraised.type, b3.events.EVT_CLIENT_TEAM_CHANGE)
     self.assertEquals(eventraised.data, 24)
Exemplo n.º 14
0
 def test_one_ban_with_reason(self):
     # GIVEN
     player1 = Client(console=self.console, guid='BillGUID', name="Bill")
     player1.save()
     penalty1 = ClientBan(clientId=player1.id, timeExpire=-1, adminId=0, reason="test reason")
     when(self.console.storage).getLastPenalties(types=whatever(), num=whatever()).thenReturn([penalty1])
     # WHEN
     self.lastbans()
     # THEN
     self.mock_command.sayLoudOrPM.assert_called_once_with(self.player, u'^2@1^7 Bill^7^7 (Perm) test reason')
Exemplo n.º 15
0
 def setUp(self):
     B3TestCase.setUp(self)
     self.client = Client(console=self.console)
     self.group_guest = self.console.storage.getGroup(Group(keyword="guest"))
     self.group_user = self.console.storage.getGroup(Group(keyword="user"))
     self.group_reg = self.console.storage.getGroup(Group(keyword="reg"))
     self.group_mod = self.console.storage.getGroup(Group(keyword="mod"))
     self.group_admin = self.console.storage.getGroup(Group(keyword="admin"))
     self.group_fulladmin = self.console.storage.getGroup(Group(keyword="fulladmin"))
     self.group_senioradmin = self.console.storage.getGroup(Group(keyword="senioradmin"))
     self.group_superadmin = self.console.storage.getGroup(Group(keyword="superadmin"))
Exemplo n.º 16
0
 def test_makeAlias_new(self):
     c = Client(console=b3.console)
     c.id = 123
     c.name = "foo"
     b3.console.storage.getClientAlias.side_effect = KeyError()
     c.makeAlias("bar")
     self.assertEquals(b3.console.storage.getClientAlias.call_count, 1)
     alias = b3.console.storage.getClientAlias.call_args[0][0]
     self.assertIsInstance(alias, b3.clients.Alias)
     self.assertEqual(alias.alias, "bar")
     self.assertEqual(alias.numUsed, 1)
Exemplo n.º 17
0
 def test_one_ban(self):
     # GIVEN
     player1 = Client(console=self.console, guid='BillGUID', name="Bill")
     player1.save()
     penalty1 = ClientBan(clientId=player1.id, timeExpire=-1, adminId=0)
     when(self.console.storage).getLastPenalties(types=whatever(),
                                                 num=whatever()).thenReturn(
                                                     [penalty1])
     # WHEN
     self.lastbans()
     # THEN
     self.mock_command.sayLoudOrPM.assert_called_once_with(
         self.player, u'^2@1^7 Bill^7^7 (Perm)')
Exemplo n.º 18
0
    def test_two_bans_with_reason(self):
        # GIVEN
        when(self.console).time().thenReturn(0)
        player1 = Client(console=self.console, guid='player1GUID', name="P1")
        player1.save()
        penalty1 = ClientBan(clientId=player1.id,
                             timeExpire=-1,
                             adminId=0,
                             reason="test reason")

        player2 = Client(console=self.console, guid='player2GUID', name="P2")
        player2.save()
        penalty2 = ClientTempBan(clientId=player2.id,
                                 timeExpire=self.console.time() + 60 * 2,
                                 adminId=0,
                                 reason="test reason f00")

        when(self.console.storage).getLastPenalties(types=whatever(),
                                                    num=whatever()).thenReturn(
                                                        [penalty1, penalty2])
        # WHEN
        self.lastbans()
        # THEN
        self.mock_command.sayLoudOrPM.assert_has_calls([
            call(self.player, u'^2@1^7 P1^7^7 (Perm) test reason'),
            call(self.player,
                 u'^2@2^7 P2^7^7 (2 minutes remaining) test reason f00'),
        ])
Exemplo n.º 19
0
 def test_makeAlias_existing(self):
     c = Client(console=b3.console)
     c.id = 123
     c.name = "foo"
     aliasFoo = b3.clients.Alias()
     aliasFoo.alias = "foo"
     aliasFoo.clientId = c.id
     aliasFoo.numUsed = 48
     b3.console.storage.getClientAlias.side_effect = lambda x: aliasFoo
     c.makeAlias("whatever")
     self.assertEquals(b3.console.storage.getClientAlias.call_count, 1)
     self.assertIsInstance(aliasFoo, b3.clients.Alias)
     self.assertEqual(aliasFoo.alias, "foo")
     self.assertEqual(aliasFoo.numUsed, 49)
Exemplo n.º 20
0
 def test_truncateTables_multiple_tables(self):
     c1 = Client(guid="aaaaaaaaa")
     c2 = Client(guid="bbbbbbbbb")
     self.storage.setClient(c1)
     self.storage.setClient(c2)
     self.storage.setClientPenalty(Penalty(clientId=c1.id, adminId=0, type='Kick'))
     self.storage.setClientPenalty(Penalty(clientId=c2.id, adminId=0, type='Kick'))
     cursor = self.storage.query("SELECT * FROM clients")
     self.assertFalse(cursor.EOF)
     cursor = self.storage.query("SELECT * FROM penalties")
     self.assertFalse(cursor.EOF)
     self.storage.truncateTable(['clients', 'penalties'])
     cursor = self.storage.query("SELECT * FROM clients")
     self.assertTrue(cursor.EOF)
     cursor = self.storage.query("SELECT * FROM penalties")
     self.assertTrue(cursor.EOF)
Exemplo n.º 21
0
 def setUp(self):
     CommandTestCase.setUp(self)
     self.init()
     self.player = Client(console=self.console, name="joe", _maxLevel=0)
     self.player.message = Mock()
     self.assertEqual(0, self.player.maskedLevel)
     self.assertIsNone(self.player.maskedGroup)
Exemplo n.º 22
0
 def on_kicked(self, name, cid, guid, team, properties):
     # L 08/28/2012 - 00:12:07: [basecommands.smx] "Console<0><Console><Console>" kicked "courgette<91><STEAM_1:0:1111111><>" (reason "f00")
     client = self.storage.getClient(Client(guid=guid))
     if client:
         p = self.parseProperties(properties)
         return self.getEvent("EVT_CLIENT_KICK", p.get('reason', ''),
                              client)
Exemplo n.º 23
0
    def test_player_connecting_with_unverified_guid_at_first_and_sync(self):
        # GIVEN
        self.clear_events()
        # WHEN
        self.parser.routeBattleyeEvent(
            u'Player #8 Max (111.222.200.50:2304) connected')
        self.parser.routeBattleyeEvent(
            u'Player #8 Max - GUID: 73c5e50a7860475f0000000000000000 (unverified)'
        )
        when(self.parser.output).write('players').thenReturn(
            u'''Players on server:
[#] [IP Address]:[Port] [Ping] [GUID] [Name]
--------------------------------------------------
8   111.222.200.50:2304   -1   73c5e50a7860475f0000000000000000(?)  Max (Lobby)
(14 players in total)''')
        self.parser.sync()
        self.parser.routeBattleyeEvent(
            u'Verified GUID (73c5e50a7860475f0000000000000000) of player #8 Max'
        )
        # THEN check events were raised
        self.assert_has_event("EVT_CLIENT_CONNECT")
        self.assert_has_event("EVT_CLIENT_AUTH")
        # check player info in database
        client_from_db = self.parser.storage.getClient(
            Client(guid="73c5e50a7860475f0000000000000000"))
        self.assertIsNotNone(client_from_db)
        self.assertEqual("Max", client_from_db.name)
        self.assertEqual("73c5e50a7860475f0000000000000000",
                         client_from_db.guid)
        self.assertEqual("111.222.200.50", client_from_db.ip)
Exemplo n.º 24
0
 def test_player_connecting_with_unverified_guid_at_first(self):
     # GIVEN
     self.clear_events()
     # WHEN
     self.parser.routeBattleyeEvent(u'Player #8 Max (111.222.200.50:2304) connected')
     self.parser.routeBattleyeEvent(u'Player #8 Max - GUID: 73c5e50a7860475f0000000000000000 (unverified)')
     self.parser.routeBattleyeEvent(u'Verified GUID (73c5e50a7860475f0000000000000000) of player #8 Max')
     # THEN
     self.assertEqual(2, len(self.evt_queue))
     event1, event2 = self.evt_queue
     # check EVT_CLIENT_CONNECT
     self.assertEqual(self.parser.getEventID("EVT_CLIENT_CONNECT"), event1.type)
     self.assertEqual("Max", event1.client.name)
     self.assertEqual("8", event1.client.cid)
     self.assertEqual("73c5e50a7860475f0000000000000000", event1.client.guid)
     self.assertEqual("111.222.200.50", event1.client.ip)
     # check EVT_CLIENT_CONNECT
     self.assertEqual(self.parser.getEventID("EVT_CLIENT_AUTH"), event2.type)
     self.assertEqual("Max", event2.client.name)
     self.assertEqual("8", event2.client.cid)
     self.assertEqual("73c5e50a7860475f0000000000000000", event2.client.guid)
     self.assertEqual("111.222.200.50", event2.client.ip)
     # check player info in database
     client_from_db = self.parser.storage.getClient(Client(guid="73c5e50a7860475f0000000000000000"))
     self.assertIsNotNone(client_from_db)
     self.assertEqual("Max", client_from_db.name)
     self.assertEqual("73c5e50a7860475f0000000000000000", client_from_db.guid)
     self.assertEqual("111.222.200.50", client_from_db.ip)
Exemplo n.º 25
0
 def test_committed_suicide(self):
     # GIVEN
     p = Client(cid="12312312312312312", guid="12312312312312312", name="courgette")
     self.queueEvent_mock.reset_mock()
     # WHEN
     self.parser.route_game_event('''"courgette<12312312312312312><1>" committed suicide with "R_DmgType_M26Grenade"''')
     # THEN
     self.assert_has_event('EVT_CLIENT_SUICIDE', data=(100, "R_DmgType_M26Grenade", 'body'), client=p, target=p)
Exemplo n.º 26
0
 def test_say_team(self):
     # GIVEN
     p = Client(cid="12312312312312312", guid="12312312312312312", name="courgette")
     self.queueEvent_mock.reset_mock()
     # WHEN
     self.parser.route_game_event(r'''"courgette<12312312312312312><1>" say_team "(Team) <FONT COLOR='#66CCFF'> hi team"''')
     # THEN
     self.assert_has_event('EVT_CLIENT_TEAM_SAY', data="hi team", client=p)
Exemplo n.º 27
0
 def test_say(self):
     # GIVEN
     p = Client(cid="12312312312312312", guid="12312312312312312", name="courgette")
     self.queueEvent_mock.reset_mock()
     # WHEN
     self.parser.route_game_event('''"courgette<12312312312312312><1>" say "<FONT COLOR='#FF0000'> hi"''')
     # THEN
     self.assert_has_event('EVT_CLIENT_SAY', data="hi", client=p)
Exemplo n.º 28
0
 def test_joined_team(self):
     # GIVEN
     p = Client(cid="12312312312312312", guid="12312312312312312", name="courgette")
     self.queueEvent_mock.reset_mock()
     # WHEN
     self.parser.route_game_event('''"courgette<12312312312312312><1>" joined team "1"''')
     # THEN
     self.assert_has_event('EVT_CLIENT_TEAM_CHANGE', data=TEAM_RESISTANCE, client=p)
Exemplo n.º 29
0
 def test_entered_the_game(self):
     # GIVEN
     p = Client(cid="12312312312312312", guid="12312312312312312", name="courgette")
     self.queueEvent_mock.reset_mock()
     # WHEN
     self.parser.route_game_event('''"courgette<12312312312312312><0>" entered the game''')
     # THEN
     self.assert_has_event('EVT_CLIENT_JOIN', client=p)
Exemplo n.º 30
0
 def test_connected(self):
     # GIVEN
     p = Client(cid="12312312312312312", guid="12312312312312312", ip="192.168.0.1")
     self.queueEvent_mock.reset_mock()
     # WHEN
     self.parser.route_game_event('''"<12312312312312312><>" connected, address "192.168.0.1"''')
     # THEN
     self.assert_has_event('EVT_CLIENT_CONNECT', data=self.whatever, client=p)
Exemplo n.º 31
0
 def test_getCounts(self):
     c1 = Client(guid="aaaaaaaaa")
     self.assertEqual({'Kicks': 0, 'TempBans': 0, 'clients': 0, 'Bans': 0, 'Warnings': 0}, self.storage.getCounts())
     self.storage.setClient(c1)
     self.assertEqual({'Kicks': 0, 'TempBans': 0, 'clients': 1, 'Bans': 0, 'Warnings': 0}, self.storage.getCounts())
     self.storage.setClient(Client(guid="bbbbbbbbbb"))
     self.assertEqual({'Kicks': 0, 'TempBans': 0, 'clients': 2, 'Bans': 0, 'Warnings': 0}, self.storage.getCounts())
     Penalty(clientId=c1.id, adminId=0, type='Ban').save(self.console)
     self.assertEqual({'Kicks': 0, 'TempBans': 0, 'clients': 2, 'Bans': 1, 'Warnings': 0}, self.storage.getCounts())
     Penalty(clientId=c1.id, adminId=0, type='Ban').save(self.console)
     self.assertEqual({'Kicks': 0, 'TempBans': 0, 'clients': 2, 'Bans': 2, 'Warnings': 0}, self.storage.getCounts())
     Penalty(clientId=c1.id, adminId=0, type='TempBan').save(self.console)
     self.assertEqual({'Kicks': 0, 'TempBans': 1, 'clients': 2, 'Bans': 2, 'Warnings': 0}, self.storage.getCounts())
     Penalty(clientId=c1.id, adminId=0, type='Kick').save(self.console)
     self.assertEqual({'Kicks': 1, 'TempBans': 1, 'clients': 2, 'Bans': 2, 'Warnings': 0}, self.storage.getCounts())
     Penalty(clientId=c1.id, adminId=0, type='Warning').save(self.console)
     self.assertEqual({'Kicks': 1, 'TempBans': 1, 'clients': 2, 'Bans': 2, 'Warnings': 1}, self.storage.getCounts())
Exemplo n.º 32
0
 def test_message(self):
     """\
     display a message to a given player
     """
     superman = Client(console=self.console, cid="11")
     self.console.msgPrefix = "B3:"
     self.console.message(superman, "something")
     verify(self.output_mock).write('tell 11 B3: ^3[pm]^7 something')
Exemplo n.º 33
0
 def test_disconnected(self):
     # GIVEN
     p = Client(cid="12312312312312312", guid="12312312312312312", name="courgette")
     self.queueEvent_mock.reset_mock()
     # WHEN
     self.parser.route_game_event('''"courgette<12312312312312312><0>"disconnected''')
     # THEN
     self.assert_has_event('EVT_CLIENT_DISCONNECT', data="12312312312312312", client=p)
Exemplo n.º 34
0
 def on_banid(self, name, cid, guid, team, duration, admin):
     # L 08/28/2012 - 00:03:01: Banid: "courgette<91><STEAM_1:0:1111111><>" was banned "for 1.00 minutes" by "Console"
     client = self.storage.getClient(Client(guid=guid))
     if client:
         return self.getEvent("EVT_CLIENT_BAN_TEMP", {
             "duration": duration,
             "admin": admin,
             'reason': None
         }, client)
Exemplo n.º 35
0
 def test_with_named_parameters__unicode(self):
     client = Client(name=u"ÄÖé")
     self.assertDictContainsSubset(
         {
             'clientname': client.name,
             'reason': 'this is a good reason'
         },
         self.parser.getMessageVariables(client=client,
                                         reason="this is a good reason"))
Exemplo n.º 36
0
 def test_with_named_parameters(self):
     client = Client(name="Jack")
     self.assertDictIsSubset(
         {
             'clientname': client.name,
             'reason': 'this is a good reason'
         },
         self.parser.getMessageVariables(client=client,
                                         reason="this is a good reason"))
Exemplo n.º 37
0
 def test_known_client_by_cid(self):
     # GIVEN
     known_client = Client(console=self.parser, guid="AAAAAAAAAAAA000000000000000", name="theName")
     known_client.save()
     self.assertEqual(1, len(self.parser.clients))
     self.assertDictContainsSubset({'clients': 2}, self.parser.storage.getCounts())
     # WHEN
     client = self.parser.getClientOrCreate(cid="2", guid="AAAAAAAAAAAA000000000000000", name="newName", team="CT")
     # THEN
     self.assertIsInstance(client, Client)
     self.assertEqual(known_client.id, client.id)
     self.assertEqual("2", client.cid)
     self.assertEqual("AAAAAAAAAAAA000000000000000", client.guid)
     self.assertEqual("newName", client.name)
     self.assertEqual(TEAM_RED, client.team)
     self.assertTrue(client.authed)
     self.assertEqual(2, len(self.parser.clients))
     self.assertDictContainsSubset({'clients': 2}, self.parser.storage.getCounts())
Exemplo n.º 38
0
 def test_known_client__guid(self):
     # GIVEN
     known_client = Client(console=self.parser, guid="12312312312312312", name="courgette", connections=15)
     known_client.save()
     self.assertEqual(1, len(self.parser.clients))
     self.assertDictContainsSubset({'clients': 2}, self.parser.storage.getCounts())
     # WHEN
     client = self.parser.getClientOrCreate(guid="12312312312312312", name="newName", team="0")
     # THEN
     self.assertIsInstance(client, Client)
     self.assertEqual(known_client.id, client.id)
     self.assertEqual("12312312312312312", client.cid)
     self.assertEqual("12312312312312312", client.guid)
     self.assertEqual("newName", client.name)
     self.assertEqual(TEAM_SCAVENGERS, client.team)
     self.assertEqual(16, client.connections)
     self.assertTrue(client.authed)
     self.assertEqual(2, len(self.parser.clients))
     self.assertDictContainsSubset({'clients': 2}, self.parser.storage.getCounts())
Exemplo n.º 39
0
class Test_Client_events(B3TestCase):
    
    def setUp(self):
        B3TestCase.setUp(self)
        self.queueEvent_patcher = patch.object(self.console, 'queueEvent')
        self.queueEvent_mock = self.queueEvent_patcher.start()
        
        self.admin = Client(console=self.console)
        self.client = Client(console=self.console)
        self.client.save()
    
    def tearDown(self):
        B3TestCase.tearDown(self)
        self.queueEvent_patcher.stop()

    def test_warn(self):
        with self.assertRaiseEvent(event_type="EVT_CLIENT_WARN", event_client=self.client, event_data={
            'reason': 'insulting admin',
            'duration': 5*60,
            'data': 'foobar',
            'admin': self.admin,
            'timeExpire': ANY
        }, event_target=None):
            self.client.warn(duration='5h', warning='insulting admin', keyword=None, admin=self.admin, data='foobar')

    def test_notice(self):
        with self.assertRaiseEvent(event_type="EVT_CLIENT_NOTICE", event_client=self.client, event_data={
            'notice': "keep a eye on this guy", 
            'admin': self.admin,
            'timeAdd': ANY
        }):
            self.client.notice(notice="keep a eye on this guy", spare=None, admin=self.admin)
Exemplo n.º 40
0
 def test_guid_readonly(self):
     c = Client(console=b3.console)
     self.assertFalse(c.authed)
     c.guid = "foo"
     self.assertEqual(c.guid, "foo")
     c.auth()
     self.assertTrue(c.authed)
     # upon guid change, prevent change and consider client not
     # authed anymore
     c.guid = "bar"
     self.assertFalse(c.authed)
     c.guid = "foo"
Exemplo n.º 41
0
    def test_two_bans_with_reason(self):
        # GIVEN
        when(self.console).time().thenReturn(0)
        player1 = Client(console=self.console, guid='player1GUID', name="P1")
        player1.save()
        penalty1 = ClientBan(clientId=player1.id, timeExpire=-1, adminId=0, reason="test reason")

        player2 = Client(console=self.console, guid='player2GUID', name="P2")
        player2.save()
        penalty2 = ClientTempBan(clientId=player2.id, timeExpire=self.console.time() + 60*2, adminId=0, reason="test reason f00")

        when(self.console.storage).getLastPenalties(types=whatever(), num=whatever()).thenReturn([penalty1, penalty2])
        # WHEN
        self.lastbans()
        # THEN
        self.mock_command.sayLoudOrPM.assert_has_calls([
            call(self.player, u'^2@1^7 P1^7^7 (Perm) test reason'),
            call(self.player, u'^2@2^7 P2^7^7 (2 minutes remaining) test reason f00'),
        ])
Exemplo n.º 42
0
 def setUp(self):
     B3TestCase.setUp(self)
     self.client = Client(console=self.console)
Exemplo n.º 43
0
class Test_Client_groups(B3TestCase):

    def setUp(self):
        B3TestCase.setUp(self)
        self.client = Client(console=self.console)
        self.group_guest = self.console.storage.getGroup(Group(keyword="guest"))
        self.group_user = self.console.storage.getGroup(Group(keyword="user"))
        self.group_reg = self.console.storage.getGroup(Group(keyword="reg"))
        self.group_mod = self.console.storage.getGroup(Group(keyword="mod"))
        self.group_admin = self.console.storage.getGroup(Group(keyword="admin"))
        self.group_fulladmin = self.console.storage.getGroup(Group(keyword="fulladmin"))
        self.group_senioradmin = self.console.storage.getGroup(Group(keyword="senioradmin"))
        self.group_superadmin = self.console.storage.getGroup(Group(keyword="superadmin"))

    def assertGroups(self, groups):
        keywords = map(operator.attrgetter('keyword'), groups)
        self.assertListEqual(keywords, map(operator.attrgetter('keyword'), self.client.groups))
        self.assertListEqual(keywords, map(operator.attrgetter('keyword'), self.client.getGroups()))

    def test_addGroup(self):
        # GIVEN
        self.client.addGroup(self.group_mod)
        self.assertGroups([self.group_mod])
        # WHEN
        self.client.addGroup(self.group_superadmin)
        # THEN
        self.assertGroups([self.group_mod, self.group_superadmin])

    def test_rmGroup(self):
        # GIVEN
        self.client.addGroup(self.group_mod)
        self.assertGroups([self.group_mod])
        self.client.addGroup(self.group_superadmin)
        # WHEN
        self.client.remGroup(self.group_mod)
        # THEN
        self.assertGroups([self.group_superadmin])

    def test_guest_group_is_the_default_group_when_none(self):
        self.assertGroups([self.group_guest])

        self.client.remGroup(self.group_guest)
        self.assertGroups([self.group_guest])

        self.client.addGroup(self.group_admin)
        self.assertGroups([self.group_admin])

    def test_inGroup(self):
        self.assertFalse(self.client.inGroup(self.group_guest))
        self.assertFalse(self.client.inGroup(self.group_user))
        self.assertFalse(self.client.inGroup(self.group_reg))
        self.assertFalse(self.client.inGroup(self.group_mod))
        self.assertFalse(self.client.inGroup(self.group_admin))
        self.assertFalse(self.client.inGroup(self.group_fulladmin))
        self.assertFalse(self.client.inGroup(self.group_senioradmin))
        self.assertFalse(self.client.inGroup(self.group_superadmin))

        self.client.addGroup(self.group_user)
        self.assertTrue(self.client.inGroup(self.group_user))
        self.client.addGroup(self.group_reg)
        self.assertTrue(self.client.inGroup(self.group_reg))
        self.client.addGroup(self.group_mod)
        self.assertTrue(self.client.inGroup(self.group_mod))
        self.client.addGroup(self.group_admin)
        self.assertTrue(self.client.inGroup(self.group_admin))
        self.client.addGroup(self.group_fulladmin)
        self.assertTrue(self.client.inGroup(self.group_fulladmin))
        self.client.addGroup(self.group_senioradmin)
        self.assertTrue(self.client.inGroup(self.group_senioradmin))
        self.client.addGroup(self.group_superadmin)
        self.assertTrue(self.client.inGroup(self.group_superadmin))
Exemplo n.º 44
0
 def test_message_with_color_codes(self):
     self.parser.msgPrefix = "[Pre]"
     player = Client(console=self.parser, guid="theGuid")
     with patch.object(self.parser, 'writelines') as write_mock:
         player.message("^7message ^1with ^2color ^8codes")
         write_mock.assert_has_calls([call(['m  [Pre] ^8[pm]^7 ^7message ^1with ^2color ^8codes'])])
Exemplo n.º 45
0
 def test_kick(self):
     player = Client(console=self.parser, cid="4", guid="theGuid", name="theName")
     with patch.object(self.parser.output, 'write') as write_mock:
         player.kick(reason="f00")
         write_mock.assert_has_calls([call('sm_kick #4 f00')])
Exemplo n.º 46
0
 def test_message(self):
     self.parser.msgPrefix = "[Pre]"
     player = Client(console=self.parser, guid="theGuid")
     with patch.object(self.parser, 'writelines') as write_mock:
         player.message("f00")
         write_mock.assert_has_calls([call(['m  [Pre] ^8[pm]^7 f00'])])
Exemplo n.º 47
0
 def test_message(self):
     self.parser.msgPrefix = "[Pre]"
     player = Client(console=self.parser, guid="theGuid")
     with patch.object(self.parser.output, 'write') as write_mock:
         player.message("f00")
         write_mock.assert_has_calls([call('sm_psay #theGuid "[Pre] f00"')])
 def test_message_with_color_codes(self):
     self.parser.msgPrefix = "[Pre]"
     player = Client(console=self.parser, guid="theGuid")
     with patch.object(self.parser.output, 'write') as write_mock:
         player.message("^7message ^1with ^2color ^8codes")
         write_mock.assert_has_calls([call('sm_psay #theGuid "[Pre] message with color codes"')])
Exemplo n.º 49
0
class Test_Client(B3TestCase):
    
    def setUp(self):
        B3TestCase.setUp(self)
        self.client = Client(console=self.console)
    
    def test_construct(self):
        c = Client(name="Courgette", guid="1234567890")
        self.assertEqual(c.name, "Courgette")
        self.assertEqual(c.exactName, "Courgette^7")
        self.assertEqual(c.guid, "1234567890")
        self.assertEqual(c.team, TEAM_UNKNOWN)
        self.assertTrue(c.connected)
        self.assertFalse(c.hide)
        self.assertEqual(c.ip, '')
        self.assertEqual(c.greeting, '')
        self.assertEqual(c.pbid, '')

    def test_team(self):
        m = Mock()
        self.client.team = m
        self.assertEqual(self.client.team, m)
        
    def test_team_change(self):
        self.console.queueEvent = Mock()
        self.client.team = 24
        self.assertEqual(self.client.team, 24)
        self.console.queueEvent.assert_called()
        args = self.console.queueEvent.call_args
        eventraised = args[0][0]
        self.assertEquals(eventraised.type, EVT_CLIENT_TEAM_CHANGE)
        self.assertEquals(eventraised.data, 24)

    def test_name_change(self):
        self.console.queueEvent = Mock()
        self.client.authed = True
        self.client.name = "cucurb"
        self.assertEqual(self.client.name, "cucurb")
        self.console.queueEvent.assert_called()
        args = self.console.queueEvent.call_args
        eventraised = args[0][0]
        self.assertEquals(eventraised.type, EVT_CLIENT_NAME_CHANGE)
        self.assertEquals(eventraised.data, 'cucurb')

    def test_makeAlias_new(self):
        self.client.id = 123
        self.console.storage.getClientAlias = Mock(side_effect = KeyError())
        self.client.makeAlias("bar")
        self.assertEquals(self.console.storage.getClientAlias.call_count, 1)
        alias = self.console.storage.getClientAlias.call_args[0][0]
        self.assertIsInstance(alias, Alias)
        self.assertEqual(alias.alias, "bar")
        self.assertEqual(alias.numUsed, 1)

    def test_makeAlias_existing(self):
        self.client.id = 123
        aliasFoo = Alias()
        aliasFoo.alias = "foo"
        aliasFoo.clientId = self.client.id
        aliasFoo.numUsed = 48
        self.console.storage.getClientAlias = Mock(side_effect = lambda x: aliasFoo)
        self.client.makeAlias("whatever")
        self.assertEquals(self.console.storage.getClientAlias.call_count, 1)
        self.assertIsInstance(aliasFoo, Alias)
        self.assertEqual(aliasFoo.alias, "foo")
        self.assertEqual(aliasFoo.numUsed, 49)

    def test_guid_readonly(self):
        self.assertFalse(self.client.authed)
        self.client.guid = "foo"
        self.assertEqual(self.client.guid, "foo")
        self.client.auth()
        self.assertTrue(self.client.authed)
        # upon guid change, prevent change and consider client not
        # authed anymore
        self.client.guid = "bar"
        self.assertFalse(self.client.authed)
        self.client.guid = "foo"

    def test_set_ip(self):
        self.client.ip = "1.2.3.4"
        self.assertEqual(self.client._ip, "1.2.3.4")
        self.client.ip = "5.6.7.8:27960"
        self.assertEqual(self.client._ip, "5.6.7.8")
        
    def test_makeIpAlias_new(self):
        self.client.id = 123
        self.console.storage.getClientIpAddress = Mock(side_effect = KeyError())
        self.client.makeIpAlias("1.4.7.8")
        self.assertEquals(self.console.storage.getClientIpAddress.call_count, 1)
        alias = self.console.storage.getClientIpAddress.call_args[0][0]
        self.assertIsInstance(alias, IpAlias)
        self.assertEqual(alias.ip, "1.4.7.8")
        self.assertEqual(alias.numUsed, 1)

    def test_makeIpAlias_existing(self):
        self.client.id = 123
        aliasFoo = IpAlias()
        aliasFoo.ip = "9.5.4.4"
        aliasFoo.clientId = self.client.id
        aliasFoo.numUsed = 8
        self.console.storage.getClientIpAddress = Mock(side_effect = lambda x: aliasFoo)
        self.client.makeIpAlias("whatever")
        self.assertEquals(self.console.storage.getClientIpAddress.call_count, 1)
        self.assertIsInstance(aliasFoo, IpAlias)
        self.assertEqual(aliasFoo.ip, "9.5.4.4")
        self.assertEqual(aliasFoo.numUsed, 9)