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 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
|
"""Tests for Roku."""
# pylint: disable=protected-access
from datetime import datetime, timedelta
from unittest.mock import AsyncMock
import pytest
from aiohttp import ClientSession
from aresponses import ResponsesMockServer
from freezegun.api import FrozenDateTimeFactory
from rokuecp import Roku, RokuError, models
from . import fake_addrinfo_results, load_fixture
HOST = "192.168.1.86"
PORT = 8060
MATCH_HOST = f"{HOST}:{PORT}"
ICON_BASE = f"http://{MATCH_HOST}/query/icon"
@pytest.mark.asyncio
async def test_loop() -> None:
"""Test loop usage is handled correctly."""
async with Roku(HOST) as roku:
assert isinstance(roku, Roku)
@pytest.mark.asyncio
async def test_app_icon_url() -> None:
"""Test app_icon_url is handled correctly."""
async with ClientSession() as session:
roku = Roku(HOST, session=session)
assert roku.app_icon_url("101") == f"{ICON_BASE}/101"
@pytest.mark.asyncio
@pytest.mark.freeze_time("2022-03-27")
async def test_get_dns_state(
aresponses: ResponsesMockServer,
resolver: AsyncMock,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test get_dns_state is handled correctly."""
aresponses.add(
f"192.168.1.99:{PORT}",
"/support/hostname",
"GET",
aresponses.Response(status=200, text="OK"),
)
aresponses.add(
f"192.168.1.89:{PORT}",
"/support/hostname",
"GET",
aresponses.Response(status=200, text="OK"),
)
async with ClientSession() as session:
roku = Roku(HOST, session=session)
assert roku.get_dns_state() == {
"enabled": False,
"hostname": None,
"ip_address": None,
"resolved_at": None,
}
roku2 = Roku("roku.dev", session=session)
assert roku2.get_dns_state() == {
"enabled": True,
"hostname": "roku.dev",
"ip_address": None,
"resolved_at": None,
}
resolver.return_value = fake_addrinfo_results(["192.168.1.99"])
assert await roku2._request("support/hostname")
dns = roku2.get_dns_state()
assert dns["enabled"]
assert dns["hostname"] == "roku.dev"
assert dns["ip_address"] == "192.168.1.99"
assert dns["resolved_at"] == datetime(2022, 3, 27, 0, 0) # noqa: DTZ001
resolver.return_value = fake_addrinfo_results(["192.168.1.89"])
freezer.tick(delta=timedelta(hours=3))
assert await roku2._request("support/hostname")
dns = roku2.get_dns_state()
assert dns["enabled"]
assert dns["hostname"] == "roku.dev"
assert dns["ip_address"] == "192.168.1.89"
assert dns["resolved_at"] == datetime(2022, 3, 27, 3, 0) # noqa: DTZ001
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_device(aresponses: ResponsesMockServer) -> None:
"""Test app property is handled correctly."""
aresponses.add(
MATCH_HOST,
"/query/device-info",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("device-info.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/apps",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("apps.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/active-app",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("active-app-roku.xml"),
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
await client.update()
assert client.device
assert isinstance(client.device, models.Device)
aresponses.assert_no_unused_routes()
aresponses.assert_all_requests_matched()
@pytest.mark.asyncio
async def test_launch(aresponses: ResponsesMockServer) -> None:
"""Test launch is handled correctly."""
aresponses.add(
MATCH_HOST,
"/launch/101",
"POST",
aresponses.Response(status=200),
)
aresponses.add(
MATCH_HOST,
"/launch/102?contentID=deeplink",
"POST",
aresponses.Response(status=200),
match_querystring=True,
)
async with ClientSession() as session:
roku = Roku(HOST, session=session)
await roku.launch("101")
await roku.launch("102", {"contentID": "deeplink"})
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_play_on_roku(aresponses: ResponsesMockServer) -> None:
"""Test play_on_roku is handled correctly."""
video_url = "http://example.com/video file÷awe.mp4?v=2"
encoded = "http%3A%2F%2Fexample.com%2Fvideo+file%C3%B7awe.mp4%3Fv%3D2"
aresponses.add(
MATCH_HOST,
f"/input/15985?t=v&u={encoded}",
"POST",
aresponses.Response(status=200),
match_querystring=True,
)
async with ClientSession() as session:
roku = Roku(HOST, session=session)
await roku.play_on_roku(video_url)
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_literal(aresponses: ResponsesMockServer) -> None:
"""Test literal is handled correctly."""
aresponses.add(
MATCH_HOST,
"/keypress/Lit_t",
"POST",
aresponses.Response(status=200),
)
aresponses.add(
MATCH_HOST,
"/keypress/Lit_h",
"POST",
aresponses.Response(status=200),
)
aresponses.add(
MATCH_HOST,
"/keypress/Lit_e",
"POST",
aresponses.Response(status=200),
)
aresponses.add(
MATCH_HOST,
"/keypress/Lit_+",
"POST",
aresponses.Response(status=200),
)
aresponses.add(
MATCH_HOST,
"/keypress/Lit_@",
"POST",
aresponses.Response(status=200),
)
async with ClientSession() as session:
roku = Roku(HOST, session=session)
await roku.literal("the @")
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_remote(aresponses: ResponsesMockServer) -> None:
"""Test remote is handled correctly."""
aresponses.add(
MATCH_HOST,
"/keypress/Home",
"POST",
aresponses.Response(status=200),
)
async with ClientSession() as session:
roku = Roku(HOST, session=session)
await roku.remote("home")
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_remote_invalid_key() -> None:
"""Test remote with invalid key is handled correctly."""
async with ClientSession() as session:
roku = Roku(HOST, session=session)
with pytest.raises(RokuError):
await roku.remote("super")
@pytest.mark.asyncio
async def test_remote_search(aresponses: ResponsesMockServer) -> None:
"""Test remote search keypress is handled correctly."""
aresponses.add(
MATCH_HOST,
"/search/browse",
"POST",
aresponses.Response(status=200),
)
async with ClientSession() as session:
roku = Roku(HOST, session=session)
await roku.remote("search")
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_remote_literal(aresponses: ResponsesMockServer) -> None:
"""Test remote literal keypress is handled correctly."""
aresponses.add(
MATCH_HOST,
"/keypress/Lit_t",
"POST",
aresponses.Response(status=200),
)
aresponses.add(
MATCH_HOST,
"/keypress/Lit_h",
"POST",
aresponses.Response(status=200),
)
aresponses.add(
MATCH_HOST,
"/keypress/Lit_e",
"POST",
aresponses.Response(status=200),
)
async with ClientSession() as session:
roku = Roku(HOST, session=session)
await roku.remote("Lit_the")
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_search(aresponses: ResponsesMockServer) -> None:
"""Test search is handled correctly."""
aresponses.add(
MATCH_HOST,
"/search/browse?keyword=test",
"POST",
aresponses.Response(status=200, text="OK"),
match_querystring=True,
)
async with ClientSession() as session:
roku = Roku(HOST, session=session)
await roku.search("test")
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_tune(aresponses: ResponsesMockServer) -> None:
"""Test tune is handled correctly."""
aresponses.add(
MATCH_HOST,
"/launch/tvinput.dtv?ch=13.4&chan=13.4&lcn=13.4",
"POST",
aresponses.Response(status=200, text="OK"),
match_querystring=True,
)
async with ClientSession() as session:
roku = Roku(HOST, session=session)
await roku.tune("13.4")
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_update(aresponses: ResponsesMockServer) -> None:
"""Test update method is handled correctly."""
aresponses.add(
MATCH_HOST,
"/query/device-info",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("device-info.xml"),
),
repeat=2,
)
aresponses.add(
MATCH_HOST,
"/query/apps",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("apps.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/active-app",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("active-app-roku.xml"),
),
repeat=2,
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
response = await client.update()
assert response
assert isinstance(response.info, models.Info)
assert isinstance(response.state, models.State)
assert isinstance(response.apps, list)
assert isinstance(response.channels, list)
assert isinstance(response.app, models.Application)
assert response.channel is None
assert response.media is None
assert response.state.available
assert not response.state.standby
assert len(response.channels) == 0
response = await client.update()
assert response
assert isinstance(response.info, models.Info)
assert isinstance(response.state, models.State)
assert isinstance(response.apps, list)
assert isinstance(response.channels, list)
assert isinstance(response.app, models.Application)
assert response.channel is None
assert response.media is None
assert response.state.available
assert not response.state.standby
assert len(response.channels) == 0
aresponses.assert_no_unused_routes()
aresponses.assert_all_requests_matched()
@pytest.mark.asyncio
async def test_update_media_state(aresponses: ResponsesMockServer) -> None:
"""Test update method is handled correctly with pluto app."""
aresponses.add(
MATCH_HOST,
"/query/device-info",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("device-info.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/apps",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("apps.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/active-app",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("active-app-pluto.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/media-player",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("media-player-pluto-play.xml"),
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
response = await client.update()
assert response
assert isinstance(response.info, models.Info)
assert isinstance(response.media, models.MediaState)
assert isinstance(response.state, models.State)
assert isinstance(response.apps, list)
assert isinstance(response.channels, list)
assert isinstance(response.app, models.Application)
assert response.channel is None
assert response.state.available
assert not response.state.standby
assert len(response.channels) == 0
assert not response.media.live
assert not response.media.paused
assert response.media.duration == 6496
assert response.media.position == 38
aresponses.assert_no_unused_routes()
aresponses.assert_all_requests_matched()
@pytest.mark.asyncio
async def test_update_power_off(aresponses: ResponsesMockServer) -> None:
"""Test update method is handled correctly when power is off."""
aresponses.add(
MATCH_HOST,
"/query/device-info",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("device-info-power-off.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/apps",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("apps.xml"),
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
response = await client.update()
assert response
assert isinstance(response.info, models.Info)
assert isinstance(response.state, models.State)
assert isinstance(response.apps, list)
assert isinstance(response.channels, list)
assert response.app is None
assert response.channel is None
assert response.media is None
assert response.state.available
assert response.state.standby
assert len(response.channels) == 0
aresponses.assert_no_unused_routes()
aresponses.assert_all_requests_matched()
@pytest.mark.asyncio
async def test_update_standby(aresponses: ResponsesMockServer) -> None:
"""Test update method is handled correctly when device transitions to standby."""
aresponses.add(
MATCH_HOST,
"/query/device-info",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("device-info.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/apps",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("apps.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/active-app",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("active-app-pluto.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/media-player",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("media-player-pluto-play.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/device-info",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("device-info-standby.xml"),
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
response = await client.update()
assert response
assert isinstance(response.info, models.Info)
assert isinstance(response.media, models.MediaState)
assert isinstance(response.state, models.State)
assert isinstance(response.apps, list)
assert isinstance(response.channels, list)
assert isinstance(response.app, models.Application)
assert response.channel is None
assert response.state.available
assert not response.state.standby
assert len(response.channels) == 0
assert not response.media.live
assert not response.media.paused
assert response.media.duration == 6496
assert response.media.position == 38
response = await client.update()
assert response
assert isinstance(response.info, models.Info)
assert isinstance(response.state, models.State)
assert isinstance(response.apps, list)
assert isinstance(response.channels, list)
assert response.app is None
assert response.channel is None
assert response.media is None
assert response.state.available
assert response.state.standby
assert len(response.channels) == 0
aresponses.assert_no_unused_routes()
aresponses.assert_all_requests_matched()
@pytest.mark.asyncio
async def test_update_tv(aresponses: ResponsesMockServer) -> None:
"""Test update method is handled correctly for TVs."""
for _ in range(2):
aresponses.add(
MATCH_HOST,
"/query/device-info",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("device-info-7820x.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/apps",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("apps-tv.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/active-app",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("active-app-tv.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/tv-active-channel",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("tv-active-channel.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/tv-channels",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("tv-channels.xml"),
),
)
aresponses.add(
MATCH_HOST,
"/query/tv-channels",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("tv-channels-single.xml"),
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
response = await client.update()
assert response
assert isinstance(response.info, models.Info)
assert isinstance(response.state, models.State)
assert isinstance(response.apps, list)
assert isinstance(response.channels, list)
assert isinstance(response.app, models.Application)
assert isinstance(response.channel, models.Channel)
assert response.media is None
assert response.state.available
assert not response.state.standby
assert len(response.channels) == 2
response = await client.update(True) # noqa: FBT003
assert response
assert isinstance(response.info, models.Info)
assert isinstance(response.state, models.State)
assert isinstance(response.apps, list)
assert isinstance(response.channels, list)
assert isinstance(response.app, models.Application)
assert isinstance(response.channel, models.Channel)
assert response.media is None
assert response.state.available
assert not response.state.standby
assert len(response.channels) == 1
aresponses.assert_no_unused_routes()
aresponses.assert_all_requests_matched()
@pytest.mark.asyncio
async def test_get_active_app(aresponses: ResponsesMockServer) -> None:
"""Test _get_active_app method is handled correctly."""
aresponses.add(
MATCH_HOST,
"/query/active-app",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("active-app-amazon.xml"),
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
assert await client._get_active_app()
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_get_active_app_invalid(aresponses: ResponsesMockServer) -> None:
"""Test _get_active_app method is handled correctly with invalid data."""
aresponses.add(
MATCH_HOST,
"/query/active-app",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text="<other>value</other>",
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
with pytest.raises(RokuError):
assert await client._get_active_app()
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_get_apps(aresponses: ResponsesMockServer) -> None:
"""Test _get_apps method is handled correctly."""
aresponses.add(
MATCH_HOST,
"/query/apps",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("apps.xml"),
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
res = await client._get_apps()
assert isinstance(res, list)
assert len(res) == 8
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_get_apps_invalid(aresponses: ResponsesMockServer) -> None:
"""Test _get_apps method is handled correctly with invalid data."""
aresponses.add(
MATCH_HOST,
"/query/apps",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text="<other>value</other>",
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
with pytest.raises(RokuError):
assert await client._get_apps()
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_get_apps_single_app(aresponses: ResponsesMockServer) -> None:
"""Test _get_apps method is handled correctly with single app."""
aresponses.add(
MATCH_HOST,
"/query/apps",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("apps-single.xml"),
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
res = await client._get_apps()
assert isinstance(res, list)
assert len(res) == 1
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_get_device_info(aresponses: ResponsesMockServer) -> None:
"""Test _get_device_info method is handled correctly."""
aresponses.add(
MATCH_HOST,
"/query/device-info",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text="<other>value</other>",
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
with pytest.raises(RokuError):
assert await client._get_device_info()
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_get_media_state_close(aresponses: ResponsesMockServer) -> None:
"""Test _get_media_state method is handled correctly with closed media."""
aresponses.add(
MATCH_HOST,
"/query/media-player",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("media-player-close.xml"),
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
assert await client._get_media_state()
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_get_media_state_invalid(aresponses: ResponsesMockServer) -> None:
"""Test _get_media_state method is handled correctly with invalid data."""
aresponses.add(
MATCH_HOST,
"/query/media-player",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text="<other>value</other>",
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
with pytest.raises(RokuError):
assert await client._get_media_state()
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_get_media_state_live(aresponses: ResponsesMockServer) -> None:
"""Test _get_media_state method is handled correctly with live media."""
aresponses.add(
MATCH_HOST,
"/query/media-player",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("media-player-pluto-live.xml"),
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
assert await client._get_media_state()
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_get_media_state_pause(aresponses: ResponsesMockServer) -> None:
"""Test _get_media_state method is handled correctly with paused media."""
aresponses.add(
MATCH_HOST,
"/query/media-player",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("media-player-pluto-pause.xml"),
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
assert await client._get_media_state()
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_get_media_state_play(aresponses: ResponsesMockServer) -> None:
"""Test _get_media_state method is handled correctly with playing media."""
aresponses.add(
MATCH_HOST,
"/query/media-player",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("media-player-pluto-play.xml"),
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
assert await client._get_media_state()
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_get_tv_active_channel(aresponses: ResponsesMockServer) -> None:
"""Test _get_tv_active_channel method is handled correctly."""
aresponses.add(
MATCH_HOST,
"/query/tv-active-channel",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text="<other>value</other>",
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
with pytest.raises(RokuError):
assert await client._get_tv_active_channel()
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_get_tv_channels(aresponses: ResponsesMockServer) -> None:
"""Test _get_tv_channels method is handled correctly."""
aresponses.add(
MATCH_HOST,
"/query/tv-channels",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text="<other>value</other>",
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
with pytest.raises(RokuError):
assert await client._get_tv_channels()
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_get_tv_channels_no_channels(aresponses: ResponsesMockServer) -> None:
"""Test _get_tv_channels method is handled correctly with no channels."""
aresponses.add(
MATCH_HOST,
"/query/tv-channels",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("tv-channels-empty.xml"),
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
res = await client._get_tv_channels()
assert isinstance(res, list)
assert len(res) == 0
aresponses.assert_plan_strictly_followed()
@pytest.mark.asyncio
async def test_get_tv_channels_single_channel(aresponses: ResponsesMockServer) -> None:
"""Test _get_tv_channels method is handled correctly with single channel."""
aresponses.add(
MATCH_HOST,
"/query/tv-channels",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/xml"},
text=load_fixture("tv-channels-single.xml"),
),
)
async with ClientSession() as session:
client = Roku(HOST, session=session)
res = await client._get_tv_channels()
assert isinstance(res, list)
assert len(res) == 1
aresponses.assert_plan_strictly_followed()
|