Add option to disable specific integrations (#16757)
* Add option to disable specific integrations * Lint
This commit is contained in:
1 parent
03de658d4d
commit
092c146eae
10 files changed
+222
-28
No files matched your search
@@ -1 +1,32 @@
|
||||
"""Tests for the cloud component."""
|
||||
from unittest.mock import patch
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components import cloud
|
||||
|
||||
from jose import jwt
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
|
||||
def mock_cloud(hass, config={}):
|
||||
"""Mock cloud."""
|
||||
with patch('homeassistant.components.cloud.Cloud.async_start',
|
||||
return_value=mock_coro()):
|
||||
assert hass.loop.run_until_complete(async_setup_component(
|
||||
hass, cloud.DOMAIN, {
|
||||
'cloud': config
|
||||
}))
|
||||
|
||||
hass.data[cloud.DOMAIN]._decode_claims = \
|
||||
lambda token: jwt.get_unverified_claims(token)
|
||||
|
||||
|
||||
def mock_cloud_prefs(hass, prefs={}):
|
||||
"""Fixture for cloud component."""
|
||||
prefs_to_set = {
|
||||
cloud.STORAGE_ENABLE_ALEXA: True,
|
||||
cloud.STORAGE_ENABLE_GOOGLE: True,
|
||||
}
|
||||
prefs_to_set.update(prefs)
|
||||
hass.data[cloud.DOMAIN]._prefs = prefs_to_set
|
||||
return prefs_to_set
|
||||
@@ -0,0 +1,11 @@
|
||||
"""Fixtures for cloud tests."""
|
||||
import pytest
|
||||
|
||||
from . import mock_cloud, mock_cloud_prefs
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_cloud_fixture(hass):
|
||||
"""Fixture for cloud component."""
|
||||
mock_cloud(hass)
|
||||
return mock_cloud_prefs(hass)
|
||||
@@ -5,11 +5,12 @@ from unittest.mock import patch, MagicMock
|
||||
import pytest
|
||||
from jose import jwt
|
||||
|
||||
from homeassistant.bootstrap import async_setup_component
|
||||
from homeassistant.components.cloud import DOMAIN, auth_api, iot
|
||||
from homeassistant.components.cloud import (
|
||||
DOMAIN, auth_api, iot, STORAGE_ENABLE_GOOGLE, STORAGE_ENABLE_ALEXA)
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
from . import mock_cloud, mock_cloud_prefs
|
||||
|
||||
GOOGLE_ACTIONS_SYNC_URL = 'https://api-test.hass.io/google_actions_sync'
|
||||
SUBSCRIPTION_INFO_URL = 'https://api-test.hass.io/subscription_info'
|
||||
@@ -25,22 +26,16 @@ def mock_auth():
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_api(hass):
|
||||
"""Initialize HTTP API."""
|
||||
with patch('homeassistant.components.cloud.Cloud.async_start',
|
||||
return_value=mock_coro()):
|
||||
assert hass.loop.run_until_complete(async_setup_component(
|
||||
hass, 'cloud', {
|
||||
'cloud': {
|
||||
'mode': 'development',
|
||||
'cognito_client_id': 'cognito_client_id',
|
||||
'user_pool_id': 'user_pool_id',
|
||||
'region': 'region',
|
||||
'relayer': 'relayer',
|
||||
'google_actions_sync_url': GOOGLE_ACTIONS_SYNC_URL,
|
||||
'subscription_info_url': SUBSCRIPTION_INFO_URL,
|
||||
}
|
||||
}))
|
||||
hass.data['cloud']._decode_claims = \
|
||||
lambda token: jwt.get_unverified_claims(token)
|
||||
mock_cloud(hass, {
|
||||
'mode': 'development',
|
||||
'cognito_client_id': 'cognito_client_id',
|
||||
'user_pool_id': 'user_pool_id',
|
||||
'region': 'region',
|
||||
'relayer': 'relayer',
|
||||
'google_actions_sync_url': GOOGLE_ACTIONS_SYNC_URL,
|
||||
'subscription_info_url': SUBSCRIPTION_INFO_URL,
|
||||
})
|
||||
return mock_cloud_prefs(hass)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -321,7 +316,7 @@ def test_resend_confirm_view_unknown_error(mock_cognito, cloud_client):
|
||||
assert req.status == 502
|
||||
|
||||
|
||||
async def test_websocket_status(hass, hass_ws_client):
|
||||
async def test_websocket_status(hass, hass_ws_client, mock_cloud_fixture):
|
||||
"""Test querying the status."""
|
||||
hass.data[DOMAIN].id_token = jwt.encode({
|
||||
'email': 'hello@home-assistant.io',
|
||||
@@ -338,6 +333,8 @@ async def test_websocket_status(hass, hass_ws_client):
|
||||
'logged_in': True,
|
||||
'email': 'hello@home-assistant.io',
|
||||
'cloud': 'connected',
|
||||
'alexa_enabled': True,
|
||||
'google_enabled': True,
|
||||
}
|
||||
|
||||
|
||||
@@ -407,3 +404,26 @@ async def test_websocket_subscription_not_logged_in(hass, hass_ws_client):
|
||||
|
||||
assert not response['success']
|
||||
assert response['error']['code'] == 'not_logged_in'
|
||||
|
||||
|
||||
async def test_websocket_update_preferences(hass, hass_ws_client,
|
||||
aioclient_mock, setup_api):
|
||||
"""Test updating preference."""
|
||||
assert setup_api[STORAGE_ENABLE_GOOGLE]
|
||||
assert setup_api[STORAGE_ENABLE_ALEXA]
|
||||
hass.data[DOMAIN].id_token = jwt.encode({
|
||||
'email': 'hello@home-assistant.io',
|
||||
'custom:sub-exp': '2018-01-03'
|
||||
}, 'test')
|
||||
client = await hass_ws_client(hass)
|
||||
await client.send_json({
|
||||
'id': 5,
|
||||
'type': 'cloud/update_prefs',
|
||||
'alexa_enabled': False,
|
||||
'google_enabled': False,
|
||||
})
|
||||
response = await client.receive_json()
|
||||
|
||||
assert response['success']
|
||||
assert not setup_api[STORAGE_ENABLE_GOOGLE]
|
||||
assert not setup_api[STORAGE_ENABLE_ALEXA]
|
||||
@@ -141,9 +141,9 @@ def test_write_user_info():
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_subscription_expired():
|
||||
def test_subscription_expired(hass):
|
||||
"""Test subscription being expired."""
|
||||
cl = cloud.Cloud(None, cloud.MODE_DEV, None, None)
|
||||
cl = cloud.Cloud(hass, cloud.MODE_DEV, None, None)
|
||||
token_val = {
|
||||
'custom:sub-exp': '2017-11-13'
|
||||
}
|
||||
@@ -154,9 +154,9 @@ def test_subscription_expired():
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_subscription_not_expired():
|
||||
def test_subscription_not_expired(hass):
|
||||
"""Test subscription not being expired."""
|
||||
cl = cloud.Cloud(None, cloud.MODE_DEV, None, None)
|
||||
cl = cloud.Cloud(hass, cloud.MODE_DEV, None, None)
|
||||
token_val = {
|
||||
'custom:sub-exp': '2017-11-13'
|
||||
}
|
||||
|
||||
@@ -6,10 +6,14 @@ from aiohttp import WSMsgType, client_exceptions
|
||||
import pytest
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components.cloud import Cloud, iot, auth_api, MODE_DEV
|
||||
from homeassistant.components.cloud import (
|
||||
Cloud, iot, auth_api, MODE_DEV, STORAGE_ENABLE_ALEXA,
|
||||
STORAGE_ENABLE_GOOGLE)
|
||||
from tests.components.alexa import test_smart_home as test_alexa
|
||||
from tests.common import mock_coro
|
||||
|
||||
from . import mock_cloud_prefs
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_client():
|
||||
@@ -284,6 +288,8 @@ def test_handler_alexa(hass):
|
||||
})
|
||||
assert setup
|
||||
|
||||
mock_cloud_prefs(hass)
|
||||
|
||||
resp = yield from iot.async_handle_alexa(
|
||||
hass, hass.data['cloud'],
|
||||
test_alexa.get_new_request('Alexa.Discovery', 'Discover'))
|
||||
@@ -299,6 +305,20 @@ def test_handler_alexa(hass):
|
||||
assert device['manufacturerName'] == 'Home Assistant'
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_handler_alexa_disabled(hass, mock_cloud_fixture):
|
||||
"""Test handler Alexa when user has disabled it."""
|
||||
mock_cloud_fixture[STORAGE_ENABLE_ALEXA] = False
|
||||
|
||||
resp = yield from iot.async_handle_alexa(
|
||||
hass, hass.data['cloud'],
|
||||
test_alexa.get_new_request('Alexa.Discovery', 'Discover'))
|
||||
|
||||
assert resp['event']['header']['namespace'] == 'Alexa'
|
||||
assert resp['event']['header']['name'] == 'ErrorResponse'
|
||||
assert resp['event']['payload']['type'] == 'BRIDGE_UNREACHABLE'
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_handler_google_actions(hass):
|
||||
"""Test handler Google Actions."""
|
||||
@@ -327,6 +347,8 @@ def test_handler_google_actions(hass):
|
||||
})
|
||||
assert setup
|
||||
|
||||
mock_cloud_prefs(hass)
|
||||
|
||||
reqid = '5711642932632160983'
|
||||
data = {'requestId': reqid, 'inputs': [{'intent': 'action.devices.SYNC'}]}
|
||||
|
||||
@@ -351,6 +373,24 @@ def test_handler_google_actions(hass):
|
||||
assert device['roomHint'] == 'living room'
|
||||
|
||||
|
||||
async def test_handler_google_actions_disabled(hass, mock_cloud_fixture):
|
||||
"""Test handler Google Actions when user has disabled it."""
|
||||
mock_cloud_fixture[STORAGE_ENABLE_GOOGLE] = False
|
||||
|
||||
with patch('homeassistant.components.cloud.Cloud.async_start',
|
||||
return_value=mock_coro()):
|
||||
assert await async_setup_component(hass, 'cloud', {})
|
||||
|
||||
reqid = '5711642932632160983'
|
||||
data = {'requestId': reqid, 'inputs': [{'intent': 'action.devices.SYNC'}]}
|
||||
|
||||
resp = await iot.async_handle_google_actions(
|
||||
hass, hass.data['cloud'], data)
|
||||
|
||||
assert resp['requestId'] == reqid
|
||||
assert resp['payload']['errorCode'] == 'deviceTurnedOff'
|
||||
|
||||
|
||||
async def test_refresh_token_expired(hass):
|
||||
"""Test handling Unauthenticated error raised if refresh token expired."""
|
||||
cloud = Cloud(hass, MODE_DEV, None, None)
|
||||
|
||||
Reference in new issue
Block a user