Drop OwnTracks bad packets (#19161)
This commit is contained in:
parent
fe2d24c240
commit
fdea9cb426
2 changed files with 38 additions and 11 deletions
|
@ -118,9 +118,18 @@ async def async_connect_mqtt(hass, component):
|
||||||
|
|
||||||
|
|
||||||
async def handle_webhook(hass, webhook_id, request):
|
async def handle_webhook(hass, webhook_id, request):
|
||||||
"""Handle webhook callback."""
|
"""Handle webhook callback.
|
||||||
|
|
||||||
|
iOS sets the "topic" as part of the payload.
|
||||||
|
Android does not set a topic but adds headers to the request.
|
||||||
|
"""
|
||||||
context = hass.data[DOMAIN]['context']
|
context = hass.data[DOMAIN]['context']
|
||||||
message = await request.json()
|
|
||||||
|
try:
|
||||||
|
message = await request.json()
|
||||||
|
except ValueError:
|
||||||
|
_LOGGER.warning('Received invalid JSON from OwnTracks')
|
||||||
|
return json_response([])
|
||||||
|
|
||||||
# Android doesn't populate topic
|
# Android doesn't populate topic
|
||||||
if 'topic' not in message:
|
if 'topic' not in message:
|
||||||
|
@ -129,11 +138,10 @@ async def handle_webhook(hass, webhook_id, request):
|
||||||
device = headers.get('X-Limit-D', user)
|
device = headers.get('X-Limit-D', user)
|
||||||
|
|
||||||
if user is None:
|
if user is None:
|
||||||
_LOGGER.warning('Set a username in Connection -> Identification')
|
_LOGGER.warning('No topic or user found in message. If on Android,'
|
||||||
return json_response(
|
' set a username in Connection -> Identification')
|
||||||
{'error': 'You need to supply username.'},
|
# Keep it as a 200 response so the incorrect packet is discarded
|
||||||
status=400
|
return json_response([])
|
||||||
)
|
|
||||||
|
|
||||||
topic_base = re.sub('/#$', '', context.mqtt_topic)
|
topic_base = re.sub('/#$', '', context.mqtt_topic)
|
||||||
message['topic'] = '{}/{}/{}'.format(topic_base, user, device)
|
message['topic'] = '{}/{}/{}'.format(topic_base, user, device)
|
||||||
|
|
|
@ -110,7 +110,7 @@ def test_handle_value_error(mock_client):
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_returns_error_missing_username(mock_client):
|
def test_returns_error_missing_username(mock_client, caplog):
|
||||||
"""Test that an error is returned when username is missing."""
|
"""Test that an error is returned when username is missing."""
|
||||||
resp = yield from mock_client.post(
|
resp = yield from mock_client.post(
|
||||||
'/api/webhook/owntracks_test',
|
'/api/webhook/owntracks_test',
|
||||||
|
@ -120,10 +120,29 @@ def test_returns_error_missing_username(mock_client):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
assert resp.status == 400
|
# Needs to be 200 or OwnTracks keeps retrying bad packet.
|
||||||
|
assert resp.status == 200
|
||||||
json = yield from resp.json()
|
json = yield from resp.json()
|
||||||
assert json == {'error': 'You need to supply username.'}
|
assert json == []
|
||||||
|
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',
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue