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
|
"""Tests for Roku."""
# pylint: disable=protected-access
import asyncio
from datetime import datetime, timedelta
from socket import gaierror
from unittest.mock import AsyncMock, patch
import pytest
from aiohttp import ClientError, ClientResponse, ClientSession
from aresponses import Response, ResponsesMockServer
from freezegun.api import FrozenDateTimeFactory
from rokuecp import Roku
from rokuecp.exceptions import (
RokuConnectionError,
RokuConnectionTimeoutError,
RokuError,
)
from tests import fake_addrinfo_results
HOSTNAME = "roku.local"
HOST = "192.168.1.86"
PORT = 8060
MATCH_HOST = f"{HOST}:{PORT}"
NON_STANDARD_PORT = 3333
@pytest.mark.asyncio
async def test_xml_request(aresponses: ResponsesMockServer) -> None:
"""Test XML response is handled correctly."""
aresponses.add(
MATCH_HOST,
"/response/xml",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text="<status>OK</status>",
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
response = await client._request("response/xml")
assert isinstance(response, dict)
assert response["status"] == "OK"
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_text_xml_request(aresponses: ResponsesMockServer) -> None:
"""Test (text) XML response is handled correctly."""
aresponses.add(
MATCH_HOST,
"/response/text-xml",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "text/xml"},
text="<status>OK</status>",
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
response = await client._request("response/text-xml")
assert isinstance(response, dict)
assert response["status"] == "OK"
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_xml_request_parse_error(aresponses: ResponsesMockServer) -> None:
"""Test invalid XML response is handled correctly."""
aresponses.add(
MATCH_HOST,
"/response/xml-parse-error",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text="<!status>>",
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
with pytest.raises(RokuError):
assert await client._request("response/xml-parse-error")
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_text_request(aresponses: ResponsesMockServer) -> None:
"""Test non XML response is handled correctly."""
aresponses.add(
MATCH_HOST,
"/response/text",
"GET",
aresponses.Response(status=200, text="OK"),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
response = await client._request("response/text")
assert response == "OK"
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_internal_session(aresponses: ResponsesMockServer) -> None:
"""Test JSON response is handled correctly with internal session."""
aresponses.add(
MATCH_HOST,
"/response/xml",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text="<status>OK</status>",
),
)
async with Roku(HOST) as client:
response = await client._request("response/xml")
assert isinstance(response, dict)
assert response["status"] == "OK"
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_post_request(aresponses: ResponsesMockServer) -> None:
"""Test POST requests are handled correctly."""
aresponses.add(
MATCH_HOST,
"/method/post",
"POST",
aresponses.Response(status=200, text="OK"),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
response = await client._request("method/post", method="POST")
assert response == "OK"
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_request_port(aresponses: ResponsesMockServer) -> None:
"""Test the handling of non-standard API port."""
aresponses.add(
f"{HOST}:{NON_STANDARD_PORT}",
"/support/port",
"GET",
aresponses.Response(status=200, text="OK"),
)
async with ClientSession() as session:
client = Roku(host=HOST, port=NON_STANDARD_PORT, session=session)
response = await client._request("support/port")
assert response == "OK"
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_timeout(aresponses: ResponsesMockServer) -> None:
"""Test request timeout from the API."""
# Faking a timeout by sleeping
async def response_handler(_: ClientResponse) -> Response:
await asyncio.sleep(2)
return aresponses.Response(body="Timeout!")
aresponses.add(
MATCH_HOST,
"/timeout",
"GET",
response_handler,
)
async with ClientSession() as session:
client = Roku(HOST, session=session, request_timeout=1)
with pytest.raises(RokuConnectionTimeoutError):
assert await client._request("timeout")
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_client_error() -> None:
"""Test HTTP client error."""
async with ClientSession() as session:
with patch.object(session, "request") as mock:
mock.side_effect = ClientError
client = Roku(HOST, session=session)
with pytest.raises(RokuConnectionError):
assert await client._request("client/error", method="ABC")
@pytest.mark.asyncio
async def test_http_error404(aresponses: ResponsesMockServer) -> None:
"""Test HTTP 404 response handling."""
aresponses.add(
MATCH_HOST,
"/http/404",
"GET",
aresponses.Response(text="Not Found!", status=404),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
with pytest.raises(RokuError):
assert await client._request("http/404")
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_http_error500(aresponses: ResponsesMockServer) -> None:
"""Test HTTP 500 response handling."""
aresponses.add(
MATCH_HOST,
"/http/500",
"GET",
aresponses.Response(text="Internal Server Error", status=500),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
with pytest.raises(RokuError):
assert await client._request("http/500")
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
@pytest.mark.freeze_time("2022-03-27 00:00:00+00:00")
async def test_resolve_hostname(
aresponses: ResponsesMockServer,
resolver: AsyncMock,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test that hostnames are resolved before request."""
resolver.return_value = fake_addrinfo_results([HOST])
aresponses.add(
MATCH_HOST,
"/support/hostname",
"GET",
aresponses.Response(status=200, text="OK"),
)
aresponses.add(
f"192.168.1.68:{PORT}",
"/support/hostname",
"GET",
aresponses.Response(status=200, text="OK"),
)
async with ClientSession() as session:
client = Roku(HOSTNAME, session=session)
assert await client._request("support/hostname")
dns = client.get_dns_state()
assert dns["enabled"]
assert dns["hostname"] == HOSTNAME
assert dns["ip_address"] == HOST
assert dns["resolved_at"] == datetime(2022, 3, 27, 0, 0) # noqa: DTZ001
freezer.tick(delta=timedelta(hours=3))
resolver.return_value = fake_addrinfo_results(["192.168.1.68"])
assert await client._request("support/hostname")
dns = client.get_dns_state()
assert dns["enabled"]
assert dns["hostname"] == HOSTNAME
assert dns["ip_address"] == "192.168.1.68"
assert dns["resolved_at"] == datetime(2022, 3, 27, 3, 0) # noqa: DTZ001
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
@pytest.mark.freeze_time("2022-03-27 00:00:00+00:00")
async def test_resolve_hostname_multiple_clients(
aresponses: ResponsesMockServer,
resolver: AsyncMock,
) -> None:
"""Test that hostnames are resolved before request with multiple clients."""
aresponses.add(
MATCH_HOST,
"/support/hostname",
"GET",
aresponses.Response(status=200, text="OK"),
)
aresponses.add(
f"192.168.1.99:{PORT}",
"/support/hostname",
"GET",
aresponses.Response(status=200, text="OK"),
)
async with ClientSession() as session:
resolver.return_value = fake_addrinfo_results([HOST])
client = Roku(HOSTNAME, session=session)
assert await client._request("support/hostname")
dns = client.get_dns_state()
assert dns["enabled"]
assert dns["hostname"] == HOSTNAME
assert dns["ip_address"] == HOST
assert dns["resolved_at"] == datetime(2022, 3, 27, 0, 0) # noqa: DTZ001
resolver.return_value = fake_addrinfo_results(["192.168.1.99"])
client2 = Roku("roku.dev", session=session)
assert await client2._request("support/hostname")
dns2 = client2.get_dns_state()
assert dns2["enabled"]
assert dns2["hostname"] == "roku.dev"
assert dns2["ip_address"] == "192.168.1.99"
assert dns2["resolved_at"] == datetime(2022, 3, 27, 0, 0) # noqa: DTZ001
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_resolve_hostname_error(resolver: AsyncMock) -> None:
"""Test that hostname resolution errors are handled."""
resolver.side_effect = gaierror
async with ClientSession() as session:
client = Roku(HOSTNAME, session=session)
with pytest.raises(RokuConnectionError):
await client._request("support/hostname-error")
|