Refactor Netatmo tests (#48277)

This commit is contained in:
Tobias Sauerwein 2021-03-26 11:13:27 +01:00 committed by GitHub
parent 4fbc3da196
commit 1ba54ac2bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 138 deletions

View file

@ -31,6 +31,10 @@ COMMON_RESPONSE = {
TEST_TIME = 1559347200.0
FAKE_WEBHOOK_ACTIVATION = {
"push_type": "webhook_activation",
}
def fake_post_request(**args):
"""Return fake data."""

View file

@ -5,6 +5,7 @@ from unittest.mock import patch
from homeassistant.components import camera
from homeassistant.components.camera import STATE_STREAMING
from homeassistant.components.netatmo.const import (
NETATMO_EVENT,
SERVICE_SET_CAMERA_LIGHT,
SERVICE_SET_PERSON_AWAY,
SERVICE_SET_PERSONS_HOME,
@ -14,7 +15,7 @@ from homeassistant.util import dt
from .common import fake_post_request, simulate_webhook
from tests.common import async_fire_time_changed
from tests.common import async_capture_events, async_fire_time_changed
async def test_setup_component_with_webhook(hass, camera_entry):
@ -253,3 +254,35 @@ async def test_camera_reconnect_webhook(hass, config_entry):
)
await hass.async_block_till_done()
mock_post.assert_called()
async def test_webhook_person_event(hass, camera_entry):
"""Test that person events are handled."""
test_netatmo_event = async_capture_events(hass, NETATMO_EVENT)
assert not test_netatmo_event
fake_webhook_event = {
"persons": [
{
"id": "91827374-7e04-5298-83ad-a0cb8372dff1",
"face_id": "a1b2c3d4e5",
"face_key": "9876543",
"is_known": True,
"face_url": "https://netatmocameraimage.blob.core.windows.net/production/12345",
}
],
"snapshot_id": "123456789abc",
"snapshot_key": "foobar123",
"snapshot_url": "https://netatmocameraimage.blob.core.windows.net/production/12346",
"event_type": "person",
"camera_id": "12:34:56:00:f1:62",
"device_id": "12:34:56:00:f1:62",
"event_id": "1234567890",
"message": "MYHOME: John Doe has been seen by Indoor Camera ",
"push_type": "NACamera-person",
}
webhook_id = camera_entry.data[CONF_WEBHOOK_ID]
await simulate_webhook(hass, webhook_id, fake_webhook_event)
assert test_netatmo_event

View file

@ -7,7 +7,7 @@ from homeassistant.components.netatmo import DOMAIN
from homeassistant.const import CONF_WEBHOOK_ID
from homeassistant.setup import async_setup_component
from .common import fake_post_request, simulate_webhook
from .common import FAKE_WEBHOOK_ACTIVATION, fake_post_request, simulate_webhook
from tests.common import MockConfigEntry
from tests.components.cloud import mock_cloud
@ -37,10 +37,6 @@ FAKE_WEBHOOK = {
"push_type": "display_change",
}
FAKE_WEBHOOK_ACTIVATION = {
"push_type": "webhook_activation",
}
async def test_setup_component(hass):
"""Test setup and teardown of the netatmo component."""

View file

@ -8,7 +8,7 @@ from homeassistant.components.light import (
)
from homeassistant.const import ATTR_ENTITY_ID, CONF_WEBHOOK_ID
from .common import simulate_webhook
from .common import FAKE_WEBHOOK_ACTIVATION, simulate_webhook
async def test_light_setup_and_services(hass, light_entry):
@ -16,10 +16,7 @@ async def test_light_setup_and_services(hass, light_entry):
webhook_id = light_entry.data[CONF_WEBHOOK_ID]
# Fake webhook activation
webhook_data = {
"push_type": "webhook_activation",
}
await simulate_webhook(hass, webhook_id, webhook_data)
await simulate_webhook(hass, webhook_id, FAKE_WEBHOOK_ACTIVATION)
await hass.async_block_till_done()
light_entity = "light.netatmo_garden"
@ -40,7 +37,6 @@ async def test_light_setup_and_services(hass, light_entry):
# Trigger light mode change with erroneous webhook data
response = {
"user_id": "91763b24c43d3e344f424e8d",
"event_type": "light_mode",
"device_id": "12:34:56:00:a5:a4",
}

View file

@ -1,126 +0,0 @@
"""The tests for Netatmo webhook events."""
from homeassistant.components.netatmo.const import DATA_DEVICE_IDS, DATA_PERSONS
from homeassistant.components.netatmo.webhook import async_handle_webhook
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.util.aiohttp import MockRequest
async def test_webhook(hass):
"""Test that webhook events are processed."""
webhook_called = False
async def handle_event(_):
nonlocal webhook_called
webhook_called = True
response = (
b'{"user_id": "123", "user": {"id": "123", "email": "foo@bar.com"},'
b'"push_type": "webhook_activation"}'
)
request = MockRequest(content=response, mock_source="test")
async_dispatcher_connect(
hass,
"signal-netatmo-webhook-None",
handle_event,
)
await async_handle_webhook(hass, "webhook_id", request)
await hass.async_block_till_done()
assert webhook_called
async def test_webhook_error_in_data(hass):
"""Test that errors in webhook data are handled."""
webhook_called = False
async def handle_event(_):
nonlocal webhook_called
webhook_called = True
response = b'""webhook_activation"}'
request = MockRequest(content=response, mock_source="test")
async_dispatcher_connect(
hass,
"signal-netatmo-webhook-None",
handle_event,
)
await async_handle_webhook(hass, "webhook_id", request)
await hass.async_block_till_done()
assert not webhook_called
async def test_webhook_climate_event(hass):
"""Test that climate events are handled."""
webhook_called = False
async def handle_event(_):
nonlocal webhook_called
webhook_called = True
response = (
b'{"user_id": "123", "user": {"id": "123", "email": "foo@bar.com"},'
b'"home_id": "456", "event_type": "therm_mode",'
b'"home": {"id": "456", "therm_mode": "away"},'
b'"mode": "away", "previous_mode": "schedule", "push_type": "home_event_changed"}'
)
request = MockRequest(content=response, mock_source="test")
hass.data["netatmo"] = {
DATA_DEVICE_IDS: {},
}
async_dispatcher_connect(
hass,
"signal-netatmo-webhook-therm_mode",
handle_event,
)
await async_handle_webhook(hass, "webhook_id", request)
await hass.async_block_till_done()
assert webhook_called
async def test_webhook_person_event(hass):
"""Test that person events are handled."""
webhook_called = False
async def handle_event(_):
nonlocal webhook_called
webhook_called = True
response = (
b'{"user_id": "5c81004xxxxxxxxxx45f4",'
b'"persons": [{"id": "e2bf7xxxxxxxxxxxxea3", "face_id": "5d66xxxxxx9b9",'
b'"face_key": "89dxxxxx22", "is_known": true,'
b'"face_url": "https://netatmocameraimage.blob.core.windows.net/production/5xxx"}],'
b'"snapshot_id": "5d19bae867368a59e81cca89", "snapshot_key": "d3b3ae0229f7xb74cf8",'
b'"snapshot_url": "https://netatmocameraimage.blob.core.windows.net/production/5xxxx",'
b'"event_type": "person", "camera_id": "70:xxxxxx:a7", "device_id": "70:xxxxxx:a7",'
b'"home_id": "5c5dxxxxxxxd594", "home_name": "Boulogne Billan.",'
b'"event_id": "5d19bxxxxxxxxcca88",'
b'"message": "Boulogne Billan.: Benoit has been seen by Indoor Camera ",'
b'"push_type": "NACamera-person"}'
)
request = MockRequest(content=response, mock_source="test")
hass.data["netatmo"] = {
DATA_DEVICE_IDS: {},
DATA_PERSONS: {},
}
async_dispatcher_connect(
hass,
"signal-netatmo-webhook-person",
handle_event,
)
await async_handle_webhook(hass, "webhook_id", request)
await hass.async_block_till_done()
assert webhook_called