2020-06-16 18:16:18 +00:00
|
|
|
"""Fixtures for tests."""
|
2023-09-11 19:06:20 +00:00
|
|
|
from datetime import timedelta
|
2023-09-09 17:11:28 +00:00
|
|
|
import time
|
2023-09-11 19:06:20 +00:00
|
|
|
from unittest.mock import AsyncMock, patch
|
2020-06-16 18:16:18 +00:00
|
|
|
|
|
|
|
import pytest
|
2023-09-11 19:06:20 +00:00
|
|
|
from withings_api import (
|
|
|
|
MeasureGetMeasResponse,
|
|
|
|
NotifyListResponse,
|
|
|
|
SleepGetSummaryResponse,
|
|
|
|
UserGetDeviceResponse,
|
|
|
|
)
|
2020-06-16 18:16:18 +00:00
|
|
|
|
2023-09-09 17:11:28 +00:00
|
|
|
from homeassistant.components.application_credentials import (
|
|
|
|
ClientCredential,
|
|
|
|
async_import_client_credential,
|
|
|
|
)
|
2023-09-11 19:06:20 +00:00
|
|
|
from homeassistant.components.withings.common import ConfigEntryWithingsApi
|
2023-09-09 17:11:28 +00:00
|
|
|
from homeassistant.components.withings.const import DOMAIN
|
2020-06-16 18:16:18 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-09-09 17:11:28 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2020-06-16 18:16:18 +00:00
|
|
|
|
2023-09-11 10:36:37 +00:00
|
|
|
from .common import ComponentFactory
|
2020-06-16 18:16:18 +00:00
|
|
|
|
2023-09-11 19:06:20 +00:00
|
|
|
from tests.common import MockConfigEntry, load_json_object_fixture
|
2020-06-16 18:16:18 +00:00
|
|
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
|
|
|
|
2023-09-09 17:11:28 +00:00
|
|
|
CLIENT_ID = "1234"
|
|
|
|
CLIENT_SECRET = "5678"
|
|
|
|
SCOPES = [
|
|
|
|
"user.info",
|
|
|
|
"user.metrics",
|
|
|
|
"user.activity",
|
|
|
|
"user.sleepevents",
|
|
|
|
]
|
|
|
|
TITLE = "henk"
|
2023-09-11 10:36:37 +00:00
|
|
|
USER_ID = 12345
|
2023-09-09 17:11:28 +00:00
|
|
|
WEBHOOK_ID = "55a7335ea8dee830eed4ef8f84cda8f6d80b83af0847dc74032e86120bffed5e"
|
|
|
|
|
2020-06-16 18:16:18 +00:00
|
|
|
|
2023-01-26 17:05:05 +00:00
|
|
|
@pytest.fixture
|
2020-06-16 18:16:18 +00:00
|
|
|
def component_factory(
|
2022-09-27 17:22:52 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
hass_client_no_auth,
|
|
|
|
aioclient_mock: AiohttpClientMocker,
|
|
|
|
current_request_with_host: None,
|
2020-06-16 18:16:18 +00:00
|
|
|
):
|
|
|
|
"""Return a factory for initializing the withings component."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.withings.common.ConfigEntryWithingsApi"
|
|
|
|
) as api_class_mock:
|
2021-09-18 21:12:02 +00:00
|
|
|
yield ComponentFactory(
|
|
|
|
hass, api_class_mock, hass_client_no_auth, aioclient_mock
|
|
|
|
)
|
2023-09-09 17:11:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="scopes")
|
|
|
|
def mock_scopes() -> list[str]:
|
|
|
|
"""Fixture to set the scopes present in the OAuth token."""
|
|
|
|
return SCOPES
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
async def setup_credentials(hass: HomeAssistant) -> None:
|
|
|
|
"""Fixture to setup credentials."""
|
|
|
|
assert await async_setup_component(hass, "application_credentials", {})
|
|
|
|
await async_import_client_credential(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
ClientCredential(CLIENT_ID, CLIENT_SECRET),
|
|
|
|
DOMAIN,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="expires_at")
|
|
|
|
def mock_expires_at() -> int:
|
|
|
|
"""Fixture to set the oauth token expiration time."""
|
|
|
|
return time.time() + 3600
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="config_entry")
|
|
|
|
def mock_config_entry(expires_at: int, scopes: list[str]) -> MockConfigEntry:
|
|
|
|
"""Create Withings entry in Home Assistant."""
|
|
|
|
return MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
title=TITLE,
|
2023-09-11 10:36:37 +00:00
|
|
|
unique_id=str(USER_ID),
|
2023-09-09 17:11:28 +00:00
|
|
|
data={
|
|
|
|
"auth_implementation": DOMAIN,
|
|
|
|
"token": {
|
|
|
|
"status": 0,
|
2023-09-11 10:36:37 +00:00
|
|
|
"userid": str(USER_ID),
|
2023-09-09 17:11:28 +00:00
|
|
|
"access_token": "mock-access-token",
|
|
|
|
"refresh_token": "mock-refresh-token",
|
|
|
|
"expires_at": expires_at,
|
|
|
|
"scope": ",".join(scopes),
|
|
|
|
},
|
|
|
|
"profile": TITLE,
|
|
|
|
"use_webhook": True,
|
|
|
|
"webhook_id": WEBHOOK_ID,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-11 19:06:20 +00:00
|
|
|
@pytest.fixture(name="withings")
|
|
|
|
def mock_withings():
|
|
|
|
"""Mock withings."""
|
2023-09-09 17:11:28 +00:00
|
|
|
|
2023-09-11 19:06:20 +00:00
|
|
|
mock = AsyncMock(spec=ConfigEntryWithingsApi)
|
|
|
|
mock.user_get_device.return_value = UserGetDeviceResponse(
|
|
|
|
**load_json_object_fixture("withings/get_device.json")
|
2023-09-09 17:11:28 +00:00
|
|
|
)
|
2023-09-11 19:06:20 +00:00
|
|
|
mock.measure_get_meas.return_value = MeasureGetMeasResponse(
|
|
|
|
**load_json_object_fixture("withings/get_meas.json")
|
|
|
|
)
|
|
|
|
mock.sleep_get_summary.return_value = SleepGetSummaryResponse(
|
|
|
|
**load_json_object_fixture("withings/get_sleep.json")
|
2023-09-09 17:11:28 +00:00
|
|
|
)
|
2023-09-11 19:06:20 +00:00
|
|
|
mock.notify_list.return_value = NotifyListResponse(
|
|
|
|
**load_json_object_fixture("withings/notify_list.json")
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.withings.common.ConfigEntryWithingsApi",
|
|
|
|
return_value=mock,
|
|
|
|
):
|
|
|
|
yield mock
|
|
|
|
|
2023-09-09 17:11:28 +00:00
|
|
|
|
2023-09-11 19:06:20 +00:00
|
|
|
@pytest.fixture(name="disable_webhook_delay")
|
|
|
|
def disable_webhook_delay():
|
|
|
|
"""Disable webhook delay."""
|
|
|
|
|
|
|
|
mock = AsyncMock()
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.withings.common.SUBSCRIBE_DELAY", timedelta(seconds=0)
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.withings.common.UNSUBSCRIBE_DELAY",
|
|
|
|
timedelta(seconds=0),
|
|
|
|
):
|
|
|
|
yield mock
|