mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00
Move local calendar diagnostics to pytest.mark.freezetime (#90886)
This commit is contained in:
parent
eb469d6a2f
commit
a01952981f
@ -1,43 +1,15 @@
|
|||||||
"""Tests for diagnostics platform of local calendar."""
|
"""Tests for diagnostics platform of local calendar."""
|
||||||
from aiohttp.test_utils import TestClient
|
|
||||||
from freezegun import freeze_time
|
|
||||||
import pytest
|
import pytest
|
||||||
from syrupy.assertion import SnapshotAssertion
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
|
||||||
from homeassistant.auth.models import Credentials
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from .conftest import TEST_ENTITY, Client
|
from .conftest import TEST_ENTITY, ClientFixture
|
||||||
|
|
||||||
from tests.common import CLIENT_ID, MockConfigEntry, MockUser
|
from tests.common import MockConfigEntry
|
||||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||||
from tests.typing import ClientSessionGenerator, WebSocketGenerator
|
from tests.typing import ClientSessionGenerator
|
||||||
|
|
||||||
|
|
||||||
async def generate_new_hass_access_token(
|
|
||||||
hass: HomeAssistant, hass_admin_user: MockUser, hass_admin_credential: Credentials
|
|
||||||
) -> str:
|
|
||||||
"""Return an access token to access Home Assistant."""
|
|
||||||
await hass.auth.async_link_user(hass_admin_user, hass_admin_credential)
|
|
||||||
|
|
||||||
refresh_token = await hass.auth.async_create_refresh_token(
|
|
||||||
hass_admin_user, CLIENT_ID, credential=hass_admin_credential
|
|
||||||
)
|
|
||||||
return hass.auth.async_create_access_token(refresh_token)
|
|
||||||
|
|
||||||
|
|
||||||
def _get_test_client_generator(
|
|
||||||
hass: HomeAssistant, aiohttp_client: ClientSessionGenerator, new_token: str
|
|
||||||
):
|
|
||||||
"""Return a test client generator.""."""
|
|
||||||
|
|
||||||
async def auth_client() -> TestClient:
|
|
||||||
return await aiohttp_client(
|
|
||||||
hass.http.app, headers={"Authorization": f"Bearer {new_token}"}
|
|
||||||
)
|
|
||||||
|
|
||||||
return auth_client
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
@ -46,58 +18,30 @@ async def setup_diag(hass):
|
|||||||
assert await async_setup_component(hass, "diagnostics", {})
|
assert await async_setup_component(hass, "diagnostics", {})
|
||||||
|
|
||||||
|
|
||||||
@freeze_time("2023-03-13 12:05:00-07:00")
|
@pytest.mark.freeze_time("2023-03-13 12:05:00-07:00")
|
||||||
async def test_empty_calendar(
|
async def test_empty_calendar(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
setup_integration: None,
|
setup_integration: None,
|
||||||
hass_admin_user: MockUser,
|
hass_client: ClientSessionGenerator,
|
||||||
hass_admin_credential: Credentials,
|
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
aiohttp_client: ClientSessionGenerator,
|
|
||||||
socket_enabled: None,
|
|
||||||
snapshot: SnapshotAssertion,
|
snapshot: SnapshotAssertion,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test diagnostics against an empty calendar."""
|
"""Test diagnostics against an empty calendar."""
|
||||||
# Since we are freezing time only when we enter this test, we need to
|
data = await get_diagnostics_for_config_entry(hass, hass_client, config_entry)
|
||||||
# manually create a new token and clients since the token created by
|
|
||||||
# the fixtures would not be valid.
|
|
||||||
#
|
|
||||||
# Ideally we would use pytest.mark.freeze_time before the fixtures, but that does not
|
|
||||||
# work with the ical library and freezegun because
|
|
||||||
# `TypeError: '<' not supported between instances of 'FakeDatetimeMeta' and 'FakeDateMeta'`
|
|
||||||
new_token = await generate_new_hass_access_token(
|
|
||||||
hass, hass_admin_user, hass_admin_credential
|
|
||||||
)
|
|
||||||
data = await get_diagnostics_for_config_entry(
|
|
||||||
hass, _get_test_client_generator(hass, aiohttp_client, new_token), config_entry
|
|
||||||
)
|
|
||||||
assert data == snapshot
|
assert data == snapshot
|
||||||
|
|
||||||
|
|
||||||
@freeze_time("2023-03-13 12:05:00-07:00")
|
@pytest.mark.freeze_time("2023-03-13 12:05:00-07:00")
|
||||||
async def test_api_date_time_event(
|
async def test_api_date_time_event(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
setup_integration: None,
|
setup_integration: None,
|
||||||
hass_admin_user: MockUser,
|
|
||||||
hass_admin_credential: Credentials,
|
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
hass_ws_client: WebSocketGenerator,
|
hass_client: ClientSessionGenerator,
|
||||||
aiohttp_client: ClientSessionGenerator,
|
ws_client: ClientFixture,
|
||||||
socket_enabled: None,
|
|
||||||
snapshot: SnapshotAssertion,
|
snapshot: SnapshotAssertion,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test an event with a start/end date time."""
|
"""Test an event with a start/end date time."""
|
||||||
# Since we are freezing time only when we enter this test, we need to
|
client = await ws_client()
|
||||||
# manually create a new token and clients since the token created by
|
|
||||||
# the fixtures would not be valid.
|
|
||||||
#
|
|
||||||
# Ideally we would use pytest.mark.freeze_time before the fixtures, but that does not
|
|
||||||
# work with the ical library and freezegun because
|
|
||||||
# `TypeError: '<' not supported between instances of 'FakeDatetimeMeta' and 'FakeDateMeta'`
|
|
||||||
new_token = await generate_new_hass_access_token(
|
|
||||||
hass, hass_admin_user, hass_admin_credential
|
|
||||||
)
|
|
||||||
client = Client(await hass_ws_client(hass, access_token=new_token))
|
|
||||||
await client.cmd_result(
|
await client.cmd_result(
|
||||||
"create",
|
"create",
|
||||||
{
|
{
|
||||||
@ -110,8 +54,5 @@ async def test_api_date_time_event(
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
data = await get_diagnostics_for_config_entry(hass, hass_client, config_entry)
|
||||||
data = await get_diagnostics_for_config_entry(
|
|
||||||
hass, _get_test_client_generator(hass, aiohttp_client, new_token), config_entry
|
|
||||||
)
|
|
||||||
assert data == snapshot
|
assert data == snapshot
|
||||||
|
Loading…
x
Reference in New Issue
Block a user