1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
|
"""Test the client."""
import asyncio
from datetime import datetime
import logging
from unittest.mock import Mock, patch
from aiohttp.client_exceptions import ClientError, WSServerHandshakeError
from aiohttp.client_reqrep import ClientResponse, RequestInfo
from aiohttp.http_websocket import WSMsgType
import pytest
from test.common import MockCommandProtocol
from zwave_js_server.client import LOGGER, Client
from zwave_js_server.const import MAX_SERVER_SCHEMA_VERSION, LogLevel, __version__
from zwave_js_server.event import Event
from zwave_js_server.exceptions import (
CannotConnect,
ConnectionFailed,
FailedCommand,
FailedZWaveCommand,
InvalidMessage,
InvalidServerVersion,
InvalidState,
NotConnected,
)
from zwave_js_server.model.driver import Driver
from zwave_js_server.model.log_config import LogConfig
async def test_connect_disconnect(client_session, url):
"""Test client connect and disconnect."""
async with Client(url, client_session) as client:
assert client.connected
assert not client.connected
@pytest.mark.parametrize(
"error",
[ClientError, WSServerHandshakeError(Mock(RequestInfo), (Mock(ClientResponse),))],
)
async def test_cannot_connect(client_session, url, error):
"""Test cannot connect."""
client_session.ws_connect.side_effect = error
client = Client(url, client_session)
with pytest.raises(CannotConnect):
await client.connect()
assert not client.connected
async def test_send_command_schema(
client_session, url, ws_client, driver_ready, driver
):
"""Test sending unsupported command."""
client = Client(url, client_session)
await client.connect()
assert client.connected
client.driver = driver
await client.listen(driver_ready)
ws_client.receive.assert_awaited()
# test schema version is at server maximum
if client.version.max_schema_version < MAX_SERVER_SCHEMA_VERSION:
assert client.schema_version == client.version.max_schema_version
# send command of current schema version should not fail
with pytest.raises(NotConnected):
await client.async_send_command(
{"command": "test"}, require_schema=client.schema_version
)
# send command of unsupported schema version should fail
with pytest.raises(InvalidServerVersion):
await client.async_send_command(
{"command": "test"}, require_schema=client.schema_version + 2
)
with pytest.raises(InvalidServerVersion):
await client.async_send_command_no_wait(
{"command": "test"}, require_schema=client.schema_version + 2
)
async def test_min_schema_version(client_session, url, version_data):
"""Test client connect with invalid schema version."""
version_data["minSchemaVersion"] = 100
client = Client(url, client_session)
with pytest.raises(InvalidServerVersion):
await client.connect()
assert not client.connected
async def test_max_schema_version(client_session, url, version_data):
"""Test client connect with invalid schema version."""
version_data["maxSchemaVersion"] = 0
client = Client(url, client_session)
with pytest.raises(InvalidServerVersion):
await client.connect()
assert not client.connected
async def test_send_json_when_disconnected(client_session, url):
"""Test send json message when disconnected."""
client = Client(url, client_session)
assert not client.connected
with pytest.raises(NotConnected):
await client.async_send_command({"test": None})
async def test_listen(client_session, url, driver_ready):
"""Test client listen."""
client = Client(url, client_session)
assert not client.driver
await client.connect()
assert client.connected
asyncio.create_task(client.listen(driver_ready))
await driver_ready.wait()
assert client.driver
await client.disconnect()
assert not client.connected
async def test_listen_client_error(
client_session, url, ws_client, messages, ws_message, driver_ready
):
"""Test websocket error on listen."""
client = Client(url, client_session)
await client.connect()
assert client.connected
messages.append(ws_message)
ws_client.receive.side_effect = asyncio.CancelledError()
# This should break out of the listen loop before any message is received.
with pytest.raises(asyncio.CancelledError):
await client.listen(driver_ready)
assert not ws_message.json.called
@pytest.mark.parametrize(
"message_type, exception",
[
(WSMsgType.ERROR, ConnectionFailed),
(WSMsgType.BINARY, InvalidMessage),
],
)
async def test_listen_error_message_types(
client_session, url, messages, ws_message, message_type, exception, driver_ready
):
"""Test different websocket message types that should raise on listen."""
client = Client(url, client_session)
await client.connect()
assert client.connected
ws_message.type = message_type
messages.append(ws_message)
with pytest.raises(exception):
await client.listen(driver_ready)
@pytest.mark.parametrize(
"message_type", [WSMsgType.CLOSE, WSMsgType.CLOSED, WSMsgType.CLOSING]
)
async def test_listen_disconnect_message_types(
client_session, url, ws_client, messages, ws_message, message_type, driver_ready
):
"""Test different websocket message types that stop listen."""
async with Client(url, client_session) as client:
assert client.connected
ws_message.type = message_type
messages.append(ws_message)
# This should break out of the listen loop before handling the received message.
# Otherwise there will be an error.
await client.listen(driver_ready)
# Assert that we received a message.
ws_client.receive.assert_awaited()
async def test_listen_invalid_message_data(
client_session, url, messages, ws_message, driver_ready
):
"""Test websocket message data that should raise on listen."""
client = Client(url, client_session)
await client.connect()
assert client.connected
ws_message.json.side_effect = ValueError("Boom")
messages.append(ws_message)
with pytest.raises(InvalidMessage):
await client.listen(driver_ready)
async def test_listen_not_success(client_session, url, result, driver_ready):
"""Test receive result message with success False on listen."""
result["success"] = False
result["errorCode"] = "error_code"
result["message"] = "test"
client = Client(url, client_session)
await client.connect()
with pytest.raises(FailedCommand):
await client.listen(driver_ready)
assert not client.connected
async def test_initialize_not_success(
client_session, url, initialize_data, driver_ready
):
"""Test receive result message with success False on listen."""
initialize_data["success"] = False
initialize_data["errorCode"] = "error_code"
initialize_data["message"] = "test"
client = Client(url, client_session)
await client.connect()
with pytest.raises(FailedCommand):
await client.listen(driver_ready)
assert not client.connected
async def test_get_log_config_not_success(
client_session, url, get_log_config_data, driver_ready
):
"""Test receive log config message with success False on listen."""
get_log_config_data["success"] = False
get_log_config_data["errorCode"] = "error_code"
get_log_config_data["message"] = "test"
client = Client(url, client_session)
await client.connect()
with pytest.raises(FailedCommand):
await client.listen(driver_ready)
assert not client.connected
async def test_listen_without_connect(client_session, url, driver_ready):
"""Test listen without first being connected."""
client = Client(url, client_session)
assert not client.connected
with pytest.raises(InvalidState):
await client.listen(driver_ready)
async def test_listen_event(
client_session, url, ws_client, messages, ws_message, result, driver_ready
):
"""Test receiving event result type on listen."""
client = Client(url, client_session)
await client.connect()
assert client.connected
result["type"] = "event"
result["event"] = {
"source": "node",
"event": "value updated",
"nodeId": 52,
"args": {
"commandClassName": "Basic",
"commandClass": 32,
"endpoint": 0,
"property": "currentValue",
"newValue": 255,
"prevValue": 255,
"propertyName": "currentValue",
},
}
messages.append(ws_message)
await client.listen(driver_ready)
ws_client.receive.assert_awaited()
async def test_listen_unknown_result_type(
client_session, url, ws_client, result, driver_ready, driver
):
"""Test websocket message with unknown type on listen."""
client = Client(url, client_session)
await client.connect()
assert client.connected
# Make sure there's a driver so we can test an unknown event.
client.driver = driver
result["type"] = "unknown"
# Receiving an unknown message type should not error.
await client.listen(driver_ready)
ws_client.receive.assert_awaited()
async def test_command_error_handling(client, mock_command):
"""Test error handling."""
mock_command(
{"command": "some_command"},
{"errorCode": "unknown_command", "message": "test"},
False,
)
with pytest.raises(FailedCommand) as raised:
await client.async_send_command({"command": "some_command"})
assert raised.value.error_code == "unknown_command"
assert str(raised.value) == "unknown_command: test"
mock_command(
{"command": "some_zjs_command"},
{
"errorCode": "zwave_error",
"zwaveErrorCode": 3,
"zwaveErrorMessage": "Node 5 is dead",
},
False,
)
with pytest.raises(FailedZWaveCommand) as raised:
await client.async_send_command({"command": "some_zjs_command"})
assert raised.value.error_code == "zwave_error"
assert raised.value.zwave_error_code == 3
assert raised.value.zwave_error_message == "Node 5 is dead"
async def test_record_messages(client, wallmote_central_scene, mock_command, uuid4):
"""Test recording messages."""
# pylint: disable=protected-access
assert not client.recording_messages
assert not client._recorded_commands
assert not client._recorded_events
client.begin_recording_messages()
mock_command(
{"command": "some_command"},
{},
)
with pytest.raises(InvalidState):
client.begin_recording_messages()
with patch("zwave_js_server.client.datetime") as mock_dt:
mock_dt.utcnow.return_value = datetime(2022, 1, 7, 1)
await client.async_send_command({"command": "some_command"})
assert len(client._recorded_commands) == 1
assert len(client._recorded_events) == 0
assert uuid4 in client._recorded_commands
assert client._recorded_commands[uuid4]["record_type"] == "command"
assert client._recorded_commands[uuid4]["command"] == "some_command"
assert client._recorded_commands[uuid4]["command_msg"] == {
"command": "some_command",
"messageId": uuid4,
}
assert client._recorded_commands[uuid4]["result_msg"] == {
"messageId": "1234",
"result": {},
"success": True,
"type": "result",
}
assert "ts" in client._recorded_commands[uuid4]
assert "result_ts" in client._recorded_commands[uuid4]
with patch("zwave_js_server.client.datetime") as mock_dt:
mock_dt.utcnow.return_value = datetime(2022, 1, 7, 0)
client._handle_incoming_message(
{
"type": "event",
"event": {
"source": "node",
"event": "value updated",
"nodeId": wallmote_central_scene.node_id,
"args": {
"commandClassName": "Binary Switch",
"commandClass": 39,
"endpoint": 0,
"property": "currentValue",
"newValue": False,
"prevValue": True,
"propertyName": "currentValue",
},
},
}
)
assert len(client._recorded_commands) == 1
assert len(client._recorded_events) == 1
logging.getLogger(__name__).error(client._recorded_events)
event = client._recorded_events[0]
assert event["record_type"] == "event"
assert event["type"] == "value updated"
assert event["event_msg"] == {
"type": "event",
"event": {
"source": "node",
"event": "value updated",
"nodeId": wallmote_central_scene.node_id,
"args": {
"commandClassName": "Binary Switch",
"commandClass": 39,
"endpoint": 0,
"property": "currentValue",
"newValue": False,
"prevValue": True,
"propertyName": "currentValue",
},
},
}
assert "ts" in event
replay_dump = client.end_recording_messages()
assert len(replay_dump) == 2
assert len(client._recorded_commands) == 0
assert len(client._recorded_events) == 0
# Testing that events are properly sorted by timestamp. Even though the event
# comes after the command in the code, the patch should make the event appear first
assert replay_dump[0]["record_type"] == "event"
with pytest.raises(InvalidState):
client.end_recording_messages()
async def test_additional_user_agent_components(client_session, url):
"""Test additionalUserAgentComponents parameter."""
# pylint: disable=protected-access
client = Client(
url, client_session, additional_user_agent_components={"foo": "bar"}
)
client._client = True
with (
patch(
"zwave_js_server.client.Client._send_json_message", return_value=None
) as send_json_mock,
patch(
"zwave_js_server.client.Client._receive_json_or_raise",
return_value={"success": True},
),
):
await client.initialize()
send_json_mock.assert_called_once_with(
{
"command": "initialize",
"messageId": "initialize",
"schemaVersion": 44,
"additionalUserAgentComponents": {
"zwave-js-server-python": __version__,
"foo": "bar",
},
}
)
async def test_pop_future_none(client_session, url, driver_ready):
"""Test when a future has been cleared from futures dict, popping still works."""
client = Client(url, client_session)
await client.connect()
assert client.connected
asyncio.create_task(client.listen(driver_ready))
await driver_ready.wait()
with pytest.raises(asyncio.CancelledError):
await client.async_send_command({"command": "some_command"})
async def test_log_server(
client: Client,
driver: Driver,
caplog: pytest.LogCaptureFixture,
mock_command: MockCommandProtocol,
) -> None:
"""Test logging from server."""
# pylint: disable=protected-access
assert client.connected
mock_command(
{"command": "start_listening_logs"},
{},
)
mock_command(
{"command": "stop_listening_logs"},
{},
)
# Set log levels to force the lib to change log levels
LOGGER.setLevel(logging.INFO)
client.driver.log_config = LogConfig(True, LogLevel.DEBUG, False, None, None)
await client.enable_server_logging()
assert client.server_logging_enabled
assert len(caplog.records) == 2
assert "logging is currently more verbose" in caplog.records[0].message
assert caplog.records[0].name == "zwave_js_server"
# Test that enabling again is a no-op
await client.enable_server_logging()
assert client.server_logging_enabled
assert len(caplog.records) == 2
LOGGER.setLevel(logging.INFO)
event = Event(
"log config updated",
data={
"source": "driver",
"event": "log config updated",
"config": {"level": "silly"},
},
)
driver.receive_event(event)
assert len(caplog.records) == 3
assert "logging is currently more verbose" in caplog.records[2].message
assert caplog.records[2].name == "zwave_js_server"
event = Event(
type="logging",
data={
"source": "driver",
"event": "logging",
"formattedMessage": [
"2021-04-18T18:03:34.051Z CNTRLR [Node 005] [~] \n",
"test",
],
"level": "debug",
"primaryTags": "[Node 005] [~] [Notification]",
"secondaryTags": "[Endpoint 0]",
"message": "Home Security[Motion sensor status]\n: 8 => 0",
"direction": " ",
"label": "CNTRLR",
"timestamp": "2021-04-18T18:03:34.051Z",
"multiline": True,
"secondaryTagPadding": -1,
"context": {
"source": "controller",
"type": "node",
"nodeId": 5,
"header": "Notification",
"direction": "none",
"change": "notification",
"endpoint": 0,
},
},
)
driver.receive_event(event)
assert len(caplog.records) == 4
assert "Node 005" in caplog.records[3].message
assert caplog.records[3].levelno == logging.DEBUG
assert caplog.records[3].name == "zwave_js_server.server"
# First time we disable should be clean
client.disable_server_logging()
assert not client.server_logging_enabled
assert len(caplog.records) == 4
# Test that disabling again is a no-op
client.disable_server_logging()
assert not client.server_logging_enabled
# Test that enabling server logging raises an error when client is not connected
client.driver = None
client._client = None
with pytest.raises(InvalidState):
await client.enable_server_logging()
client.disable_server_logging()
|