1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-26 05:57:01 +00:00
This commit is contained in:
Paulus Schoutsen
2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View File

@@ -8,28 +8,28 @@ from homeassistant.components import owntracks
from tests.common import mock_component, MockConfigEntry
MINIMAL_LOCATION_MESSAGE = {
'_type': 'location',
'lon': 45,
'lat': 90,
'p': 101.3977584838867,
'tid': 'test',
'tst': 1,
"_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
"_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,
}
@@ -42,15 +42,14 @@ def mock_dev_track(mock_device_tracker_conf):
@pytest.fixture
def mock_client(hass, aiohttp_client):
"""Start the Hass HTTP component."""
mock_component(hass, 'group')
mock_component(hass, 'zone')
mock_component(hass, 'device_tracker')
mock_component(hass, "group")
mock_component(hass, "zone")
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', {}))
MockConfigEntry(
domain="owntracks", data={"webhook_id": "owntracks_test", "secret": "abcd"}
).add_to_hass(hass)
hass.loop.run_until_complete(async_setup_component(hass, "owntracks", {}))
return hass.loop.run_until_complete(aiohttp_client(hass.http.app))
@@ -59,12 +58,9 @@ def mock_client(hass, aiohttp_client):
def test_handle_valid_message(mock_client):
"""Test that we forward messages correctly to OwnTracks."""
resp = yield from mock_client.post(
'/api/webhook/owntracks_test',
"/api/webhook/owntracks_test",
json=LOCATION_MESSAGE,
headers={
'X-Limit-u': 'Paulus',
'X-Limit-d': 'Pixel',
}
headers={"X-Limit-u": "Paulus", "X-Limit-d": "Pixel"},
)
assert resp.status == 200
@@ -77,12 +73,9 @@ def test_handle_valid_message(mock_client):
def test_handle_valid_minimal_message(mock_client):
"""Test that we forward messages correctly to OwnTracks."""
resp = yield from mock_client.post(
'/api/webhook/owntracks_test',
"/api/webhook/owntracks_test",
json=MINIMAL_LOCATION_MESSAGE,
headers={
'X-Limit-u': 'Paulus',
'X-Limit-d': 'Pixel',
}
headers={"X-Limit-u": "Paulus", "X-Limit-d": "Pixel"},
)
assert resp.status == 200
@@ -95,12 +88,9 @@ def test_handle_valid_minimal_message(mock_client):
def test_handle_value_error(mock_client):
"""Test we don't disclose that this is a valid webhook."""
resp = yield from mock_client.post(
'/api/webhook/owntracks_test',
json='',
headers={
'X-Limit-u': 'Paulus',
'X-Limit-d': 'Pixel',
}
"/api/webhook/owntracks_test",
json="",
headers={"X-Limit-u": "Paulus", "X-Limit-d": "Pixel"},
)
assert resp.status == 200
@@ -113,47 +103,39 @@ def test_handle_value_error(mock_client):
def test_returns_error_missing_username(mock_client, caplog):
"""Test that an error is returned when username is missing."""
resp = yield from mock_client.post(
'/api/webhook/owntracks_test',
"/api/webhook/owntracks_test",
json=LOCATION_MESSAGE,
headers={
'X-Limit-d': 'Pixel',
}
headers={"X-Limit-d": "Pixel"},
)
# 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
assert "No topic or user found" in caplog.text
@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',
}
"/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
json = yield from resp.json()
assert json == []
assert 'invalid JSON' in caplog.text
assert "invalid JSON" in caplog.text
@asyncio.coroutine
def test_returns_error_missing_device(mock_client):
"""Test that an error is returned when device name is missing."""
resp = yield from mock_client.post(
'/api/webhook/owntracks_test',
"/api/webhook/owntracks_test",
json=LOCATION_MESSAGE,
headers={
'X-Limit-u': 'Paulus',
}
headers={"X-Limit-u": "Paulus"},
)
assert resp.status == 200
@@ -164,18 +146,16 @@ def test_returns_error_missing_device(mock_client):
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')
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'}
assert received[0] == {"hello": "world"}
assert received[1] == {"world": "hello"}
received.clear()