示例#1
0
class Test_cmd_login(LoginTestCase):

    def setUp(self):
        LoginTestCase.setUp(self)
        self.init_plugin()
        # create a client which needs to log in and has a password saved in database
        self.jack = FakeClient(self.console, name="Jack", guid="jackguid", groupBits=128, password=F00_MD5)
        self.jack.save()

    def test_already_logged_in(self):
        # GIVEN
        joe = FakeClient(self.console, name="Joe", guid="joeguid", groupBits=128)
        joe.setvar(self.p, 'loggedin', 1)
        joe.connects("0")
        # WHEN
        joe.clearMessageHistory()
        joe.says("!login")
        # THEN
        self.assertEqual(['You are already logged in'], joe.message_history)

    def test_low_level(self):
        # GIVEN
        joe = FakeClient(self.console, name="Joe", guid="joeguid", groupBits=8)
        joe.connects("0")
        # WHEN
        joe.clearMessageHistory()
        joe.says("!login")
        # THEN
        self.assertEqual(['You do not need to log in'], joe.message_history)
        self.assertFalse(self.jack.isvar(self.p, 'loggedin'))

    def test_high_level_no_parameter(self):
        # GIVEN
        self.jack.connects("0")
        self.assertEqual(2, self.jack.groupBits) # the login plugin set his level down to 2 while waiting for the password
        # WHEN
        self.jack.clearMessageHistory()
        self.jack.says("!login")
        # THEN
        self.assertEqual(['Usage (via console): /tell 0 !login yourpassword'], self.jack.message_history)
        self.assertEqual(2, self.jack.groupBits)
        self.assertFalse(self.jack.isvar(self.p, 'loggedin'))

    def test_high_level_wrong_password(self):
        # GIVEN
        self.jack.connects("0")
        self.assertEqual(2, self.jack.groupBits) # the login plugin set his level down to 2 while waiting for the password
        # WHEN
        self.jack.clearMessageHistory()
        self.jack.says("!login qsfddqsf")
        # THEN
        self.assertEqual(['***Access denied***'], self.jack.message_history)
        self.assertEqual(2, self.jack.groupBits)
        self.assertFalse(self.jack.isvar(self.p, 'loggedin'))

    def test_high_level_correct_password(self):
        # GIVEN
        self.jack.connects("0")
        self.assertEqual(2, self.jack.groupBits) # the login plugin set his level down to 2 while waiting for the password
        # WHEN
        self.jack.clearMessageHistory()
        self.jack.says("!login f00")
        # THEN
        self.assertEqual(['You are successfully logged in'], self.jack.message_history)
        self.assertEqual(128, self.jack.groupBits)
        self.assertTrue(self.jack.isvar(self.p, 'loggedin'))

    def test_high_level_spoofed_password_with_compromised_client_object(self):
        """
        in some B3 game parser implementation there is an issue which could let the 'password' property of client
        objects be compromised.
        """
        # GIVEN
        batman_md5 = 'ec0e2603172c73a8b644bb9456c1ff6e'
        self.jack.connects("0")
        self.assertEqual(2, self.jack.groupBits) # the login plugin set his level down to 2 while waiting for the password
        self.jack.password = batman_md5
        # WHEN
        self.jack.clearMessageHistory()
        self.jack.says("!login batman")
        # THEN
        self.assertEqual(['***Access denied***'], self.jack.message_history)
        self.assertEqual(2, self.jack.groupBits)
        self.assertFalse(self.jack.isvar(self.p, 'loggedin'))

    def test_high_level_correct_password_with_compromised_client_object(self):
        """
        in some B3 game parser implementation there is an issue which could let the 'password' property of client
        objects be compromised.
        """
        # GIVEN
        batman_md5 = 'ec0e2603172c73a8b644bb9456c1ff6e'
        self.jack.connects("0")
        self.assertEqual(2, self.jack.groupBits) # the login plugin set his level down to 2 while waiting for the password
        self.jack.password = batman_md5
        # WHEN
        self.jack.clearMessageHistory()
        self.jack.says("!login f00")
        # THEN
        self.assertEqual(['You are successfully logged in'], self.jack.message_history)
        self.assertEqual(128, self.jack.groupBits)
        self.assertTrue(self.jack.isvar(self.p, 'loggedin'))
示例#2
0
class Test_cmd_login(LoginTestCase):
    def setUp(self):
        LoginTestCase.setUp(self)
        self.init_plugin()
        # create a client which needs to log in and has a password saved in database
        self.jack = FakeClient(self.console,
                               name="Jack",
                               guid="jackguid",
                               groupBits=128,
                               password=F00_MD5)
        self.jack.save()

    def test_already_logged_in(self):
        # GIVEN
        joe = FakeClient(self.console,
                         name="Joe",
                         guid="joeguid",
                         groupBits=128)
        joe.setvar(self.p, 'loggedin', 1)
        joe.connects("0")
        # WHEN
        joe.clearMessageHistory()
        joe.says("!login")
        # THEN
        self.assertEqual(['You are already logged in'], joe.message_history)

    def test_low_level(self):
        # GIVEN
        joe = FakeClient(self.console, name="Joe", guid="joeguid", groupBits=8)
        joe.connects("0")
        # WHEN
        joe.clearMessageHistory()
        joe.says("!login")
        # THEN
        self.assertEqual(['You do not need to log in'], joe.message_history)
        self.assertFalse(self.jack.isvar(self.p, 'loggedin'))

    def test_high_level_no_parameter(self):
        # GIVEN
        self.jack.connects("0")
        self.assertEqual(
            2, self.jack.groupBits
        )  # the login plugin set his level down to 2 while waiting for the password
        # WHEN
        self.jack.clearMessageHistory()
        self.jack.says("!login")
        # THEN
        self.assertEqual(['Usage (via console): /tell 0 !login yourpassword'],
                         self.jack.message_history)
        self.assertEqual(2, self.jack.groupBits)
        self.assertFalse(self.jack.isvar(self.p, 'loggedin'))

    def test_high_level_wrong_password(self):
        # GIVEN
        self.jack.connects("0")
        self.assertEqual(
            2, self.jack.groupBits
        )  # the login plugin set his level down to 2 while waiting for the password
        # WHEN
        self.jack.clearMessageHistory()
        self.jack.says("!login qsfddqsf")
        # THEN
        self.assertEqual(['***Access denied***'], self.jack.message_history)
        self.assertEqual(2, self.jack.groupBits)
        self.assertFalse(self.jack.isvar(self.p, 'loggedin'))

    def test_high_level_correct_password(self):
        # GIVEN
        self.jack.connects("0")
        self.assertEqual(
            2, self.jack.groupBits
        )  # the login plugin set his level down to 2 while waiting for the password
        # WHEN
        self.jack.clearMessageHistory()
        self.jack.says("!login f00")
        # THEN
        self.assertEqual(['You are successfully logged in'],
                         self.jack.message_history)
        self.assertEqual(128, self.jack.groupBits)
        self.assertTrue(self.jack.isvar(self.p, 'loggedin'))

    def test_high_level_spoofed_password_with_compromised_client_object(self):
        """
        in some B3 game parser implementation there is an issue which could let the 'password' property of client
        objects be compromised.
        """
        # GIVEN
        batman_md5 = 'ec0e2603172c73a8b644bb9456c1ff6e'
        self.jack.connects("0")
        self.assertEqual(
            2, self.jack.groupBits
        )  # the login plugin set his level down to 2 while waiting for the password
        self.jack.password = batman_md5
        # WHEN
        self.jack.clearMessageHistory()
        self.jack.says("!login batman")
        # THEN
        self.assertEqual(['***Access denied***'], self.jack.message_history)
        self.assertEqual(2, self.jack.groupBits)
        self.assertFalse(self.jack.isvar(self.p, 'loggedin'))

    def test_high_level_correct_password_with_compromised_client_object(self):
        """
        in some B3 game parser implementation there is an issue which could let the 'password' property of client
        objects be compromised.
        """
        # GIVEN
        batman_md5 = 'ec0e2603172c73a8b644bb9456c1ff6e'
        self.jack.connects("0")
        self.assertEqual(
            2, self.jack.groupBits
        )  # the login plugin set his level down to 2 while waiting for the password
        self.jack.password = batman_md5
        # WHEN
        self.jack.clearMessageHistory()
        self.jack.says("!login f00")
        # THEN
        self.assertEqual(['You are successfully logged in'],
                         self.jack.message_history)
        self.assertEqual(128, self.jack.groupBits)
        self.assertTrue(self.jack.isvar(self.p, 'loggedin'))
class Test_events(JumperTestCase):
    def setUp(self):
        JumperTestCase.setUp(self)
        with logging_disabled():
            from b3.fake import FakeClient

        # create some clients
        self.mike = FakeClient(console=self.console,
                               name="Mike",
                               guid="mikeguid",
                               team=TEAM_FREE,
                               groupBits=1)
        self.bill = FakeClient(console=self.console,
                               name="Bill",
                               guid="billguid",
                               team=TEAM_FREE,
                               groupBits=1)
        self.mike.connects("1")
        self.bill.connects("2")

        # force fake mapname
        self.console.game.mapName = 'ut42_bstjumps_u2'

    def tearDown(self):
        self.mike.disconnects()
        self.bill.disconnects()
        JumperTestCase.tearDown(self)

    ####################################################################################################################
    #                                                                                                                  #
    #   JUMPRUN EVENTS                                                                                                 #
    #                                                                                                                  #
    ####################################################################################################################

    def test_event_client_jumprun_started(self):
        # WHEN
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.mike,
                                  data={'way_id': '1'}))
        # THEN
        self.assertEqual(True, self.mike.isvar(self.p, 'jumprun'))
        self.assertIsNone(self.mike.var(self.p, 'jumprun').value.demo)
        self.assertIsInstance(self.mike.var(self.p, 'jumprun').value, JumpRun)

    def test_event_client_jumprun_stopped(self):
        # WHEN
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP',
                                  client=self.mike,
                                  data={
                                      'way_id': '1',
                                      'way_time': '12345'
                                  }))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertListEqual([],
                             self.p.getClientRecords(
                                 self.mike, self.console.game.mapName))

    def test_event_client_jumprun_canceled(self):
        # WHEN
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_CANCEL',
                                  client=self.mike,
                                  data={'way_id': '1'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertListEqual([],
                             self.p.getClientRecords(
                                 self.mike, self.console.game.mapName))

    def test_event_client_jumprun_started_stopped(self):
        # WHEN
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.mike,
                                  data={'way_id': '1'}))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP',
                                  client=self.mike,
                                  data={
                                      'way_id': '1',
                                      'way_time': '12345'
                                  }))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(
            1,
            len(self.p.getClientRecords(self.mike, self.console.game.mapName)))

    def test_event_client_jumprun_started_canceled(self):
        # WHEN
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.mike,
                                  data={'way_id': '1'}))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_CANCEL',
                                  client=self.mike,
                                  data={'way_id': '1'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(
            0,
            len(self.p.getClientRecords(self.mike, self.console.game.mapName)))

    def test_event_client_jumprun_started_stopped_multiple_clients(self):
        # WHEN
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.mike,
                                  data={'way_id': '1'}))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP',
                                  client=self.mike,
                                  data={
                                      'way_id': '1',
                                      'way_time': '12345'
                                  }))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.bill,
                                  data={'way_id': '1'}))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP',
                                  client=self.bill,
                                  data={
                                      'way_id': '1',
                                      'way_time': '12345'
                                  }))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(False, self.bill.isvar(self.p, 'jumprun'))
        self.assertEqual(
            1,
            len(self.p.getClientRecords(self.mike, self.console.game.mapName)))
        self.assertEqual(
            1,
            len(self.p.getClientRecords(self.bill, self.console.game.mapName)))

    def test_event_client_jumprun_started_stopped_multiple_ways(self):
        # WHEN
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.mike,
                                  data={'way_id': '1'}))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP',
                                  client=self.mike,
                                  data={
                                      'way_id': '1',
                                      'way_time': '12345'
                                  }))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.mike,
                                  data={'way_id': '2'}))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP',
                                  client=self.mike,
                                  data={
                                      'way_id': '2',
                                      'way_time': '12345'
                                  }))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(
            2,
            len(self.p.getClientRecords(self.mike, self.console.game.mapName)))

    def test_event_client_jumprun_started_stopped_multiple_clients_multiple_ways(
            self):
        # WHEN
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.mike,
                                  data={'way_id': '1'}))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP',
                                  client=self.mike,
                                  data={
                                      'way_id': '1',
                                      'way_time': '12345'
                                  }))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.mike,
                                  data={'way_id': '2'}))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP',
                                  client=self.mike,
                                  data={
                                      'way_id': '2',
                                      'way_time': '12345'
                                  }))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.bill,
                                  data={'way_id': '1'}))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP',
                                  client=self.bill,
                                  data={
                                      'way_id': '1',
                                      'way_time': '12345'
                                  }))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.bill,
                                  data={'way_id': '2'}))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP',
                                  client=self.bill,
                                  data={
                                      'way_id': '2',
                                      'way_time': '12345'
                                  }))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(False, self.bill.isvar(self.p, 'jumprun'))
        self.assertEqual(
            2,
            len(self.p.getClientRecords(self.mike, self.console.game.mapName)))
        self.assertEqual(
            2,
            len(self.p.getClientRecords(self.bill, self.console.game.mapName)))

    ####################################################################################################################
    #                                                                                                                  #
    #   OTHER EVENTS                                                                                                   #
    #                                                                                                                  #
    ####################################################################################################################

    def test_event_game_map_change(self):
        # WHEN
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.mike,
                                  data={'way_id': '1'}))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.bill,
                                  data={'way_id': '1'}))
        self.console.queueEvent(
            self.console.getEvent(
                'EVT_GAME_MAP_CHANGE',
                data=
                '\sv_allowdownload\0\g_matchmode\0\g_gametype\9\sv_maxclients\32\sv_floodprotect\1'
            ))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(False, self.bill.isvar(self.p, 'jumprun'))
        self.assertListEqual([],
                             self.p.getClientRecords(
                                 self.mike, self.console.game.mapName))
        self.assertListEqual([],
                             self.p.getClientRecords(
                                 self.bill, self.console.game.mapName))

    def test_event_client_disconnect(self):
        # WHEN
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.mike,
                                  data={'way_id': '1'}))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_DISCONNECT',
                                  client=self.mike,
                                  data=None))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))

    def test_event_client_team_change(self):
        # WHEN
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.mike,
                                  data={'way_id': '1'}))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.bill,
                                  data={'way_id': '1'}))
        self.mike.team = TEAM_SPEC  # will raise EVT_CLIENT_TEAM_CHANGE
        self.bill.team = TEAM_FREE  # will not raise EVT_CLIENT_TEAM_CHANGE
        # THEN
        self.assertEqual(TEAM_SPEC, self.mike.team)
        self.assertEqual(TEAM_FREE, self.bill.team)
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(True, self.bill.isvar(self.p, 'jumprun'))
        self.assertIsInstance(self.bill.var(self.p, 'jumprun').value, JumpRun)

    ####################################################################################################################
    #                                                                                                                  #
    #   PLUGIN HOOKS                                                                                                   #
    #                                                                                                                  #
    ####################################################################################################################

    def test_plugin_disable(self):
        # WHEN
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.mike,
                                  data={'way_id': '1'}))
        self.console.queueEvent(
            self.console.getEvent('EVT_CLIENT_JUMP_RUN_START',
                                  client=self.bill,
                                  data={'way_id': '1'}))
        self.p.disable()
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(False, self.bill.isvar(self.p, 'jumprun'))

    def test_plugin_enable(self):
        # GIVEN
        self.p.console.write = Mock()
        self.p.disable()
        self.p._cycle_count = 0
        self.console.game.mapName = 'ut4_casa'
        # WHEN
        self.p.enable()
        self.p.console.write.assert_called_with('cyclemap')
        self.assertEqual(1, self.p._cycle_count)
class Test_events(JumperTestCase):

    def setUp(self):
        JumperTestCase.setUp(self)
        with logging_disabled():
            from b3.fake import FakeClient

        # create some clients
        self.mike = FakeClient(console=self.console, name="Mike", guid="mikeguid", team=TEAM_FREE, groupBits=1)
        self.bill = FakeClient(console=self.console, name="Bill", guid="billguid", team=TEAM_FREE, groupBits=1)
        self.mike.connects("1")
        self.bill.connects("2")

        # force fake mapname
        self.console.game.mapName = 'ut42_bstjumps_u2'

    def tearDown(self):
        self.mike.disconnects()
        self.bill.disconnects()
        JumperTestCase.tearDown(self)

    ####################################################################################################################
    #                                                                                                                  #
    #   JUMPRUN EVENTS                                                                                                 #
    #                                                                                                                  #
    ####################################################################################################################

    def test_event_client_jumprun_started(self):
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'}))
        # THEN
        self.assertEqual(True, self.mike.isvar(self.p, 'jumprun'))
        self.assertIsNone(self.mike.var(self.p, 'jumprun').value.demo)
        self.assertIsInstance(self.mike.var(self.p, 'jumprun').value, JumpRun)

    def test_event_client_jumprun_stopped(self):
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={'way_id' : '1', 'way_time' : '12345'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertListEqual([], self.p.getClientRecords(self.mike, self.console.game.mapName))

    def test_event_client_jumprun_canceled(self):
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_CANCEL', client=self.mike, data={'way_id' : '1'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertListEqual([], self.p.getClientRecords(self.mike, self.console.game.mapName))

    def test_event_client_jumprun_started_stopped(self):
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={'way_id' : '1', 'way_time' : '12345'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(1, len(self.p.getClientRecords(self.mike, self.console.game.mapName)))

    def test_event_client_jumprun_started_canceled(self):
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_CANCEL', client=self.mike, data={'way_id' : '1'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(0, len(self.p.getClientRecords(self.mike, self.console.game.mapName)))

    def test_event_client_jumprun_started_stopped_multiple_clients(self):
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={'way_id' : '1', 'way_time' : '12345'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id' : '1'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.bill, data={'way_id' : '1', 'way_time' : '12345'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(False, self.bill.isvar(self.p, 'jumprun'))
        self.assertEqual(1, len(self.p.getClientRecords(self.mike, self.console.game.mapName)))
        self.assertEqual(1, len(self.p.getClientRecords(self.bill, self.console.game.mapName)))

    def test_event_client_jumprun_started_stopped_multiple_ways(self):
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={'way_id' : '1', 'way_time' : '12345'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '2'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={'way_id' : '2', 'way_time' : '12345'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(2, len(self.p.getClientRecords(self.mike, self.console.game.mapName)))

    def test_event_client_jumprun_started_stopped_multiple_clients_multiple_ways(self):
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={'way_id' : '1', 'way_time' : '12345'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '2'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.mike, data={'way_id' : '2', 'way_time' : '12345'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id' : '1'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.bill, data={'way_id' : '1', 'way_time' : '12345'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id' : '2'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_STOP', client=self.bill, data={'way_id' : '2', 'way_time' : '12345'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(False, self.bill.isvar(self.p, 'jumprun'))
        self.assertEqual(2, len(self.p.getClientRecords(self.mike, self.console.game.mapName)))
        self.assertEqual(2, len(self.p.getClientRecords(self.bill, self.console.game.mapName)))

    ####################################################################################################################
    #                                                                                                                  #
    #   OTHER EVENTS                                                                                                   #
    #                                                                                                                  #
    ####################################################################################################################

    def test_event_game_map_change(self):
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id' : '1'}))
        self.console.queueEvent(self.console.getEvent('EVT_GAME_MAP_CHANGE', data='\sv_allowdownload\0\g_matchmode\0\g_gametype\9\sv_maxclients\32\sv_floodprotect\1'))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(False, self.bill.isvar(self.p, 'jumprun'))
        self.assertListEqual([], self.p.getClientRecords(self.mike, self.console.game.mapName))
        self.assertListEqual([], self.p.getClientRecords(self.bill, self.console.game.mapName))

    def test_event_client_disconnect(self):
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_DISCONNECT', client=self.mike, data=None))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))

    def test_event_client_team_change(self):
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id' : '1'}))
        self.mike.team = TEAM_SPEC  # will raise EVT_CLIENT_TEAM_CHANGE
        self.bill.team = TEAM_FREE  # will not raise EVT_CLIENT_TEAM_CHANGE
        # THEN
        self.assertEqual(TEAM_SPEC, self.mike.team)
        self.assertEqual(TEAM_FREE, self.bill.team)
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(True, self.bill.isvar(self.p, 'jumprun'))
        self.assertIsInstance(self.bill.var(self.p, 'jumprun').value, JumpRun)

    ####################################################################################################################
    #                                                                                                                  #
    #   PLUGIN HOOKS                                                                                                   #
    #                                                                                                                  #
    ####################################################################################################################

    def test_plugin_disable(self):
        # WHEN
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(self.console.getEvent('EVT_CLIENT_JUMP_RUN_START', client=self.bill, data={'way_id' : '1'}))
        self.p.disable()
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(False, self.bill.isvar(self.p, 'jumprun'))

    def test_plugin_enable(self):
        # GIVEN
        self.p.console.write = Mock()
        self.p.disable()
        self.p._cycle_count = 0
        self.console.game.mapName = 'ut4_casa'
        # WHEN
        self.p.enable()
        self.p.console.write.assert_called_with('cyclemap')
        self.assertEqual(1, self.p._cycle_count)
class Test_events(JumperTestCase):

    def setUp(self):

        JumperTestCase.setUp(self)

        with logging_disabled():
            from b3.fake import FakeClient

        # prevent the test to query the api: we handle this somewhere else
        when(self.p).getMapsData().thenReturn(dict())

        # create some clients
        self.mike = FakeClient(console=self.console, name="Mike", guid="mikeguid", team=TEAM_FREE, groupBits=1)
        self.bill = FakeClient(console=self.console, name="Bill", guid="billguid", team=TEAM_FREE, groupBits=1)
        self.mike.connects("1")
        self.bill.connects("2")

        # force fake mapname
        self.console.game.mapName = 'ut42_bstjumps_u2'

    def tearDown(self):
        self.mike.disconnects()
        self.bill.disconnects()
        JumperTestCase.tearDown(self)

    ####################################################################################################################
    ##                                                                                                                ##
    ##   JUMPRUN EVENTS                                                                                               ##
    ##                                                                                                                ##
    ####################################################################################################################

    def test_event_client_jumprun_started(self):
        # WHEN
        event = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START')
        self.console.queueEvent(Event(event, client=self.mike, data={'way_id' : '1'}))
        # THEN
        self.assertEqual(True, self.mike.isvar(self.p, 'jumprun'))
        self.assertIsNone(self.mike.var(self.p, 'jumprun').value.demo)
        self.assertIsInstance(self.mike.var(self.p, 'jumprun').value, JumpRun)

    def test_event_client_jumprun_stopped(self):
        # WHEN
        event = self.console.getEventID('EVT_CLIENT_JUMP_RUN_STOP')
        self.console.queueEvent(Event(event, client=self.mike, data={'way_id' : '1', 'way_time' : '12345'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertListEqual([], self.p.getClientRecords(self.mike, self.console.game.mapName))

    def test_event_client_jumprun_canceled(self):
        # WHEN
        event = self.console.getEventID('EVT_CLIENT_JUMP_RUN_CANCEL')
        self.console.queueEvent(Event(event, client=self.mike, data={'way_id' : '1'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertListEqual([], self.p.getClientRecords(self.mike, self.console.game.mapName))

    def test_event_client_jumprun_started_stopped(self):
        # WHEN
        event1 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START')
        event2 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_STOP')
        self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(Event(event2, client=self.mike, data={'way_id' : '1', 'way_time' : '12345'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(1, len(self.p.getClientRecords(self.mike, self.console.game.mapName)))

    def test_event_client_jumprun_started_canceled(self):
        # WHEN
        event1 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START')
        event2 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_CANCEL')
        self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(Event(event2, client=self.mike, data={'way_id' : '1'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(0, len(self.p.getClientRecords(self.mike, self.console.game.mapName)))

    def test_event_client_jumprun_started_stopped_multiple_clients(self):
        # WHEN
        event1 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START')
        event2 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_STOP')
        self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(Event(event2, client=self.mike, data={'way_id' : '1', 'way_time' : '12345'}))
        self.console.queueEvent(Event(event1, client=self.bill, data={'way_id' : '1'}))
        self.console.queueEvent(Event(event2, client=self.bill, data={'way_id' : '1', 'way_time' : '12345'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(False, self.bill.isvar(self.p, 'jumprun'))
        self.assertEqual(1, len(self.p.getClientRecords(self.mike, self.console.game.mapName)))
        self.assertEqual(1, len(self.p.getClientRecords(self.bill, self.console.game.mapName)))

    def test_event_client_jumprun_started_stopped_multiple_ways(self):
        # WHEN
        event1 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START')
        event2 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_STOP')
        self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(Event(event2, client=self.mike, data={'way_id' : '1', 'way_time' : '12345'}))
        self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '2'}))
        self.console.queueEvent(Event(event2, client=self.mike, data={'way_id' : '2', 'way_time' : '12345'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(2, len(self.p.getClientRecords(self.mike, self.console.game.mapName)))

    def test_event_client_jumprun_started_stopped_multiple_clients_multiple_ways(self):
        # WHEN
        event1 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START')
        event2 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_STOP')
        self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(Event(event2, client=self.mike, data={'way_id' : '1', 'way_time' : '12345'}))
        self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '2'}))
        self.console.queueEvent(Event(event2, client=self.mike, data={'way_id' : '2', 'way_time' : '12345'}))
        self.console.queueEvent(Event(event1, client=self.bill, data={'way_id' : '1'}))
        self.console.queueEvent(Event(event2, client=self.bill, data={'way_id' : '1', 'way_time' : '12345'}))
        self.console.queueEvent(Event(event1, client=self.bill, data={'way_id' : '2'}))
        self.console.queueEvent(Event(event2, client=self.bill, data={'way_id' : '2', 'way_time' : '12345'}))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(False, self.bill.isvar(self.p, 'jumprun'))
        self.assertEqual(2, len(self.p.getClientRecords(self.mike, self.console.game.mapName)))
        self.assertEqual(2, len(self.p.getClientRecords(self.bill, self.console.game.mapName)))

    ####################################################################################################################
    ##                                                                                                                ##
    ##   OTHER EVENTS                                                                                                 ##
    ##                                                                                                                ##
    ####################################################################################################################

    def test_event_game_map_change(self):
        # WHEN
        event1 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START')
        event2 = self.console.getEventID('EVT_GAME_MAP_CHANGE')
        self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(Event(event1, client=self.bill, data={'way_id' : '1'}))
        self.console.queueEvent(Event(event2, data='\sv_allowdownload\0\g_matchmode\0\g_gametype\9\sv_maxclients\32\sv_floodprotect\1'))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(False, self.bill.isvar(self.p, 'jumprun'))
        self.assertListEqual([], self.p.getClientRecords(self.mike, self.console.game.mapName))
        self.assertListEqual([], self.p.getClientRecords(self.bill, self.console.game.mapName))

    def test_event_client_disconnect(self):
        # WHEN
        event1 = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START')
        event2 = self.console.getEventID('EVT_CLIENT_DISCONNECT')
        self.console.queueEvent(Event(event1, client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(Event(event2, client=self.mike, data=None))
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))

    def test_event_client_team_change(self):
        # WHEN
        event = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START')
        self.console.queueEvent(Event(event, client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(Event(event, client=self.bill, data={'way_id' : '1'}))
        self.mike.team = TEAM_SPEC  # will raise EVT_CLIENT_TEAM_CHANGE
        self.bill.team = TEAM_FREE  # will not raise EVT_CLIENT_TEAM_CHANGE
        # THEN
        self.assertEqual(TEAM_SPEC, self.mike.team)
        self.assertEqual(TEAM_FREE, self.bill.team)
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(True, self.bill.isvar(self.p, 'jumprun'))
        self.assertIsInstance(self.bill.var(self.p, 'jumprun').value, JumpRun)

    ####################################################################################################################
    ##                                                                                                                ##
    ##   PLUGIN HOOKS                                                                                                 ##
    ##                                                                                                                ##
    ####################################################################################################################

    @unittest2.skipUnless(HAS_ENABLE_DISABLE_HOOKS, "B3 %s doesn't provide onDisable() plugin hook" % b3_version)
    def test_plugin_disable(self):
        # WHEN
        event = self.console.getEventID('EVT_CLIENT_JUMP_RUN_START')
        self.console.queueEvent(Event(event, client=self.mike, data={'way_id' : '1'}))
        self.console.queueEvent(Event(event, client=self.bill, data={'way_id' : '1'}))
        self.p.disable()
        # THEN
        self.assertEqual(False, self.mike.isvar(self.p, 'jumprun'))
        self.assertEqual(False, self.bill.isvar(self.p, 'jumprun'))

    @unittest2.skipUnless(HAS_ENABLE_DISABLE_HOOKS, "B3 %s doesn't provide onEnable() plugin hook" % b3_version)
    def test_plugin_enable(self):
        # GIVEN
        self.p.console.write = Mock()
        self.p.disable()
        self.p.settings['cycle_count'] = 0
        self.console.game.mapName = 'ut4_casa'
        # WHEN
        self.p.enable()
        self.p.console.write.assert_called_with('cyclemap')
        self.assertEqual(1, self.p.settings['cycle_count'])