Generate a seperate UUID for the analytics integration (#48742)
This commit is contained in:
parent
82cc5148d7
commit
89f2f458d2
5 changed files with 60 additions and 42 deletions
|
@ -1,5 +1,5 @@
|
|||
"""The tests for the analytics ."""
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
from unittest.mock import AsyncMock, Mock, PropertyMock, patch
|
||||
|
||||
import aiohttp
|
||||
import pytest
|
||||
|
@ -13,10 +13,11 @@ from homeassistant.components.analytics.const import (
|
|||
ATTR_STATISTICS,
|
||||
ATTR_USAGE,
|
||||
)
|
||||
from homeassistant.components.api import ATTR_UUID
|
||||
from homeassistant.const import __version__ as HA_VERSION
|
||||
from homeassistant.loader import IntegrationNotFound
|
||||
|
||||
MOCK_HUUID = "abcdefg"
|
||||
MOCK_UUID = "abcdefg"
|
||||
|
||||
|
||||
async def test_no_send(hass, caplog, aioclient_mock):
|
||||
|
@ -26,8 +27,7 @@ async def test_no_send(hass, caplog, aioclient_mock):
|
|||
with patch(
|
||||
"homeassistant.components.hassio.is_hassio",
|
||||
side_effect=Mock(return_value=False),
|
||||
), patch("homeassistant.helpers.instance_id.async_get", return_value=MOCK_HUUID):
|
||||
await analytics.load()
|
||||
):
|
||||
assert not analytics.preferences[ATTR_BASE]
|
||||
|
||||
await analytics.send_analytics()
|
||||
|
@ -76,9 +76,7 @@ async def test_failed_to_send(hass, caplog, aioclient_mock):
|
|||
analytics = Analytics(hass)
|
||||
await analytics.save_preferences({ATTR_BASE: True})
|
||||
assert analytics.preferences[ATTR_BASE]
|
||||
|
||||
with patch("homeassistant.helpers.instance_id.async_get", return_value=MOCK_HUUID):
|
||||
await analytics.send_analytics()
|
||||
await analytics.send_analytics()
|
||||
assert "Sending analytics failed with statuscode 400" in caplog.text
|
||||
|
||||
|
||||
|
@ -88,9 +86,7 @@ async def test_failed_to_send_raises(hass, caplog, aioclient_mock):
|
|||
analytics = Analytics(hass)
|
||||
await analytics.save_preferences({ATTR_BASE: True})
|
||||
assert analytics.preferences[ATTR_BASE]
|
||||
|
||||
with patch("homeassistant.helpers.instance_id.async_get", return_value=MOCK_HUUID):
|
||||
await analytics.send_analytics()
|
||||
await analytics.send_analytics()
|
||||
assert "Error sending analytics" in caplog.text
|
||||
|
||||
|
||||
|
@ -98,12 +94,15 @@ async def test_send_base(hass, caplog, aioclient_mock):
|
|||
"""Test send base prefrences are defined."""
|
||||
aioclient_mock.post(ANALYTICS_ENDPOINT_URL, status=200)
|
||||
analytics = Analytics(hass)
|
||||
|
||||
await analytics.save_preferences({ATTR_BASE: True})
|
||||
assert analytics.preferences[ATTR_BASE]
|
||||
|
||||
with patch("homeassistant.helpers.instance_id.async_get", return_value=MOCK_HUUID):
|
||||
with patch("uuid.UUID.hex", new_callable=PropertyMock) as hex:
|
||||
hex.return_value = MOCK_UUID
|
||||
await analytics.send_analytics()
|
||||
assert f"'huuid': '{MOCK_HUUID}'" in caplog.text
|
||||
|
||||
assert f"'uuid': '{MOCK_UUID}'" in caplog.text
|
||||
assert f"'version': '{HA_VERSION}'" in caplog.text
|
||||
assert "'installation_type':" in caplog.text
|
||||
assert "'integration_count':" not in caplog.text
|
||||
|
@ -131,10 +130,14 @@ async def test_send_base_with_supervisor(hass, caplog, aioclient_mock):
|
|||
"homeassistant.components.hassio.is_hassio",
|
||||
side_effect=Mock(return_value=True),
|
||||
), patch(
|
||||
"homeassistant.helpers.instance_id.async_get", return_value=MOCK_HUUID
|
||||
):
|
||||
"uuid.UUID.hex", new_callable=PropertyMock
|
||||
) as hex:
|
||||
hex.return_value = MOCK_UUID
|
||||
await analytics.load()
|
||||
|
||||
await analytics.send_analytics()
|
||||
assert f"'huuid': '{MOCK_HUUID}'" in caplog.text
|
||||
|
||||
assert f"'uuid': '{MOCK_UUID}'" in caplog.text
|
||||
assert f"'version': '{HA_VERSION}'" in caplog.text
|
||||
assert "'supervisor': {'healthy': True, 'supported': True}}" in caplog.text
|
||||
assert "'installation_type':" in caplog.text
|
||||
|
@ -147,12 +150,13 @@ async def test_send_usage(hass, caplog, aioclient_mock):
|
|||
aioclient_mock.post(ANALYTICS_ENDPOINT_URL, status=200)
|
||||
analytics = Analytics(hass)
|
||||
await analytics.save_preferences({ATTR_BASE: True, ATTR_USAGE: True})
|
||||
|
||||
assert analytics.preferences[ATTR_BASE]
|
||||
assert analytics.preferences[ATTR_USAGE]
|
||||
hass.config.components = ["default_config"]
|
||||
|
||||
with patch("homeassistant.helpers.instance_id.async_get", return_value=MOCK_HUUID):
|
||||
await analytics.send_analytics()
|
||||
await analytics.send_analytics()
|
||||
|
||||
assert "'integrations': ['default_config']" in caplog.text
|
||||
assert "'integration_count':" not in caplog.text
|
||||
|
||||
|
@ -195,8 +199,6 @@ async def test_send_usage_with_supervisor(hass, caplog, aioclient_mock):
|
|||
), patch(
|
||||
"homeassistant.components.hassio.is_hassio",
|
||||
side_effect=Mock(return_value=True),
|
||||
), patch(
|
||||
"homeassistant.helpers.instance_id.async_get", return_value=MOCK_HUUID
|
||||
):
|
||||
await analytics.send_analytics()
|
||||
assert (
|
||||
|
@ -215,8 +217,7 @@ async def test_send_statistics(hass, caplog, aioclient_mock):
|
|||
assert analytics.preferences[ATTR_STATISTICS]
|
||||
hass.config.components = ["default_config"]
|
||||
|
||||
with patch("homeassistant.helpers.instance_id.async_get", return_value=MOCK_HUUID):
|
||||
await analytics.send_analytics()
|
||||
await analytics.send_analytics()
|
||||
assert (
|
||||
"'state_count': 0, 'automation_count': 0, 'integration_count': 1, 'user_count': 0"
|
||||
in caplog.text
|
||||
|
@ -236,11 +237,11 @@ async def test_send_statistics_one_integration_fails(hass, caplog, aioclient_moc
|
|||
with patch(
|
||||
"homeassistant.components.analytics.analytics.async_get_integration",
|
||||
side_effect=IntegrationNotFound("any"),
|
||||
), patch("homeassistant.helpers.instance_id.async_get", return_value=MOCK_HUUID):
|
||||
):
|
||||
await analytics.send_analytics()
|
||||
|
||||
post_call = aioclient_mock.mock_calls[0]
|
||||
assert "huuid" in post_call[2]
|
||||
assert "uuid" in post_call[2]
|
||||
assert post_call[2]["integration_count"] == 0
|
||||
|
||||
|
||||
|
@ -258,7 +259,7 @@ async def test_send_statistics_async_get_integration_unknown_exception(
|
|||
with pytest.raises(ValueError), patch(
|
||||
"homeassistant.components.analytics.analytics.async_get_integration",
|
||||
side_effect=ValueError,
|
||||
), patch("homeassistant.helpers.instance_id.async_get", return_value=MOCK_HUUID):
|
||||
):
|
||||
await analytics.send_analytics()
|
||||
|
||||
|
||||
|
@ -298,9 +299,23 @@ async def test_send_statistics_with_supervisor(hass, caplog, aioclient_mock):
|
|||
), patch(
|
||||
"homeassistant.components.hassio.is_hassio",
|
||||
side_effect=Mock(return_value=True),
|
||||
), patch(
|
||||
"homeassistant.helpers.instance_id.async_get", return_value=MOCK_HUUID
|
||||
):
|
||||
await analytics.send_analytics()
|
||||
assert "'addon_count': 1" in caplog.text
|
||||
assert "'integrations':" not in caplog.text
|
||||
|
||||
|
||||
async def test_reusing_uuid(hass, aioclient_mock):
|
||||
"""Test reusing the stored UUID."""
|
||||
aioclient_mock.post(ANALYTICS_ENDPOINT_URL, status=200)
|
||||
analytics = Analytics(hass)
|
||||
analytics._data[ATTR_UUID] = "NOT_MOCK_UUID"
|
||||
|
||||
await analytics.save_preferences({ATTR_BASE: True})
|
||||
|
||||
with patch("uuid.UUID.hex", new_callable=PropertyMock) as hex:
|
||||
# This is not actually called but that in itself prove the test
|
||||
hex.return_value = MOCK_UUID
|
||||
await analytics.send_analytics()
|
||||
|
||||
assert analytics.uuid == "NOT_MOCK_UUID"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue