mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Improve test coverage of adguard (#156839)
This commit is contained in:
@@ -1,25 +1,23 @@
|
||||
"""Tests for the AdGuard Home integration."""
|
||||
|
||||
from homeassistant.const import CONTENT_TYPE_JSON
|
||||
from collections.abc import AsyncGenerator
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
|
||||
async def setup_integration(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
adguard_mock: AsyncGenerator,
|
||||
) -> None:
|
||||
"""Fixture for setting up the component."""
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
aioclient_mock.get(
|
||||
"https://127.0.0.1:3000/control/status",
|
||||
json={"version": "v0.107.50"},
|
||||
headers={"Content-Type": CONTENT_TYPE_JSON},
|
||||
)
|
||||
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
with patch(
|
||||
"homeassistant.components.adguard.AdGuardHome",
|
||||
return_value=adguard_mock,
|
||||
):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
"""Common fixtures for the adguard tests."""
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from adguardhome.update import AdGuardHomeAvailableUpdate
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.adguard import DOMAIN
|
||||
@@ -30,3 +33,43 @@ def mock_config_entry() -> MockConfigEntry:
|
||||
},
|
||||
title="AdGuard Home",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def mock_adguard() -> AsyncMock:
|
||||
"""Fixture for setting up the component."""
|
||||
adguard_mock = AsyncMock()
|
||||
|
||||
# static properties
|
||||
adguard_mock.host = "127.0.0.1"
|
||||
adguard_mock.port = 3000
|
||||
adguard_mock.tls = True
|
||||
adguard_mock.base_path = "/control"
|
||||
|
||||
# async method mocks
|
||||
adguard_mock.version = AsyncMock(return_value="v0.107.50")
|
||||
adguard_mock.stats.dns_queries = AsyncMock(return_value=666)
|
||||
adguard_mock.stats.blocked_filtering = AsyncMock(return_value=1337)
|
||||
adguard_mock.stats.blocked_percentage = AsyncMock(return_value=200.75)
|
||||
adguard_mock.stats.replaced_parental = AsyncMock(return_value=13)
|
||||
adguard_mock.stats.replaced_safebrowsing = AsyncMock(return_value=42)
|
||||
adguard_mock.stats.replaced_safesearch = AsyncMock(return_value=18)
|
||||
adguard_mock.stats.avg_processing_time = AsyncMock(return_value=31.41)
|
||||
adguard_mock.filtering.rules_count = AsyncMock(return_value=100)
|
||||
adguard_mock.filtering.add_url = AsyncMock()
|
||||
adguard_mock.filtering.remove_url = AsyncMock()
|
||||
adguard_mock.filtering.enable_url = AsyncMock()
|
||||
adguard_mock.filtering.disable_url = AsyncMock()
|
||||
adguard_mock.filtering.refresh = AsyncMock()
|
||||
adguard_mock.update.update_available = AsyncMock(
|
||||
return_value=AdGuardHomeAvailableUpdate(
|
||||
new_version="v0.107.59",
|
||||
announcement="AdGuard Home v0.107.59 is now available!",
|
||||
announcement_url="https://github.com/AdguardTeam/AdGuardHome/releases/tag/v0.107.59",
|
||||
can_autoupdate=True,
|
||||
disabled=False,
|
||||
)
|
||||
)
|
||||
adguard_mock.update.begin_update = AsyncMock()
|
||||
|
||||
return adguard_mock
|
||||
|
||||
393
tests/components/adguard/snapshots/test_sensor.ambr
Normal file
393
tests/components/adguard/snapshots/test_sensor.ambr
Normal file
@@ -0,0 +1,393 @@
|
||||
# serializer version: 1
|
||||
# name: test_sensors[sensor.adguard_home_average_processing_speed-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.adguard_home_average_processing_speed',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Average processing speed',
|
||||
'platform': 'adguard',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'average_processing_speed',
|
||||
'unique_id': 'adguard_127.0.0.1_3000_sensor_average_speed',
|
||||
'unit_of_measurement': <UnitOfTime.MILLISECONDS: 'ms'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.adguard_home_average_processing_speed-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'AdGuard Home Average processing speed',
|
||||
'unit_of_measurement': <UnitOfTime.MILLISECONDS: 'ms'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.adguard_home_average_processing_speed',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '31.41',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.adguard_home_dns_queries-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.adguard_home_dns_queries',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'DNS queries',
|
||||
'platform': 'adguard',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'dns_queries',
|
||||
'unique_id': 'adguard_127.0.0.1_3000_sensor_dns_queries',
|
||||
'unit_of_measurement': 'queries',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.adguard_home_dns_queries-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'AdGuard Home DNS queries',
|
||||
'unit_of_measurement': 'queries',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.adguard_home_dns_queries',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '666',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.adguard_home_dns_queries_blocked-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.adguard_home_dns_queries_blocked',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'DNS queries blocked',
|
||||
'platform': 'adguard',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'dns_queries_blocked',
|
||||
'unique_id': 'adguard_127.0.0.1_3000_sensor_blocked_filtering',
|
||||
'unit_of_measurement': 'queries',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.adguard_home_dns_queries_blocked-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'AdGuard Home DNS queries blocked',
|
||||
'unit_of_measurement': 'queries',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.adguard_home_dns_queries_blocked',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '1337',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.adguard_home_dns_queries_blocked_ratio-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.adguard_home_dns_queries_blocked_ratio',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'DNS queries blocked ratio',
|
||||
'platform': 'adguard',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'dns_queries_blocked_ratio',
|
||||
'unique_id': 'adguard_127.0.0.1_3000_sensor_blocked_percentage',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.adguard_home_dns_queries_blocked_ratio-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'AdGuard Home DNS queries blocked ratio',
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.adguard_home_dns_queries_blocked_ratio',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '200.75',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.adguard_home_parental_control_blocked-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.adguard_home_parental_control_blocked',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Parental control blocked',
|
||||
'platform': 'adguard',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'parental_control_blocked',
|
||||
'unique_id': 'adguard_127.0.0.1_3000_sensor_blocked_parental',
|
||||
'unit_of_measurement': 'requests',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.adguard_home_parental_control_blocked-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'AdGuard Home Parental control blocked',
|
||||
'unit_of_measurement': 'requests',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.adguard_home_parental_control_blocked',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '13',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.adguard_home_rules_count-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.adguard_home_rules_count',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Rules count',
|
||||
'platform': 'adguard',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'rules_count',
|
||||
'unique_id': 'adguard_127.0.0.1_3000_sensor_rules_count',
|
||||
'unit_of_measurement': 'rules',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.adguard_home_rules_count-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'AdGuard Home Rules count',
|
||||
'unit_of_measurement': 'rules',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.adguard_home_rules_count',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '100',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.adguard_home_safe_browsing_blocked-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.adguard_home_safe_browsing_blocked',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Safe browsing blocked',
|
||||
'platform': 'adguard',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'safe_browsing_blocked',
|
||||
'unique_id': 'adguard_127.0.0.1_3000_sensor_blocked_safebrowsing',
|
||||
'unit_of_measurement': 'requests',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.adguard_home_safe_browsing_blocked-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'AdGuard Home Safe browsing blocked',
|
||||
'unit_of_measurement': 'requests',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.adguard_home_safe_browsing_blocked',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '42',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.adguard_home_safe_searches_enforced-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.adguard_home_safe_searches_enforced',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Safe searches enforced',
|
||||
'platform': 'adguard',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'safe_searches_enforced',
|
||||
'unique_id': 'adguard_127.0.0.1_3000_sensor_enforced_safesearch',
|
||||
'unit_of_measurement': 'requests',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.adguard_home_safe_searches_enforced-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'AdGuard Home Safe searches enforced',
|
||||
'unit_of_measurement': 'requests',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.adguard_home_safe_searches_enforced',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '18',
|
||||
})
|
||||
# ---
|
||||
35
tests/components/adguard/test_init.py
Normal file
35
tests/components/adguard/test_init.py
Normal file
@@ -0,0 +1,35 @@
|
||||
"""Tests for the AdGuard Home."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from adguardhome import AdGuardHomeConnectionError
|
||||
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_setup(
|
||||
hass: HomeAssistant,
|
||||
mock_adguard: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the adguard setup."""
|
||||
with patch("homeassistant.components.adguard.PLATFORMS", []):
|
||||
await setup_integration(hass, mock_config_entry, mock_adguard)
|
||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
|
||||
async def test_setup_failed(
|
||||
hass: HomeAssistant,
|
||||
mock_adguard: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the adguard setup failed."""
|
||||
mock_adguard.version.side_effect = AdGuardHomeConnectionError("Connection error")
|
||||
|
||||
await setup_integration(hass, mock_config_entry, mock_adguard)
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
29
tests/components/adguard/test_sensor.py
Normal file
29
tests/components/adguard/test_sensor.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""Tests for the AdGuard Home sensor entities."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_sensors(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
snapshot: SnapshotAssertion,
|
||||
mock_adguard: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the adguard sensor platform."""
|
||||
with patch("homeassistant.components.adguard.PLATFORMS", [Platform.SENSOR]):
|
||||
await setup_integration(hass, mock_config_entry, mock_adguard)
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
111
tests/components/adguard/test_service.py
Normal file
111
tests/components/adguard/test_service.py
Normal file
@@ -0,0 +1,111 @@
|
||||
"""Tests for the AdGuard Home sensor entities."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.adguard.const import (
|
||||
DOMAIN,
|
||||
SERVICE_ADD_URL,
|
||||
SERVICE_DISABLE_URL,
|
||||
SERVICE_ENABLE_URL,
|
||||
SERVICE_REFRESH,
|
||||
SERVICE_REMOVE_URL,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_service_registration(
|
||||
hass: HomeAssistant,
|
||||
mock_adguard: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the adguard services be registered."""
|
||||
with patch("homeassistant.components.adguard.PLATFORMS", []):
|
||||
await setup_integration(hass, mock_config_entry, mock_adguard)
|
||||
|
||||
services = hass.services.async_services_for_domain(DOMAIN)
|
||||
|
||||
assert len(services) == 5
|
||||
assert SERVICE_ADD_URL in services
|
||||
assert SERVICE_DISABLE_URL in services
|
||||
assert SERVICE_ENABLE_URL in services
|
||||
assert SERVICE_REFRESH in services
|
||||
assert SERVICE_REMOVE_URL in services
|
||||
|
||||
|
||||
async def test_service_unregistration(
|
||||
hass: HomeAssistant,
|
||||
mock_adguard: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the adguard services be unregistered with unloading last entry."""
|
||||
with patch("homeassistant.components.adguard.PLATFORMS", []):
|
||||
await setup_integration(hass, mock_config_entry, mock_adguard)
|
||||
|
||||
services = hass.services.async_services_for_domain(DOMAIN)
|
||||
assert len(services) == 5
|
||||
|
||||
await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
services = hass.services.async_services_for_domain(DOMAIN)
|
||||
assert len(services) == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("service", "service_call_data", "call_assertion"),
|
||||
[
|
||||
(
|
||||
SERVICE_ADD_URL,
|
||||
{"name": "Example", "url": "https://example.com/1.txt"},
|
||||
lambda mock: mock.filtering.add_url.assert_called_once(),
|
||||
),
|
||||
(
|
||||
SERVICE_DISABLE_URL,
|
||||
{"url": "https://example.com/1.txt"},
|
||||
lambda mock: mock.filtering.disable_url.assert_called_once(),
|
||||
),
|
||||
(
|
||||
SERVICE_ENABLE_URL,
|
||||
{"url": "https://example.com/1.txt"},
|
||||
lambda mock: mock.filtering.enable_url.assert_called_once(),
|
||||
),
|
||||
(
|
||||
SERVICE_REFRESH,
|
||||
{"force": False},
|
||||
lambda mock: mock.filtering.refresh.assert_called_once(),
|
||||
),
|
||||
(
|
||||
SERVICE_REMOVE_URL,
|
||||
{"url": "https://example.com/1.txt"},
|
||||
lambda mock: mock.filtering.remove_url.assert_called_once(),
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_service(
|
||||
hass: HomeAssistant,
|
||||
mock_adguard: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
service: str,
|
||||
service_call_data: dict,
|
||||
call_assertion: Callable[[AsyncMock], Any],
|
||||
) -> None:
|
||||
"""Test the adguard services be unregistered with unloading last entry."""
|
||||
with patch("homeassistant.components.adguard.PLATFORMS", []):
|
||||
await setup_integration(hass, mock_config_entry, mock_adguard)
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
service,
|
||||
service_call_data,
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
call_assertion(mock_adguard)
|
||||
@@ -1,12 +1,13 @@
|
||||
"""Tests for the AdGuard Home update entity."""
|
||||
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from adguardhome import AdGuardHomeError
|
||||
from adguardhome.update import AdGuardHomeAvailableUpdate
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.const import CONTENT_TYPE_JSON, Platform
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
@@ -14,76 +15,46 @@ from homeassistant.helpers import entity_registry as er
|
||||
from . import setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
|
||||
async def test_update(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
snapshot: SnapshotAssertion,
|
||||
mock_adguard: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the adguard update platform."""
|
||||
aioclient_mock.post(
|
||||
"https://127.0.0.1:3000/control/version.json",
|
||||
json={
|
||||
"new_version": "v0.107.59",
|
||||
"announcement": "AdGuard Home v0.107.59 is now available!",
|
||||
"announcement_url": "https://github.com/AdguardTeam/AdGuardHome/releases/tag/v0.107.59",
|
||||
"can_autoupdate": True,
|
||||
"disabled": False,
|
||||
},
|
||||
headers={"Content-Type": CONTENT_TYPE_JSON},
|
||||
)
|
||||
|
||||
with patch("homeassistant.components.adguard.PLATFORMS", [Platform.UPDATE]):
|
||||
await setup_integration(hass, mock_config_entry, aioclient_mock)
|
||||
await setup_integration(hass, mock_config_entry, mock_adguard)
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
|
||||
async def test_update_disabled(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
mock_adguard: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the adguard update is disabled."""
|
||||
aioclient_mock.post(
|
||||
"https://127.0.0.1:3000/control/version.json",
|
||||
json={"disabled": True},
|
||||
headers={"Content-Type": CONTENT_TYPE_JSON},
|
||||
mock_adguard.update.update_available.return_value = AdGuardHomeAvailableUpdate(
|
||||
disabled=True,
|
||||
)
|
||||
|
||||
with patch("homeassistant.components.adguard.PLATFORMS", [Platform.UPDATE]):
|
||||
await setup_integration(hass, mock_config_entry, aioclient_mock)
|
||||
await setup_integration(hass, mock_config_entry, mock_adguard)
|
||||
|
||||
assert not hass.states.async_all()
|
||||
|
||||
|
||||
async def test_update_install(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
mock_adguard: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the adguard update installation."""
|
||||
aioclient_mock.post(
|
||||
"https://127.0.0.1:3000/control/version.json",
|
||||
json={
|
||||
"new_version": "v0.107.59",
|
||||
"announcement": "AdGuard Home v0.107.59 is now available!",
|
||||
"announcement_url": "https://github.com/AdguardTeam/AdGuardHome/releases/tag/v0.107.59",
|
||||
"can_autoupdate": True,
|
||||
"disabled": False,
|
||||
},
|
||||
headers={"Content-Type": CONTENT_TYPE_JSON},
|
||||
)
|
||||
aioclient_mock.post("https://127.0.0.1:3000/control/update")
|
||||
|
||||
with patch("homeassistant.components.adguard.PLATFORMS", [Platform.UPDATE]):
|
||||
await setup_integration(hass, mock_config_entry, aioclient_mock)
|
||||
|
||||
aioclient_mock.mock_calls.clear()
|
||||
await setup_integration(hass, mock_config_entry, mock_adguard)
|
||||
|
||||
await hass.services.async_call(
|
||||
"update",
|
||||
@@ -91,38 +62,19 @@ async def test_update_install(
|
||||
{"entity_id": "update.adguard_home"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
assert aioclient_mock.mock_calls[0][0] == "POST"
|
||||
assert (
|
||||
str(aioclient_mock.mock_calls[0][1]) == "https://127.0.0.1:3000/control/update"
|
||||
)
|
||||
mock_adguard.update.begin_update.assert_called_once()
|
||||
|
||||
|
||||
async def test_update_install_failed(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
mock_adguard: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the adguard update install failed."""
|
||||
aioclient_mock.post(
|
||||
"https://127.0.0.1:3000/control/version.json",
|
||||
json={
|
||||
"new_version": "v0.107.59",
|
||||
"announcement": "AdGuard Home v0.107.59 is now available!",
|
||||
"announcement_url": "https://github.com/AdguardTeam/AdGuardHome/releases/tag/v0.107.59",
|
||||
"can_autoupdate": True,
|
||||
"disabled": False,
|
||||
},
|
||||
headers={"Content-Type": CONTENT_TYPE_JSON},
|
||||
)
|
||||
aioclient_mock.post(
|
||||
"https://127.0.0.1:3000/control/update", exc=AdGuardHomeError("boom")
|
||||
)
|
||||
mock_adguard.update.begin_update.side_effect = AdGuardHomeError("boom")
|
||||
|
||||
with patch("homeassistant.components.adguard.PLATFORMS", [Platform.UPDATE]):
|
||||
await setup_integration(hass, mock_config_entry, aioclient_mock)
|
||||
|
||||
aioclient_mock.mock_calls.clear()
|
||||
await setup_integration(hass, mock_config_entry, mock_adguard)
|
||||
|
||||
with pytest.raises(HomeAssistantError):
|
||||
await hass.services.async_call(
|
||||
@@ -131,8 +83,3 @@ async def test_update_install_failed(
|
||||
{"entity_id": "update.adguard_home"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
assert aioclient_mock.mock_calls[0][0] == "POST"
|
||||
assert (
|
||||
str(aioclient_mock.mock_calls[0][1]) == "https://127.0.0.1:3000/control/update"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user