def test_cloud_calling_handler(mock_client, mock_handle_message, mock_cloud): """Test we call handle message with correct info.""" conn = iot.CloudIoT(mock_cloud) mock_client.receive.return_value = mock_coro( MagicMock(type=WSMsgType.text, json=MagicMock( return_value={ 'msgid': 'test-msg-id', 'handler': 'test-handler', 'payload': 'test-payload' }))) mock_handle_message.return_value = mock_coro('response') mock_client.send_json.return_value = mock_coro(None) yield from conn.connect() # Check that we sent message to handler correctly assert len(mock_handle_message.mock_calls) == 1 p_hass, p_cloud, handler_name, payload = \ mock_handle_message.mock_calls[0][1] assert p_hass is mock_cloud.hass assert p_cloud is mock_cloud assert handler_name == 'test-handler' assert payload == 'test-payload' # Check that we forwarded response from handler to cloud assert len(mock_client.send_json.mock_calls) == 1 assert mock_client.send_json.mock_calls[0][1][0] == { 'msgid': 'test-msg-id', 'payload': 'response' }
def test_cloud_unable_to_connect(mock_client, caplog, mock_cloud): """Test unable to connect error.""" conn = iot.CloudIoT(mock_cloud) mock_client.receive.side_effect = client_exceptions.ClientError(None, None) yield from conn.connect() assert 'Unable to connect:' in caplog.text
def test_cloud_random_exception(mock_client, caplog, mock_cloud): """Test random exception.""" conn = iot.CloudIoT(mock_cloud) mock_client.receive.side_effect = Exception yield from conn.connect() assert 'Unexpected error' in caplog.text
def test_cloud_check_token_raising(mock_client, caplog, mock_cloud): """Test cloud unable to check token.""" conn = iot.CloudIoT(mock_cloud) mock_cloud.hass.async_add_job.side_effect = auth_api.CloudError("BLA") yield from conn.connect() assert 'Unable to refresh token: BLA' in caplog.text
def test_cloud_sending_invalid_json(mock_client, caplog, mock_cloud): """Test cloud sending invalid JSON.""" conn = iot.CloudIoT(mock_cloud) mock_client.receive.return_value = mock_coro( MagicMock(type=WSMsgType.TEXT, json=MagicMock(side_effect=ValueError))) yield from conn.connect() assert 'Connection closed: Received invalid JSON.' in caplog.text
def test_cloud_connect_invalid_auth(mock_client, caplog, mock_cloud): """Test invalid auth detected by server.""" conn = iot.CloudIoT(mock_cloud) mock_client.receive.side_effect = \ client_exceptions.WSServerHandshakeError(None, None, code=401) yield from conn.connect() assert 'Connection closed: Invalid auth.' in caplog.text
def test_cloud_check_token_raising(mock_client, caplog, mock_cloud): """Test cloud sending invalid JSON.""" conn = iot.CloudIoT(mock_cloud) mock_client.receive.side_effect = auth_api.CloudError yield from conn.connect() assert 'Unable to connect: Unable to refresh token.' in caplog.text assert 'connect' in str(mock_cloud.hass.async_add_job.mock_calls[-1][1][0])
def test_cloud_receiving_bytes(mock_client, caplog, mock_cloud): """Test server disconnecting instance.""" conn = iot.CloudIoT(mock_cloud) mock_client.receive.return_value = mock_coro( MagicMock(type=WSMsgType.BINARY, )) yield from conn.connect() assert 'Connection closed: Received non-Text message' in caplog.text
def test_cloud_getting_disconnected_by_server(mock_client, caplog, mock_cloud): """Test server disconnecting instance.""" conn = iot.CloudIoT(mock_cloud) mock_client.receive.return_value = mock_coro( MagicMock(type=WSMsgType.CLOSING, )) with patch('asyncio.sleep', side_effect=[None, asyncio.CancelledError]): yield from conn.connect() assert 'Connection closed' in caplog.text
def test_cloud_getting_disconnected_by_server(mock_client, caplog, mock_cloud): """Test server disconnecting instance.""" conn = iot.CloudIoT(mock_cloud) mock_client.receive.return_value = mock_coro( MagicMock(type=WSMsgType.CLOSING, )) yield from conn.connect() assert 'Connection closed: Connection cancelled.' in caplog.text assert 'connect' in str(mock_cloud.hass.async_add_job.mock_calls[-1][1][0])
def test_cloud_sending_invalid_json(mock_client, caplog): """Test cloud sending invalid JSON.""" cloud = MagicMock() conn = iot.CloudIoT(cloud) mock_client.receive.return_value = mock_coro( MagicMock(type=WSMsgType.TEXT, json=MagicMock(side_effect=ValueError))) yield from conn.connect() assert 'Connection closed: Received invalid JSON.' in caplog.text assert 'connect' in str(cloud.hass.async_add_job.mock_calls[-1][1][0])
def test_cloud_receiving_bytes(mock_client, caplog): """Test server disconnecting instance.""" cloud = MagicMock() conn = iot.CloudIoT(cloud) mock_client.receive.return_value = mock_coro( MagicMock(type=WSMsgType.BINARY, )) yield from conn.connect() assert 'Connection closed: Received non-Text message' in caplog.text assert 'connect' in str(cloud.hass.async_add_job.mock_calls[-1][1][0])
async def test_send_message_no_answer(mock_cloud): """Test sending a message that expects no answer.""" cloud_iot = iot.CloudIoT(mock_cloud) cloud_iot.state = iot.STATE_CONNECTED cloud_iot.client = MagicMock(send_json=MagicMock(return_value=mock_coro())) await cloud_iot.async_send_message('webhook', {'msg': 'yo'}, expect_answer=False) assert not cloud_iot._response_handler assert len(cloud_iot.client.send_json.mock_calls) == 1 msg = cloud_iot.client.send_json.mock_calls[0][1][0] assert msg['handler'] == 'webhook' assert msg['payload'] == {'msg': 'yo'}
def test_refresh_token_before_expiration_fails(hass, mock_cloud): """Test that we don't connect if token is expired.""" mock_cloud.subscription_expired = True mock_cloud.hass = hass conn = iot.CloudIoT(mock_cloud) with patch('homeassistant.components.cloud.auth_api.check_token', return_value=mock_coro()) as mock_check_token, \ patch.object(hass.components.persistent_notification, 'async_create') as mock_create: yield from conn.connect() assert len(mock_check_token.mock_calls) == 1 assert len(mock_create.mock_calls) == 1
def test_connection_msg_for_unknown_handler(mock_client, mock_cloud): """Test a msg for an unknown handler.""" conn = iot.CloudIoT(mock_cloud) mock_client.receive.return_value = mock_coro( MagicMock(type=WSMsgType.text, json=MagicMock( return_value={ 'msgid': 'test-msg-id', 'handler': 'non-existing-handler', 'payload': 'test-payload' }))) mock_client.send_json.return_value = mock_coro(None) yield from conn.connect() # Check that we sent the correct error assert len(mock_client.send_json.mock_calls) == 1 assert mock_client.send_json.mock_calls[0][1][0] == { 'msgid': 'test-msg-id', 'error': 'unknown-handler', }
async def test_send_message_answer(loop, mock_cloud): """Test sending a message that expects no answer.""" cloud_iot = iot.CloudIoT(mock_cloud) cloud_iot.state = iot.STATE_CONNECTED cloud_iot.client = MagicMock(send_json=MagicMock(return_value=mock_coro())) uuid = 5 with patch('homeassistant.components.cloud.iot.uuid.uuid4', return_value=MagicMock(hex=uuid)): send_task = loop.create_task(cloud_iot.async_send_message( 'webhook', {'msg': 'yo'})) await asyncio.sleep(0) assert len(cloud_iot.client.send_json.mock_calls) == 1 assert len(cloud_iot._response_handler) == 1 msg = cloud_iot.client.send_json.mock_calls[0][1][0] assert msg['handler'] == 'webhook' assert msg['payload'] == {'msg': 'yo'} cloud_iot._response_handler[uuid].set_result({'response': True}) response = await send_task assert response == {'response': True}
def test_connection_msg_for_handler_raising(mock_client, mock_handle_message, mock_cloud): """Test we sent error when handler raises exception.""" conn = iot.CloudIoT(mock_cloud) mock_client.receive.return_value = mock_coro( MagicMock(type=WSMsgType.text, json=MagicMock( return_value={ 'msgid': 'test-msg-id', 'handler': 'test-handler', 'payload': 'test-payload' }))) mock_handle_message.side_effect = Exception('Broken') mock_client.send_json.return_value = mock_coro(None) yield from conn.connect() # Check that we sent the correct error assert len(mock_client.send_json.mock_calls) == 1 assert mock_client.send_json.mock_calls[0][1][0] == { 'msgid': 'test-msg-id', 'error': 'exception', }
async def test_send_message_not_connected(mock_cloud): """Test sending a message that expects no answer.""" cloud_iot = iot.CloudIoT(mock_cloud) with pytest.raises(iot.NotConnected): await cloud_iot.async_send_message('webhook', {'msg': 'yo'})