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
|
"""Define fixtures, constants, etc. available for all tests."""
from __future__ import annotations
import asyncio
import json
from collections import deque
from collections.abc import Generator
from typing import Any, cast
from unittest.mock import AsyncMock, Mock
import aiohttp
import pytest
import pytest_asyncio
from aresponses import ResponsesMockServer
from simplipy.api import API
from tests.common import (
TEST_SUBSCRIPTION_ID,
TEST_USER_ID,
create_ws_message,
load_fixture,
)
@pytest.fixture(name="api_token_response")
def api_token_response_fixture() -> dict[str, Any]:
"""Define a fixture to return a successful token response.
Returns:
An API response payload.
"""
return cast(dict[str, Any], json.loads(load_fixture("api_token_response.json")))
@pytest.fixture(name="auth_check_response", scope="session")
def auth_check_response_fixture() -> dict[str, Any]:
"""Define a fixture to return a successful authorization check.
Returns:
An API response payload.
"""
return cast(dict[str, Any], json.loads(load_fixture("auth_check_response.json")))
@pytest.fixture(name="authenticated_simplisafe_server")
def authenticated_simplisafe_server_fixture(
api_token_response: dict[str, Any], auth_check_response: dict[str, Any]
) -> Generator[ResponsesMockServer, None, None]:
"""Define a fixture that returns an authenticated API connection.
Args:
api_token_response: An API response payload.
auth_check_response: An API response payload.
"""
server = ResponsesMockServer()
server.add(
"auth.simplisafe.com",
"/oauth/token",
"post",
response=aiohttp.web_response.json_response(api_token_response, status=200),
)
server.add(
"api.simplisafe.com",
"/v1/api/authCheck",
"get",
response=aiohttp.web_response.json_response(auth_check_response, status=200),
)
yield server
@pytest.fixture(name="authenticated_simplisafe_server_v2")
def authenticated_simplisafe_server_v2_fixture(
authenticated_simplisafe_server: ResponsesMockServer,
v2_settings_response: dict[str, Any],
v2_subscriptions_response: dict[str, Any],
) -> Generator[ResponsesMockServer, None, None]:
"""Define a fixture that returns an authenticated API connection to a V2 system.
Args:
authenticated_simplisafe_server: A mock SimpliSafe cloud API connection.
v2_settings_response: An API response payload.
v2_subscriptions_response: An API response payload.
"""
authenticated_simplisafe_server.add(
"api.simplisafe.com",
f"/v1/users/{TEST_SUBSCRIPTION_ID}/subscriptions",
"get",
response=aiohttp.web_response.json_response(
v2_subscriptions_response, status=200
),
)
authenticated_simplisafe_server.add(
"api.simplisafe.com",
f"/v1/subscriptions/{TEST_SUBSCRIPTION_ID}/settings",
"get",
response=aiohttp.web_response.json_response(v2_settings_response, status=200),
)
yield authenticated_simplisafe_server
@pytest.fixture(name="authenticated_simplisafe_server_v3")
def authenticated_simplisafe_server_v3_fixture(
authenticated_simplisafe_server: ResponsesMockServer,
subscriptions_response: dict[str, Any],
v3_sensors_response: dict[str, Any],
v3_settings_response: dict[str, Any],
) -> Generator[ResponsesMockServer, None, None]:
"""Define a fixture that returns an authenticated API connection to a V3 system.
Args:
authenticated_simplisafe_server: A mock SimpliSafe cloud API connection.
subscriptions_response: An API response payload.
v3_sensors_response: An API response payload.
v3_settings_response: An API response payload.
"""
authenticated_simplisafe_server.add(
"api.simplisafe.com",
f"/v1/users/{TEST_USER_ID}/subscriptions",
"get",
response=aiohttp.web_response.json_response(subscriptions_response, status=200),
)
authenticated_simplisafe_server.add(
"api.simplisafe.com",
f"/v1/ss3/subscriptions/{TEST_SUBSCRIPTION_ID}/settings/normal",
"get",
response=aiohttp.web_response.json_response(v3_settings_response, status=200),
)
authenticated_simplisafe_server.add(
"api.simplisafe.com",
f"/v1/ss3/subscriptions/{TEST_SUBSCRIPTION_ID}/sensors",
"get",
response=aiohttp.web_response.json_response(v3_sensors_response, status=200),
)
yield authenticated_simplisafe_server
@pytest.fixture(name="events_response", scope="session")
def events_response_fixture() -> dict[str, Any]:
"""Define a fixture to return an events response.
Returns:
An API response payload.
"""
return cast(dict[str, Any], json.loads(load_fixture("events_response.json")))
@pytest.fixture(name="invalid_authorization_code_response", scope="session")
def invalid_authorization_code_response_fixture() -> dict[str, Any]:
"""Define a fixture to return an invalid authorization code response.
Returns:
An API response payload.
"""
return cast(
dict[str, Any],
json.loads(load_fixture("invalid_authorization_code_response.json")),
)
@pytest.fixture(name="invalid_refresh_token_response", scope="session")
def invalid_refresh_token_response_fixture() -> dict[str, Any]:
"""Define a fixture to return an invalid refresh token response.
Returns:
An API response payload.
"""
return cast(
dict[str, Any], json.loads(load_fixture("invalid_refresh_token_response.json"))
)
@pytest.fixture(name="latest_event_response", scope="session")
def latest_event_response_fixture() -> dict[str, Any]:
"""Define a fixture to return the latest system event.
Returns:
An API response payload.
"""
return cast(dict[str, Any], json.loads(load_fixture("latest_event_response.json")))
@pytest.fixture(name="mock_api")
def mock_api_fixture(ws_client_session: AsyncMock) -> Mock:
"""Define a fixture to return a mock simplipy.API object.
Args:
ws_client_session: The mocked websocket client session.
Returns:
The mock object.
"""
mock_api = Mock(API)
mock_api.access_token = "12345" # noqa: S105
mock_api.session = ws_client_session
mock_api.user_id = 98765
return mock_api
@pytest.fixture(name="subscriptions_response")
def subscriptions_response_fixture() -> dict[str, Any]:
"""Define a fixture to return a subscriptions response.
Returns:
An API response payload.
"""
return cast(dict[str, Any], json.loads(load_fixture("subscriptions_response.json")))
@pytest.fixture(name="unavailable_endpoint_response", scope="session")
def unavailable_endpoint_response_fixture() -> dict[str, Any]:
"""Define a fixture to return an unavailable endpoint response.
Returns:
An API response payload.
"""
return cast(
dict[str, Any], json.loads(load_fixture("unavailable_endpoint_response.json"))
)
@pytest.fixture(name="v2_pins_response", scope="session")
def v2_pins_response_fixture() -> dict[str, Any]:
"""Define a fixture that returns a V2 PINs response.
Returns:
An API response payload.
"""
return cast(dict[str, Any], json.loads(load_fixture("v2_pins_response.json")))
@pytest.fixture(name="v2_settings_response", scope="session")
def v2_settings_response_fixture() -> dict[str, Any]:
"""Define a fixture that returns a V2 settings response.
Returns:
An API response payload.
"""
return cast(dict[str, Any], json.loads(load_fixture("v2_settings_response.json")))
@pytest.fixture(name="v2_state_response", scope="session")
def v2_state_response_fixture() -> dict[str, Any]:
"""Define a fixture that returns a V2 state change response.
Returns:
An API response payload.
"""
return cast(dict[str, Any], json.loads(load_fixture("v2_state_response.json")))
@pytest.fixture(name="v2_subscriptions_response")
def v2_subscriptions_response_fixture(
subscriptions_response: dict[str, Any],
) -> dict[str, Any]:
"""Define a fixture that returns a V2 subscriptions response.
Returns:
An API response payload.
"""
response = {**subscriptions_response}
response["subscriptions"][0]["location"]["system"]["version"] = 2
return response
@pytest.fixture(name="v3_sensors_response", scope="session")
def v3_sensors_response_fixture() -> dict[str, Any]:
"""Define a fixture that returns a V3 sensors response.
Returns:
An API response payload.
"""
return cast(dict[str, Any], json.loads(load_fixture("v3_sensors_response.json")))
@pytest.fixture(name="v3_settings_response")
def v3_settings_response_fixture() -> dict[str, Any]:
"""Define a fixture that returns a V3 settings response.
Returns:
An API response payload.
"""
return cast(dict[str, Any], json.loads(load_fixture("v3_settings_response.json")))
@pytest.fixture(name="v3_state_response", scope="session")
def v3_state_response_fixture() -> dict[str, Any]:
"""Define a fixture that returns a V3 state change response.
Returns:
An API response payload.
"""
return cast(dict[str, Any], json.loads(load_fixture("v3_state_response.json")))
@pytest_asyncio.fixture(name="ws_client")
async def ws_client_fixture(
ws_message_hello: dict[str, Any],
ws_message_registered: dict[str, Any],
ws_message_subscribed: dict[str, Any],
ws_messages: deque,
) -> AsyncMock:
"""Mock a websocket client.
This fixture only allows a single message to be received.
Args:
ws_message_hello: A mocked websocket message.
ws_message_registered: A mocked websocket message.
ws_message_subscribed: A mocked websocket message.
ws_messages: A message queue.
Returns:
A mocked websocket client.
"""
ws_client = AsyncMock(spec_set=aiohttp.ClientWebSocketResponse, closed=False)
ws_client.receive_json.side_effect = (
ws_message_hello,
ws_message_registered,
ws_message_subscribed,
)
for data in (ws_message_hello, ws_message_registered, ws_message_subscribed):
ws_messages.append(create_ws_message(data))
async def receive() -> Mock:
"""Return a websocket message."""
await asyncio.sleep(0)
message: Mock = ws_messages.popleft()
if not ws_messages:
ws_client.closed = True
return message
ws_client.receive.side_effect = receive
async def reset_close() -> None:
"""Reset the websocket client close method."""
ws_client.closed = True
ws_client.close.side_effect = reset_close
return ws_client
@pytest.fixture(name="ws_client_session")
def ws_client_session_fixture(ws_client: AsyncMock) -> dict[str, Any]:
"""Mock an aiohttp client session.
Args:
ws_client: A mocked websocket client.
Returns:
A mocked websocket client session.
"""
client_session = AsyncMock(spec_set=aiohttp.ClientSession)
client_session.ws_connect.side_effect = AsyncMock(return_value=ws_client)
return client_session
@pytest.fixture(name="ws_message_event")
def ws_message_event_fixture(ws_message_event_data: dict[str, Any]) -> dict[str, Any]:
"""Define a fixture to represent an event response.
Args:
ws_message_event_data: A mocked websocket response payload.
Returns:
A websocket response payload.
"""
return {
"data": ws_message_event_data,
"datacontenttype": "application/json",
"id": "id:16803409109",
"source": "messagequeue",
"specversion": "1.0",
"time": "2021-09-29T23:14:46.000Z",
"type": "com.simplisafe.event.standard",
}
@pytest.fixture(name="ws_message_event_data", scope="session")
def ws_message_event_data_fixture() -> dict[str, Any]:
"""Define a fixture that returns the data payload from a data event.
Returns:
A API response payload.
"""
return cast(dict[str, Any], json.loads(load_fixture("ws_message_event_data.json")))
@pytest.fixture(name="ws_motion_event")
def ws_motion_event_fixture(ws_motion_event_data: dict[str, Any]) -> dict[str, Any]:
"""Define a fixture to represent an event response.
Args:
ws_motion_event_data: A mocked websocket response payload.
Returns:
A websocket response payload.
"""
return {
"data": ws_motion_event_data,
"datacontenttype": "application/json",
"id": "id:16803409109",
"source": "messagequeue",
"specversion": "1.0",
"time": "2021-09-29T23:14:46.000Z",
"type": "com.simplisafe.event.standard",
}
@pytest.fixture(name="ws_motion_event_data", scope="session")
def ws_motion_event_data_fixture() -> dict[str, Any]:
"""Define a fixture that returns the data payload from a data event.
Returns:
A API response payload.
"""
return cast(dict[str, Any], json.loads(load_fixture("ws_motion_event_data.json")))
@pytest.fixture(name="ws_message_hello")
def ws_message_hello_fixture(ws_message_hello_data: dict[str, Any]) -> dict[str, Any]:
"""Define a fixture to represent the "hello" response.
Args:
ws_message_hello_data: A mocked websocket response payload.
Returns:
A websocket response payload.
"""
return {
"data": ws_message_hello_data,
"datacontenttype": "application/json",
"id": "id:16803409109",
"source": "service",
"specversion": "1.0",
"time": "2021-09-29T23:14:46.000Z",
"type": "com.simplisafe.service.hello",
}
@pytest.fixture(name="ws_message_hello_data", scope="session")
def ws_message_hello_data_fixture() -> dict[str, Any]:
"""Define a fixture that returns the data payload from a "hello" event.
Returns:
A API response payload.
"""
return cast(dict[str, Any], json.loads(load_fixture("ws_message_hello_data.json")))
@pytest.fixture(name="ws_message_registered", scope="session")
def ws_message_registered_fixture() -> dict[str, Any]:
"""Define a fixture to represent the "registered" response.
Returns:
A websocket response payload.
"""
return {
"datacontenttype": "application/json",
"id": "id:16803409109",
"source": "service",
"specversion": "1.0",
"time": "2021-09-29T23:14:46.000Z",
"type": "com.simplisafe.service.registered",
}
@pytest.fixture(name="ws_message_registered_data", scope="session")
def ws_message_registered_data_fixture() -> dict[str, Any]:
"""Define a fixture that returns the data payload from a "registered" event.
Returns:
An API response payload.
"""
return cast(
dict[str, Any], json.loads(load_fixture("ws_message_registered_data.json"))
)
@pytest.fixture(name="ws_message_subscribed")
def ws_message_subscribed_fixture(
ws_message_subscribed_data: dict[str, Any],
) -> dict[str, Any]:
"""Define a fixture to represent the "registered" response.
Args:
ws_message_subscribed_data: A mocked websocket response payload.
Returns:
A websocket response payload.
"""
return {
"data": ws_message_subscribed_data,
"datacontenttype": "application/json",
"id": "id:16803409109",
"source": "service",
"specversion": "1.0",
"time": "2021-09-29T23:14:46.000Z",
"type": "com.simplisafe.service.subscribed",
}
@pytest.fixture(name="ws_message_subscribed_data", scope="session")
def ws_message_subscribed_data_fixture() -> dict[str, Any]:
"""Define a fixture that returns the data payload from a "subscribed" event.
Returns:
An API response payload.
"""
return cast(
dict[str, Any], json.loads(load_fixture("ws_message_subscribed_data.json"))
)
@pytest.fixture(name="ws_messages")
def ws_messages_fixture() -> deque:
"""Return a message buffer for the WS client.
Returns:
A queue.
"""
return deque()
|