Ejemplo n.º 1
0
    def test_websockets_demultiplexer_custom_multiplexer(self):

        class MyWebsocketConsumer(websockets.JsonWebsocketConsumer):
            def connect(self, message, multiplexer=None, **kwargs):
                multiplexer.send({"THIS_SHOULD_BE_LOWERCASED": "1"})

        class MyMultiplexer(websockets.WebsocketMultiplexer):
            @classmethod
            def encode_json(cls, content):
                lowered = {
                    "stream": content["stream"],
                    "payload": dict((key.lower(), content["payload"][key]) for key in content["payload"])
                }
                return json.dumps(lowered)

        class Demultiplexer(websockets.WebsocketDemultiplexer):
            multiplexer_class = MyMultiplexer

            consumers = {
                "mystream": MyWebsocketConsumer
            }

        with apply_routes([route_class(Demultiplexer, path='/path/(?P<ID>\d+)')]):
            client = HttpClient()

            client.send_and_consume('websocket.connect', path='/path/1')
            self.assertEqual(client.receive(), {
                "stream": "mystream",
                "payload": {"this_should_be_lowercased": "1"},
            })
Ejemplo n.º 2
0
    def test_websockets_demultiplexer_custom_multiplexer(self):

        class MyWebsocketConsumer(websockets.JsonWebsocketConsumer):
            def connect(self, message, multiplexer=None, **kwargs):
                multiplexer.send({"THIS_SHOULD_BE_LOWERCASED": "1"})

        class MyMultiplexer(websockets.WebsocketMultiplexer):
            @classmethod
            def encode_json(cls, content):
                lowered = {
                    "stream": content["stream"],
                    "payload": dict((key.lower(), content["payload"][key]) for key in content["payload"])
                }
                return json.dumps(lowered)

        class Demultiplexer(websockets.WebsocketDemultiplexer):
            multiplexer_class = MyMultiplexer

            consumers = {
                "mystream": MyWebsocketConsumer
            }

        with apply_routes([route_class(Demultiplexer, path='/path/(?P<ID>\d+)')]):
            client = HttpClient()

            client.send_and_consume('websocket.connect', path='/path/1')
            self.assertEqual(client.receive(), {
                "stream": "mystream",
                "payload": {"this_should_be_lowercased": "1"},
            })
Ejemplo n.º 3
0
    def test_simple_as_route_method(self):

        class WebsocketConsumer(websockets.WebsocketConsumer):

            def connect(self, message, **kwargs):
                self.message.reply_channel.send({'accept': True})
                self.send(text=message.get('order'))

        routes = [
            WebsocketConsumer.as_route(attrs={"strict_ordering": True}, path='^/path$'),
            WebsocketConsumer.as_route(path='^/path/2$'),
        ]

        self.assertIsNot(routes[0].consumer, WebsocketConsumer)
        self.assertIs(routes[1].consumer, WebsocketConsumer)

        with apply_routes(routes):
            client = HttpClient()

            client.send('websocket.connect', {'path': '/path', 'order': 1})
            client.send('websocket.connect', {'path': '/path', 'order': 0})
            client.consume('websocket.connect', check_accept=False)
            client.consume('websocket.connect')
            self.assertEqual(client.receive(json=False), 0)
            client.consume('websocket.connect')
            self.assertEqual(client.receive(json=False), 1)

            client.send_and_consume('websocket.connect', {'path': '/path/2', 'order': 'next'})
            self.assertEqual(client.receive(json=False), 'next')
Ejemplo n.º 4
0
    def test_simple_as_route_method(self):

        class WebsocketConsumer(websockets.WebsocketConsumer):

            def connect(self, message, **kwargs):
                self.message.reply_channel.send({'accept': True})
                self.send(text=message.get('order'))

        routes = [
            WebsocketConsumer.as_route(attrs={"strict_ordering": True}, path='^/path$'),
            WebsocketConsumer.as_route(path='^/path/2$'),
        ]

        self.assertIsNot(routes[0].consumer, WebsocketConsumer)
        self.assertIs(routes[1].consumer, WebsocketConsumer)

        with apply_routes(routes):
            client = HttpClient()

            client.send('websocket.connect', {'path': '/path', 'order': 1})
            client.send('websocket.connect', {'path': '/path', 'order': 0})
            client.consume('websocket.connect', check_accept=False)
            client.consume('websocket.connect')
            self.assertEqual(client.receive(json=False), 0)
            client.consume('websocket.connect')
            self.assertEqual(client.receive(json=False), 1)

            client.send_and_consume('websocket.connect', {'path': '/path/2', 'order': 'next'})
            self.assertEqual(client.receive(json=False), 'next')
Ejemplo n.º 5
0
    def test_inbound_delete(self):
        user = User.objects.create(username='******', email='*****@*****.**')

        class UserBinding(WebsocketBinding):
            model = User
            stream = 'users'
            fields = ['username', ]

            @classmethod
            def group_names(cls, instance):
                return ['users_outbound']

            def has_permission(self, user, action, pk):
                return True

        class Demultiplexer(WebsocketDemultiplexer):
            consumers = {
                'users': UserBinding.consumer,
            }

            groups = ['inbound']

        with apply_routes([Demultiplexer.as_route(path='/')]):
            client = HttpClient()
            client.send_and_consume('websocket.connect', path='/')
            client.send_and_consume('websocket.receive', path='/', text={
                'stream': 'users',
                'payload': {'action': DELETE, 'pk': user.pk}
            })

        self.assertIsNone(User.objects.filter(pk=user.pk).first())
        self.assertIsNone(client.receive())
Ejemplo n.º 6
0
	def test_consumers_functions(self):
		# TO DO
		client = HttpClient()
		user = User.objects.create_user(
			username='******', email='*****@*****.**', password='******')
		client.login(username='******', password='******')

		client2 = HttpClient()
		user = User.objects.create_user(
			username='******', email='*****@*****.**', password='******')
		client.login(username='******', password='******')

		client.send_and_consume('websocket.receive', text='hello')
Ejemplo n.º 7
0
    def test_consumers_functions(self):
        # TO DO
        client = HttpClient()
        user = User.objects.create_user(username='******',
                                        email='*****@*****.**',
                                        password='******')
        client.login(username='******', password='******')

        client2 = HttpClient()
        user = User.objects.create_user(username='******',
                                        email='*****@*****.**',
                                        password='******')
        client.login(username='******', password='******')

        client.send_and_consume('websocket.receive', text='hello')
Ejemplo n.º 8
0
    def test_websocket_custom_json_serialization(self):

        class WebsocketConsumer(websockets.JsonWebsocketConsumer):
            @classmethod
            def decode_json(cls, text):
                obj = json.loads(text)
                return dict((key.upper(), obj[key]) for key in obj)

            @classmethod
            def encode_json(cls, content):
                lowered = dict((key.lower(), content[key]) for key in content)
                return json.dumps(lowered)

            def receive(self, content, multiplexer=None, **kwargs):
                self.content_received = content
                self.send({"RESPONSE": "HI"})

        class MyMultiplexer(websockets.WebsocketMultiplexer):
            @classmethod
            def encode_json(cls, content):
                lowered = dict((key.lower(), content[key]) for key in content)
                return json.dumps(lowered)

        with apply_routes([route_class(WebsocketConsumer, path='/path')]):
            client = HttpClient()

            consumer = client.send_and_consume('websocket.receive', path='/path', text={"key": "value"})
            self.assertEqual(consumer.content_received, {"KEY": "value"})

            self.assertEqual(client.receive(), {"response": "HI"})

            client.join_group('test_group')
            WebsocketConsumer.group_send('test_group', {"KEY": "VALUE"})
            self.assertEqual(client.receive(), {"key": "VALUE"})
Ejemplo n.º 9
0
    def test_websocket_custom_json_serialization(self):

        class WebsocketConsumer(websockets.JsonWebsocketConsumer):
            @classmethod
            def decode_json(cls, text):
                obj = json.loads(text)
                return dict((key.upper(), obj[key]) for key in obj)

            @classmethod
            def encode_json(cls, content):
                lowered = dict((key.lower(), content[key]) for key in content)
                return json.dumps(lowered)

            def receive(self, content, multiplexer=None, **kwargs):
                self.content_received = content
                self.send({"RESPONSE": "HI"})

        class MyMultiplexer(websockets.WebsocketMultiplexer):
            @classmethod
            def encode_json(cls, content):
                lowered = dict((key.lower(), content[key]) for key in content)
                return json.dumps(lowered)

        with apply_routes([route_class(WebsocketConsumer, path='/path')]):
            client = HttpClient()

            consumer = client.send_and_consume('websocket.receive', path='/path', text={"key": "value"})
            self.assertEqual(consumer.content_received, {"KEY": "value"})

            self.assertEqual(client.receive(), {"response": "HI"})

            client.join_group('test_group')
            WebsocketConsumer.group_send('test_group', {"KEY": "VALUE"})
            self.assertEqual(client.receive(), {"key": "VALUE"})
Ejemplo n.º 10
0
    def test_route_params_saved_in_kwargs(self):

        class UserBinding(WebsocketBinding):
            model = User
            stream = 'users'
            fields = ['username', 'email', 'password', 'last_name']

            @classmethod
            def group_names(cls, instance):
                return ['users_outbound']

            def has_permission(self, user, action, pk):
                return True

        class Demultiplexer(WebsocketDemultiplexer):
            consumers = {
                'users': UserBinding.consumer,
            }

            groups = ['inbound']

        with apply_routes([Demultiplexer.as_route(path='/path/(?P<id>\d+)')]):
            client = HttpClient()
            consumer = client.send_and_consume('websocket.connect', path='/path/789')
            self.assertEqual(consumer.kwargs['id'], '789')
Ejemplo n.º 11
0
    def test_inbound_create(self):
        self.assertEqual(User.objects.all().count(), 0)

        class UserBinding(WebsocketBinding):
            model = User
            stream = 'users'
            fields = ['username', 'email', 'password', 'last_name']

            @classmethod
            def group_names(cls, instance):
                return ['users_outbound']

            def has_permission(self, user, action, pk):
                return True

        class Demultiplexer(WebsocketDemultiplexer):
            consumers = {
                'users': UserBinding.consumer,
            }

            groups = ['inbound']

        with apply_routes([Demultiplexer.as_route(path='/')]):
            client = HttpClient()
            client.send_and_consume('websocket.connect', path='/')
            client.send_and_consume('websocket.receive',
                                    path='/',
                                    text={
                                        'stream': 'users',
                                        'payload': {
                                            'action': CREATE,
                                            'data': {
                                                'username': '******',
                                                'email': 'test@user_steam.com'
                                            },
                                        },
                                    })

        self.assertEqual(User.objects.all().count(), 1)
        user = User.objects.all().first()
        self.assertEqual(user.username, 'test_inbound')
        self.assertEqual(user.email, 'test@user_steam.com')

        self.assertIsNone(client.receive())
Ejemplo n.º 12
0
    def test_websockets_http_session_and_channel_session(self):

        class WebsocketConsumer(websockets.WebsocketConsumer):
            http_user_and_session = True

        user_model = get_user_model()
        user = user_model.objects.create_user(username='******', email='*****@*****.**', password='******')

        client = HttpClient()
        client.force_login(user)
        with apply_routes([route_class(WebsocketConsumer, path='/path')]):
            connect = client.send_and_consume('websocket.connect', {'path': '/path'})
            receive = client.send_and_consume('websocket.receive', {'path': '/path'}, text={'key': 'value'})
            disconnect = client.send_and_consume('websocket.disconnect', {'path': '/path'})
        self.assertEqual(
            connect.message.http_session.session_key,
            receive.message.http_session.session_key
        )
        self.assertEqual(
            connect.message.http_session.session_key,
            disconnect.message.http_session.session_key
        )
Ejemplo n.º 13
0
    def test_websocket_demultiplexer_send(self):

        class MyWebsocketConsumer(websockets.JsonWebsocketConsumer):
            def receive(self, content, multiplexer=None, **kwargs):
                self.send(content)

        class Demultiplexer(websockets.WebsocketDemultiplexer):

            consumers = {
                "mystream": MyWebsocketConsumer
            }

        with apply_routes([route_class(Demultiplexer, path='/path/(?P<id>\d+)')]):
            client = HttpClient()

            with self.assertRaises(SendNotAvailableOnDemultiplexer):
                client.send_and_consume('websocket.receive', path='/path/1', text={
                    "stream": "mystream",
                    "payload": {"text_field": "mytext"},
                })

                client.receive()
Ejemplo n.º 14
0
    def test_inbound_create(self):
        self.assertEqual(User.objects.all().count(), 0)

        class UserBinding(WebsocketBinding):
            model = User
            stream = 'users'
            fields = ['username', 'email', 'password', 'last_name']

            @classmethod
            def group_names(cls, instance):
                return ['users_outbound']

            def has_permission(self, user, action, pk):
                return True

        class Demultiplexer(WebsocketDemultiplexer):
            consumers = {
                'users': UserBinding.consumer,
            }

            groups = ['inbound']

        with apply_routes([Demultiplexer.as_route(path='/')]):
            client = HttpClient()
            client.send_and_consume('websocket.connect', path='/')
            client.send_and_consume('websocket.receive', path='/', text={
                'stream': 'users',
                'payload': {
                    'action': CREATE,
                    'data': {'username': '******', 'email': 'test@user_steam.com'},
                },
            })

        self.assertEqual(User.objects.all().count(), 1)
        user = User.objects.all().first()
        self.assertEqual(user.username, 'test_inbound')
        self.assertEqual(user.email, 'test@user_steam.com')

        self.assertIsNone(client.receive())
Ejemplo n.º 15
0
    def test_websocket_demultiplexer_send(self):

        class MyWebsocketConsumer(websockets.JsonWebsocketConsumer):
            def receive(self, content, multiplexer=None, **kwargs):
                self.send(content)

        class Demultiplexer(websockets.WebsocketDemultiplexer):

            consumers = {
                "mystream": MyWebsocketConsumer
            }

        with apply_routes([route_class(Demultiplexer, path='/path/(?P<id>\d+)')]):
            client = HttpClient()

            with self.assertRaises(SendNotAvailableOnDemultiplexer):
                client.send_and_consume('websocket.receive', path='/path/1', text={
                    "stream": "mystream",
                    "payload": {"text_field": "mytext"},
                })

                client.receive()
Ejemplo n.º 16
0
    def test_inbound_update(self):
        user = User.objects.create(username='******', email='*****@*****.**')

        class UserBinding(WebsocketBinding):
            model = User
            stream = 'users'
            fields = ['username', ]

            @classmethod
            def group_names(cls, instance):
                return ['users_outbound']

            def has_permission(self, user, action, pk):
                return True

        class Demultiplexer(WebsocketDemultiplexer):
            consumers = {
                'users': UserBinding.consumer,
            }

            groups = ['inbound']

        with apply_routes([Demultiplexer.as_route(path='/')]):
            client = HttpClient()
            client.send_and_consume('websocket.connect', path='/')
            client.send_and_consume('websocket.receive', path='/', text={
                'stream': 'users',
                'payload': {'action': UPDATE, 'pk': user.pk, 'data': {'username': '******'}}
            })

            user = User.objects.get(pk=user.pk)
            self.assertEqual(user.username, 'test_inbound')
            self.assertEqual(user.email, '*****@*****.**')

            # trying change field that not in binding fields
            client.send_and_consume('websocket.receive', path='/', text={
                'stream': 'users',
                'payload': {'action': UPDATE, 'pk': user.pk, 'data': {'email': '*****@*****.**'}}
            })

            user = User.objects.get(pk=user.pk)
            self.assertEqual(user.username, 'test_inbound')
            self.assertEqual(user.email, '*****@*****.**')

            self.assertIsNone(client.receive())
Ejemplo n.º 17
0
    def test_websockets_demultiplexer(self):

        class MyWebsocketConsumer(websockets.JsonWebsocketConsumer):
            def connect(self, message, multiplexer=None, **kwargs):
                multiplexer.send(kwargs)

            def disconnect(self, message, multiplexer=None, **kwargs):
                multiplexer.send(kwargs)

            def receive(self, content, multiplexer=None, **kwargs):
                multiplexer.send(content)

        class Demultiplexer(websockets.WebsocketDemultiplexer):

            consumers = {
                "mystream": MyWebsocketConsumer
            }

        with apply_routes([route_class(Demultiplexer, path='/path/(?P<id>\d+)')]):
            client = HttpClient()

            client.send_and_consume('websocket.connect', path='/path/1')
            self.assertEqual(client.receive(), {
                "stream": "mystream",
                "payload": {"id": "1"},
            })

            client.send_and_consume('websocket.receive', text={
                "stream": "mystream",
                "payload": {"text_field": "mytext"},
            }, path='/path/1')
            self.assertEqual(client.receive(), {
                "stream": "mystream",
                "payload": {"text_field": "mytext"},
            })

            client.send_and_consume('websocket.disconnect', path='/path/1')
            self.assertEqual(client.receive(), {
                "stream": "mystream",
                "payload": {"id": "1"},
            })
Ejemplo n.º 18
0
    def test_websockets_demultiplexer(self):

        class MyWebsocketConsumer(websockets.JsonWebsocketConsumer):
            def connect(self, message, multiplexer=None, **kwargs):
                multiplexer.send(kwargs)

            def disconnect(self, message, multiplexer=None, **kwargs):
                multiplexer.send(kwargs)

            def receive(self, content, multiplexer=None, **kwargs):
                multiplexer.send(content)

        class Demultiplexer(websockets.WebsocketDemultiplexer):

            consumers = {
                "mystream": MyWebsocketConsumer
            }

        with apply_routes([route_class(Demultiplexer, path='/path/(?P<id>\d+)')]):
            client = HttpClient()

            client.send_and_consume('websocket.connect', path='/path/1')
            self.assertEqual(client.receive(), {
                "stream": "mystream",
                "payload": {"id": "1"},
            })

            client.send_and_consume('websocket.receive', text={
                "stream": "mystream",
                "payload": {"text_field": "mytext"},
            }, path='/path/1')
            self.assertEqual(client.receive(), {
                "stream": "mystream",
                "payload": {"text_field": "mytext"},
            })

            client.send_and_consume('websocket.disconnect', path='/path/1')
            self.assertEqual(client.receive(), {
                "stream": "mystream",
                "payload": {"id": "1"},
            })
Ejemplo n.º 19
0
class WebSocketTests(ChannelTestCase):
    def setUp(self):
        self.client = HttpClient()

    def test_creating_new_todo_items_in_public_todo_list_send_notification(
            self):
        todo_list = TodoList.objects.create(title="Products")

        self.client.send_and_consume(
            'websocket.connect',
            path='/api/ws/',
            content={'query_string': 'todo_list={}'.format(str(todo_list.id))},
        )

        TodoItem.objects.create(title="test_item", todo_list=todo_list)

        received = self.client.receive()

        self.assertNotEquals(received, None)

    def test_can_subscribe_to_read_only_todo_list(self):
        todo_list = TodoList.objects.create(title="Products",
                                            mode=TodoList.ALLOW_READ)

        self.client.send_and_consume(
            'websocket.connect',
            path='/api/ws/',
            content={'query_string': 'todo_list={}'.format(str(todo_list.id))},
        )

        TodoItem.objects.create(title="test_item", todo_list=todo_list)

        received = self.client.receive()
        self.assertNotEquals(received, None)

    def test_cant_subscribe_to_private_todo_list(self):
        todo_list = TodoList.objects.create(title="Products",
                                            mode=TodoList.PRIVATE)

        self.client.send_and_consume(
            'websocket.connect',
            path='/api/ws/',
            content={'query_string': 'todo_list={}'.format(str(todo_list.id))},
        )

        TodoItem.objects.create(title="test_item", todo_list=todo_list)

        received = self.client.receive()
        self.assertEquals(received, None)

    def test_cant_add_new_todo_item_by_ws(self):
        todo_list = TodoList.objects.create(title="Products")
        self.client.join_group(str(todo_list.id))

        payload = {
            'stream': 'todo_item',
            'payload': {
                'data': {
                    'title': 'test_item',
                    'todo_list': str(todo_list.id)
                },
                'action': 'create',
            }
        }

        self.client.send_and_consume('websocket.receive',
                                     path='/api/ws/',
                                     text=payload)

        self.assertEqual(TodoItem.objects.count(), 0)

    def test_todo_lists_have_separated_notifications(self):
        todo_list1 = TodoList.objects.create(title="Homework")
        todo_list2 = TodoList.objects.create(title="Products")

        # connect to todo_list1
        self.client.join_group(str(todo_list1.id))
        TodoItem.objects.create(title="brush teeth", todo_list=todo_list1)

        # get messages from todo_list1
        received = self.client.receive()
        self.assertNotEquals(received, None)

        # not get messages from todo_list2
        TodoItem.objects.create(title="milk", todo_list=todo_list2)
        received = self.client.receive()
        self.assertEquals(received, None)

    def test_if_not_connected_to_todo_list_not_get_notifications(self):
        todo_list = TodoList.objects.create(title="Products")

        self.client.send_and_consume('websocket.connect', path='/api/ws/')
        TodoItem.objects.create(title="test_item", todo_list=todo_list)

        received = self.client.receive()
        self.assertEquals(received, None)

    def test_get_notification_from_watched_not_private_todo_lists_and_own_private(
            self):
        user = UserModel.objects.create(username='******')
        todo_list_private_own = TodoList.objects.create(title="Private own",
                                                        mode=TodoList.PRIVATE,
                                                        owner=user)
        todo_list_private = TodoList.objects.create(title="Private",
                                                    mode=TodoList.PRIVATE)
        todo_list_read_only = TodoList.objects.create(title="Read only",
                                                      mode=TodoList.ALLOW_READ)
        todo_list_public = TodoList.objects.create(
            title="Public", mode=TodoList.ALLOW_FULL_ACCESS)

        [
            Watch.objects.create(user=user, todo_list=todo_list)
            for todo_list in TodoList.objects.all()
        ]

        self.client.force_login(user)
        self.client.send_and_consume('websocket.connect', path='/api/ws/')

        TodoItem.objects.create(title="test_item1",
                                todo_list=todo_list_private_own)
        received = self.client.receive()
        self.assertNotEquals(received, None)

        TodoItem.objects.create(title="test_item2",
                                todo_list=todo_list_private)
        received = self.client.receive()
        self.assertEquals(received, None)

        TodoItem.objects.create(title="test_item3",
                                todo_list=todo_list_read_only)
        received = self.client.receive()
        self.assertNotEquals(received, None)

        TodoItem.objects.create(title="test_item4", todo_list=todo_list_public)
        received = self.client.receive()
        self.assertNotEquals(received, None)
Ejemplo n.º 20
0
Archivo: tests.py Proyecto: vubon/uber
 def test_passenger_can_connect_via_websockets(self):
     client = HttpClient()
     client.login(username=self.passenger.username, password='******')
     client.send_and_consume('websocket.connect', path='/passenger/')
     message = client.receive()
     self.assertIsNone(message)
Ejemplo n.º 21
0
 def connect_as_driver(self, driver):
     client = HttpClient()
     client.login(username=driver.username, password=PASSWORD)
     client.send_and_consume('websocket.connect', path='/driver/')
     return client