core/tests/components/updater/test_init.py

131 lines
4.1 KiB
Python
Raw Normal View History

2021-03-30 20:22:03 +00:00
"""The tests for the Updater integration."""
2021-01-01 21:31:56 +00:00
from unittest.mock import patch
import pytest
2015-11-29 20:13:06 +00:00
from homeassistant.components import updater
from homeassistant.helpers.update_coordinator import UpdateFailed
2019-08-27 08:30:41 +00:00
from homeassistant.setup import async_setup_component
2020-04-30 23:31:00 +00:00
from tests.common import mock_component
2015-11-29 20:13:06 +00:00
2019-07-31 19:25:30 +00:00
NEW_VERSION = "10000.0"
MOCK_VERSION = "10.0"
MOCK_DEV_VERSION = "10.0.dev0"
2021-03-30 20:22:03 +00:00
MOCK_RESPONSE = {
"current_version": "0.15",
"release_notes": "https://home-assistant.io",
}
2019-07-31 19:25:30 +00:00
MOCK_CONFIG = {updater.DOMAIN: {"reporting": True}}
RELEASE_NOTES = "test release notes"
@pytest.fixture(autouse=True)
def mock_version():
"""Mock current version."""
with patch("homeassistant.components.updater.current_version", MOCK_VERSION):
yield
2019-08-27 08:30:41 +00:00
@pytest.fixture(name="mock_get_newest_version")
def mock_get_newest_version_fixture():
"""Fixture to mock get_newest_version."""
with patch(
"homeassistant.components.updater.get_newest_version",
return_value=(NEW_VERSION, RELEASE_NOTES),
) as mock:
yield mock
2021-03-30 20:22:03 +00:00
async def test_new_version_shows_entity_true(hass, mock_get_newest_version):
"""Test if sensor is true if new version is available."""
assert await async_setup_component(hass, updater.DOMAIN, {updater.DOMAIN: {}})
2019-08-27 08:30:41 +00:00
await hass.async_block_till_done()
assert hass.states.is_state("binary_sensor.updater", "on")
assert (
hass.states.get("binary_sensor.updater").attributes["newest_version"]
== NEW_VERSION
)
assert (
hass.states.get("binary_sensor.updater").attributes["release_notes"]
== RELEASE_NOTES
)
2021-03-30 20:22:03 +00:00
async def test_same_version_shows_entity_false(hass, mock_get_newest_version):
"""Test if sensor is false if no new version is available."""
mock_get_newest_version.return_value = (MOCK_VERSION, "")
assert await async_setup_component(hass, updater.DOMAIN, {updater.DOMAIN: {}})
2019-08-27 08:30:41 +00:00
await hass.async_block_till_done()
assert hass.states.is_state("binary_sensor.updater", "off")
assert (
hass.states.get("binary_sensor.updater").attributes["newest_version"]
== MOCK_VERSION
)
assert "release_notes" not in hass.states.get("binary_sensor.updater").attributes
2021-03-30 20:22:03 +00:00
async def test_deprecated_reporting(hass, mock_get_newest_version, caplog):
"""Test we do not gather analytics when disable reporting is active."""
mock_get_newest_version.return_value = (MOCK_VERSION, "")
assert await async_setup_component(
2021-03-30 20:22:03 +00:00
hass, updater.DOMAIN, {updater.DOMAIN: {"reporting": True}}
2019-07-31 19:25:30 +00:00
)
2019-08-27 08:30:41 +00:00
await hass.async_block_till_done()
2021-03-30 20:22:03 +00:00
assert "deprecated" in caplog.text
2019-08-27 08:30:41 +00:00
async def test_error_fetching_new_version_bad_json(hass, aioclient_mock):
"""Test we handle json error while fetching new version."""
2021-03-30 20:22:03 +00:00
aioclient_mock.get(updater.UPDATER_URL, text="not json")
2019-07-31 19:25:30 +00:00
with patch(
"homeassistant.helpers.system_info.async_get_system_info",
return_value={"fake": "bla"},
), pytest.raises(UpdateFailed):
2021-03-30 20:22:03 +00:00
await updater.get_newest_version(hass)
2019-08-27 08:30:41 +00:00
async def test_error_fetching_new_version_invalid_response(hass, aioclient_mock):
"""Test we handle response error while fetching new version."""
2021-03-30 20:22:03 +00:00
aioclient_mock.get(
2019-07-31 19:25:30 +00:00
updater.UPDATER_URL,
json={
"version": "0.15"
# 'release-notes' is missing
},
)
with patch(
"homeassistant.helpers.system_info.async_get_system_info",
return_value={"fake": "bla"},
), pytest.raises(UpdateFailed):
2021-03-30 20:22:03 +00:00
await updater.get_newest_version(hass)
2019-08-27 08:30:41 +00:00
async def test_new_version_shows_entity_after_hour_hassio(
2021-03-30 20:22:03 +00:00
hass, mock_get_newest_version
2019-07-31 19:25:30 +00:00
):
"""Test if binary sensor gets updated if new version is available / Hass.io."""
2019-07-31 19:25:30 +00:00
mock_component(hass, "hassio")
hass.data["hassio_core_info"] = {"version_latest": "999.0"}
assert await async_setup_component(hass, updater.DOMAIN, {updater.DOMAIN: {}})
2019-08-27 08:30:41 +00:00
await hass.async_block_till_done()
assert hass.states.is_state("binary_sensor.updater", "on")
assert (
hass.states.get("binary_sensor.updater").attributes["newest_version"] == "999.0"
)
assert (
hass.states.get("binary_sensor.updater").attributes["release_notes"]
== RELEASE_NOTES
)