|
| 1 | +"""Tests for the AdGuard Home update entity.""" |
| 2 | + |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +from adguardhome import AdGuardHomeError |
| 6 | +import pytest |
| 7 | +from syrupy.assertion import SnapshotAssertion |
| 8 | + |
| 9 | +from homeassistant.const import CONTENT_TYPE_JSON, Platform |
| 10 | +from homeassistant.core import HomeAssistant |
| 11 | +from homeassistant.exceptions import HomeAssistantError |
| 12 | +from homeassistant.helpers import entity_registry as er |
| 13 | + |
| 14 | +from . import setup_integration |
| 15 | + |
| 16 | +from tests.common import MockConfigEntry, snapshot_platform |
| 17 | +from tests.test_util.aiohttp import AiohttpClientMocker |
| 18 | + |
| 19 | + |
| 20 | +async def test_update( |
| 21 | + hass: HomeAssistant, |
| 22 | + entity_registry: er.EntityRegistry, |
| 23 | + aioclient_mock: AiohttpClientMocker, |
| 24 | + snapshot: SnapshotAssertion, |
| 25 | + mock_config_entry: MockConfigEntry, |
| 26 | +) -> None: |
| 27 | + """Test the adguard update platform.""" |
| 28 | + aioclient_mock.post( |
| 29 | + "https://127.0.0.1:3000/control/version.json", |
| 30 | + json={ |
| 31 | + "new_version": "v0.107.59", |
| 32 | + "announcement": "AdGuard Home v0.107.59 is now available!", |
| 33 | + "announcement_url": "https://github.com/AdguardTeam/AdGuardHome/releases/tag/v0.107.59", |
| 34 | + "can_autoupdate": True, |
| 35 | + "disabled": False, |
| 36 | + }, |
| 37 | + headers={"Content-Type": CONTENT_TYPE_JSON}, |
| 38 | + ) |
| 39 | + |
| 40 | + with patch("homeassistant.components.adguard.PLATFORMS", [Platform.UPDATE]): |
| 41 | + await setup_integration(hass, mock_config_entry, aioclient_mock) |
| 42 | + |
| 43 | + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) |
| 44 | + |
| 45 | + |
| 46 | +async def test_update_disabled( |
| 47 | + hass: HomeAssistant, |
| 48 | + aioclient_mock: AiohttpClientMocker, |
| 49 | + mock_config_entry: MockConfigEntry, |
| 50 | +) -> None: |
| 51 | + """Test the adguard update is disabled.""" |
| 52 | + aioclient_mock.post( |
| 53 | + "https://127.0.0.1:3000/control/version.json", |
| 54 | + json={"disabled": True}, |
| 55 | + headers={"Content-Type": CONTENT_TYPE_JSON}, |
| 56 | + ) |
| 57 | + |
| 58 | + with patch("homeassistant.components.adguard.PLATFORMS", [Platform.UPDATE]): |
| 59 | + await setup_integration(hass, mock_config_entry, aioclient_mock) |
| 60 | + |
| 61 | + assert not hass.states.async_all() |
| 62 | + |
| 63 | + |
| 64 | +async def test_update_install( |
| 65 | + hass: HomeAssistant, |
| 66 | + aioclient_mock: AiohttpClientMocker, |
| 67 | + mock_config_entry: MockConfigEntry, |
| 68 | +) -> None: |
| 69 | + """Test the adguard update installation.""" |
| 70 | + aioclient_mock.post( |
| 71 | + "https://127.0.0.1:3000/control/version.json", |
| 72 | + json={ |
| 73 | + "new_version": "v0.107.59", |
| 74 | + "announcement": "AdGuard Home v0.107.59 is now available!", |
| 75 | + "announcement_url": "https://github.com/AdguardTeam/AdGuardHome/releases/tag/v0.107.59", |
| 76 | + "can_autoupdate": True, |
| 77 | + "disabled": False, |
| 78 | + }, |
| 79 | + headers={"Content-Type": CONTENT_TYPE_JSON}, |
| 80 | + ) |
| 81 | + aioclient_mock.post("https://127.0.0.1:3000/control/update") |
| 82 | + |
| 83 | + with patch("homeassistant.components.adguard.PLATFORMS", [Platform.UPDATE]): |
| 84 | + await setup_integration(hass, mock_config_entry, aioclient_mock) |
| 85 | + |
| 86 | + aioclient_mock.mock_calls.clear() |
| 87 | + |
| 88 | + await hass.services.async_call( |
| 89 | + "update", |
| 90 | + "install", |
| 91 | + {"entity_id": "update.adguard_home"}, |
| 92 | + blocking=True, |
| 93 | + ) |
| 94 | + |
| 95 | + assert aioclient_mock.mock_calls[0][0] == "POST" |
| 96 | + assert ( |
| 97 | + str(aioclient_mock.mock_calls[0][1]) == "https://127.0.0.1:3000/control/update" |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +async def test_update_install_failed( |
| 102 | + hass: HomeAssistant, |
| 103 | + aioclient_mock: AiohttpClientMocker, |
| 104 | + mock_config_entry: MockConfigEntry, |
| 105 | +) -> None: |
| 106 | + """Test the adguard update install failed.""" |
| 107 | + aioclient_mock.post( |
| 108 | + "https://127.0.0.1:3000/control/version.json", |
| 109 | + json={ |
| 110 | + "new_version": "v0.107.59", |
| 111 | + "announcement": "AdGuard Home v0.107.59 is now available!", |
| 112 | + "announcement_url": "https://github.com/AdguardTeam/AdGuardHome/releases/tag/v0.107.59", |
| 113 | + "can_autoupdate": True, |
| 114 | + "disabled": False, |
| 115 | + }, |
| 116 | + headers={"Content-Type": CONTENT_TYPE_JSON}, |
| 117 | + ) |
| 118 | + aioclient_mock.post( |
| 119 | + "https://127.0.0.1:3000/control/update", exc=AdGuardHomeError("boom") |
| 120 | + ) |
| 121 | + |
| 122 | + with patch("homeassistant.components.adguard.PLATFORMS", [Platform.UPDATE]): |
| 123 | + await setup_integration(hass, mock_config_entry, aioclient_mock) |
| 124 | + |
| 125 | + aioclient_mock.mock_calls.clear() |
| 126 | + |
| 127 | + with pytest.raises(HomeAssistantError): |
| 128 | + await hass.services.async_call( |
| 129 | + "update", |
| 130 | + "install", |
| 131 | + {"entity_id": "update.adguard_home"}, |
| 132 | + blocking=True, |
| 133 | + ) |
| 134 | + |
| 135 | + assert aioclient_mock.mock_calls[0][0] == "POST" |
| 136 | + assert ( |
| 137 | + str(aioclient_mock.mock_calls[0][1]) == "https://127.0.0.1:3000/control/update" |
| 138 | + ) |
0 commit comments