2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Updater component."""
|
2017-05-02 06:29:01 +00:00
|
|
|
import asyncio
|
|
|
|
from datetime import timedelta
|
|
|
|
from unittest.mock import patch, Mock
|
2015-11-29 20:13:06 +00:00
|
|
|
|
2017-05-02 06:29:01 +00:00
|
|
|
import pytest
|
2015-11-29 20:13:06 +00:00
|
|
|
|
2017-05-02 06:29:01 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2015-11-29 20:13:06 +00:00
|
|
|
from homeassistant.components import updater
|
2017-05-02 06:29:01 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2019-01-30 18:57:53 +00:00
|
|
|
from tests.common import (
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_time_changed,
|
|
|
|
mock_coro,
|
|
|
|
mock_component,
|
|
|
|
MockDependency,
|
|
|
|
)
|
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"
|
|
|
|
MOCK_HUUID = "abcdefg"
|
|
|
|
MOCK_RESPONSE = {"version": "0.15", "release-notes": "https://home-assistant.io"}
|
|
|
|
MOCK_CONFIG = {updater.DOMAIN: {"reporting": True}}
|
2017-05-02 06:29:01 +00:00
|
|
|
|
|
|
|
|
2019-01-30 18:57:53 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def mock_distro():
|
|
|
|
"""Mock distro dep."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with MockDependency("distro"):
|
2019-01-30 18:57:53 +00:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
2017-05-02 06:29:01 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_get_newest_version():
|
|
|
|
"""Fixture to mock get_newest_version."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.updater.get_newest_version") as mock:
|
2017-05-02 06:29:01 +00:00
|
|
|
yield mock
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_get_uuid():
|
|
|
|
"""Fixture to mock get_uuid."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.updater._load_uuid") as mock:
|
2017-05-02 06:29:01 +00:00
|
|
|
yield mock
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2017-08-10 17:31:28 +00:00
|
|
|
def test_new_version_shows_entity_after_hour(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, mock_get_uuid, mock_get_newest_version
|
|
|
|
):
|
2017-05-02 06:29:01 +00:00
|
|
|
"""Test if new entity is created if new version is available."""
|
|
|
|
mock_get_uuid.return_value = MOCK_HUUID
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_get_newest_version.return_value = mock_coro((NEW_VERSION, ""))
|
2017-05-02 06:29:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
res = yield from async_setup_component(hass, updater.DOMAIN, {updater.DOMAIN: {}})
|
|
|
|
assert res, "Updater failed to set up"
|
2017-05-02 06:29:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.updater.current_version", MOCK_VERSION):
|
2017-05-02 06:29:01 +00:00
|
|
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(hours=1))
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.is_state(updater.ENTITY_ID, NEW_VERSION)
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2019-07-31 19:25:30 +00:00
|
|
|
def test_same_version_not_show_entity(hass, mock_get_uuid, mock_get_newest_version):
|
2017-05-02 06:29:01 +00:00
|
|
|
"""Test if new entity is created if new version is available."""
|
|
|
|
mock_get_uuid.return_value = MOCK_HUUID
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_get_newest_version.return_value = mock_coro((MOCK_VERSION, ""))
|
2017-05-02 06:29:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
res = yield from async_setup_component(hass, updater.DOMAIN, {updater.DOMAIN: {}})
|
|
|
|
assert res, "Updater failed to set up"
|
2017-05-02 06:29:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.updater.current_version", MOCK_VERSION):
|
2017-05-02 06:29:01 +00:00
|
|
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(hours=1))
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get(updater.ENTITY_ID) is None
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_disable_reporting(hass, mock_get_uuid, mock_get_newest_version):
|
|
|
|
"""Test if new entity is created if new version is available."""
|
|
|
|
mock_get_uuid.return_value = MOCK_HUUID
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_get_newest_version.return_value = mock_coro((MOCK_VERSION, ""))
|
2017-05-02 06:29:01 +00:00
|
|
|
|
|
|
|
res = yield from async_setup_component(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, updater.DOMAIN, {updater.DOMAIN: {"reporting": False}}
|
|
|
|
)
|
|
|
|
assert res, "Updater failed to set up"
|
2017-05-02 06:29:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.updater.current_version", MOCK_VERSION):
|
2017-05-02 06:29:01 +00:00
|
|
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(hours=1))
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get(updater.ENTITY_ID) is None
|
2017-06-16 05:29:18 +00:00
|
|
|
res = yield from updater.get_newest_version(hass, MOCK_HUUID, MOCK_CONFIG)
|
2017-05-02 06:29:01 +00:00
|
|
|
call = mock_get_newest_version.mock_calls[0][1]
|
|
|
|
assert call[0] is hass
|
|
|
|
assert call[1] is None
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_get_newest_version_no_analytics_when_no_huuid(hass, aioclient_mock):
|
|
|
|
"""Test we do not gather analytics when no huuid is passed in."""
|
|
|
|
aioclient_mock.post(updater.UPDATER_URL, json=MOCK_RESPONSE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.system_info.async_get_system_info", side_effect=Exception
|
|
|
|
):
|
2017-06-16 05:29:18 +00:00
|
|
|
res = yield from updater.get_newest_version(hass, None, False)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert res == (MOCK_RESPONSE["version"], MOCK_RESPONSE["release-notes"])
|
2017-05-02 06:29:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_get_newest_version_analytics_when_huuid(hass, aioclient_mock):
|
|
|
|
"""Test we do not gather analytics when no huuid is passed in."""
|
|
|
|
aioclient_mock.post(updater.UPDATER_URL, json=MOCK_RESPONSE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.system_info.async_get_system_info",
|
|
|
|
Mock(return_value=mock_coro({"fake": "bla"})),
|
|
|
|
):
|
2017-06-16 05:29:18 +00:00
|
|
|
res = yield from updater.get_newest_version(hass, MOCK_HUUID, False)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert res == (MOCK_RESPONSE["version"], MOCK_RESPONSE["release-notes"])
|
2017-05-02 06:29:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_error_fetching_new_version_timeout(hass):
|
|
|
|
"""Test we do not gather analytics when no huuid is passed in."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.system_info.async_get_system_info",
|
|
|
|
Mock(return_value=mock_coro({"fake": "bla"})),
|
|
|
|
), patch("async_timeout.timeout", side_effect=asyncio.TimeoutError):
|
2017-06-16 05:29:18 +00:00
|
|
|
res = yield from updater.get_newest_version(hass, MOCK_HUUID, False)
|
2017-05-02 06:29:01 +00:00
|
|
|
assert res is None
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_error_fetching_new_version_bad_json(hass, aioclient_mock):
|
|
|
|
"""Test we do not gather analytics when no huuid is passed in."""
|
2019-07-31 19:25:30 +00:00
|
|
|
aioclient_mock.post(updater.UPDATER_URL, text="not json")
|
2017-05-02 06:29:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.system_info.async_get_system_info",
|
|
|
|
Mock(return_value=mock_coro({"fake": "bla"})),
|
|
|
|
):
|
2017-06-16 05:29:18 +00:00
|
|
|
res = yield from updater.get_newest_version(hass, MOCK_HUUID, False)
|
2017-05-02 06:29:01 +00:00
|
|
|
assert res is None
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_error_fetching_new_version_invalid_response(hass, aioclient_mock):
|
|
|
|
"""Test we do not gather analytics when no huuid is passed in."""
|
2019-07-31 19:25:30 +00:00
|
|
|
aioclient_mock.post(
|
|
|
|
updater.UPDATER_URL,
|
|
|
|
json={
|
|
|
|
"version": "0.15"
|
|
|
|
# 'release-notes' is missing
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.system_info.async_get_system_info",
|
|
|
|
Mock(return_value=mock_coro({"fake": "bla"})),
|
|
|
|
):
|
2017-06-16 05:29:18 +00:00
|
|
|
res = yield from updater.get_newest_version(hass, MOCK_HUUID, False)
|
2017-05-02 06:29:01 +00:00
|
|
|
assert res is None
|
2018-01-12 14:29:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_new_version_shows_entity_after_hour_hassio(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, mock_get_uuid, mock_get_newest_version
|
|
|
|
):
|
2018-01-12 14:29:58 +00:00
|
|
|
"""Test if new entity is created if new version is available / hass.io."""
|
|
|
|
mock_get_uuid.return_value = MOCK_HUUID
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_get_newest_version.return_value = mock_coro((NEW_VERSION, ""))
|
|
|
|
mock_component(hass, "hassio")
|
|
|
|
hass.data["hassio_hass_version"] = "999.0"
|
2018-01-12 14:29:58 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
res = yield from async_setup_component(hass, updater.DOMAIN, {updater.DOMAIN: {}})
|
|
|
|
assert res, "Updater failed to set up"
|
2018-01-12 14:29:58 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.updater.current_version", MOCK_VERSION):
|
2018-01-12 14:29:58 +00:00
|
|
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(hours=1))
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.is_state(updater.ENTITY_ID, "999.0")
|