Esempio n. 1
0
    async def test_request_write_async(self):
        with asynctest.patch('synse_server.cmd.write_async') as mock_cmd:
            with asynctest.patch(
                    'websockets.WebSocketCommonProtocol.send') as mock_send:
                mock_cmd.return_value = {
                    'key': 'value',
                }

                p = make_payload(data={
                    'device': '123',
                    'payload': {
                        'action': 'foo'
                    }
                })
                m = websocket.MessageHandler(
                    websockets.WebSocketCommonProtocol())
                await m.handle_request_write_async(p)

        mock_cmd.assert_called_once()
        mock_cmd.assert_called_with(device_id='123', payload={'action': 'foo'})
        mock_send.assert_called_once()
        mock_send.assert_called_with(
            json.dumps({
                'id': 'testing',
                'event': 'response/transaction_info',
                'data': mock_cmd.return_value,
            }))
Esempio n. 2
0
    async def test_request_tags_data_ids(self):
        with asynctest.patch('synse_server.cmd.tags') as mock_cmd:
            with asynctest.patch(
                    'websockets.WebSocketCommonProtocol.send') as mock_send:
                mock_cmd.return_value = [
                    'foo/bar',
                    'vapor:io',
                ]

                p = make_payload(data={'ids': True})
                m = websocket.MessageHandler(
                    websockets.WebSocketCommonProtocol())
                await m.handle_request_tags(p)

        mock_cmd.assert_called_once()
        mock_cmd.assert_called_with(
            [],
            with_id_tags=True,
        )
        mock_send.assert_called_once()
        mock_send.assert_called_with(
            json.dumps({
                'id': 'testing',
                'event': 'response/tags',
                'data': mock_cmd.return_value,
            }))
Esempio n. 3
0
    async def test_request_read_data_tags_multiple_groups(self):
        with asynctest.patch('synse_server.cmd.read') as mock_cmd:
            with asynctest.patch(
                    'websockets.WebSocketCommonProtocol.send') as mock_send:
                mock_cmd.return_value = [{
                    'key': 'value',
                }]

                p = make_payload(data={'tags': [['foo', 'bar'], ['baz']]})
                m = websocket.MessageHandler(
                    websockets.WebSocketCommonProtocol())
                await m.handle_request_read(p)

        mock_cmd.assert_called_once()
        mock_cmd.assert_called_with(
            ns='default',
            tag_groups=[['foo', 'bar'], ['baz']],
        )
        mock_send.assert_called_once()
        mock_send.assert_called_with(
            json.dumps({
                'id': 'testing',
                'event': 'response/reading',
                'data': mock_cmd.return_value,
            }))
Esempio n. 4
0
    async def test_response_error(self):
        with asynctest.patch(
                'synse_server.api.websocket.MessageHandler.handle_request_status'
        ) as mock_handler:  # noqa: E501
            mock_handler.side_effect = ValueError('test error')
            with asynctest.patch(
                    'websockets.WebSocketCommonProtocol.send') as mock_send:

                p = websocket.Payload(
                    json.dumps(dict(
                        id=4,
                        event='request/status',
                        data={},
                    )))
                m = websocket.MessageHandler(
                    websockets.WebSocketCommonProtocol())
                await m.dispatch(p)

            mock_send.assert_called_once()
            mock_send.assert_called_with(
                json.dumps({
                    'id': 4,
                    'event': 'response/error',
                    'data': {
                        'http_code': 500,
                        'description': 'An unexpected error occurred.',
                        'timestamp': '2019-04-22T13:30:00Z',
                        'context': 'test error',
                    }
                }))
Esempio n. 5
0
    async def test_request_scan_data_tags_multiple_groups(self):
        with asynctest.patch('synse_server.cmd.scan') as mock_cmd:
            with asynctest.patch(
                    'websockets.WebSocketCommonProtocol.send') as mock_send:
                mock_cmd.return_value = [{
                    'key': 'value',
                }]

                p = make_payload(
                    data={'tags': [['ns/ann:lab', 'foo'], ['bar']]})
                m = websocket.MessageHandler(
                    websockets.WebSocketCommonProtocol())
                await m.handle_request_scan(p)

        mock_cmd.assert_called_once()
        mock_cmd.assert_called_with(
            ns='default',
            tag_groups=[['ns/ann:lab', 'foo'], ['bar']],
            sort='plugin,sortIndex,id',
            force=False,
        )
        mock_send.assert_called_once()
        mock_send.assert_called_with(
            json.dumps({
                'id': 'testing',
                'event': 'response/device_summary',
                'data': mock_cmd.return_value,
            }))
Esempio n. 6
0
 def switch_protocols():
     ws_proto = websockets.WebSocketCommonProtocol()
     # Disconnect transport from http_proto and connect it to ws_proto.
     http_proto.transport = DummyTransport()
     transport._protocol = ws_proto
     ws_proto.connection_made(transport)
     # Run the WebSocket handler in an asyncio Task.
     asyncio.Task(run_ws_handler(ws_proto))
Esempio n. 7
0
            def switch_protocols():
                # TODO: Determine if there is a more standard way to do this
                ws_protocol = websockets.WebSocketCommonProtocol()
                transport = request.environ['async.writer'].transport

                http_protocol = request.environ['async.protocol']
                http_protocol.connection_lost(None)

                transport._protocol = ws_protocol
                ws_protocol.connection_made(transport)
                asyncio.async(_ensure_ws_close(ws_protocol))
Esempio n. 8
0
        def switch_protocols():
            # Switch transport from http_protocol to ws_protocol (YOLO).
            ws_protocol = websockets.WebSocketCommonProtocol()
            transport._protocol = ws_protocol
            ws_protocol.connection_made(transport)

            # Ensure aiohttp doesn't interfere.
            http_protocol.transport = None

            # Fire'n'forget the WebSocket handler.
            asyncio.async(run_ws_handler(ws_protocol))
async def test_ensure_open_exception(client, socket_state, monkeypatch,
                                     mocker):

    mocker.patch('asyncio.shield', AsyncMock(return_value=MagicMock()))
    client = ProtobufClient(websockets.WebSocketCommonProtocol())
    client._socket.close_code = 1
    client._socket.close_reason = "Close reason"
    client._socket.close_connection_task = MagicMock()
    client._socket.state = socket_state

    with pytest.raises(
        (websockets.ConnectionClosedError, websockets.InvalidState)):
        await client._get_obfuscated_private_ip()
Esempio n. 10
0
    async def test_response(self):
        with asynctest.patch(
                'synse_server.api.websocket.MessageHandler.handle_request_status'
        ) as mock_handler:  # noqa: E501

            p = websocket.Payload(
                json.dumps(dict(
                    id=2,
                    event='request/status',
                    data={},
                )))
            m = websocket.MessageHandler(websockets.WebSocketCommonProtocol())
            await m.dispatch(p)

        mock_handler.assert_called_once()
Esempio n. 11
0
    async def test_request_read_device_error(self):
        with asynctest.patch('synse_server.cmd.read_device') as mock_cmd:
            with asynctest.patch(
                    'websockets.WebSocketCommonProtocol.send') as mock_send:
                mock_cmd.return_value = {
                    'key': 'value',
                }

                p = make_payload(data={})
                m = websocket.MessageHandler(
                    websockets.WebSocketCommonProtocol())
                with pytest.raises(errors.InvalidUsage):
                    await m.handle_request_read_device(p)

        mock_cmd.assert_not_called()
        mock_send.assert_not_called()
Esempio n. 12
0
    async def test_request_read_cache_data_end(self):
        # Need to define a side-effect function for the test rather than utilizing
        # asynctest's implicit behavior for iterable side_effects because the function
        # we are mocking (cmd.read_cache) is an async generator, and the implicit
        # handling via asynctest does not appear to to handle that case well.
        async def mock_read_cache(*args, **kwargs):
            values = [
                {
                    'value': 1,
                    'type': 'temperature',
                },
            ]

            for v in values:
                yield v

        with asynctest.patch('synse_server.cmd.read_cache') as mock_cmd:
            mock_cmd.side_effect = mock_read_cache
            with asynctest.patch(
                    'websockets.WebSocketCommonProtocol.send') as mock_send:

                p = make_payload(data={'end': 'now'})
                m = websocket.MessageHandler(
                    websockets.WebSocketCommonProtocol())
                await m.handle_request_read_cache(p)

        mock_cmd.assert_called_once()
        mock_cmd.assert_called_with(
            start=None,
            end='now',
        )
        mock_send.assert_called_once()
        mock_send.assert_called_with(
            json.dumps({
                'id': 'testing',
                'event': 'response/reading',
                'data': [
                    {
                        'value': 1,
                        'type': 'temperature',
                    },
                ],
            }))
Esempio n. 13
0
    async def test_request_plugin_health(self):
        with asynctest.patch('synse_server.cmd.plugin_health') as mock_cmd:
            with asynctest.patch(
                    'websockets.WebSocketCommonProtocol.send') as mock_send:
                mock_cmd.return_value = {
                    'key': 'value',
                }

                p = make_payload(data={})
                m = websocket.MessageHandler(
                    websockets.WebSocketCommonProtocol())
                await m.handle_request_plugin_health(p)

        mock_cmd.assert_called_once()
        mock_send.assert_called_once()
        mock_send.assert_called_with(
            json.dumps({
                'id': 'testing',
                'event': 'response/plugin_health',
                'data': mock_cmd.return_value,
            }))
Esempio n. 14
0
    async def test_request_status(self):
        with asynctest.patch('synse_server.cmd.test') as mock_cmd:
            with asynctest.patch(
                    'websockets.WebSocketCommonProtocol.send') as mock_send:
                mock_cmd.return_value = {
                    'status': 'ok',
                    'timestamp': '2019-04-22T13:30:00Z',
                }

                p = make_payload(data={})
                m = websocket.MessageHandler(
                    websockets.WebSocketCommonProtocol())
                await m.handle_request_status(p)

        mock_cmd.assert_called_once()
        mock_send.assert_called_once()
        mock_send.assert_called_with(
            json.dumps({
                'id': 'testing',
                'event': 'response/status',
                'data': mock_cmd.return_value,
            }))
Esempio n. 15
0
    async def test_request_transaction(self):
        with asynctest.patch('synse_server.cmd.transaction') as mock_cmd:
            with asynctest.patch(
                    'websockets.WebSocketCommonProtocol.send') as mock_send:
                mock_cmd.return_value = [{
                    'key': 'value',
                }]

                p = make_payload(data={'transaction': 'foo'})
                m = websocket.MessageHandler(
                    websockets.WebSocketCommonProtocol())
                await m.handle_request_transaction(p)

        mock_cmd.assert_called_once()
        mock_cmd.assert_called_with('foo')
        mock_send.assert_called_once()
        mock_send.assert_called_with(
            json.dumps({
                'id': 'testing',
                'event': 'response/transaction_status',
                'data': mock_cmd.return_value,
            }))
Esempio n. 16
0
    async def test_response_no_handler(self):
        with asynctest.patch(
                'websockets.WebSocketCommonProtocol.send') as mock_send:

            p = websocket.Payload(
                json.dumps(dict(
                    id=3,
                    event='foo/bar',
                    data={},
                )))
            m = websocket.MessageHandler(websockets.WebSocketCommonProtocol())
            await m.dispatch(p)

        mock_send.assert_called_once()
        mock_send.assert_called_with(
            json.dumps({
                'id': 3,
                'event': 'response/error',
                'data': {
                    'description': 'unsupported event type: foo/bar',
                    'timestamp': '2019-04-22T13:30:00Z',
                }
            }))