示例#1
0
class MockUser:
    def __init__(self,name):
        self.client=HttpClient()
        User.objects.create_user(username=name,
            email='*****@*****.**',password='******')
        self.client.force_login(User.objects.get(username=name))
        self.client.send_and_consume('websocket.connect',path='/')
示例#2
0
class QueueSocketsBasicTestCase(ChannelTestCase):
    '''
	Basic Websocket Function tests
	'''
    def setUp(self):
        self.test_user = User.objects.create(username='******')
        self.client = HttpClient()
        # Authenticate user
        self.client.force_login(user=self.test_user)
        self.test_player = Player.objects.create(user=self.test_user,
                                                 username='******')
        self.test_game_player = GamePlayer.objects.get(
            user_acct=self.test_player)
        self.test_team = Team.objects.create(name="TESTER",
                                             description='LOL',
                                             captain=self.test_game_player)

        self.endpoint_path = '/%s/sockets/status/TESTER/' % GAME_NAME

    def test_connect_and_disconnect(self):
        '''
		Test if connect/disconnect events are reflected in database
		'''
        # Craft message with predictable reply channel & send
        con_message = {
            'text': json.dumps({'team_name': 'TESTER'}),
            'reply_channel': 'test-queue-receive'
        }
        self.client.send_and_consume(channel='websocket.connect',
                                     path=self.endpoint_path,
                                     content=con_message)
        con_response = json.loads(
            self.get_next_message('test-queue-receive', require=True)['text'])
        con_mm_status = GamePlayer.objects.get(
            user_acct=self.test_player).status

        dis_message = {
            'text': json.dumps({'team_name': 'TESTER'}),
            'reply_channel': 'test-queue-receive'
        }
        self.client.send_and_consume(channel='websocket.disconnect',
                                     path=self.endpoint_path,
                                     content=con_message)
        dis_response = json.loads(
            self.get_next_message('test-queue-receive', require=True)['text'])
        dis_mm_status = GamePlayer.objects.get(
            user_acct=self.test_player).status

        try:
            # Was user logged in to queue?
            self.assertEqual(con_response['status'], ONLINE)
            self.assertEqual(con_mm_status.state, ONLINE)
            # Was the correct action returned?
            self.assertEqual(con_response['action'],
                             'team-queue-player-update')
        except AssertionError:
            print(
                "[WS_QUEUE] Connect function failed! [%s] action resulted in status [%s]"
                % (
                    con_response['action'],
                    con_response['status'],
                ))

        try:
            # Was user logged out of queue?
            self.assertEqual(dis_response['status'], OFFLINE)
            self.assertEqual(dis_mm_status.state, OFFLINE)
            # Was correct action returned?
            self.assertEqual(dis_response['action'], con_response['action'])
        except AssertionError:
            print(
                "[WS_QUEUE] Disconnect function failed! [%s] action resulted in status [%s]"
                % (
                    dis_response['action'],
                    dis_response['status'],
                ))

    def test_player_can_toggle_ready(self):
        '''
		Test if the server can receive and ready a new player up
		'''
        message = {
            'text': json.dumps({'team_name': 'TESTER'}),
            'reply_channel': 'test-queue-receive'
        }
        self.client.send_and_consume(channel='websocket.connect',
                                     path=self.endpoint_path,
                                     content=message)
        # Flush queued message
        self.get_next_message('test-queue-receive', require=True)

        self.client.send_and_consume(channel='websocket.receive',
                                     path=self.endpoint_path,
                                     content=message)
        rdy_response = json.loads(
            self.get_next_message('test-queue-receive', require=True)['text'])
        rdy_mm_status = GamePlayer.objects.get(
            user_acct=self.test_player).status

        self.client.send_and_consume(channel='websocket.receive',
                                     path=self.endpoint_path,
                                     content=message)
        un_rdy_response = json.loads(
            self.get_next_message('test-queue-receive', require=True)['text'])
        un_rdy_mm_status = GamePlayer.objects.get(
            user_acct=self.test_player).status

        try:
            self.assertEqual(rdy_response['status'], READY)
            self.assertEqual(rdy_response['action'],
                             'team-queue-player-update')
            self.assertEqual(rdy_mm_status.current_team, self.test_team)
        except AssertionError:
            print(
                "[WS_QUEUE] Ready Up function failed: [%s] action resulted in status [%s] bound to team [%s]"
                % (rdy_response['action'], rdy_response['status'],
                   rdy_mm_status.current_team.name))

        try:
            self.assertEqual(un_rdy_response['status'], ONLINE)
            self.assertEqual(un_rdy_response['action'],
                             'team-queue-player-update')
            self.assertNotEqual(un_rdy_mm_status.current_team, self.test_team)
        except AssertionError:
            print(
                "[WS_QUEUE] Reverse Ready Up function failed: [%s] action resulted in status [%s] bound to team [%s]"
                % (rdy_response['action'], rdy_response['status'],
                   rdy_mm_status.current_team.name))

    def test_unready_cannot_queue(self):
        '''
		Ensure ready matchmaking constraints are enforced on unready team
		'''
        try:
            # Test if team is unready
            self.assertFalse(
                mm_can_team_queue(self.test_game_player,
                                  self.test_team.players))
        except AssertionError:
            print('[MM RULES] Unready team allowed into queue')