Migrate geofency over to the Webhook component (#18951)

* Migrate geofency over to the Webhook component

* Return web.Response correctly

* Fix test

* Lint

* Fix error that tests caught
This commit is contained in:
Rohan Kapoor authored and Paulus Schoutsen committed 2019-01-08 20:47:05 -08:00
1 parent 23382ab199
commit 2bdbf6955d
5 files changed
+186 -106

No files matched your search

+45 -17
View File
@@ -1,12 +1,13 @@
"""The tests for the Geofency device tracker platform."""
# pylint: disable=redefined-outer-name
from unittest.mock import patch
from unittest.mock import patch, Mock
import pytest
from homeassistant import data_entry_flow
from homeassistant.components import zone
from homeassistant.components.geofency import (
CONF_MOBILE_BEACONS, URL, DOMAIN)
CONF_MOBILE_BEACONS, DOMAIN)
from homeassistant.const import (
HTTP_OK, HTTP_UNPROCESSABLE_ENTITY, STATE_HOME,
STATE_NOT_HOME)
@@ -113,6 +114,9 @@ def mock_dev_track(mock_device_tracker_conf):
@pytest.fixture
def geofency_client(loop, hass, hass_client):
"""Geofency mock client."""
assert loop.run_until_complete(async_setup_component(
hass, 'persistent_notification', {}))
assert loop.run_until_complete(async_setup_component(
hass, DOMAIN, {
DOMAIN: {
@@ -138,10 +142,28 @@ def setup_zones(loop, hass):
}}))
async def test_data_validation(geofency_client):
@pytest.fixture
async def webhook_id(hass, geofency_client):
"""Initialize the Geofency component and get the webhook_id."""
hass.config.api = Mock(base_url='http://example.com')
result = await hass.config_entries.flow.async_init('geofency', context={
'source': 'user'
})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM, result
result = await hass.config_entries.flow.async_configure(
result['flow_id'], {})
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
return result['result'].data['webhook_id']
async def test_data_validation(geofency_client, webhook_id):
"""Test data validation."""
url = '/api/webhook/{}'.format(webhook_id)
# No data
req = await geofency_client.post(URL)
req = await geofency_client.post(url)
assert req.status == HTTP_UNPROCESSABLE_ENTITY
missing_attributes = ['address', 'device',
@@ -151,14 +173,16 @@ async def test_data_validation(geofency_client):
for attribute in missing_attributes:
copy = GPS_ENTER_HOME.copy()
del copy[attribute]
req = await geofency_client.post(URL, data=copy)
req = await geofency_client.post(url, data=copy)
assert req.status == HTTP_UNPROCESSABLE_ENTITY
async def test_gps_enter_and_exit_home(hass, geofency_client):
async def test_gps_enter_and_exit_home(hass, geofency_client, webhook_id):
"""Test GPS based zone enter and exit."""
url = '/api/webhook/{}'.format(webhook_id)
# Enter the Home zone
req = await geofency_client.post(URL, data=GPS_ENTER_HOME)
req = await geofency_client.post(url, data=GPS_ENTER_HOME)
await hass.async_block_till_done()
assert req.status == HTTP_OK
device_name = slugify(GPS_ENTER_HOME['device'])
@@ -167,7 +191,7 @@ async def test_gps_enter_and_exit_home(hass, geofency_client):
assert STATE_HOME == state_name
# Exit the Home zone
req = await geofency_client.post(URL, data=GPS_EXIT_HOME)
req = await geofency_client.post(url, data=GPS_EXIT_HOME)
await hass.async_block_till_done()
assert req.status == HTTP_OK
device_name = slugify(GPS_EXIT_HOME['device'])
@@ -180,7 +204,7 @@ async def test_gps_enter_and_exit_home(hass, geofency_client):
data['currentLatitude'] = NOT_HOME_LATITUDE
data['currentLongitude'] = NOT_HOME_LONGITUDE
req = await geofency_client.post(URL, data=data)
req = await geofency_client.post(url, data=data)
await hass.async_block_till_done()
assert req.status == HTTP_OK
device_name = slugify(GPS_EXIT_HOME['device'])
@@ -192,10 +216,12 @@ async def test_gps_enter_and_exit_home(hass, geofency_client):
assert NOT_HOME_LONGITUDE == current_longitude
async def test_beacon_enter_and_exit_home(hass, geofency_client):
async def test_beacon_enter_and_exit_home(hass, geofency_client, webhook_id):
"""Test iBeacon based zone enter and exit - a.k.a stationary iBeacon."""
url = '/api/webhook/{}'.format(webhook_id)
# Enter the Home zone
req = await geofency_client.post(URL, data=BEACON_ENTER_HOME)
req = await geofency_client.post(url, data=BEACON_ENTER_HOME)
await hass.async_block_till_done()
assert req.status == HTTP_OK
device_name = slugify("beacon_{}".format(BEACON_ENTER_HOME['name']))
@@ -204,7 +230,7 @@ async def test_beacon_enter_and_exit_home(hass, geofency_client):
assert STATE_HOME == state_name
# Exit the Home zone
req = await geofency_client.post(URL, data=BEACON_EXIT_HOME)
req = await geofency_client.post(url, data=BEACON_EXIT_HOME)
await hass.async_block_till_done()
assert req.status == HTTP_OK
device_name = slugify("beacon_{}".format(BEACON_ENTER_HOME['name']))
@@ -213,10 +239,12 @@ async def test_beacon_enter_and_exit_home(hass, geofency_client):
assert STATE_NOT_HOME == state_name
async def test_beacon_enter_and_exit_car(hass, geofency_client):
async def test_beacon_enter_and_exit_car(hass, geofency_client, webhook_id):
"""Test use of mobile iBeacon."""
url = '/api/webhook/{}'.format(webhook_id)
# Enter the Car away from Home zone
req = await geofency_client.post(URL, data=BEACON_ENTER_CAR)
req = await geofency_client.post(url, data=BEACON_ENTER_CAR)
await hass.async_block_till_done()
assert req.status == HTTP_OK
device_name = slugify("beacon_{}".format(BEACON_ENTER_CAR['name']))
@@ -225,7 +253,7 @@ async def test_beacon_enter_and_exit_car(hass, geofency_client):
assert STATE_NOT_HOME == state_name
# Exit the Car away from Home zone
req = await geofency_client.post(URL, data=BEACON_EXIT_CAR)
req = await geofency_client.post(url, data=BEACON_EXIT_CAR)
await hass.async_block_till_done()
assert req.status == HTTP_OK
device_name = slugify("beacon_{}".format(BEACON_ENTER_CAR['name']))
@@ -237,7 +265,7 @@ async def test_beacon_enter_and_exit_car(hass, geofency_client):
data = BEACON_ENTER_CAR.copy()
data['latitude'] = HOME_LATITUDE
data['longitude'] = HOME_LONGITUDE
req = await geofency_client.post(URL, data=data)
req = await geofency_client.post(url, data=data)
await hass.async_block_till_done()
assert req.status == HTTP_OK
device_name = slugify("beacon_{}".format(data['name']))
@@ -246,7 +274,7 @@ async def test_beacon_enter_and_exit_car(hass, geofency_client):
assert STATE_HOME == state_name
# Exit the Car in the Home zone
req = await geofency_client.post(URL, data=data)
req = await geofency_client.post(url, data=data)
await hass.async_block_till_done()
assert req.status == HTTP_OK
device_name = slugify("beacon_{}".format(data['name']))