Only create cloud user if cloud in use (#29150)
* Only create cloud user if cloud in use * Pass context to alexa * Update requirements * Fix handing & design pattern for 0.30 * fix tests * Fix lint & tests * rename internal user
This commit is contained in:
parent
5d5d053bce
commit
b847d55077
15 changed files with 221 additions and 145 deletions
|
@ -7,6 +7,7 @@ import pytest
|
|||
from homeassistant.core import State
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components.cloud import DOMAIN
|
||||
from homeassistant.components.cloud.client import CloudClient
|
||||
from homeassistant.components.cloud.const import PREF_ENABLE_ALEXA, PREF_ENABLE_GOOGLE
|
||||
from tests.components.alexa import test_smart_home as test_alexa
|
||||
from tests.common import mock_coro
|
||||
|
@ -187,25 +188,42 @@ async def test_google_config_expose_entity(hass, mock_cloud_setup, mock_cloud_lo
|
|||
"""Test Google config exposing entity method uses latest config."""
|
||||
cloud_client = hass.data[DOMAIN].client
|
||||
state = State("light.kitchen", "on")
|
||||
gconf = await cloud_client.get_google_config()
|
||||
|
||||
assert cloud_client.google_config.should_expose(state)
|
||||
assert gconf.should_expose(state)
|
||||
|
||||
await cloud_client.prefs.async_update_google_entity_config(
|
||||
entity_id="light.kitchen", should_expose=False
|
||||
)
|
||||
|
||||
assert not cloud_client.google_config.should_expose(state)
|
||||
assert not gconf.should_expose(state)
|
||||
|
||||
|
||||
async def test_google_config_should_2fa(hass, mock_cloud_setup, mock_cloud_login):
|
||||
"""Test Google config disabling 2FA method uses latest config."""
|
||||
cloud_client = hass.data[DOMAIN].client
|
||||
gconf = await cloud_client.get_google_config()
|
||||
state = State("light.kitchen", "on")
|
||||
|
||||
assert cloud_client.google_config.should_2fa(state)
|
||||
assert gconf.should_2fa(state)
|
||||
|
||||
await cloud_client.prefs.async_update_google_entity_config(
|
||||
entity_id="light.kitchen", disable_2fa=True
|
||||
)
|
||||
|
||||
assert not cloud_client.google_config.should_2fa(state)
|
||||
assert not gconf.should_2fa(state)
|
||||
|
||||
|
||||
async def test_set_username(hass):
|
||||
"""Test we set username during loggin."""
|
||||
prefs = MagicMock(
|
||||
alexa_enabled=False,
|
||||
google_enabled=False,
|
||||
async_set_username=MagicMock(return_value=mock_coro()),
|
||||
)
|
||||
client = CloudClient(hass, prefs, None, {}, {})
|
||||
client.cloud = MagicMock(is_logged_in=True, username="mock-username")
|
||||
await client.logged_in()
|
||||
|
||||
assert len(prefs.async_set_username.mock_calls) == 1
|
||||
assert prefs.async_set_username.mock_calls[0][1][0] == "mock-username"
|
||||
|
|
|
@ -15,6 +15,7 @@ async def test_google_update_report_state(hass, cloud_prefs):
|
|||
config = CloudGoogleConfig(
|
||||
hass,
|
||||
GACTIONS_SCHEMA({}),
|
||||
"mock-user-id",
|
||||
cloud_prefs,
|
||||
Mock(claims={"cognito:username": "abcdefghjkl"}),
|
||||
)
|
||||
|
@ -37,6 +38,7 @@ async def test_sync_entities(aioclient_mock, hass, cloud_prefs):
|
|||
config = CloudGoogleConfig(
|
||||
hass,
|
||||
GACTIONS_SCHEMA({}),
|
||||
"mock-user-id",
|
||||
cloud_prefs,
|
||||
Mock(
|
||||
google_actions_sync_url="http://example.com",
|
||||
|
@ -52,6 +54,7 @@ async def test_google_update_expose_trigger_sync(hass, cloud_prefs):
|
|||
config = CloudGoogleConfig(
|
||||
hass,
|
||||
GACTIONS_SCHEMA({}),
|
||||
"mock-user-id",
|
||||
cloud_prefs,
|
||||
Mock(claims={"cognito:username": "abcdefghjkl"}),
|
||||
)
|
||||
|
@ -90,7 +93,7 @@ async def test_google_update_expose_trigger_sync(hass, cloud_prefs):
|
|||
async def test_google_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
|
||||
"""Test Google config responds to entity registry."""
|
||||
config = CloudGoogleConfig(
|
||||
hass, GACTIONS_SCHEMA({}), cloud_prefs, hass.data["cloud"]
|
||||
hass, GACTIONS_SCHEMA({}), "mock-user-id", cloud_prefs, hass.data["cloud"]
|
||||
)
|
||||
|
||||
with patch.object(
|
||||
|
|
|
@ -103,32 +103,18 @@ async def test_google_actions_sync_fails(
|
|||
assert req.status == 403
|
||||
|
||||
|
||||
async def test_login_view(hass, cloud_client, mock_cognito):
|
||||
async def test_login_view(hass, cloud_client):
|
||||
"""Test logging in."""
|
||||
mock_cognito.id_token = jwt.encode(
|
||||
{"email": "hello@home-assistant.io", "custom:sub-exp": "2018-01-03"}, "test"
|
||||
)
|
||||
mock_cognito.access_token = "access_token"
|
||||
mock_cognito.refresh_token = "refresh_token"
|
||||
hass.data["cloud"] = MagicMock(login=MagicMock(return_value=mock_coro()))
|
||||
|
||||
with patch("hass_nabucasa.iot.CloudIoT.connect") as mock_connect, patch(
|
||||
"hass_nabucasa.auth.CognitoAuth._authenticate", return_value=mock_cognito
|
||||
) as mock_auth:
|
||||
req = await cloud_client.post(
|
||||
"/api/cloud/login", json={"email": "my_username", "password": "my_password"}
|
||||
)
|
||||
req = await cloud_client.post(
|
||||
"/api/cloud/login", json={"email": "my_username", "password": "my_password"}
|
||||
)
|
||||
|
||||
assert req.status == 200
|
||||
result = await req.json()
|
||||
assert result == {"success": True}
|
||||
|
||||
assert len(mock_connect.mock_calls) == 1
|
||||
|
||||
assert len(mock_auth.mock_calls) == 1
|
||||
result_user, result_pass = mock_auth.mock_calls[0][1]
|
||||
assert result_user == "my_username"
|
||||
assert result_pass == "my_password"
|
||||
|
||||
|
||||
async def test_login_view_random_exception(cloud_client):
|
||||
"""Try logging in with invalid JSON."""
|
||||
|
@ -351,7 +337,6 @@ async def test_websocket_status(
|
|||
"cloud": "connected",
|
||||
"prefs": {
|
||||
"alexa_enabled": True,
|
||||
"cloud_user": None,
|
||||
"cloudhooks": {},
|
||||
"google_enabled": True,
|
||||
"google_entity_configs": {},
|
||||
|
|
|
@ -5,7 +5,6 @@ import pytest
|
|||
|
||||
from homeassistant.core import Context
|
||||
from homeassistant.exceptions import Unauthorized
|
||||
from homeassistant.auth.const import GROUP_ID_ADMIN
|
||||
from homeassistant.components import cloud
|
||||
from homeassistant.components.cloud.const import DOMAIN
|
||||
from homeassistant.components.cloud.prefs import STORAGE_KEY
|
||||
|
@ -142,68 +141,11 @@ async def test_setup_existing_cloud_user(hass, hass_storage):
|
|||
assert hass_storage[STORAGE_KEY]["data"]["cloud_user"] == user.id
|
||||
|
||||
|
||||
async def test_setup_invalid_cloud_user(hass, hass_storage):
|
||||
"""Test setup with API push default data."""
|
||||
hass_storage[STORAGE_KEY] = {"version": 1, "data": {"cloud_user": "non-existing"}}
|
||||
with patch("hass_nabucasa.Cloud.start", return_value=mock_coro()):
|
||||
result = await async_setup_component(
|
||||
hass,
|
||||
"cloud",
|
||||
{
|
||||
"http": {},
|
||||
"cloud": {
|
||||
cloud.CONF_MODE: cloud.MODE_DEV,
|
||||
"cognito_client_id": "test-cognito_client_id",
|
||||
"user_pool_id": "test-user_pool_id",
|
||||
"region": "test-region",
|
||||
"relayer": "test-relayer",
|
||||
},
|
||||
},
|
||||
)
|
||||
assert result
|
||||
|
||||
assert hass_storage[STORAGE_KEY]["data"]["cloud_user"] != "non-existing"
|
||||
cloud_user = await hass.auth.async_get_user(
|
||||
hass_storage[STORAGE_KEY]["data"]["cloud_user"]
|
||||
)
|
||||
|
||||
assert cloud_user
|
||||
assert cloud_user.groups[0].id == GROUP_ID_ADMIN
|
||||
|
||||
|
||||
async def test_setup_setup_cloud_user(hass, hass_storage):
|
||||
"""Test setup with API push default data."""
|
||||
hass_storage[STORAGE_KEY] = {"version": 1, "data": {"cloud_user": None}}
|
||||
with patch("hass_nabucasa.Cloud.start", return_value=mock_coro()):
|
||||
result = await async_setup_component(
|
||||
hass,
|
||||
"cloud",
|
||||
{
|
||||
"http": {},
|
||||
"cloud": {
|
||||
cloud.CONF_MODE: cloud.MODE_DEV,
|
||||
"cognito_client_id": "test-cognito_client_id",
|
||||
"user_pool_id": "test-user_pool_id",
|
||||
"region": "test-region",
|
||||
"relayer": "test-relayer",
|
||||
},
|
||||
},
|
||||
)
|
||||
assert result
|
||||
|
||||
cloud_user = await hass.auth.async_get_user(
|
||||
hass_storage[STORAGE_KEY]["data"]["cloud_user"]
|
||||
)
|
||||
|
||||
assert cloud_user
|
||||
assert cloud_user.groups[0].id == GROUP_ID_ADMIN
|
||||
|
||||
|
||||
async def test_on_connect(hass, mock_cloud_fixture):
|
||||
"""Test cloud on connect triggers."""
|
||||
cl = hass.data["cloud"]
|
||||
|
||||
assert len(cl.iot._on_connect) == 4
|
||||
assert len(cl.iot._on_connect) == 3
|
||||
|
||||
assert len(hass.states.async_entity_ids("binary_sensor")) == 0
|
||||
|
||||
|
|
80
tests/components/cloud/test_prefs.py
Normal file
80
tests/components/cloud/test_prefs.py
Normal file
|
@ -0,0 +1,80 @@
|
|||
"""Test Cloud preferences."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.auth.const import GROUP_ID_ADMIN
|
||||
from homeassistant.components.cloud.prefs import CloudPreferences, STORAGE_KEY
|
||||
|
||||
|
||||
async def test_set_username(hass):
|
||||
"""Test we clear config if we set different username."""
|
||||
prefs = CloudPreferences(hass)
|
||||
await prefs.async_initialize()
|
||||
|
||||
assert prefs.google_enabled
|
||||
|
||||
await prefs.async_update(google_enabled=False)
|
||||
|
||||
assert not prefs.google_enabled
|
||||
|
||||
await prefs.async_set_username("new-username")
|
||||
|
||||
assert prefs.google_enabled
|
||||
|
||||
|
||||
async def test_set_username_migration(hass):
|
||||
"""Test we not clear config if we had no username."""
|
||||
prefs = CloudPreferences(hass)
|
||||
|
||||
with patch.object(prefs, "_empty_config", return_value=prefs._empty_config(None)):
|
||||
await prefs.async_initialize()
|
||||
|
||||
assert prefs.google_enabled
|
||||
|
||||
await prefs.async_update(google_enabled=False)
|
||||
|
||||
assert not prefs.google_enabled
|
||||
|
||||
await prefs.async_set_username("new-username")
|
||||
|
||||
assert not prefs.google_enabled
|
||||
|
||||
|
||||
async def test_load_invalid_cloud_user(hass, hass_storage):
|
||||
"""Test loading cloud user with invalid storage."""
|
||||
hass_storage[STORAGE_KEY] = {"version": 1, "data": {"cloud_user": "non-existing"}}
|
||||
|
||||
prefs = CloudPreferences(hass)
|
||||
await prefs.async_initialize()
|
||||
|
||||
cloud_user_id = await prefs.get_cloud_user()
|
||||
|
||||
assert cloud_user_id != "non-existing"
|
||||
|
||||
cloud_user = await hass.auth.async_get_user(
|
||||
hass_storage[STORAGE_KEY]["data"]["cloud_user"]
|
||||
)
|
||||
|
||||
assert cloud_user
|
||||
assert cloud_user.groups[0].id == GROUP_ID_ADMIN
|
||||
|
||||
|
||||
async def test_setup_remove_cloud_user(hass, hass_storage):
|
||||
"""Test creating and removing cloud user."""
|
||||
hass_storage[STORAGE_KEY] = {"version": 1, "data": {"cloud_user": None}}
|
||||
|
||||
prefs = CloudPreferences(hass)
|
||||
await prefs.async_initialize()
|
||||
await prefs.async_set_username("user1")
|
||||
|
||||
cloud_user = await hass.auth.async_get_user(await prefs.get_cloud_user())
|
||||
|
||||
assert cloud_user
|
||||
assert cloud_user.groups[0].id == GROUP_ID_ADMIN
|
||||
|
||||
await prefs.async_set_username("user2")
|
||||
|
||||
cloud_user2 = await hass.auth.async_get_user(await prefs.get_cloud_user())
|
||||
|
||||
assert cloud_user2
|
||||
assert cloud_user2.groups[0].id == GROUP_ID_ADMIN
|
||||
assert cloud_user2.id != cloud_user.id
|
Loading…
Add table
Add a link
Reference in a new issue