2017-09-28 07:49:35 +00:00
|
|
|
"""Test the owntracks_http platform."""
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.setup import async_setup_component
|
2019-06-04 21:06:49 +00:00
|
|
|
from homeassistant.components import owntracks
|
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
|
2018-12-10 11:24:56 +00:00
|
|
|
def test_returns_error_missing_username(mock_client, caplog):
|
2018-11-06 15:10:17 +00:00
|
|
|
"""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-12-10 11:24:56 +00:00
|
|
|
# Needs to be 200 or OwnTracks keeps retrying bad packet.
|
|
|
|
assert resp.status == 200
|
|
|
|
json = yield from resp.json()
|
|
|
|
assert json == []
|
|
|
|
assert 'No topic or user found' in caplog.text
|
|
|
|
|
2018-11-06 15:10:17 +00:00
|
|
|
|
2018-12-10 11:24:56 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_returns_error_incorrect_json(mock_client, caplog):
|
|
|
|
"""Test that an error is returned when username is missing."""
|
|
|
|
resp = yield from mock_client.post(
|
|
|
|
'/api/webhook/owntracks_test',
|
|
|
|
data='not json',
|
|
|
|
headers={
|
|
|
|
'X-Limit-d': 'Pixel',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
# Needs to be 200 or OwnTracks keeps retrying bad packet.
|
|
|
|
assert resp.status == 200
|
2018-11-06 15:10:17 +00:00
|
|
|
json = yield from resp.json()
|
2018-12-10 11:24:56 +00:00
|
|
|
assert json == []
|
|
|
|
assert 'invalid JSON' in caplog.text
|
2018-11-06 15:10:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@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 == []
|
2019-06-04 21:06:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_context_delivers_pending_msg():
|
|
|
|
"""Test that context is able to hold pending messages while being init."""
|
|
|
|
context = owntracks.OwnTracksContext(
|
|
|
|
None, None, None, None, None, None, None, None
|
|
|
|
)
|
|
|
|
context.async_see(hello='world')
|
|
|
|
context.async_see(world='hello')
|
|
|
|
received = []
|
|
|
|
|
|
|
|
context.set_async_see(lambda **data: received.append(data))
|
|
|
|
|
|
|
|
assert len(received) == 2
|
|
|
|
assert received[0] == {'hello': 'world'}
|
|
|
|
assert received[1] == {'world': 'hello'}
|
|
|
|
|
|
|
|
received.clear()
|
|
|
|
|
|
|
|
context.set_async_see(lambda **data: received.append(data))
|
|
|
|
assert len(received) == 0
|