mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Improve cloud system health tests (#106235)
This commit is contained in:
parent
5156a93b9e
commit
1d0cee5e8a
@ -1,5 +1,5 @@
|
||||
"""Fixtures for cloud tests."""
|
||||
from collections.abc import AsyncGenerator
|
||||
from collections.abc import AsyncGenerator, Callable, Coroutine
|
||||
from typing import Any
|
||||
from unittest.mock import DEFAULT, MagicMock, PropertyMock, patch
|
||||
|
||||
@ -15,6 +15,7 @@ import jwt
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.cloud import CloudClient, const, prefs
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from . import mock_cloud, mock_cloud_prefs
|
||||
|
||||
@ -62,7 +63,8 @@ async def cloud_fixture() -> AsyncGenerator[MagicMock, None]:
|
||||
f"{name}_server": server
|
||||
for name, server in DEFAULT_SERVERS[mode].items()
|
||||
}
|
||||
mock_cloud.configure_mock(**default_values, **servers, **kwargs)
|
||||
mock_cloud.configure_mock(**default_values, **servers)
|
||||
mock_cloud.configure_mock(**kwargs)
|
||||
mock_cloud.mode = mode
|
||||
|
||||
# Properties that we mock as attributes from the constructor.
|
||||
@ -101,7 +103,15 @@ async def cloud_fixture() -> AsyncGenerator[MagicMock, None]:
|
||||
claims = PropertyMock(side_effect=mock_claims)
|
||||
type(mock_cloud).claims = claims
|
||||
|
||||
def mock_is_connected() -> bool:
|
||||
"""Return True if we are connected."""
|
||||
return mock_cloud.iot.state == STATE_CONNECTED
|
||||
|
||||
is_connected = PropertyMock(side_effect=mock_is_connected)
|
||||
type(mock_cloud).is_connected = is_connected
|
||||
|
||||
# Properties that we mock as attributes.
|
||||
mock_cloud.expiration_date = utcnow()
|
||||
mock_cloud.subscription_expired = False
|
||||
|
||||
# Methods that we mock with a custom side effect.
|
||||
@ -119,6 +129,23 @@ async def cloud_fixture() -> AsyncGenerator[MagicMock, None]:
|
||||
yield mock_cloud
|
||||
|
||||
|
||||
@pytest.fixture(name="set_cloud_prefs")
|
||||
def set_cloud_prefs_fixture(
|
||||
cloud: MagicMock,
|
||||
) -> Callable[[dict[str, Any]], Coroutine[Any, Any, None]]:
|
||||
"""Fixture for cloud component."""
|
||||
|
||||
async def set_cloud_prefs(prefs_settings: dict[str, Any]) -> None:
|
||||
"""Set cloud prefs."""
|
||||
prefs_to_set = cloud.client.prefs.as_dict()
|
||||
prefs_to_set.pop(prefs.PREF_ALEXA_DEFAULT_EXPOSE)
|
||||
prefs_to_set.pop(prefs.PREF_GOOGLE_DEFAULT_EXPOSE)
|
||||
prefs_to_set.update(prefs_settings)
|
||||
await cloud.client.prefs.async_update(**prefs_to_set)
|
||||
|
||||
return set_cloud_prefs
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_tts_cache_dir_autouse(mock_tts_cache_dir):
|
||||
"""Mock the TTS cache dir with empty dir."""
|
||||
|
@ -1,20 +1,25 @@
|
||||
"""Test cloud system health."""
|
||||
import asyncio
|
||||
from unittest.mock import Mock
|
||||
from collections.abc import Callable, Coroutine
|
||||
from typing import Any
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from aiohttp import ClientError
|
||||
from hass_nabucasa.remote import CertificateStatus
|
||||
|
||||
from homeassistant.components.cloud import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from tests.common import get_system_health_info
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
|
||||
async def test_cloud_system_health(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
cloud: MagicMock,
|
||||
set_cloud_prefs: Callable[[dict[str, Any]], Coroutine[Any, Any, None]],
|
||||
) -> None:
|
||||
"""Test cloud system health."""
|
||||
aioclient_mock.get("https://cloud.bla.com/status", text="")
|
||||
@ -23,32 +28,27 @@ async def test_cloud_system_health(
|
||||
"https://cognito-idp.us-east-1.amazonaws.com/AAAA/.well-known/jwks.json",
|
||||
exc=ClientError,
|
||||
)
|
||||
hass.config.components.add("cloud")
|
||||
assert await async_setup_component(hass, "system_health", {})
|
||||
now = utcnow()
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
{
|
||||
DOMAIN: {
|
||||
"user_pool_id": "AAAA",
|
||||
"region": "us-east-1",
|
||||
"acme_server": "cert-server",
|
||||
"relayer_server": "cloud.bla.com",
|
||||
},
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.data["cloud"] = Mock(
|
||||
region="us-east-1",
|
||||
user_pool_id="AAAA",
|
||||
relayer_server="cloud.bla.com",
|
||||
acme_server="cert-server",
|
||||
is_logged_in=True,
|
||||
remote=Mock(
|
||||
is_connected=False,
|
||||
snitun_server="us-west-1",
|
||||
certificate_status=CertificateStatus.READY,
|
||||
),
|
||||
expiration_date=now,
|
||||
is_connected=True,
|
||||
client=Mock(
|
||||
relayer_region="xx-earth-616",
|
||||
prefs=Mock(
|
||||
remote_enabled=True,
|
||||
alexa_enabled=True,
|
||||
google_enabled=False,
|
||||
instance_id="12345678901234567890",
|
||||
),
|
||||
),
|
||||
cloud.remote.snitun_server = "us-west-1"
|
||||
cloud.remote.certificate_status = CertificateStatus.READY
|
||||
|
||||
await cloud.client.async_system_message({"region": "xx-earth-616"})
|
||||
await set_cloud_prefs(
|
||||
{"alexa_enabled": True, "google_enabled": False, "remote_enabled": True}
|
||||
)
|
||||
|
||||
info = await get_system_health_info(hass, "cloud")
|
||||
@ -59,8 +59,8 @@ async def test_cloud_system_health(
|
||||
|
||||
assert info == {
|
||||
"logged_in": True,
|
||||
"subscription_expiration": now,
|
||||
"certificate_status": "ready",
|
||||
"subscription_expiration": cloud.expiration_date,
|
||||
"certificate_status": CertificateStatus.READY,
|
||||
"relayer_connected": True,
|
||||
"relayer_region": "xx-earth-616",
|
||||
"remote_enabled": True,
|
||||
@ -71,5 +71,5 @@ async def test_cloud_system_health(
|
||||
"can_reach_cert_server": "ok",
|
||||
"can_reach_cloud_auth": {"type": "failed", "error": "unreachable"},
|
||||
"can_reach_cloud": "ok",
|
||||
"instance_id": "12345678901234567890",
|
||||
"instance_id": cloud.client.prefs.instance_id,
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user