class Test_command_cancelvote(Bf3TestCase):
    def setUp(self):
        Bf3TestCase.setUp(self)
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""\
[commands]
cancelvote-cv: 20
""")
        self.p = VotemapPlugin(self.console, self.conf)
        self.p.onLoadConfig()
        self.p.onStartup()

    def test_no_vote(self):
        self.admin.connects('admin')
        self.admin.says('!cancelvote')
        self.assertEqual(['There is no vote to cancel'], self.admin.message_history)

    def test_nominal(self):
        self.admin.connects('admin')
        self.p.current_vote_session = Mock()
        self.p.current_vote_session_timer = Mock()
        self.admin.says('!cancelvote')
        self.assertEqual(['Vote canceled'], self.admin.message_history)
        self.assertIsNone(self.p.current_vote_session)
        self.assertIsNone(self.p.current_vote_session_timer)

    def test_alias(self):
        self.admin.connects('admin')
        self.admin.clearMessageHistory()
        self.admin.says('!cv')
        self.assertEqual(['There is no vote to cancel'], self.admin.message_history)
class Votesession_TestCase(Bf3TestCase):
    map_list = MapListBlock(
        ['3', '3', 'MP_001', 'RushLarge0', '1', 'MP_003', 'ConquestSmall0', '2', 'MP_007', 'SquadDeathMatch0', '3'])

    @staticmethod
    def write(data):
        """ simulate the write method of the B3 parser used to communicate with the BF3 server """
        if data == ('mapList.getMapIndices',):
            return ['0', '1']

    def setUp(self):
        Bf3TestCase.setUp(self)
        self.conf = CfgConfigParser()
        self.conf.loadFromString(self.__class__.CONFIG)
        self.p = VotemapPlugin(self.console, self.conf)
        self.p.onLoadConfig()
        self.p.onStartup()

        self.getFullMapRotationList_patcher = patch.object(self.console, 'getFullMapRotationList',
            Mock(return_value=Test_command_votemap.map_list))
        self.getFullMapRotationList_patcher.start()

        self.write_patcher = patch.object(self.console, 'write', wraps=Test_command_votemap.write)
        self.write_mock = self.write_patcher.start()

    def tearDown(self):
        Bf3TestCase.tearDown(self)
        if hasattr(self.p, 'current_vote_session_timer') and self.p.current_vote_session_timer:
            self.p.current_vote_session_timer.cancel()
        self.getFullMapRotationList_patcher.stop()
        self.write_patcher.stop()
class Test_read_maplist_file(Bf3TestCase):
    def setUp(self):
        Bf3TestCase.setUp(self)
        self.conf = CfgConfigParser()
        self.p = VotemapPlugin(self.console, self.conf)
        logger = logging.getLogger("output")
        logger.setLevel(logging.NOTSET)

    @patch("__builtin__.open")
    def test(self, open_mock):
        context_manager_mock = MagicMock()
        open_mock.return_value = context_manager_mock
        context_manager_mock.__exit__ = Mock()
        context_manager_mock.__enter__ = Mock(
            side_effect=lambda *args, **kwargs: StringIO.StringIO(
                r"""
MP_007 RushLarge0 2
MP_018 ConquestSmall0 2
# comment
MP_Subway RushLarge0 4
MP_003 RushLarge0 4
    """
            )
        )
        maplist_block = self.p._read_maplist_file("f00")
        self.assertEqual(
            "MapListBlock[MP_007:RushLarge0:2, MP_018:ConquestSmall0:2, MP_Subway:RushLarge0:4, MP_003:RushLarge0:4]",
            str(maplist_block),
        )
    def setUp(self):
        Bf3TestCase.setUp(self)
        self.conf = CfgConfigParser()
        self.conf.loadFromString("""\
[commands]
cancelvote-cv: 20
""")
        self.p = VotemapPlugin(self.console, self.conf)
        self.p.onLoadConfig()
        self.p.onStartup()
class Test_messages(Bf3TestCase):
    def setUp(self):
        Bf3TestCase.setUp(self)
        self.conf = CfgConfigParser()
        self.p = VotemapPlugin(self.console, self.conf)
        logger = logging.getLogger('output')
        logger.setLevel(logging.INFO)


    def test_default_messages(self):
        # GIVEN
        self.conf.loadFromString(r"""
[messages]
        """)
        # WHEN
        self.p._load_default_messages()
        # THEN
        for msg_key in messages_keys:
            self.assertIn(msg_key, self.p._messages)
            self.assertTrue(self.p._messages[msg_key])


    def test_default_config(self):
        # GIVEN
        self.conf.load(os.path.join(os.path.dirname(__file__), '../extplugins/conf/plugin_votemapbf3.ini'))
        # WHEN
        self.p._messages = {}
        self.p._load_messages()
        # THEN
        for msg_key in messages_keys:
            self.assertIn(msg_key, self.p._messages)
            self.assertTrue(self.p._messages[msg_key])


    def test_custom_messages(self):
        # GIVEN
        self.conf.loadFromString(r"""
[messages]
msg2: custom messages for msg2
        """)
        # WHEN
        self.p._messages = {
            'msg1': 'this is the default message for msg1',
            'msg2': 'this is the default message for msg2'
        }
        self.p._load_messages()
        # THEN
        self.assertEqual("this is the default message for msg1", self.p._messages['msg1'])
        self.assertEqual("this is the default message for msg1", self.p.getMessage('msg1'))
        self.assertEqual("custom messages for msg2", self.p._messages['msg2'])
        self.assertEqual("custom messages for msg2", self.p.getMessage('msg2'))
class Votesession_mockito_TestCase(Bf3TestCase):

    def setUp(self):
        Bf3TestCase.setUp(self)
        self.conf = CfgConfigParser()
        self.conf.loadFromString(self.__class__.CONFIG)
        self.p = VotemapPlugin(self.console, self.conf)
        self.p.onLoadConfig()
        self.p.onStartup()

        when(self.console).write(('mapList.list', 0)).thenReturn(['3', '3', 'MP_001', 'RushLarge0', '1', 'MP_003', 'ConquestSmall0', '2', 'MP_007', 'SquadDeathMatch0', '3'])
        when(self.console).write(('mapList.getMapIndices',)).thenReturn(['0', '1'])
        when(self.console).write(('serverInfo',)).thenReturn(['BigBrotherBot #2 (US)', '0', '16',
                                              'ConquestSmall0', 'MP_001', '0', '1',
                                              '2', '1', '1', '0', '', 'true', 'true', 'false', '1385628', '1372265',
                                              '', '', '', 'NAm', 'dfw', 'US', 'false'])

        logging.getLogger('output').setLevel(logging.NOTSET)

    def tearDown(self):
        Bf3TestCase.tearDown(self)
        if hasattr(self.p, 'current_vote_session_timer') and self.p.current_vote_session_timer:
            self.p.current_vote_session_timer.cancel()
    def setUp(self):
        Bf3TestCase.setUp(self)
        self.conf = CfgConfigParser()
        self.conf.loadFromString(self.__class__.CONFIG)
        self.p = VotemapPlugin(self.console, self.conf)
        self.p.onLoadConfig()
        self.p.onStartup()

        self.getFullMapRotationList_patcher = patch.object(self.console, 'getFullMapRotationList',
            Mock(return_value=Test_command_votemap.map_list))
        self.getFullMapRotationList_patcher.start()

        self.write_patcher = patch.object(self.console, 'write', wraps=Test_command_votemap.write)
        self.write_mock = self.write_patcher.start()
class Test_make_map_label(Bf3TestCase):
    def setUp(self):
        Bf3TestCase.setUp(self)
        self.conf = CfgConfigParser()
        self.p = VotemapPlugin(self.console, self.conf)
        self.p.onLoadConfig()
        self.p.onStartup()

    def test_nominal(self):
        self.assertEqual("Caspian Border (Rush)", self.p._make_map_label({"gamemode": "RushLarge0", "name": "MP_007"}))

    def test_unknown_gamemode(self):
        self.assertEqual("Caspian Border", self.p._make_map_label({"gamemode": "Elimination0", "name": "MP_007"}))

    def test_unknown_map(self):
        self.assertEqual("f00 (Rush)", self.p._make_map_label({"gamemode": "RushLarge0", "name": "f00"}))
class Test_make_map_label(Bf4TestCase):
    def setUp(self):
        Bf4TestCase.setUp(self)
        self.conf = CfgConfigParser()
        self.p = VotemapPlugin(self.console, self.conf)
        self.p.onLoadConfig()
        self.p.onStartup()

    def test_nominal(self):
        self.assertEqual(
            "Operation Locker (Defuse)", self.p._make_map_label({"gamemode": "Elimination0", "name": "MP_Prison"})
        )

    def test_unknown_gamemode(self):
        self.assertEqual("Siege of Shanghai", self.p._make_map_label({"gamemode": "f00", "name": "MP_Siege"}))

    def test_unknown_map(self):
        self.assertEqual("f00 (Rush)", self.p._make_map_label({"gamemode": "RushLarge0", "name": "f00"}))
 def setUp(self):
     Bf3TestCase.setUp(self)
     self.conf = CfgConfigParser()
     self.p = VotemapPlugin(self.console, self.conf)
     logger = logging.getLogger('output')
     logger.setLevel(logging.INFO)
 def setUp(self):
     Bf4TestCase.setUp(self)
     self.conf = CfgConfigParser()
     self.p = VotemapPlugin(self.console, self.conf)
     self.p.onLoadConfig()
     self.p.onStartup()