2019-11-28 13:23:59 +00:00
|
|
|
"""Test Cloud preferences."""
|
2023-02-13 13:22:49 +00:00
|
|
|
from typing import Any
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2024-02-12 18:24:21 +00:00
|
|
|
import pytest
|
|
|
|
|
2019-11-28 13:23:59 +00:00
|
|
|
from homeassistant.auth.const import GROUP_ID_ADMIN
|
2019-12-08 17:01:12 +00:00
|
|
|
from homeassistant.components.cloud.prefs import STORAGE_KEY, CloudPreferences
|
2023-02-08 17:08:43 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-11-28 13:23:59 +00:00
|
|
|
|
|
|
|
|
2023-02-08 17:08:43 +00:00
|
|
|
async def test_set_username(hass: HomeAssistant) -> None:
|
2019-11-28 13:23:59 +00:00
|
|
|
"""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
|
|
|
|
|
|
|
|
|
2023-02-08 17:08:43 +00:00
|
|
|
async def test_set_username_migration(hass: HomeAssistant) -> None:
|
2019-11-28 13:23:59 +00:00
|
|
|
"""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
|
|
|
|
|
|
|
|
|
2023-02-13 13:22:49 +00:00
|
|
|
async def test_set_new_username(
|
|
|
|
hass: HomeAssistant, hass_storage: dict[str, Any]
|
|
|
|
) -> None:
|
2021-03-29 17:26:51 +00:00
|
|
|
"""Test if setting new username returns true."""
|
|
|
|
hass_storage[STORAGE_KEY] = {"version": 1, "data": {"username": "old-user"}}
|
|
|
|
|
|
|
|
prefs = CloudPreferences(hass)
|
|
|
|
await prefs.async_initialize()
|
|
|
|
|
|
|
|
assert not await prefs.async_set_username("old-user")
|
|
|
|
|
|
|
|
assert await prefs.async_set_username("new-user")
|
|
|
|
|
|
|
|
|
2023-02-13 13:22:49 +00:00
|
|
|
async def test_load_invalid_cloud_user(
|
|
|
|
hass: HomeAssistant, hass_storage: dict[str, Any]
|
|
|
|
) -> None:
|
2019-11-28 13:23:59 +00:00
|
|
|
"""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
|
|
|
|
|
|
|
|
|
2023-02-13 13:22:49 +00:00
|
|
|
async def test_setup_remove_cloud_user(
|
|
|
|
hass: HomeAssistant, hass_storage: dict[str, Any]
|
|
|
|
) -> None:
|
2019-11-28 13:23:59 +00:00
|
|
|
"""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
|
2024-02-12 18:24:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("google_assistant_users", "google_connected"),
|
|
|
|
[([], False), (["cloud-user"], True), (["other-user"], False)],
|
|
|
|
)
|
|
|
|
async def test_import_google_assistant_settings(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
hass_storage: dict[str, Any],
|
|
|
|
google_assistant_users: list[str],
|
|
|
|
google_connected: bool,
|
|
|
|
) -> None:
|
|
|
|
"""Test importing from the google assistant store."""
|
|
|
|
hass_storage[STORAGE_KEY] = {"version": 1, "data": {"username": "cloud-user"}}
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.cloud.prefs.async_get_google_assistant_users"
|
|
|
|
) as mock_get_users:
|
|
|
|
mock_get_users.return_value = google_assistant_users
|
|
|
|
prefs = CloudPreferences(hass)
|
|
|
|
await prefs.async_initialize()
|
|
|
|
assert prefs.google_connected == google_connected
|