mirror of
https://github.com/home-assistant/core.git
synced 2026-02-21 02:18:47 +00:00
Do not send empty snapshots in analytics (#163351)
This commit is contained in:
@@ -534,6 +534,10 @@ class Analytics:
|
||||
|
||||
payload = await _async_snapshot_payload(self._hass)
|
||||
|
||||
if not payload:
|
||||
LOGGER.info("Skipping snapshot submission, no data to send")
|
||||
return
|
||||
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": f"home-assistant/{HA_VERSION}",
|
||||
|
||||
18
tests/components/analytics/conftest.py
Normal file
18
tests/components/analytics/conftest.py
Normal file
@@ -0,0 +1,18 @@
|
||||
"""Common fixtures for the analytics tests."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
MOCK_SNAPSHOT_PAYLOAD = {"mock_integration": {"devices": [], "entities": []}}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_snapshot_payload() -> Generator[None]:
|
||||
"""Mock _async_snapshot_payload to return non-empty data."""
|
||||
with patch(
|
||||
"homeassistant.components.analytics.analytics._async_snapshot_payload",
|
||||
return_value=MOCK_SNAPSHOT_PAYLOAD,
|
||||
):
|
||||
yield
|
||||
@@ -1464,7 +1464,7 @@ async def test_analytics_platforms(
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled")
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload")
|
||||
async def test_send_snapshot_disabled(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
@@ -1481,6 +1481,24 @@ async def test_send_snapshot_disabled(
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled")
|
||||
async def test_send_snapshot_empty(
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
) -> None:
|
||||
"""Test no snapshots are sent when payload is empty."""
|
||||
aioclient_mock.post(SNAPSHOT_ENDPOINT_URL, status=200, json={})
|
||||
|
||||
analytics = Analytics(hass)
|
||||
|
||||
await analytics.save_preferences({ATTR_SNAPSHOTS: True})
|
||||
await analytics.send_snapshot()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 0
|
||||
assert "Skipping snapshot submission, no data to send" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload")
|
||||
async def test_send_snapshot_success(
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
@@ -1505,7 +1523,7 @@ async def test_send_snapshot_success(
|
||||
assert "Submitted snapshot analytics to Home Assistant servers" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled")
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload")
|
||||
async def test_send_snapshot_with_existing_identifier(
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
@@ -1541,7 +1559,7 @@ async def test_send_snapshot_with_existing_identifier(
|
||||
assert "Submitted snapshot analytics to Home Assistant servers" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled")
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload")
|
||||
async def test_send_snapshot_invalid_identifier(
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
@@ -1578,7 +1596,7 @@ async def test_send_snapshot_invalid_identifier(
|
||||
assert "Invalid submission identifier" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled")
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload")
|
||||
@pytest.mark.parametrize(
|
||||
("post_kwargs", "expected_log"),
|
||||
[
|
||||
@@ -1643,7 +1661,7 @@ async def test_send_snapshot_error(
|
||||
assert expected_log in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled")
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload")
|
||||
async def test_async_schedule(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
@@ -1680,7 +1698,7 @@ async def test_async_schedule(
|
||||
assert 0 <= preferences["snapshot_submission_time"] <= 86400
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled")
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload")
|
||||
async def test_async_schedule_disabled(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
@@ -1705,7 +1723,7 @@ async def test_async_schedule_disabled(
|
||||
assert len(aioclient_mock.mock_calls) == 0
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled")
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload")
|
||||
async def test_async_schedule_already_scheduled(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
@@ -1739,7 +1757,7 @@ async def test_async_schedule_already_scheduled(
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled")
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload")
|
||||
@pytest.mark.parametrize(("onboarded"), [True, False])
|
||||
async def test_async_schedule_cancel_when_disabled(
|
||||
hass: HomeAssistant,
|
||||
@@ -1778,7 +1796,7 @@ async def test_async_schedule_cancel_when_disabled(
|
||||
assert len(aioclient_mock.mock_calls) == 0
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled")
|
||||
@pytest.mark.usefixtures("labs_snapshots_enabled", "mock_snapshot_payload")
|
||||
async def test_async_schedule_snapshots_url(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
|
||||
@@ -37,6 +37,7 @@ async def test_setup(hass: HomeAssistant) -> None:
|
||||
assert DOMAIN in hass.data
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_snapshot_payload")
|
||||
async def test_labs_feature_toggle(
|
||||
hass: HomeAssistant,
|
||||
hass_storage: dict[str, Any],
|
||||
|
||||
Reference in New Issue
Block a user