|
1 | 1 | """The tests for the LG webOS media player platform.""" |
2 | 2 | import asyncio |
3 | 3 | from datetime import timedelta |
| 4 | +from http import HTTPStatus |
4 | 5 | from unittest.mock import Mock |
5 | 6 |
|
6 | 7 | import pytest |
@@ -697,3 +698,68 @@ async def test_supported_features_ignore_cache(hass, client): |
697 | 698 | attrs = hass.states.get(ENTITY_ID).attributes |
698 | 699 |
|
699 | 700 | assert attrs[ATTR_SUPPORTED_FEATURES] == supported |
| 701 | + |
| 702 | + |
| 703 | +async def test_get_image_http( |
| 704 | + hass, client, hass_client_no_auth, aioclient_mock, monkeypatch |
| 705 | +): |
| 706 | + """Test get image via http.""" |
| 707 | + url = "http://something/valid_icon" |
| 708 | + monkeypatch.setitem(client.apps[LIVE_TV_APP_ID], "icon", url) |
| 709 | + await setup_webostv(hass) |
| 710 | + await client.mock_state_update() |
| 711 | + |
| 712 | + attrs = hass.states.get(ENTITY_ID).attributes |
| 713 | + assert "entity_picture_local" not in attrs |
| 714 | + |
| 715 | + aioclient_mock.get(url, text="image") |
| 716 | + client = await hass_client_no_auth() |
| 717 | + |
| 718 | + resp = await client.get(attrs["entity_picture"]) |
| 719 | + content = await resp.read() |
| 720 | + |
| 721 | + assert content == b"image" |
| 722 | + |
| 723 | + |
| 724 | +async def test_get_image_http_error( |
| 725 | + hass, client, hass_client_no_auth, aioclient_mock, caplog, monkeypatch |
| 726 | +): |
| 727 | + """Test get image via http error.""" |
| 728 | + url = "http://something/icon_error" |
| 729 | + monkeypatch.setitem(client.apps[LIVE_TV_APP_ID], "icon", url) |
| 730 | + await setup_webostv(hass) |
| 731 | + await client.mock_state_update() |
| 732 | + |
| 733 | + attrs = hass.states.get(ENTITY_ID).attributes |
| 734 | + assert "entity_picture_local" not in attrs |
| 735 | + |
| 736 | + aioclient_mock.get(url, exc=asyncio.TimeoutError()) |
| 737 | + client = await hass_client_no_auth() |
| 738 | + |
| 739 | + resp = await client.get(attrs["entity_picture"]) |
| 740 | + content = await resp.read() |
| 741 | + |
| 742 | + assert resp.status == HTTPStatus.INTERNAL_SERVER_ERROR |
| 743 | + assert f"Error retrieving proxied image from {url}" in caplog.text |
| 744 | + assert content == b"" |
| 745 | + |
| 746 | + |
| 747 | +async def test_get_image_https( |
| 748 | + hass, client, hass_client_no_auth, aioclient_mock, monkeypatch |
| 749 | +): |
| 750 | + """Test get image via http.""" |
| 751 | + url = "https://something/valid_icon_https" |
| 752 | + monkeypatch.setitem(client.apps[LIVE_TV_APP_ID], "icon", url) |
| 753 | + await setup_webostv(hass) |
| 754 | + await client.mock_state_update() |
| 755 | + |
| 756 | + attrs = hass.states.get(ENTITY_ID).attributes |
| 757 | + assert "entity_picture_local" not in attrs |
| 758 | + |
| 759 | + aioclient_mock.get(url, text="https_image") |
| 760 | + client = await hass_client_no_auth() |
| 761 | + |
| 762 | + resp = await client.get(attrs["entity_picture"]) |
| 763 | + content = await resp.read() |
| 764 | + |
| 765 | + assert content == b"https_image" |
0 commit comments