Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add myuplink diagnostics download #109925

Merged
merged 12 commits into from Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion tests/components/myuplink/__init__.py
@@ -1 +1,11 @@
"""Tests for the myUplink integration."""
"""Tests for the myuplink integration."""
from homeassistant.core import HomeAssistant

from tests.common import MockConfigEntry


async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None:
"""Fixture for setting up the component."""
config_entry.add_to_hass(hass)

await hass.config_entries.async_setup(config_entry.entry_id)
91 changes: 67 additions & 24 deletions tests/components/myuplink/conftest.py
@@ -1,59 +1,102 @@
"""Common fixtures and mocks for myplink tests."""

"""Test helpers for myuplink."""
from collections.abc import Generator
from unittest.mock import MagicMock, patch
import time
from unittest.mock import AsyncMock, MagicMock, patch

import pytest

from homeassistant.components.application_credentials import (
ClientCredential,
async_import_client_credential,
)
from homeassistant.components.myuplink.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

from tests.common import MockConfigEntry, load_json_value_fixture
from .const import CLIENT_ID, CLIENT_SECRET

from tests.common import MockConfigEntry, load_fixture, load_json_value_fixture


@pytest.fixture(name="jwt")
def load_jwt_fixture():
"""Load Fixture data."""
return load_fixture("jwt", DOMAIN)

astrandb marked this conversation as resolved.
Show resolved Hide resolved

@pytest.fixture(name="expires_at")
def mock_expires_at() -> float:
"""Fixture to set the oauth token expiration time."""
return time.time() + 3600


@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
def mock_config_entry(jwt, expires_at: int) -> MockConfigEntry:
"""Return the default mocked config entry."""
return MockConfigEntry(
title="MyUplink Setup",
version=1,
domain=DOMAIN,
data={},
title="myUplink test",
data={
"auth_implementation": DOMAIN,
"token": {
"access_token": jwt,
"scope": "READSYSTEM offline",
"expires_in": 86399,
"refresh_token": "3012bc9f-7a65-4240-b817-9154ffdcc30f",
"token_type": "Bearer",
"expires_at": expires_at,
},
},
entry_id="myuplink_test",
)


@pytest.fixture
def mock_myuplink(request: pytest.FixtureRequest) -> Generator[None, MagicMock, None]:
"""Return a mocked myuplink client."""
@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
def mock_myuplink_client() -> Generator[MagicMock, AsyncMock, None]:
"""Mock a myuplink client."""
with patch(
"homeassistant.components.myuplink.MyUplinkAPI", autospec=True
) as myuplink_mock:
myuplink = myuplink_mock.return_value
myuplink.async_get_systems_json.return_value = load_json_value_fixture(
"homeassistant.components.myuplink.MyUplinkAPI",
autospec=True,
) as mock_client:
client = mock_client.return_value
client.async_get_systems_json.return_value = load_json_value_fixture(
"systems.json", DOMAIN
)
myuplink.async_get_device_json.return_value = load_json_value_fixture(
client.async_get_device_json.return_value = load_json_value_fixture(
"device.json", DOMAIN
)
myuplink.async_get_device_points_json.return_value = load_json_value_fixture(
client.async_get_device_points_json.return_value = load_json_value_fixture(
"device_points_nibe_f730.json", DOMAIN
)

yield myuplink
yield client


@pytest.fixture
async def init_integration(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_myuplink: MagicMock
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_myuplink_client: MagicMock,
) -> MockConfigEntry:
"""Set up the myuplink integration for testing."""
mock_config_entry.add_to_hass(hass)

with patch(
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation"
):
assert await async_setup_component(hass, "myuplink", {})
await hass.async_block_till_done()
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()

return mock_config_entry
3 changes: 3 additions & 0 deletions tests/components/myuplink/const.py
@@ -0,0 +1,3 @@
"""Constants for myuplink tests."""
CLIENT_ID = "12345"
CLIENT_SECRET = "67890"
1 change: 1 addition & 0 deletions tests/components/myuplink/fixtures/jwt
@@ -0,0 +1 @@
eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjVlZDU2ZDUzLTEyNWYtNDExZi04ZTFlLTNlNDRkMGVkOGJmOCJ9.eyJqdGkiOiI2MGYxNGQ1OS1iY2M4LTQwMzktYmMzOC0yNWRiMzc2MGQwNDciLCJpc3MiOiJodXNxdmFybmEiLCJyb2xlcyI6W10sImdyb3VwcyI6WyJhbWMiLCJkZXZlbG9wZXItcG9ydGFsIiwiZmQ3OGIzYTQtYTdmOS00Yzc2LWJlZjktYWE1YTUwNTgzMzgyIiwiZ2FyZGVuYS1teWFjY291bnQiLCJodXNxdmFybmEtY29ubmVjdCIsImh1c3F2YXJuYS1teXBhZ2VzIiwic21hcnRnYXJkZW4iXSwic2NvcGVzIjpbImlhbTpyZWFkIiwiYW1jOmFwaSJdLCJzY29wZSI6ImlhbTpyZWFkIGFtYzphcGkiLCJjbGllbnRfaWQiOiI0MzNlNWZkZi01MTI5LTQ1MmMteHh4eC1mYWRjZTMyMTMwNDIiLCJjdXN0b21lcl9pZCI6IjQ3NTU5OTc3MjA0NTh4eHh4IiwidXNlciI6eyJmaXJzdF9uYW1lIjoiSm9obiIsImxhc3RfbmFtZSI6IkRvZSIsImN1c3RvbV9hdHRyaWJ1dGVzIjp7ImhjX2NvdW50cnkiOiJERSJ9LCJjdXN0b21lcl9pZCI6IjQ3NTU5OTc3MjA0NTh4eHh4In0sImlhdCI6MTY5NzY2Njk0NywiZXhwIjoxNjk3NzUzMzQ3LCJzdWIiOiI1YTkzMTQxZS01NWE3LTQ3OWYtOTZlMi04YTYzMTg4YzA1NGYifQ.1O3FOoWHaWpo-PrW88097ai6nsUGlK2NWyqIDLkUl1BTatQoFhIA1nKmCthf6A9CAYeoPS4c8CBhqqLj-5VrJXfNc7pFZ1nAw69pT33Ku7_S9QqonPf_JRvWX8-A7sTCKXEkCTso6v_jbmiePK6C9_psClJx_PUgFFOoNaROZhSsAlq9Gftvzs9UTcd2UO9ohsku_Kpx480C0QCKRjm4LTrFTBpgijRPc3F0BnyfgW8rT3Trl290f3CyEzLk8k9bgGA0qDlAanKuNNKK1j7hwRsiq_28A7bWJzlLc6Wgrq8Pc2CnnMada_eXavkTu-VzB-q8_PGFkLyeG16CR-NXlox9mEB6NxTn5stYSMUkiTApAfgCwLuj4c_WCXnxUZn0VdnsswvaIZON3bTSOMATXLG8PFUyDOcDxHBV4LEDyTVspo-QblanTTBLFWMTfWIWApBmRO9OkiJrcq9g7T8hKNNImeN4skk2vIZVXkCq_cEOdVAG4099b1V8zXCBgtDc
8 changes: 8 additions & 0 deletions tests/components/myuplink/snapshots/test_diagnostics.ambr
Expand Up @@ -2,6 +2,14 @@
# name: test_diagnostics
dict({
'config_entry_data': dict({
'auth_implementation': 'myuplink',
'token': dict({
'access_token': '**REDACTED**',
'expires_in': 86399,
'refresh_token': '**REDACTED**',
'scope': 'READSYSTEM offline',
'token_type': 'Bearer',
}),
}),
'myuplink_data': dict({
'my_systems': dict({
Expand Down
12 changes: 8 additions & 4 deletions tests/components/myuplink/test_diagnostics.py
@@ -1,6 +1,7 @@
"""Tests for the diagnostics data provided by the myuplink integration."""

from syrupy import SnapshotAssertion
from syrupy.filters import paths

from homeassistant.core import HomeAssistant

Expand All @@ -12,11 +13,14 @@
async def test_diagnostics(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
init_integration: MockConfigEntry,
mock_config_entry: MockConfigEntry,
init_integration,
snapshot: SnapshotAssertion,
) -> None:
"""Test diagnostics."""
assert (
await get_diagnostics_for_config_entry(hass, hass_client, init_integration)
== snapshot

assert await get_diagnostics_for_config_entry(
hass, hass_client, mock_config_entry
) == snapshot(
exclude=paths("config_entry_data.token.expires_at", "myuplink_test.entry_id")
)