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, }))
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, }))
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, }))
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', } }))
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, }))
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()
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()
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', }, ], }))
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, }))
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, }))
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, }))
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', } }))