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
|
"""Define base tests for System objects."""
from datetime import datetime
from typing import Any, cast
from unittest.mock import Mock
import aiohttp
import pytest
from aresponses import ResponsesMockServer
from simplipy import API
from simplipy.system import SystemStates
from simplipy.system.v3 import SystemV3
from tests.common import (
TEST_ADDRESS,
TEST_AUTHORIZATION_CODE,
TEST_CODE_VERIFIER,
TEST_SUBSCRIPTION_ID,
TEST_SYSTEM_ID,
TEST_SYSTEM_SERIAL_NO,
TEST_USER_ID,
)
@pytest.mark.asyncio
async def test_deactivated_system(
aresponses: ResponsesMockServer,
authenticated_simplisafe_server: ResponsesMockServer,
subscriptions_response: dict[str, Any],
) -> None:
"""Test that API.async_get_systems doesn't return deactivated systems.
Args:
aresponses: An aresponses server.
authenticated_simplisafe_server: A authenticated API connection.
subscriptions_response: An API response payload.
"""
subscriptions_response["subscriptions"][0]["status"]["hasBaseStation"] = 0
async with authenticated_simplisafe_server:
authenticated_simplisafe_server.add(
"api.simplisafe.com",
f"/v1/users/{TEST_SUBSCRIPTION_ID}/subscriptions",
"get",
response=aiohttp.web_response.json_response(
subscriptions_response, status=200
),
)
async with aiohttp.ClientSession() as session:
simplisafe = await API.async_from_auth(
TEST_AUTHORIZATION_CODE, TEST_CODE_VERIFIER, session=session
)
systems = await simplisafe.async_get_systems()
assert len(systems) == 0
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_get_events(
aresponses: ResponsesMockServer,
authenticated_simplisafe_server_v2: ResponsesMockServer,
events_response: dict[str, Any],
) -> None:
"""Test getting events from a system.
Args:
aresponses: An aresponses server.
authenticated_simplisafe_server_v2: A authenticated API connection.
events_response: An API response payload.
"""
async with authenticated_simplisafe_server_v2:
authenticated_simplisafe_server_v2.add(
"api.simplisafe.com",
f"/v1/subscriptions/{TEST_SYSTEM_ID}/events",
"get",
response=aiohttp.web_response.json_response(events_response, status=200),
)
async with aiohttp.ClientSession() as session:
simplisafe = await API.async_from_auth(
TEST_AUTHORIZATION_CODE, TEST_CODE_VERIFIER, session=session
)
systems = await simplisafe.async_get_systems()
system = systems[TEST_SYSTEM_ID]
events = await system.async_get_events(datetime.now(), 2)
assert len(events) == 2
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_missing_property( # pylint: disable=too-many-arguments
aresponses: ResponsesMockServer,
authenticated_simplisafe_server: ResponsesMockServer,
caplog: Mock,
subscriptions_response: dict[str, Any],
v3_sensors_response: dict[str, Any],
v3_settings_response: dict[str, Any],
) -> None:
"""Test that missing property data is handled correctly.
Args:
aresponses: An aresponses server.
authenticated_simplisafe_server: A authenticated API connection.
caplog: A mocked logging utility.
subscriptions_response: An API response payload.
v3_sensors_response: An API response payload.
v3_settings_response: An API response payload.
"""
subscriptions_response["subscriptions"][0]["location"]["system"].pop("isOffline")
async with authenticated_simplisafe_server:
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
),
)
async with aiohttp.ClientSession() as session:
simplisafe = await API.async_from_auth(
TEST_AUTHORIZATION_CODE, TEST_CODE_VERIFIER, session=session
)
systems = await simplisafe.async_get_systems()
system: SystemV3 = cast(SystemV3, systems[TEST_SYSTEM_ID])
assert system.offline is False
assert any(
"SimpliSafe didn't return data for property: offline" in e.message
for e in caplog.records
)
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_missing_system_info(
aresponses: ResponsesMockServer,
authenticated_simplisafe_server: ResponsesMockServer,
caplog: Mock,
subscriptions_response: dict[str, Any],
) -> None:
"""Test that a subscription with missing system data is handled correctly.
Args:
aresponses: An aresponses server.
authenticated_simplisafe_server: A authenticated API connection.
caplog: A mocked logging utility.
subscriptions_response: An API response payload.
"""
subscriptions_response["subscriptions"][0]["location"]["system"] = {}
async with authenticated_simplisafe_server:
authenticated_simplisafe_server.add(
"api.simplisafe.com",
f"/v1/users/{TEST_SUBSCRIPTION_ID}/subscriptions",
"get",
response=aiohttp.web_response.json_response(
subscriptions_response, status=200
),
)
async with aiohttp.ClientSession() as session:
simplisafe = await API.async_from_auth(
TEST_AUTHORIZATION_CODE, TEST_CODE_VERIFIER, session=session
)
await simplisafe.async_get_systems()
assert any(
"Skipping subscription with missing system data" in e.message
for e in caplog.records
)
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_properties(
aresponses: ResponsesMockServer,
authenticated_simplisafe_server_v2: ResponsesMockServer,
) -> None:
"""Test that base system properties are created properly.
Args:
aresponses: An aresponses server.
authenticated_simplisafe_server_v2: A authenticated API connection.
"""
async with authenticated_simplisafe_server_v2, aiohttp.ClientSession() as session:
simplisafe = await API.async_from_auth(
TEST_AUTHORIZATION_CODE, TEST_CODE_VERIFIER, session=session
)
systems = await simplisafe.async_get_systems()
system = systems[TEST_SYSTEM_ID]
assert not system.alarm_going_off
assert system.address == TEST_ADDRESS
assert system.connection_type == "wifi"
assert system.serial == TEST_SYSTEM_SERIAL_NO
assert system.state == SystemStates.OFF
assert system.system_id == TEST_SYSTEM_ID
assert system.temperature == 67
assert system.version == 2
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_unknown_sensor_type(
aresponses: ResponsesMockServer,
authenticated_simplisafe_server_v2: ResponsesMockServer,
caplog: Mock,
) -> None:
"""Test whether a message is logged upon finding an unknown sensor type.
Args:
aresponses: An aresponses server.
authenticated_simplisafe_server_v2: A authenticated API connection.
caplog: A mocked logging utility.
"""
async with authenticated_simplisafe_server_v2, aiohttp.ClientSession() as session:
simplisafe = await API.async_from_auth(
TEST_AUTHORIZATION_CODE, TEST_CODE_VERIFIER, session=session
)
await simplisafe.async_get_systems()
assert any("Unknown device type" in e.message for e in caplog.records)
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_unknown_system_state( # pylint: disable=too-many-arguments
aresponses: ResponsesMockServer,
authenticated_simplisafe_server: ResponsesMockServer,
caplog: Mock,
subscriptions_response: dict[str, Any],
v3_sensors_response: dict[str, Any],
v3_settings_response: dict[str, Any],
) -> None:
"""Test that an unknown system state is logged.
Args:
aresponses: An aresponses server.
authenticated_simplisafe_server: A authenticated API connection.
caplog: A mocked logging utility.
subscriptions_response: An API response payload.
v3_sensors_response: An API response payload.
v3_settings_response: An API response payload.
"""
subscriptions_response["subscriptions"][0]["location"]["system"][
"alarmState"
] = "NOT_REAL_STATE"
async with authenticated_simplisafe_server:
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
),
)
async with aiohttp.ClientSession() as session:
simplisafe = await API.async_from_auth(
TEST_AUTHORIZATION_CODE, TEST_CODE_VERIFIER, session=session
)
await simplisafe.async_get_systems()
assert any("Unknown raw system state" in e.message for e in caplog.records)
assert any("NOT_REAL_STATE" in e.message for e in caplog.records)
aresponses.assert_plan_strictly_followed()
|