If registration supports encryption then return encrypted payloads (#21853)

This commit is contained in:
Robbie Trencheny authored and Charles Garwood committed 2019-03-11 08:34:58 -04:00
1 parent c401f35a43
commit 785fd273e3
7 files changed
+91 -24

No files matched your search

+6
View File
@@ -26,6 +26,12 @@ def webhook_client(hass, aiohttp_client, hass_storage, hass_admin_user):
CONF_WEBHOOK_ID: 'mobile_app_test',
'device_name': 'Test Device',
CONF_USER_ID: hass_admin_user.id,
},
'mobile_app_test_cleartext': {
'supports_encryption': False,
CONF_WEBHOOK_ID: 'mobile_app_test_cleartext',
'device_name': 'Test Device (Cleartext)',
CONF_USER_ID: hass_admin_user.id,
}
},
DATA_DELETED_IDS: [],
+12
View File
@@ -32,6 +32,18 @@ REGISTER = {
'supports_encryption': True
}
REGISTER_CLEARTEXT = {
'app_data': {'foo': 'bar'},
'app_id': 'io.homeassistant.mobile_app_test',
'app_name': 'Mobile App Tests',
'app_version': '1.0.0',
'device_name': 'Test 1',
'manufacturer': 'mobile_app',
'model': 'Test',
'os_version': '1.0',
'supports_encryption': False
}
RENDER_TEMPLATE = {
'type': 'render_template',
'data': {
+7 -1
View File
@@ -56,4 +56,10 @@ async def test_registration(hass_client, authed_api_client): # noqa: F811
assert resp.status == 200
webhook_json = await resp.json()
assert webhook_json == {'rendered': 'Hello world'}
assert 'encrypted_data' in webhook_json
decrypted_data = SecretBox(key).decrypt(webhook_json['encrypted_data'],
encoder=Base64Encoder)
decrypted_data = decrypted_data.decode("utf-8")
assert json.loads(decrypted_data) == {'rendered': 'Hello world'}
+15 -9
View File
@@ -10,14 +10,14 @@ from tests.common import async_mock_service
from . import authed_api_client, webhook_client # noqa: F401
from .const import (CALL_SERVICE, FIRE_EVENT, REGISTER, RENDER_TEMPLATE,
UPDATE)
from .const import (CALL_SERVICE, FIRE_EVENT, REGISTER_CLEARTEXT,
RENDER_TEMPLATE, UPDATE)
async def test_webhook_handle_render_template(webhook_client): # noqa: F811
"""Test that we render templates properly."""
resp = await webhook_client.post(
'/api/webhook/mobile_app_test',
'/api/webhook/mobile_app_test_cleartext',
json=RENDER_TEMPLATE
)
@@ -32,7 +32,7 @@ async def test_webhook_handle_call_services(hass, webhook_client): # noqa: E501
calls = async_mock_service(hass, 'test', 'mobile_app')
resp = await webhook_client.post(
'/api/webhook/mobile_app_test',
'/api/webhook/mobile_app_test_cleartext',
json=CALL_SERVICE
)
@@ -53,7 +53,7 @@ async def test_webhook_handle_fire_event(hass, webhook_client): # noqa: F811
hass.bus.async_listen('test_event', store_event)
resp = await webhook_client.post(
'/api/webhook/mobile_app_test',
'/api/webhook/mobile_app_test_cleartext',
json=FIRE_EVENT
)
@@ -69,7 +69,7 @@ async def test_webhook_update_registration(webhook_client, hass_client): # noqa
"""Test that a we can update an existing registration via webhook."""
authed_api_client = await hass_client() # noqa: F811
register_resp = await authed_api_client.post(
'/api/mobile_app/registrations', json=REGISTER
'/api/mobile_app/registrations', json=REGISTER_CLEARTEXT
)
assert register_resp.status == 201
@@ -96,7 +96,7 @@ async def test_webhook_update_registration(webhook_client, hass_client): # noqa
async def test_webhook_returns_error_incorrect_json(webhook_client, caplog): # noqa: E501 F811
"""Test that an error is returned when JSON is invalid."""
resp = await webhook_client.post(
'/api/webhook/mobile_app_test',
'/api/webhook/mobile_app_test_cleartext',
data='not json'
)
@@ -141,5 +141,11 @@ async def test_webhook_handle_decryption(webhook_client): # noqa: F811
assert resp.status == 200
json = await resp.json()
assert json == {'rendered': 'Hello world'}
webhook_json = await resp.json()
assert 'encrypted_data' in webhook_json
decrypted_data = SecretBox(key).decrypt(webhook_json['encrypted_data'],
encoder=Base64Encoder)
decrypted_data = decrypted_data.decode("utf-8")
assert json.loads(decrypted_data) == {'rendered': 'Hello world'}