2017-09-28 07:49:35 +00:00
|
|
|
"""Test the owntracks_http platform."""
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
|
2018-11-28 21:20:13 +00:00
|
|
|
from tests.common import mock_component, MockConfigEntry
|
2018-11-06 15:10:17 +00:00
|
|
|
|
|
|
|
MINIMAL_LOCATION_MESSAGE = {
|
|
|
|
'_type': 'location',
|
|
|
|
'lon': 45,
|
|
|
|
'lat': 90,
|
|
|
|
'p': 101.3977584838867,
|
|
|
|
'tid': 'test',
|
|
|
|
'tst': 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCATION_MESSAGE = {
|
|
|
|
'_type': 'location',
|
|
|
|
'acc': 60,
|
|
|
|
'alt': 27,
|
|
|
|
'batt': 92,
|
|
|
|
'cog': 248,
|
|
|
|
'lon': 45,
|
|
|
|
'lat': 90,
|
|
|
|
'p': 101.3977584838867,
|
|
|
|
'tid': 'test',
|
|
|
|
't': 'u',
|
|
|
|
'tst': 1,
|
|
|
|
'vac': 4,
|
|
|
|
'vel': 0
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-04 09:45:41 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def mock_dev_track(mock_device_tracker_conf):
|
|
|
|
"""Mock device tracker config loading."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-09-28 07:49:35 +00:00
|
|
|
@pytest.fixture
|
2018-03-15 20:49:49 +00:00
|
|
|
def mock_client(hass, aiohttp_client):
|
2017-09-28 07:49:35 +00:00
|
|
|
"""Start the Hass HTTP component."""
|
|
|
|
mock_component(hass, 'group')
|
|
|
|
mock_component(hass, 'zone')
|
2018-11-28 21:20:13 +00:00
|
|
|
mock_component(hass, 'device_tracker')
|
|
|
|
|
|
|
|
MockConfigEntry(domain='owntracks', data={
|
|
|
|
'webhook_id': 'owntracks_test',
|
|
|
|
'secret': 'abcd',
|
|
|
|
}).add_to_hass(hass)
|
|
|
|
hass.loop.run_until_complete(async_setup_component(hass, 'owntracks', {}))
|
|
|
|
|
2018-03-15 20:49:49 +00:00
|
|
|
return hass.loop.run_until_complete(aiohttp_client(hass.http.app))
|
2017-09-28 07:49:35 +00:00
|
|
|
|
|
|
|
|
2018-11-06 15:10:17 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_handle_valid_message(mock_client):
|
|
|
|
"""Test that we forward messages correctly to OwnTracks."""
|
2018-11-28 21:20:13 +00:00
|
|
|
resp = yield from mock_client.post(
|
|
|
|
'/api/webhook/owntracks_test',
|
|
|
|
json=LOCATION_MESSAGE,
|
|
|
|
headers={
|
|
|
|
'X-Limit-u': 'Paulus',
|
|
|
|
'X-Limit-d': 'Pixel',
|
|
|
|
}
|
|
|
|
)
|
2018-11-06 15:10:17 +00:00
|
|
|
|
|
|
|
assert resp.status == 200
|
|
|
|
|
|
|
|
json = yield from resp.json()
|
|
|
|
assert json == []
|
2017-09-28 07:49:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2018-11-06 15:10:17 +00:00
|
|
|
def test_handle_valid_minimal_message(mock_client):
|
|
|
|
"""Test that we forward messages correctly to OwnTracks."""
|
2018-11-28 21:20:13 +00:00
|
|
|
resp = yield from mock_client.post(
|
|
|
|
'/api/webhook/owntracks_test',
|
|
|
|
json=MINIMAL_LOCATION_MESSAGE,
|
|
|
|
headers={
|
|
|
|
'X-Limit-u': 'Paulus',
|
|
|
|
'X-Limit-d': 'Pixel',
|
|
|
|
}
|
|
|
|
)
|
2018-11-06 15:10:17 +00:00
|
|
|
|
2017-09-28 07:49:35 +00:00
|
|
|
assert resp.status == 200
|
|
|
|
|
2018-11-06 15:10:17 +00:00
|
|
|
json = yield from resp.json()
|
|
|
|
assert json == []
|
2017-09-28 07:49:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2018-11-06 15:10:17 +00:00
|
|
|
def test_handle_value_error(mock_client):
|
|
|
|
"""Test we don't disclose that this is a valid webhook."""
|
2018-11-28 21:20:13 +00:00
|
|
|
resp = yield from mock_client.post(
|
|
|
|
'/api/webhook/owntracks_test',
|
|
|
|
json='',
|
|
|
|
headers={
|
|
|
|
'X-Limit-u': 'Paulus',
|
|
|
|
'X-Limit-d': 'Pixel',
|
|
|
|
}
|
|
|
|
)
|
2018-11-06 15:10:17 +00:00
|
|
|
|
|
|
|
assert resp.status == 200
|
|
|
|
|
|
|
|
json = yield from resp.text()
|
|
|
|
assert json == ""
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_returns_error_missing_username(mock_client):
|
|
|
|
"""Test that an error is returned when username is missing."""
|
2018-11-28 21:20:13 +00:00
|
|
|
resp = yield from mock_client.post(
|
|
|
|
'/api/webhook/owntracks_test',
|
|
|
|
json=LOCATION_MESSAGE,
|
|
|
|
headers={
|
|
|
|
'X-Limit-d': 'Pixel',
|
|
|
|
}
|
|
|
|
)
|
2018-11-06 15:10:17 +00:00
|
|
|
|
2018-11-28 21:20:13 +00:00
|
|
|
assert resp.status == 400
|
2018-11-06 15:10:17 +00:00
|
|
|
|
|
|
|
json = yield from resp.json()
|
|
|
|
assert json == {'error': 'You need to supply username.'}
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_returns_error_missing_device(mock_client):
|
|
|
|
"""Test that an error is returned when device name is missing."""
|
2018-11-28 21:20:13 +00:00
|
|
|
resp = yield from mock_client.post(
|
|
|
|
'/api/webhook/owntracks_test',
|
|
|
|
json=LOCATION_MESSAGE,
|
|
|
|
headers={
|
|
|
|
'X-Limit-u': 'Paulus',
|
|
|
|
}
|
|
|
|
)
|
2018-11-06 15:10:17 +00:00
|
|
|
|
|
|
|
assert resp.status == 200
|
|
|
|
|
|
|
|
json = yield from resp.json()
|
2018-11-28 21:20:13 +00:00
|
|
|
assert json == []
|
|
|
|
|
|
|
|
|
|
|
|
async def test_config_flow_import(hass):
|
|
|
|
"""Test that we automatically create a config flow."""
|
|
|
|
assert not hass.config_entries.async_entries('owntracks')
|
|
|
|
assert await async_setup_component(hass, 'owntracks', {
|
|
|
|
'owntracks': {
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert hass.config_entries.async_entries('owntracks')
|