| 1 |
|
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
import asyncio |
| 5 |
import json |
| 6 |
from contextlib import suppress |
| 7 |
from typing import TYPE_CHECKING, Any |
| 8 |
from unittest.mock import AsyncMock, MagicMock, patch |
| 9 |
|
| 10 |
import botocore |
| 11 |
import pytest |
| 12 |
|
| 13 |
from nice_go import ( |
| 14 |
ApiError, |
| 15 |
AuthFailedError, |
| 16 |
NiceGOApi, |
| 17 |
NoAuthError, |
| 18 |
WebSocketError, |
| 19 |
) |
| 20 |
|
| 21 |
GET_ALL_BARRIERS_RESPONSE = { |
| 22 |
"data": { |
| 23 |
"devicesListAll": { |
| 24 |
"devices": [ |
| 25 |
{ |
| 26 |
"state": { |
| 27 |
"connectionState": { |
| 28 |
"connected": True, |
| 29 |
"updatedTimestamp": "1234567890", |
| 30 |
}, |
| 31 |
"deviceId": "test_id", |
| 32 |
"desired": json.dumps({"test": "value"}), |
| 33 |
"reported": json.dumps({"test": "value"}), |
| 34 |
"timestamp": "1234567890", |
| 35 |
"version": 1, |
| 36 |
}, |
| 37 |
"id": "test_id", |
| 38 |
"type": "test_type", |
| 39 |
"controlLevel": "test_control_level", |
| 40 |
"attr": [{"key": "test_key", "value": "test_value"}], |
| 41 |
}, |
| 42 |
], |
| 43 |
}, |
| 44 |
}, |
| 45 |
} |
| 46 |
|
| 47 |
GET_ALL_BARRIERS_RESPONSE_NO_CONNECTION_STATE = { |
| 48 |
"data": { |
| 49 |
"devicesListAll": { |
| 50 |
"devices": [ |
| 51 |
{ |
| 52 |
"state": { |
| 53 |
"connectionState": None, |
| 54 |
"deviceId": "test_id", |
| 55 |
"desired": json.dumps({"test": "value"}), |
| 56 |
"reported": json.dumps({"test": "value"}), |
| 57 |
"timestamp": "1234567890", |
| 58 |
"version": 1, |
| 59 |
}, |
| 60 |
"id": "test_id", |
| 61 |
"type": "test_type", |
| 62 |
"controlLevel": "test_control_level", |
| 63 |
"attr": [{"key": "test_key", "value": "test_value"}], |
| 64 |
}, |
| 65 |
], |
| 66 |
}, |
| 67 |
}, |
| 68 |
} |
| 69 |
|
| 70 |
if TYPE_CHECKING: |
| 71 |
from syrupy.assertion import SnapshotAssertion |
| 72 |
|
| 73 |
|
| 74 |
async def test_schedule_event(mock_api: NiceGOApi) -> None: |
| 75 |
with patch("nice_go.nice_go_api.asyncio.create_task") as mock_create_task: |
| 76 |
coro = AsyncMock() |
| 77 |
mock_api._schedule_event(coro, "test_event", {"key": "value"}) |
| 78 |
mock_create_task.assert_called_once() |
| 79 |
await mock_create_task.call_args[0][0] |
| 80 |
|
| 81 |
|
| 82 |
async def test_dispatch_event(mock_api: NiceGOApi) -> None: |
| 83 |
coro = AsyncMock() |
| 84 |
mock_api._events["on_test_event"] = [coro] |
| 85 |
mock_api._dispatch("test_event", {"key": "value"}) |
| 86 |
await asyncio.sleep(0) |
| 87 |
coro.assert_called_once() |
| 88 |
|
| 89 |
|
| 90 |
async def test_authenticate(mock_api: NiceGOApi) -> None: |
| 91 |
with patch("nice_go.nice_go_api.asyncio.to_thread") as mock_to_thread: |
| 92 |
mock_to_thread.return_value = MagicMock( |
| 93 |
id_token="test_token", |
| 94 |
refresh_token="refresh_token", |
| 95 |
) |
| 96 |
assert mock_api._session is not None |
| 97 |
result = await mock_api.authenticate("username", "password", mock_api._session) |
| 98 |
assert result == "refresh_token" |
| 99 |
assert mock_api.id_token == "test_token" |
| 100 |
|
| 101 |
|
| 102 |
async def test_connect_not_authenticated(mock_api: NiceGOApi) -> None: |
| 103 |
mock_api.id_token = None |
| 104 |
with pytest.raises(NoAuthError): |
| 105 |
await mock_api.connect() |
| 106 |
|
| 107 |
|
| 108 |
async def test_connect_no_endpoints(mock_api: NiceGOApi) -> None: |
| 109 |
mock_api.id_token = "test_token" |
| 110 |
mock_api._endpoints = None |
| 111 |
with pytest.raises(ApiError): |
| 112 |
await mock_api.connect() |
| 113 |
|
| 114 |
|
| 115 |
async def test_subscribe_no_ws(mock_api: NiceGOApi) -> None: |
| 116 |
mock_api._device_ws = None |
| 117 |
with pytest.raises(WebSocketError): |
| 118 |
await mock_api.subscribe("receiver") |
| 119 |
mock_api._device_ws = AsyncMock() |
| 120 |
mock_api._events_ws = None |
| 121 |
with pytest.raises(WebSocketError): |
| 122 |
await mock_api.subscribe("receiver") |
| 123 |
|
| 124 |
|
| 125 |
async def test_unsubscribe_no_ws(mock_api: NiceGOApi) -> None: |
| 126 |
mock_api._device_ws = None |
| 127 |
with pytest.raises(WebSocketError): |
| 128 |
await mock_api.unsubscribe("receiver") |
| 129 |
mock_api._device_ws = AsyncMock() |
| 130 |
mock_api._events_ws = None |
| 131 |
with pytest.raises(WebSocketError): |
| 132 |
await mock_api.unsubscribe("receiver") |
| 133 |
|
| 134 |
|
| 135 |
async def test_get_all_barriers_not_authenticated(mock_api: NiceGOApi) -> None: |
| 136 |
mock_api.id_token = None |
| 137 |
with pytest.raises(NoAuthError): |
| 138 |
await mock_api.get_all_barriers() |
| 139 |
|
| 140 |
|
| 141 |
@pytest.mark.parametrize( |
| 142 |
("method_name", "expected_result"), |
| 143 |
[ |
| 144 |
("open_barrier", True), |
| 145 |
("close_barrier", True), |
| 146 |
("light_on", True), |
| 147 |
("light_off", True), |
| 148 |
("vacation_mode_on", None), |
| 149 |
("vacation_mode_off", None), |
| 150 |
], |
| 151 |
) |
| 152 |
async def test_barrier_operations( |
| 153 |
mock_api: NiceGOApi, |
| 154 |
method_name: str, |
| 155 |
expected_result: Any, |
| 156 |
) -> None: |
| 157 |
with patch("nice_go.nice_go_api.get_request_template") as mock_get_request_template: |
| 158 |
mock_api.id_token = "test_token" |
| 159 |
mock_get_request_template.return_value = {"query": method_name} |
| 160 |
assert mock_api._session is not None |
| 161 |
assert isinstance(mock_api._session, AsyncMock) |
| 162 |
mock_api._session.post.return_value.json.return_value = { |
| 163 |
"data": {"devicesControl": True}, |
| 164 |
} |
| 165 |
method = getattr(mock_api, method_name) |
| 166 |
result = await method("barrier_id") |
| 167 |
assert result is expected_result |
| 168 |
|
| 169 |
|
| 170 |
async def test_event_decorator(mock_api: NiceGOApi) -> None: |
| 171 |
@mock_api.event |
| 172 |
async def on_test_event(data: dict[str, Any]) -> None: |
| 173 |
pass |
| 174 |
|
| 175 |
assert "on_test_event" in mock_api._events |
| 176 |
assert mock_api._events["on_test_event"][0] == on_test_event |
| 177 |
mock_api._dispatch("test_event", {}) |
| 178 |
|
| 179 |
|
| 180 |
async def test_sync_event_decorator(mock_api: NiceGOApi) -> None: |
| 181 |
with pytest.raises(TypeError): |
| 182 |
|
| 183 |
@mock_api.event |
| 184 |
def on_test_event(data: dict[str, Any]) -> None: |
| 185 |
|
| 186 |
|
| 187 |
pass |
| 188 |
|
| 189 |
|
| 190 |
@pytest.mark.parametrize("error", [asyncio.CancelledError, Exception]) |
| 191 |
async def test_run_event_errors(mock_api: NiceGOApi, error: Exception) -> None: |
| 192 |
coro = AsyncMock() |
| 193 |
coro.side_effect = error |
| 194 |
mock_api._events["on_test_event"] = [coro] |
| 195 |
mock_api._dispatch("test_event", {}) |
| 196 |
await asyncio.sleep(0) |
| 197 |
coro.assert_called_once() |
| 198 |
|
| 199 |
|
| 200 |
async def test_dispatch_no_listener(mock_api: NiceGOApi) -> None: |
| 201 |
mock_api._dispatch("test_event", {}) |
| 202 |
await asyncio.sleep(0) |
| 203 |
|
| 204 |
|
| 205 |
async def test_authenticate_refresh(mock_api: NiceGOApi) -> None: |
| 206 |
with patch("nice_go.nice_go_api.asyncio.to_thread") as mock_to_thread: |
| 207 |
mock_to_thread.return_value = MagicMock( |
| 208 |
id_token="test_token", |
| 209 |
) |
| 210 |
mock_api.id_token = "test_token" |
| 211 |
assert mock_api._session is not None |
| 212 |
await mock_api.authenticate_refresh( |
| 213 |
"refresh_token", |
| 214 |
session=mock_api._session, |
| 215 |
) |
| 216 |
|
| 217 |
|
| 218 |
async def test_authenticate_botocore_client_error(mock_api: NiceGOApi) -> None: |
| 219 |
with patch("nice_go.nice_go_api.asyncio.to_thread") as mock_to_thread: |
| 220 |
mock_to_thread.side_effect = botocore.exceptions.ClientError( |
| 221 |
error_response={"Error": {"Code": "TestException"}}, |
| 222 |
operation_name="test", |
| 223 |
) |
| 224 |
assert mock_api._session is not None |
| 225 |
with pytest.raises(ApiError): |
| 226 |
await mock_api.authenticate("username", "password", mock_api._session) |
| 227 |
|
| 228 |
|
| 229 |
async def test_authenticate_not_authorized_exception(mock_api: NiceGOApi) -> None: |
| 230 |
with patch("nice_go.nice_go_api.asyncio.to_thread") as mock_to_thread: |
| 231 |
mock_to_thread.side_effect = botocore.exceptions.ClientError( |
| 232 |
error_response={"Error": {"Code": "NotAuthorizedException"}}, |
| 233 |
operation_name="test", |
| 234 |
) |
| 235 |
assert mock_api._session is not None |
| 236 |
with pytest.raises(AuthFailedError): |
| 237 |
await mock_api.authenticate("username", "password", mock_api._session) |
| 238 |
|
| 239 |
|
| 240 |
async def test_connect_error(mock_api: NiceGOApi) -> None: |
| 241 |
mock_api.id_token = "test_token" |
| 242 |
|
| 243 |
with patch("nice_go.nice_go_api.WebSocketClient") as mock_ws_client: |
| 244 |
mock_ws_client_instance = AsyncMock() |
| 245 |
mock_ws_client.return_value = mock_ws_client_instance |
| 246 |
mock_ws_client_instance.poll.side_effect = WebSocketError() |
| 247 |
with pytest.raises(WebSocketError): |
| 248 |
await mock_api.connect(reconnect=False) |
| 249 |
expected_call_count = 2 |
| 250 |
assert mock_ws_client_instance.connect.call_count == expected_call_count |
| 251 |
|
| 252 |
|
| 253 |
async def test_connect_closed(mock_api: NiceGOApi) -> None: |
| 254 |
mock_api.id_token = "test_token" |
| 255 |
|
| 256 |
with patch("nice_go.nice_go_api.WebSocketClient") as mock_ws_client: |
| 257 |
mock_ws_client_instance = AsyncMock() |
| 258 |
mock_ws_client.return_value = mock_ws_client_instance |
| 259 |
|
| 260 |
async def side_effect() -> None: |
| 261 |
mock_api._closing_task = asyncio.create_task(asyncio.sleep(0)) |
| 262 |
await asyncio.sleep(0) |
| 263 |
raise WebSocketError |
| 264 |
|
| 265 |
mock_ws_client_instance.poll = AsyncMock(side_effect=side_effect) |
| 266 |
await mock_api.connect(reconnect=True) |
| 267 |
assert mock_ws_client_instance.connect.call_count == 2 |
| 268 |
await mock_api.close() |
| 269 |
|
| 270 |
|
| 271 |
async def test_connect_reconnect(mock_api: NiceGOApi) -> None: |
| 272 |
mock_api.id_token = "test_token" |
| 273 |
|
| 274 |
with patch("nice_go.nice_go_api.WebSocketClient") as mock_ws_client: |
| 275 |
mock_ws_client_instance = AsyncMock() |
| 276 |
mock_ws_client.return_value = mock_ws_client_instance |
| 277 |
mock_ws_client_instance.connect.side_effect = WebSocketError |
| 278 |
with suppress(asyncio.TimeoutError): |
| 279 |
await asyncio.wait_for(mock_api.connect(reconnect=True), timeout=0.1) |
| 280 |
assert mock_ws_client_instance.connect.call_count > 4 |
| 281 |
|
| 282 |
|
| 283 |
async def test_subscribe(mock_api: NiceGOApi) -> None: |
| 284 |
mock_api.id_token = "test_token" |
| 285 |
mock_api._device_ws = AsyncMock() |
| 286 |
mock_api._device_ws.subscribe.return_value = "test_id" |
| 287 |
mock_api._events_ws = AsyncMock() |
| 288 |
mock_api._events_ws.subscribe.return_value = "test_id_2" |
| 289 |
result = await mock_api.subscribe("receiver") |
| 290 |
assert result == ["test_id", "test_id_2"] |
| 291 |
|
| 292 |
|
| 293 |
async def test_unsubscribe(mock_api: NiceGOApi) -> None: |
| 294 |
mock_api.id_token = "test_token" |
| 295 |
mock_api._device_ws = AsyncMock() |
| 296 |
mock_api._events_ws = AsyncMock() |
| 297 |
await mock_api.unsubscribe("test_id") |
| 298 |
mock_api._device_ws.unsubscribe.assert_called_once_with("test_id") |
| 299 |
mock_api._events_ws.unsubscribe.assert_called_once_with("test_id") |
| 300 |
|
| 301 |
|
| 302 |
async def test_get_all_barriers( |
| 303 |
mock_api: NiceGOApi, |
| 304 |
snapshot: SnapshotAssertion, |
| 305 |
) -> None: |
| 306 |
mock_api.id_token = "test_token" |
| 307 |
assert mock_api._session is not None |
| 308 |
assert isinstance(mock_api._session, AsyncMock) |
| 309 |
mock_api._session.post.return_value.json.return_value = GET_ALL_BARRIERS_RESPONSE |
| 310 |
result = await mock_api.get_all_barriers() |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
for barrier in result: |
| 315 |
barrier.api = None |
| 316 |
assert result == snapshot |
| 317 |
|
| 318 |
|
| 319 |
async def test_get_all_barriers_no_connection_state( |
| 320 |
mock_api: NiceGOApi, |
| 321 |
snapshot: SnapshotAssertion, |
| 322 |
) -> None: |
| 323 |
mock_api.id_token = "test_token" |
| 324 |
assert mock_api._session is not None |
| 325 |
assert isinstance(mock_api._session, AsyncMock) |
| 326 |
mock_api._session.post.return_value.json.return_value = ( |
| 327 |
GET_ALL_BARRIERS_RESPONSE_NO_CONNECTION_STATE |
| 328 |
) |
| 329 |
result = await mock_api.get_all_barriers() |
| 330 |
for barrier in result: |
| 331 |
barrier.api = None |
| 332 |
|
| 333 |
assert result == snapshot |
| 334 |
|
| 335 |
|
| 336 |
@pytest.mark.parametrize( |
| 337 |
("method_name"), |
| 338 |
[ |
| 339 |
("open_barrier"), |
| 340 |
("close_barrier"), |
| 341 |
("light_on"), |
| 342 |
("light_off"), |
| 343 |
("vacation_mode_on"), |
| 344 |
("vacation_mode_off"), |
| 345 |
], |
| 346 |
) |
| 347 |
async def test_barrier_operations_no_auth( |
| 348 |
mock_api: NiceGOApi, |
| 349 |
method_name: str, |
| 350 |
) -> None: |
| 351 |
mock_api.id_token = None |
| 352 |
method = getattr(mock_api, method_name) |
| 353 |
with pytest.raises(NoAuthError): |
| 354 |
await method("barrier_id") |
| 355 |
|
| 356 |
|
| 357 |
@pytest.mark.parametrize( |
| 358 |
("method_name", "args"), |
| 359 |
[ |
| 360 |
("authenticate", ("username", "password", None)), |
| 361 |
("connect", ()), |
| 362 |
("get_all_barriers", ()), |
| 363 |
("open_barrier", ("barrier_id",)), |
| 364 |
("close_barrier", ("barrier_id",)), |
| 365 |
("light_on", ("barrier_id",)), |
| 366 |
("light_off", ("barrier_id",)), |
| 367 |
("vacation_mode_on", ("barrier_id",)), |
| 368 |
("vacation_mode_off", ("barrier_id",)), |
| 369 |
], |
| 370 |
) |
| 371 |
async def test_no_client_session( |
| 372 |
mock_api: NiceGOApi, |
| 373 |
method_name: str, |
| 374 |
args: tuple[str | None], |
| 375 |
) -> None: |
| 376 |
mock_api._session = None |
| 377 |
mock_api.id_token = "test_token" |
| 378 |
method = getattr(mock_api, method_name) |
| 379 |
with pytest.raises(ValueError, match="ClientSession not provided"): |
| 380 |
await method(*args) |
| 381 |
|
| 382 |
|
| 383 |
@pytest.mark.parametrize( |
| 384 |
("method_name", "args"), |
| 385 |
[ |
| 386 |
("connect", ()), |
| 387 |
("get_all_barriers", ()), |
| 388 |
("open_barrier", ("barrier_id",)), |
| 389 |
("close_barrier", ("barrier_id",)), |
| 390 |
("light_on", ("barrier_id",)), |
| 391 |
("light_off", ("barrier_id",)), |
| 392 |
("vacation_mode_on", ("barrier_id",)), |
| 393 |
("vacation_mode_off", ("barrier_id",)), |
| 394 |
], |
| 395 |
) |
| 396 |
async def test_no_endpoints( |
| 397 |
mock_api: NiceGOApi, |
| 398 |
method_name: str, |
| 399 |
args: tuple[str | None], |
| 400 |
) -> None: |
| 401 |
mock_api._endpoints = None |
| 402 |
mock_api.id_token = "test_token" |
| 403 |
method = getattr(mock_api, method_name) |
| 404 |
with pytest.raises(ApiError, match="Endpoints not available"): |
| 405 |
await method(*args) |
| 406 |
|
| 407 |
|
| 408 |
async def test_auth_no_endpoints( |
| 409 |
mock_api: NiceGOApi, |
| 410 |
) -> None: |
| 411 |
mock_api._session = AsyncMock( |
| 412 |
get=AsyncMock( |
| 413 |
return_value=AsyncMock( |
| 414 |
json=AsyncMock( |
| 415 |
return_value={"endpoints": None}, |
| 416 |
), |
| 417 |
), |
| 418 |
), |
| 419 |
) |
| 420 |
|
| 421 |
with pytest.raises(ApiError, match="Endpoints not available"): |
| 422 |
await mock_api.authenticate("username", "password", mock_api._session) |
| 423 |
|
| 424 |
|
| 425 |
async def test_on_device_connected(mock_api: NiceGOApi) -> None: |
| 426 |
mock_api._events_connected = True |
| 427 |
coro = AsyncMock() |
| 428 |
mock_api.on_connected = coro |
| 429 |
await mock_api.on_device_connected() |
| 430 |
|
| 431 |
|
| 432 |
async def test_on_events_connected(mock_api: NiceGOApi) -> None: |
| 433 |
mock_api._device_connected = True |
| 434 |
coro = AsyncMock() |
| 435 |
mock_api.on_connected = coro |
| 436 |
await mock_api.on_events_connected() |
| 437 |
|
| 438 |
|
| 439 |
async def test_poll_ws_no_ws(mock_api: NiceGOApi) -> None: |
| 440 |
mock_api._device_ws = None |
| 441 |
mock_api._events_ws = None |
| 442 |
await mock_api._poll_device_ws() |
| 443 |
await mock_api._poll_events_ws() |
| 444 |
|
| 445 |
|
| 446 |
async def test_connect_poll_task_exception(mock_api: NiceGOApi) -> None: |
| 447 |
mock_api.id_token = "test_token" |
| 448 |
|
| 449 |
with patch("nice_go.nice_go_api.asyncio.wait") as mock_wait, patch( |
| 450 |
"nice_go.nice_go_api.WebSocketClient", |
| 451 |
) as mock_ws_client, patch("nice_go.nice_go_api.NiceGOApi._poll_device_ws"), patch( |
| 452 |
"nice_go.nice_go_api.NiceGOApi._poll_events_ws", |
| 453 |
): |
| 454 |
mock_ws_client_instance = AsyncMock() |
| 455 |
mock_ws_client.return_value = mock_ws_client_instance |
| 456 |
cancel_mock = MagicMock() |
| 457 |
mock_wait.return_value = ( |
| 458 |
( |
| 459 |
MagicMock( |
| 460 |
exception=MagicMock(return_value=ValueError("test")), |
| 461 |
), |
| 462 |
), |
| 463 |
(MagicMock(cancel=cancel_mock),), |
| 464 |
) |
| 465 |
with pytest.raises(ValueError, match="test"): |
| 466 |
await mock_api.connect(reconnect=False) |
| 467 |
cancel_mock.assert_called_once() |
| 468 |
|
| 469 |
|
| 470 |
async def test_graphql_error_response(mock_api: NiceGOApi) -> None: |
| 471 |
mock_api.id_token = "test_token" |
| 472 |
assert mock_api._session is not None |
| 473 |
assert isinstance(mock_api._session, AsyncMock) |
| 474 |
mock_api._session.post.return_value.json.return_value = { |
| 475 |
"errors": [{"errorType": "TestError", "message": "test_error"}], |
| 476 |
} |
| 477 |
with pytest.raises(ApiError, match="test_error"): |
| 478 |
await mock_api.get_all_barriers() |
| 479 |
|
| 480 |
mock_api._session.post.return_value.json.return_value = { |
| 481 |
"errors": [ |
| 482 |
{"errorType": "UnauthorizedException", "message": "unauthorized_error"}, |
| 483 |
], |
| 484 |
} |
| 485 |
with pytest.raises(AuthFailedError, match="unauthorized_error"): |
| 486 |
await mock_api.get_all_barriers() |