1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Migrate gpslogger to the automatically generated webhook (#20079)

* Migrate gpslogger to the automatically generated webhook

* Lint

* Lint and return error code
This commit is contained in:
Rohan Kapoor
2019-01-16 10:56:25 -08:00
committed by Paulus Schoutsen
parent 075b575bde
commit b5bfc759ec
5 changed files with 165 additions and 145 deletions

View File

@@ -1,29 +1,21 @@
"""The tests the for GPSLogger device tracker platform."""
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.device_tracker import \
DOMAIN as DEVICE_TRACKER_DOMAIN
from homeassistant.components.gpslogger import URL, DOMAIN
from homeassistant.components.http import CONF_API_PASSWORD
from homeassistant.components.gpslogger import DOMAIN
from homeassistant.const import HTTP_OK, HTTP_UNPROCESSABLE_ENTITY, \
STATE_HOME, STATE_NOT_HOME, HTTP_UNAUTHORIZED, CONF_PASSWORD
STATE_HOME, STATE_NOT_HOME
from homeassistant.setup import async_setup_component
HOME_LATITUDE = 37.239622
HOME_LONGITUDE = -115.815811
def _url(data=None):
"""Generate URL."""
data = data or {}
data = "&".join(["{}={}".format(name, value) for
name, value in data.items()])
return "{}?{}".format(URL, data)
@pytest.fixture(autouse=True)
def mock_dev_track(mock_device_tracker_conf):
"""Mock device tracker config loading."""
@@ -31,28 +23,14 @@ def mock_dev_track(mock_device_tracker_conf):
@pytest.fixture
def authenticated_gpslogger_client(loop, hass, hass_client):
"""Locative mock client (authenticated)."""
assert loop.run_until_complete(async_setup_component(
hass, DOMAIN, {
DOMAIN: {}
}))
with patch('homeassistant.components.device_tracker.update_config'):
yield loop.run_until_complete(hass_client())
@pytest.fixture
def unauthenticated_gpslogger_client(loop, hass, aiohttp_client):
"""Locative mock client (unauthenticated)."""
def gpslogger_client(loop, hass, aiohttp_client):
"""Mock client for GPSLogger (unauthenticated)."""
assert loop.run_until_complete(async_setup_component(
hass, 'persistent_notification', {}))
assert loop.run_until_complete(async_setup_component(
hass, DOMAIN, {
DOMAIN: {
CONF_PASSWORD: 'test'
}
DOMAIN: {}
}))
with patch('homeassistant.components.device_tracker.update_config'):
@@ -72,31 +50,26 @@ def setup_zones(loop, hass):
}}))
async def test_authentication(hass, unauthenticated_gpslogger_client):
@pytest.fixture
async def webhook_id(hass, gpslogger_client):
"""Initialize the GPSLogger component and get the webhook_id."""
hass.config.api = Mock(base_url='http://example.com')
result = await hass.config_entries.flow.async_init(DOMAIN, 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_missing_data(hass, gpslogger_client, webhook_id):
"""Test missing data."""
data = {
'latitude': 1.0,
'longitude': 1.1,
'device': '123',
CONF_API_PASSWORD: 'test'
}
url = '/api/webhook/{}'.format(webhook_id)
# No auth
req = await unauthenticated_gpslogger_client.get(_url({}))
await hass.async_block_till_done()
assert req.status == HTTP_UNAUTHORIZED
# Authenticated
req = await unauthenticated_gpslogger_client.get(_url(data))
await hass.async_block_till_done()
assert req.status == HTTP_OK
state_name = hass.states.get('{}.{}'.format(DEVICE_TRACKER_DOMAIN,
data['device'])).state
assert STATE_NOT_HOME == state_name
async def test_missing_data(hass, authenticated_gpslogger_client):
"""Test missing data."""
data = {
'latitude': 1.0,
'longitude': 1.1,
@@ -104,27 +77,29 @@ async def test_missing_data(hass, authenticated_gpslogger_client):
}
# No data
req = await authenticated_gpslogger_client.get(_url({}))
req = await gpslogger_client.post(url)
await hass.async_block_till_done()
assert req.status == HTTP_UNPROCESSABLE_ENTITY
# No latitude
copy = data.copy()
del copy['latitude']
req = await authenticated_gpslogger_client.get(_url(copy))
req = await gpslogger_client.post(url, data=copy)
await hass.async_block_till_done()
assert req.status == HTTP_UNPROCESSABLE_ENTITY
# No device
copy = data.copy()
del copy['device']
req = await authenticated_gpslogger_client.get(_url(copy))
req = await gpslogger_client.post(url, data=copy)
await hass.async_block_till_done()
assert req.status == HTTP_UNPROCESSABLE_ENTITY
async def test_enter_and_exit(hass, authenticated_gpslogger_client):
async def test_enter_and_exit(hass, gpslogger_client, webhook_id):
"""Test when there is a known zone."""
url = '/api/webhook/{}'.format(webhook_id)
data = {
'latitude': HOME_LATITUDE,
'longitude': HOME_LONGITUDE,
@@ -132,7 +107,7 @@ async def test_enter_and_exit(hass, authenticated_gpslogger_client):
}
# Enter the Home
req = await authenticated_gpslogger_client.get(_url(data))
req = await gpslogger_client.post(url, data=data)
await hass.async_block_till_done()
assert req.status == HTTP_OK
state_name = hass.states.get('{}.{}'.format(DEVICE_TRACKER_DOMAIN,
@@ -140,7 +115,7 @@ async def test_enter_and_exit(hass, authenticated_gpslogger_client):
assert STATE_HOME == state_name
# Enter Home again
req = await authenticated_gpslogger_client.get(_url(data))
req = await gpslogger_client.post(url, data=data)
await hass.async_block_till_done()
assert req.status == HTTP_OK
state_name = hass.states.get('{}.{}'.format(DEVICE_TRACKER_DOMAIN,
@@ -151,7 +126,7 @@ async def test_enter_and_exit(hass, authenticated_gpslogger_client):
data['latitude'] = 0
# Enter Somewhere else
req = await authenticated_gpslogger_client.get(_url(data))
req = await gpslogger_client.post(url, data=data)
await hass.async_block_till_done()
assert req.status == HTTP_OK
state_name = hass.states.get('{}.{}'.format(DEVICE_TRACKER_DOMAIN,
@@ -159,8 +134,10 @@ async def test_enter_and_exit(hass, authenticated_gpslogger_client):
assert STATE_NOT_HOME == state_name
async def test_enter_with_attrs(hass, authenticated_gpslogger_client):
async def test_enter_with_attrs(hass, gpslogger_client, webhook_id):
"""Test when additional attributes are present."""
url = '/api/webhook/{}'.format(webhook_id)
data = {
'latitude': 1.0,
'longitude': 1.1,
@@ -174,13 +151,13 @@ async def test_enter_with_attrs(hass, authenticated_gpslogger_client):
'activity': 'running'
}
req = await authenticated_gpslogger_client.get(_url(data))
req = await gpslogger_client.post(url, data=data)
await hass.async_block_till_done()
assert req.status == HTTP_OK
state = hass.states.get('{}.{}'.format(DEVICE_TRACKER_DOMAIN,
data['device']))
assert STATE_NOT_HOME == state.state
assert 10 == state.attributes['gps_accuracy']
assert 10.5 == state.attributes['gps_accuracy']
assert 10.0 == state.attributes['battery']
assert 100.0 == state.attributes['speed']
assert 105.32 == state.attributes['direction']