Example #1
0
class Test_parser_API(CsgoTestCase):

    def setUp(self):
        self.conf = XmlConfigParser()
        self.conf.loadFromString("""<configuration></configuration>""")
        self.parser = CsgoParser(self.conf)
        self.parser.output = Mock()
        when(self.parser.output).write("status").thenReturn(STATUS_RESPONSE)
        self.parser.startup()

    def tearDown(self):
        if hasattr(self, "parser"):
            del self.parser.clients
            self.parser.working = False


    def test_getPlayerList(self):
        # GIVEN
        with patch.object(self.parser, "queryServerInfo") as queryServerInfo_Mock:
            # WHEN
            c3 = Mock()
            c4 = Mock()
            c12 = Mock()
            queryServerInfo_Mock.return_value = {'3': c3, '4': c4, '12': c12, }
            rv = self.parser.getPlayerList()
            # THEN
            self.assertDictEqual({'3': c3, '4': c4, '12': c12, }, rv)


    def test_say(self):
        self.parser.msgPrefix = "[Pre]"
        with patch.object(self.parser.output, 'write') as write_mock:
            self.parser.say("f00")
            write_mock.assert_has_calls([call('sm_say [Pre] f00')])


    def test_saybig(self):
        self.parser.msgPrefix = "[Pre]"
        with patch.object(self.parser.output, 'write') as write_mock:
            self.parser.saybig("f00")
            write_mock.assert_has_calls([call('sm_hsay [Pre] f00')])

#    @unittest.skipIf(WAS_FROSTBITE_LOADED, "Frostbite(1|2) parsers monkey patch the Client class and make this test fail")
    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_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')])


    def test_ban(self):
        # GIVEN
        player = Client(console=self.parser, cid="2", name="courgette", guid="STEAM_1:0:1111111")
        # WHEN
        self.clear_events()
        with patch.object(self.parser.output, 'write') as write_mock:
            self.parser.ban(player, reason="test")
        # THEN
        write_mock.assert_has_calls([call('sm_addban 0 "STEAM_1:0:1111111" test'),
                                     call('sm_kick #2 test'),
                                     call('sm_say courgette was banned test')])


    def test_ban__not_connected(self):
        # GIVEN
        player = Client(console=self.parser, cid=None, name="courgette", guid="STEAM_1:0:1111111")
        # WHEN
        self.clear_events()
        with patch.object(self.parser.output, 'write') as write_mock:
            self.parser.ban(player, reason="test")
        # THEN
        write_mock.assert_has_calls([call('sm_addban 0 "STEAM_1:0:1111111" test'),
                                     call('sm_say courgette was banned test')])


    def test_unban(self):
        # GIVEN
        player = Client(console=self.parser, cid=None, name="courgette", guid="STEAM_1:0:1111111")
        # WHEN
        self.clear_events()
        with patch.object(self.parser.output, 'write') as write_mock:
            self.parser.unban(player)
        # THEN
        write_mock.assert_has_calls([call('sm_unban "STEAM_1:0:1111111"')])



    def test_tempban(self):
        # GIVEN
        player = Client(console=self.parser, cid="2", name="courgette", guid="STEAM_1:0:1111111")
        # WHEN
        self.clear_events()
        with patch.object(self.parser.output, 'write') as write_mock:
            self.parser.tempban(player, reason="test", duration="45m")
        # THEN
        write_mock.assert_has_calls([call('sm_addban 45 "STEAM_1:0:1111111" test'),
                                     call('sm_kick #2 test'),
                                     call('sm_say courgette was temp banned for 45 minutes test')])


    def test_getMap(self):
        # WHEN
        rv = self.parser.getMap()
        # THEN
        self.assertEqual('cs_foobar', rv)


    def test_getMaps(self):
        when(self.parser.output).write("listmaps").thenReturn('''Map Cycle:
cs_italy
de_dust
de_aztec
cs_office
de_dust2
de_train
de_inferno
de_nuke
L 08/28/2012 - 01:16:28: rcon from "11.222.111.222:4107": command "listmaps"
''')
        maps = self.parser.getMaps()
        verify(self.parser.output).write("listmaps")
        self.assertListEqual(["cs_italy",
                              "de_dust",
                              "de_aztec",
                              "cs_office",
                              "de_dust2",
                              "de_train",
                              "de_inferno",
                              "de_nuke",
                              ], maps)


    @patch('time.sleep')
    def test_rotateMap(self, sleep_mock):
        # GIVEN
        when(self.parser).getNextMap().thenReturn('the_next_map')
        # WHEN
        with patch.object(self.parser.output, 'write') as write_mock:
            self.parser.rotateMap()
        # THEN
        write_mock.assert_has_calls([call('sm_hsay Changing to next map : the_next_map'),
                                     call('map the_next_map')])
        sleep_mock.assert_was_called_once_with(1)


    def test_changeMap(self):
        # GIVEN
        when(self.parser).getMapsSoundingLike("de_f00").thenReturn("de_f00")
        # WHEN
        with patch.object(self.parser.output, 'write') as write_mock:
            self.parser.changeMap("de_f00")
        # THEN
        write_mock.assert_has_calls([call('sm_map de_f00')])


    def test_changeMap__suggestions(self):
        # GIVEN
        when(self.parser).getMapsSoundingLike("f00").thenReturn(["de_f001", "de_f002"])
        # WHEN
        with patch.object(self.parser.output, 'write') as write_mock:
            rv = self.parser.changeMap("f00")
        # THEN
        self.assertSetEqual(set(["de_f001", "de_f002"]), set(rv))
        self.assertEqual(0, write_mock.call_count)


    def test_getPlayerPings(self):
        # GIVEN
        with patch.object(self.parser, "queryServerInfo") as queryServerInfo_Mock:
            # WHEN
            queryServerInfo_Mock.return_value = {
                '3': Client(ping="45"),
                '4': Client(ping="112"),
                '12': Client(ping="54"),
            }
            rv = self.parser.getPlayerPings()
            # THEN
            self.assertEqual(3, len(rv))
            self.assertEqual("45", rv['3'])
            self.assertEqual("112", rv['4'])
            self.assertEqual("54", rv['12'])


    @unittest.skip("TODO")
    def test_getPlayerScores(self):
        pass


    @unittest.skip("TODO")
    def test_inflictCustomPenalty(self):
        pass