|
| 1 | +"""Test the PlayStation Network image platform.""" |
| 2 | + |
| 3 | +from collections.abc import Generator |
| 4 | +from datetime import timedelta |
| 5 | +from http import HTTPStatus |
| 6 | +from unittest.mock import MagicMock, patch |
| 7 | + |
| 8 | +from freezegun.api import FrozenDateTimeFactory |
| 9 | +import pytest |
| 10 | +import respx |
| 11 | + |
| 12 | +from homeassistant.config_entries import ConfigEntryState |
| 13 | +from homeassistant.const import Platform |
| 14 | +from homeassistant.core import HomeAssistant |
| 15 | + |
| 16 | +from tests.common import MockConfigEntry, async_fire_time_changed |
| 17 | +from tests.typing import ClientSessionGenerator |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture(autouse=True) |
| 21 | +def image_only() -> Generator[None]: |
| 22 | + """Enable only the image platform.""" |
| 23 | + with patch( |
| 24 | + "homeassistant.components.playstation_network.PLATFORMS", |
| 25 | + [Platform.IMAGE], |
| 26 | + ): |
| 27 | + yield |
| 28 | + |
| 29 | + |
| 30 | +@respx.mock |
| 31 | +@pytest.mark.usefixtures("mock_psnawpapi") |
| 32 | +async def test_image_platform( |
| 33 | + hass: HomeAssistant, |
| 34 | + config_entry: MockConfigEntry, |
| 35 | + hass_client: ClientSessionGenerator, |
| 36 | + freezer: FrozenDateTimeFactory, |
| 37 | + mock_psnawpapi: MagicMock, |
| 38 | +) -> None: |
| 39 | + """Test image platform.""" |
| 40 | + freezer.move_to("2025-06-16T00:00:00-00:00") |
| 41 | + |
| 42 | + respx.get( |
| 43 | + "http://static-resource.np.community.playstation.net/avatar_xl/WWS_A/UP90001312L24_DD96EB6A4FF5FE883C09_XL.png" |
| 44 | + ).respond(status_code=HTTPStatus.OK, content_type="image/png", content=b"Test") |
| 45 | + config_entry.add_to_hass(hass) |
| 46 | + await hass.config_entries.async_setup(config_entry.entry_id) |
| 47 | + await hass.async_block_till_done() |
| 48 | + |
| 49 | + assert config_entry.state is ConfigEntryState.LOADED |
| 50 | + |
| 51 | + assert (state := hass.states.get("image.testuser_avatar")) |
| 52 | + assert state.state == "2025-06-16T00:00:00+00:00" |
| 53 | + |
| 54 | + access_token = state.attributes["access_token"] |
| 55 | + assert ( |
| 56 | + state.attributes["entity_picture"] |
| 57 | + == f"/api/image_proxy/image.testuser_avatar?token={access_token}" |
| 58 | + ) |
| 59 | + |
| 60 | + client = await hass_client() |
| 61 | + resp = await client.get(state.attributes["entity_picture"]) |
| 62 | + assert resp.status == HTTPStatus.OK |
| 63 | + body = await resp.read() |
| 64 | + assert body == b"Test" |
| 65 | + assert resp.content_type == "image/png" |
| 66 | + assert resp.content_length == 4 |
| 67 | + |
| 68 | + ava = "https://static-resource.np.community.playstation.net/avatar_m/WWS_E/E0011_m.png" |
| 69 | + profile = mock_psnawpapi.user.return_value.profile.return_value |
| 70 | + profile["avatars"] = [{"size": "xl", "url": ava}] |
| 71 | + mock_psnawpapi.user.return_value.profile.return_value = profile |
| 72 | + respx.get(ava).respond( |
| 73 | + status_code=HTTPStatus.OK, content_type="image/png", content=b"Test2" |
| 74 | + ) |
| 75 | + |
| 76 | + freezer.tick(timedelta(seconds=30)) |
| 77 | + async_fire_time_changed(hass) |
| 78 | + await hass.async_block_till_done() |
| 79 | + await hass.async_block_till_done() |
| 80 | + |
| 81 | + assert (state := hass.states.get("image.testuser_avatar")) |
| 82 | + assert state.state == "2025-06-16T00:00:30+00:00" |
| 83 | + |
| 84 | + access_token = state.attributes["access_token"] |
| 85 | + assert ( |
| 86 | + state.attributes["entity_picture"] |
| 87 | + == f"/api/image_proxy/image.testuser_avatar?token={access_token}" |
| 88 | + ) |
| 89 | + |
| 90 | + client = await hass_client() |
| 91 | + resp = await client.get(state.attributes["entity_picture"]) |
| 92 | + assert resp.status == HTTPStatus.OK |
| 93 | + body = await resp.read() |
| 94 | + assert body == b"Test2" |
| 95 | + assert resp.content_type == "image/png" |
| 96 | + assert resp.content_length == 5 |
0 commit comments