|
| 1 | +"""Common fixtures for the Xbox tests.""" |
| 2 | + |
| 3 | +from collections.abc import Generator |
| 4 | +from unittest.mock import AsyncMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | +from xbox.webapi.api.provider.catalog.models import CatalogResponse |
| 8 | +from xbox.webapi.api.provider.people.models import PeopleResponse |
| 9 | +from xbox.webapi.api.provider.smartglass.models import ( |
| 10 | + SmartglassConsoleList, |
| 11 | + SmartglassConsoleStatus, |
| 12 | +) |
| 13 | + |
| 14 | +from homeassistant.components.application_credentials import ( |
| 15 | + ClientCredential, |
| 16 | + async_import_client_credential, |
| 17 | +) |
| 18 | +from homeassistant.components.xbox.const import DOMAIN |
| 19 | +from homeassistant.core import HomeAssistant |
| 20 | +from homeassistant.setup import async_setup_component |
| 21 | + |
| 22 | +from tests.common import MockConfigEntry, load_json_object_fixture |
| 23 | + |
| 24 | +CLIENT_ID = "1234" |
| 25 | +CLIENT_SECRET = "5678" |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(autouse=True) |
| 29 | +async def setup_credentials(hass: HomeAssistant) -> None: |
| 30 | + """Fixture to setup credentials.""" |
| 31 | + assert await async_setup_component(hass, "application_credentials", {}) |
| 32 | + await async_import_client_credential( |
| 33 | + hass, DOMAIN, ClientCredential(CLIENT_ID, CLIENT_SECRET), "imported-cred" |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture(autouse=True) |
| 38 | +def mock_oauth2_implementation() -> Generator[AsyncMock]: |
| 39 | + """Mock config entry oauth2 implementation.""" |
| 40 | + with patch( |
| 41 | + "homeassistant.components.xbox.config_entry_oauth2_flow.async_get_config_entry_implementation", |
| 42 | + return_value=AsyncMock(), |
| 43 | + ) as mock_client: |
| 44 | + client = mock_client.return_value |
| 45 | + |
| 46 | + yield client |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture(name="config_entry") |
| 50 | +def mock_config_entry() -> MockConfigEntry: |
| 51 | + """Mock Xbox configuration entry.""" |
| 52 | + return MockConfigEntry( |
| 53 | + domain=DOMAIN, |
| 54 | + title="Home Assistant Cloud", |
| 55 | + data={ |
| 56 | + "auth_implementation": "cloud", |
| 57 | + "token": { |
| 58 | + "access_token": "1234567890", |
| 59 | + "expires_at": 1760697327.7298331, |
| 60 | + "expires_in": 3600, |
| 61 | + "refresh_token": "0987654321", |
| 62 | + "scope": "XboxLive.signin XboxLive.offline_access", |
| 63 | + "service": "xbox", |
| 64 | + "token_type": "bearer", |
| 65 | + "user_id": "AAAAAAAAAAAAAAAAAAAAA", |
| 66 | + }, |
| 67 | + }, |
| 68 | + unique_id="xbox", |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.fixture(name="signed_session") |
| 73 | +def mock_signed_session() -> Generator[AsyncMock]: |
| 74 | + """Mock xbox-webapi SignedSession.""" |
| 75 | + |
| 76 | + with patch( |
| 77 | + "homeassistant.components.xbox.SignedSession", autospec=True |
| 78 | + ) as mock_client: |
| 79 | + client = mock_client.return_value |
| 80 | + |
| 81 | + yield client |
| 82 | + |
| 83 | + |
| 84 | +@pytest.fixture(name="xbox_live_client") |
| 85 | +def mock_xbox_live_client(signed_session) -> Generator[AsyncMock]: |
| 86 | + """Mock xbox-webapi XboxLiveClient.""" |
| 87 | + |
| 88 | + with patch( |
| 89 | + "homeassistant.components.xbox.XboxLiveClient", autospec=True |
| 90 | + ) as mock_client: |
| 91 | + client = mock_client.return_value |
| 92 | + |
| 93 | + client.smartglass = AsyncMock() |
| 94 | + client.smartglass.get_console_list.return_value = SmartglassConsoleList( |
| 95 | + **load_json_object_fixture("smartglass_console_list.json", DOMAIN) |
| 96 | + ) |
| 97 | + client.smartglass.get_console_status.return_value = SmartglassConsoleStatus( |
| 98 | + **load_json_object_fixture("smartglass_console_status.json", DOMAIN) |
| 99 | + ) |
| 100 | + |
| 101 | + client.catalog = AsyncMock() |
| 102 | + client.catalog.get_product_from_alternate_id.return_value = CatalogResponse( |
| 103 | + **load_json_object_fixture("catalog_product_lookup.json", DOMAIN) |
| 104 | + ) |
| 105 | + |
| 106 | + client.people = AsyncMock() |
| 107 | + client.people.get_friends_own_batch.return_value = PeopleResponse( |
| 108 | + **load_json_object_fixture("people_batch.json", DOMAIN) |
| 109 | + ) |
| 110 | + client.people.get_friends_own.return_value = PeopleResponse( |
| 111 | + **load_json_object_fixture("people_friends_own.json", DOMAIN) |
| 112 | + ) |
| 113 | + yield client |
0 commit comments