def test_base_consumer(self): class Consumers(BaseConsumer): method_mapping = { 'test.create': 'create', 'test.test': 'test', } def create(self, message, **kwargs): self.called = 'create' def test(self, message, **kwargs): self.called = 'test' with apply_routes([route_class(Consumers)]): client = Client() # check that methods for certain channels routes successfully self.assertEqual( client.send_and_consume('test.create').called, 'create') self.assertEqual( client.send_and_consume('test.test').called, 'test') # send to the channels without routes client.send('test.wrong') message = self.get_next_message('test.wrong') self.assertEqual(client.channel_layer.router.match(message), None) client.send('test') message = self.get_next_message('test') self.assertEqual(client.channel_layer.router.match(message), None)
def test_websockets_consumers_handlers(self): class WebsocketConsumer(websockets.WebsocketConsumer): def connect(self, message, **kwargs): self.called = 'connect' self.id = kwargs['id'] def disconnect(self, message, **kwargs): self.called = 'disconnect' def receive(self, text=None, bytes=None, **kwargs): self.text = text with apply_routes( [route_class(WebsocketConsumer, path='/path/(?P<id>\d+)')]): client = Client() consumer = client.send_and_consume('websocket.connect', {'path': '/path/1'}) self.assertEqual(consumer.called, 'connect') self.assertEqual(consumer.id, '1') consumer = client.send_and_consume('websocket.receive', { 'path': '/path/1', 'text': 'text' }) self.assertEqual(consumer.text, 'text') consumer = client.send_and_consume('websocket.disconnect', {'path': '/path/1'}) self.assertEqual(consumer.called, 'disconnect')
def test_websockets_consumers_handlers(self): class WebsocketConsumer(websockets.WebsocketConsumer): def connect(self, message, **kwargs): self.called = 'connect' self.id = kwargs['id'] def disconnect(self, message, **kwargs): self.called = 'disconnect' def receive(self, text=None, bytes=None, **kwargs): self.text = text with apply_routes([route_class(WebsocketConsumer, path='/path/(?P<id>\d+)')]): client = Client() consumer = client.send_and_consume('websocket.connect', {'path': '/path/1'}) self.assertEqual(consumer.called, 'connect') self.assertEqual(consumer.id, '1') consumer = client.send_and_consume('websocket.receive', {'path': '/path/1', 'text': 'text'}) self.assertEqual(consumer.text, 'text') consumer = client.send_and_consume('websocket.disconnect', {'path': '/path/1'}) self.assertEqual(consumer.called, 'disconnect')
def test_base_consumer(self): class Consumers(BaseConsumer): method_mapping = { 'test.create': 'create', 'test.test': 'test', } def create(self, message, **kwargs): self.called = 'create' def test(self, message, **kwargs): self.called = 'test' with apply_routes([route_class(Consumers)]): client = Client() # check that methods for certain channels routes successfully self.assertEqual(client.send_and_consume('test.create').called, 'create') self.assertEqual(client.send_and_consume('test.test').called, 'test') # send to the channels without routes client.send('test.wrong') message = self.get_next_message('test.wrong') self.assertEqual(client.channel_layer.router.match(message), None) client.send('test') message = self.get_next_message('test') self.assertEqual(client.channel_layer.router.match(message), None)
def test_start_consumer(self): # set up initial room state room = new_room_data( room_id='test', player_number=5, ) for i in range(5): client = Client() username = '******'.format(i) player_data = new_player_data( username=username, reply=client.reply_channel, ready=True, ) room['players'].append(player_data) cache.set('player-room:' + username, 'test') self.clients.append(client) cache.set('room:test', room) client = Client() content = { 'room_id': 'test', } client.send_and_consume('gameplay-start', content) self.flush_all() room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.BIDDING) for player in room['players']: self.assertEqual(len(player['cards']), 10) self.assertEqual(len(room['game']['floor_cards']), 3)
def test_ai_play(self): room = new_room_data( room_id='test', player_number=5, ) for i in range(5): nickname = ['doge', 'bitcoin', 'ethereum', 'egger', 'ha'] create_user(username='******'.format(nickname[i]), password='******', nickname='*AI-{}'.format(nickname[i]), email='*****@*****.**') for i in range(5): room['players'].append(AI(i)) room['game']['state'] = RoomState.BIDDING cache.set('room:test', room) client = Client() client.send_and_consume('gameplay-start', {'room_id': 'test'}) room = cache.get('room:test') while room['game']['state'] is RoomState.BIDDING: client.consume('gameplay-bid', fail_on_none=False) self.flush_ai() room = cache.get('room:test') president = room['game']['president'] client.consume('gameplay-friend-select') room = cache.get('room:test') for _ in range(50): client.consume('gameplay-play', fail_on_none=False) self.flush_ai() room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.RESULT) history = GameHistory.objects.all()[0] self.assertEqual(president, history.president.username)
def test_websockets_consumer(self): # Create a channels test client client = Client() # Send a websocket.connect message, passing in the path so it maps to the right consumer via the routing in routing.py # Consume portion maps that message to a consumer and runs and returns an instance of the consumer connect_consumer = client.send_and_consume('websocket.connect', {'path': '/game/123/'}) connect_reply = client.receive( ) # receive() grabs the content of the next message off of the client's reply_channel self.assertEqual( connect_reply, {'accept': True }) # websocket.connect should return acceptance of connection receive_consumer = client.send_and_consume('websocket.receive', { 'path': '/game/123/', 'text': 'text' }) receive_reply = client.receive( ) # receive() grabs the content of the next message off of the client's reply_channel self.assertEqual(receive_reply, {'text': 'textreply'}) receive_consumer.group_send( '123', text='grouptext' ) # This sends a message out to a group - shortcut off of the Websocket Consumer group_reply = client.receive( ) # receive() grabs the content of the next message off of the client's reply_channel self.assertEqual(group_reply, {'text': 'grouptext'}) disconnect_consumer = client.send_and_consume('websocket.disconnect', {'path': '/game/123/'}) disconnect_consumer.close()
def test_as_route_method(self): class WebsocketConsumer(BaseConsumer): trigger = 'new' def test(self, message, **kwargs): self.message.reply_channel.send({'trigger': self.trigger}) method_mapping = {'mychannel': 'test'} with apply_routes([ WebsocketConsumer.as_route( {'method_mapping': method_mapping, 'trigger': 'from_as_route'}, name='filter', ), ]): client = Client() client.send_and_consume('mychannel', {'name': 'filter'}) self.assertEqual(client.receive(), {'trigger': 'from_as_route'})
def test_as_route_method(self): class WebsocketConsumer(BaseConsumer): trigger = 'new' def test(self, message, **kwargs): self.message.reply_channel.send({'trigger': self.trigger}) method_mapping = {'mychannel': 'test'} with apply_routes([ WebsocketConsumer.as_route( {'method_mapping': method_mapping, 'trigger': 'from_as_route'}, name='filter', ), ]): client = Client() client.send_and_consume('mychannel', {'name': 'filter'}) self.assertEqual(client.receive(), {'trigger': 'from_as_route'})
def test_close_session(self, mock_callback): """Check that 'close_session' is called at disconnect.""" client = Client() client.send_and_consume('websocket.connect', {}) message_content = {'text': json.dumps({"token": self.TEST_TOKEN})} client.send_and_consume('websocket.receive', message_content) client.send_and_consume('websocket.disconnect', {}) mock_callback.assert_called_with(self.TEST_TOKEN)