2017-09-28 07:49:35 +00:00
|
|
|
"""Test the owntracks_http platform."""
|
|
|
|
import pytest
|
|
|
|
|
2019-06-04 21:06:49 +00:00
|
|
|
from homeassistant.components import owntracks
|
2019-12-09 11:11:27 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry, mock_component
|
2018-11-06 15:10:17 +00:00
|
|
|
|
|
|
|
MINIMAL_LOCATION_MESSAGE = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"_type": "location",
|
|
|
|
"lon": 45,
|
|
|
|
"lat": 90,
|
|
|
|
"p": 101.3977584838867,
|
|
|
|
"tid": "test",
|
|
|
|
"tst": 1,
|
2018-11-06 15:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LOCATION_MESSAGE = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"_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-11-06 15:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
2021-09-02 12:49:40 +00:00
|
|
|
def mock_client(hass, hass_client_no_auth):
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Start the Home Assistant HTTP component."""
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_component(hass, "group")
|
|
|
|
mock_component(hass, "zone")
|
|
|
|
mock_component(hass, "device_tracker")
|
2018-11-28 21:20:13 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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-11-28 21:20:13 +00:00
|
|
|
|
2021-09-02 12:49:40 +00:00
|
|
|
return hass.loop.run_until_complete(hass_client_no_auth())
|
2017-09-28 07:49:35 +00:00
|
|
|
|
|
|
|
|
2020-01-01 23:18:20 +00:00
|
|
|
async def test_handle_valid_message(mock_client):
|
2018-11-06 15:10:17 +00:00
|
|
|
"""Test that we forward messages correctly to OwnTracks."""
|
2020-01-01 23:18:20 +00:00
|
|
|
resp = await mock_client.post(
|
2019-07-31 19:25:30 +00:00
|
|
|
"/api/webhook/owntracks_test",
|
2018-11-28 21:20:13 +00:00
|
|
|
json=LOCATION_MESSAGE,
|
2019-07-31 19:25:30 +00:00
|
|
|
headers={"X-Limit-u": "Paulus", "X-Limit-d": "Pixel"},
|
2018-11-28 21:20:13 +00:00
|
|
|
)
|
2018-11-06 15:10:17 +00:00
|
|
|
|
|
|
|
assert resp.status == 200
|
|
|
|
|
2020-01-01 23:18:20 +00:00
|
|
|
json = await resp.json()
|
2018-11-06 15:10:17 +00:00
|
|
|
assert json == []
|
2017-09-28 07:49:35 +00:00
|
|
|
|
|
|
|
|
2020-01-01 23:18:20 +00:00
|
|
|
async def test_handle_valid_minimal_message(mock_client):
|
2018-11-06 15:10:17 +00:00
|
|
|
"""Test that we forward messages correctly to OwnTracks."""
|
2020-01-01 23:18:20 +00:00
|
|
|
resp = await mock_client.post(
|
2019-07-31 19:25:30 +00:00
|
|
|
"/api/webhook/owntracks_test",
|
2018-11-28 21:20:13 +00:00
|
|
|
json=MINIMAL_LOCATION_MESSAGE,
|
2019-07-31 19:25:30 +00:00
|
|
|
headers={"X-Limit-u": "Paulus", "X-Limit-d": "Pixel"},
|
2018-11-28 21:20:13 +00:00
|
|
|
)
|
2018-11-06 15:10:17 +00:00
|
|
|
|
2017-09-28 07:49:35 +00:00
|
|
|
assert resp.status == 200
|
|
|
|
|
2020-01-01 23:18:20 +00:00
|
|
|
json = await resp.json()
|
2018-11-06 15:10:17 +00:00
|
|
|
assert json == []
|
2017-09-28 07:49:35 +00:00
|
|
|
|
|
|
|
|
2020-01-01 23:18:20 +00:00
|
|
|
async def test_handle_value_error(mock_client):
|
2018-11-06 15:10:17 +00:00
|
|
|
"""Test we don't disclose that this is a valid webhook."""
|
2020-01-01 23:18:20 +00:00
|
|
|
resp = await mock_client.post(
|
2019-07-31 19:25:30 +00:00
|
|
|
"/api/webhook/owntracks_test",
|
|
|
|
json="",
|
|
|
|
headers={"X-Limit-u": "Paulus", "X-Limit-d": "Pixel"},
|
2018-11-28 21:20:13 +00:00
|
|
|
)
|
2018-11-06 15:10:17 +00:00
|
|
|
|
|
|
|
assert resp.status == 200
|
|
|
|
|
2020-01-01 23:18:20 +00:00
|
|
|
json = await resp.text()
|
2018-11-06 15:10:17 +00:00
|
|
|
assert json == ""
|
|
|
|
|
|
|
|
|
2020-01-01 23:18:20 +00:00
|
|
|
async 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."""
|
2020-01-01 23:18:20 +00:00
|
|
|
resp = await mock_client.post(
|
2019-07-31 19:25:30 +00:00
|
|
|
"/api/webhook/owntracks_test",
|
2018-11-28 21:20:13 +00:00
|
|
|
json=LOCATION_MESSAGE,
|
2019-07-31 19:25:30 +00:00
|
|
|
headers={"X-Limit-d": "Pixel"},
|
2018-11-28 21:20:13 +00:00
|
|
|
)
|
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
|
2020-01-01 23:18:20 +00:00
|
|
|
json = await resp.json()
|
2018-12-10 11:24:56 +00:00
|
|
|
assert json == []
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "No topic or user found" in caplog.text
|
2018-12-10 11:24:56 +00:00
|
|
|
|
2018-11-06 15:10:17 +00:00
|
|
|
|
2020-01-01 23:18:20 +00:00
|
|
|
async def test_returns_error_incorrect_json(mock_client, caplog):
|
2018-12-10 11:24:56 +00:00
|
|
|
"""Test that an error is returned when username is missing."""
|
2020-01-01 23:18:20 +00:00
|
|
|
resp = await mock_client.post(
|
2019-07-31 19:25:30 +00:00
|
|
|
"/api/webhook/owntracks_test", data="not json", headers={"X-Limit-d": "Pixel"}
|
2018-12-10 11:24:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Needs to be 200 or OwnTracks keeps retrying bad packet.
|
|
|
|
assert resp.status == 200
|
2020-01-01 23:18:20 +00:00
|
|
|
json = await resp.json()
|
2018-12-10 11:24:56 +00:00
|
|
|
assert json == []
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "invalid JSON" in caplog.text
|
2018-11-06 15:10:17 +00:00
|
|
|
|
|
|
|
|
2020-01-01 23:18:20 +00:00
|
|
|
async def test_returns_error_missing_device(mock_client):
|
2018-11-06 15:10:17 +00:00
|
|
|
"""Test that an error is returned when device name is missing."""
|
2020-01-01 23:18:20 +00:00
|
|
|
resp = await mock_client.post(
|
2019-07-31 19:25:30 +00:00
|
|
|
"/api/webhook/owntracks_test",
|
2018-11-28 21:20:13 +00:00
|
|
|
json=LOCATION_MESSAGE,
|
2019-07-31 19:25:30 +00:00
|
|
|
headers={"X-Limit-u": "Paulus"},
|
2018-11-28 21:20:13 +00:00
|
|
|
)
|
2018-11-06 15:10:17 +00:00
|
|
|
|
|
|
|
assert resp.status == 200
|
|
|
|
|
2020-01-01 23:18:20 +00:00
|
|
|
json = await 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."""
|
2019-07-31 19:25:30 +00:00
|
|
|
context = owntracks.OwnTracksContext(None, None, None, None, None, None, None, None)
|
|
|
|
context.async_see(hello="world")
|
|
|
|
context.async_see(world="hello")
|
2019-06-04 21:06:49 +00:00
|
|
|
received = []
|
|
|
|
|
|
|
|
context.set_async_see(lambda **data: received.append(data))
|
|
|
|
|
|
|
|
assert len(received) == 2
|
2019-07-31 19:25:30 +00:00
|
|
|
assert received[0] == {"hello": "world"}
|
|
|
|
assert received[1] == {"world": "hello"}
|
2019-06-04 21:06:49 +00:00
|
|
|
|
|
|
|
received.clear()
|
|
|
|
|
|
|
|
context.set_async_see(lambda **data: received.append(data))
|
|
|
|
assert len(received) == 0
|