2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for Home Assistant frontend."""
|
2020-08-05 15:42:23 +00:00
|
|
|
from datetime import timedelta
|
2015-01-30 07:56:04 +00:00
|
|
|
import re
|
|
|
|
|
2017-03-30 07:50:53 +00:00
|
|
|
import pytest
|
2015-01-30 07:56:04 +00:00
|
|
|
|
2017-08-27 16:07:58 +00:00
|
|
|
from homeassistant.components.frontend import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_EXTRA_HTML_URL,
|
|
|
|
CONF_EXTRA_HTML_URL_ES5,
|
2019-12-09 13:14:40 +00:00
|
|
|
CONF_JS_VERSION,
|
|
|
|
CONF_THEMES,
|
|
|
|
DOMAIN,
|
2019-07-31 19:25:30 +00:00
|
|
|
EVENT_PANELS_UPDATED,
|
2020-08-05 15:42:23 +00:00
|
|
|
THEMES_STORAGE_KEY,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-01 09:21:00 +00:00
|
|
|
from homeassistant.components.websocket_api.const import TYPE_RESULT
|
2020-04-08 22:57:47 +00:00
|
|
|
from homeassistant.const import HTTP_NOT_FOUND
|
2020-04-26 00:30:15 +00:00
|
|
|
from homeassistant.loader import async_get_integration
|
2019-12-09 13:14:40 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2020-08-05 15:42:23 +00:00
|
|
|
from homeassistant.util import dt
|
2015-01-30 07:56:04 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
from tests.async_mock import patch
|
2020-08-05 15:42:23 +00:00
|
|
|
from tests.common import async_capture_events, async_fire_time_changed
|
|
|
|
|
|
|
|
CONFIG_THEMES = {
|
|
|
|
DOMAIN: {
|
|
|
|
CONF_THEMES: {
|
|
|
|
"happy": {"primary-color": "red"},
|
|
|
|
"dark": {"primary-color": "black"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-06 08:12:43 +00:00
|
|
|
|
2015-01-30 07:56:04 +00:00
|
|
|
|
2017-03-30 07:50:53 +00:00
|
|
|
@pytest.fixture
|
2018-03-15 20:49:49 +00:00
|
|
|
def mock_http_client(hass, aiohttp_client):
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Start the Home Assistant HTTP component."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.loop.run_until_complete(async_setup_component(hass, "frontend", {}))
|
2018-03-15 20:49:49 +00:00
|
|
|
return hass.loop.run_until_complete(aiohttp_client(hass.http.app))
|
2015-01-30 07:56:04 +00:00
|
|
|
|
|
|
|
|
2017-07-13 01:08:13 +00:00
|
|
|
@pytest.fixture
|
2018-03-15 20:49:49 +00:00
|
|
|
def mock_http_client_with_themes(hass, aiohttp_client):
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Start the Home Assistant HTTP component."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.loop.run_until_complete(
|
|
|
|
async_setup_component(
|
|
|
|
hass,
|
|
|
|
"frontend",
|
|
|
|
{DOMAIN: {CONF_THEMES: {"happy": {"primary-color": "red"}}}},
|
|
|
|
)
|
|
|
|
)
|
2018-03-15 20:49:49 +00:00
|
|
|
return hass.loop.run_until_complete(aiohttp_client(hass.http.app))
|
2017-07-13 01:08:13 +00:00
|
|
|
|
|
|
|
|
2017-08-27 16:07:58 +00:00
|
|
|
@pytest.fixture
|
2018-03-15 20:49:49 +00:00
|
|
|
def mock_http_client_with_urls(hass, aiohttp_client):
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Start the Home Assistant HTTP component."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.loop.run_until_complete(
|
|
|
|
async_setup_component(
|
|
|
|
hass,
|
|
|
|
"frontend",
|
|
|
|
{
|
|
|
|
DOMAIN: {
|
|
|
|
CONF_JS_VERSION: "auto",
|
|
|
|
CONF_EXTRA_HTML_URL: ["https://domain.com/my_extra_url.html"],
|
|
|
|
CONF_EXTRA_HTML_URL_ES5: [
|
|
|
|
"https://domain.com/my_extra_url_es5.html"
|
|
|
|
],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
2018-03-15 20:49:49 +00:00
|
|
|
return hass.loop.run_until_complete(aiohttp_client(hass.http.app))
|
2017-08-27 16:07:58 +00:00
|
|
|
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_onboarded():
|
|
|
|
"""Mock that we're onboarded."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.onboarding.async_is_onboarded", return_value=True
|
|
|
|
):
|
2018-12-02 15:32:53 +00:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
2020-01-02 20:23:56 +00:00
|
|
|
async def test_frontend_and_static(mock_http_client, mock_onboarded):
|
2017-03-30 07:50:53 +00:00
|
|
|
"""Test if we can get the frontend."""
|
2020-01-02 20:23:56 +00:00
|
|
|
resp = await mock_http_client.get("")
|
2017-03-30 07:50:53 +00:00
|
|
|
assert resp.status == 200
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "cache-control" not in resp.headers
|
2015-01-30 07:56:04 +00:00
|
|
|
|
2020-01-02 20:23:56 +00:00
|
|
|
text = await resp.text()
|
2015-01-30 07:56:04 +00:00
|
|
|
|
2017-03-30 07:50:53 +00:00
|
|
|
# Test we can retrieve frontend.js
|
2019-07-31 19:25:30 +00:00
|
|
|
frontendjs = re.search(r"(?P<app>\/frontend_es5\/app.[A-Za-z0-9]{8}.js)", text)
|
2015-01-30 07:56:04 +00:00
|
|
|
|
2019-04-11 08:26:36 +00:00
|
|
|
assert frontendjs is not None, text
|
2020-01-02 20:23:56 +00:00
|
|
|
resp = await mock_http_client.get(frontendjs.groups(0)[0])
|
2017-03-30 07:50:53 +00:00
|
|
|
assert resp.status == 200
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "public" in resp.headers.get("cache-control")
|
2015-01-30 07:56:04 +00:00
|
|
|
|
|
|
|
|
2020-01-02 20:23:56 +00:00
|
|
|
async def test_dont_cache_service_worker(mock_http_client):
|
2017-03-30 07:50:53 +00:00
|
|
|
"""Test that we don't cache the service worker."""
|
2020-01-02 20:23:56 +00:00
|
|
|
resp = await mock_http_client.get("/service_worker.js")
|
2017-03-30 07:50:53 +00:00
|
|
|
assert resp.status == 200
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "cache-control" not in resp.headers
|
2015-01-30 07:56:04 +00:00
|
|
|
|
|
|
|
|
2020-01-02 20:23:56 +00:00
|
|
|
async def test_404(mock_http_client):
|
2017-03-30 07:50:53 +00:00
|
|
|
"""Test for HTTP 404 error."""
|
2020-01-02 20:23:56 +00:00
|
|
|
resp = await mock_http_client.get("/not-existing")
|
2020-04-08 22:57:47 +00:00
|
|
|
assert resp.status == HTTP_NOT_FOUND
|
2016-05-14 07:58:36 +00:00
|
|
|
|
2015-01-30 07:56:04 +00:00
|
|
|
|
2020-01-02 20:23:56 +00:00
|
|
|
async def test_we_cannot_POST_to_root(mock_http_client):
|
2017-03-30 07:50:53 +00:00
|
|
|
"""Test that POST is not allow to root."""
|
2020-01-02 20:23:56 +00:00
|
|
|
resp = await mock_http_client.post("/")
|
2017-03-30 07:50:53 +00:00
|
|
|
assert resp.status == 405
|
2015-01-30 07:56:04 +00:00
|
|
|
|
|
|
|
|
2018-06-06 08:12:43 +00:00
|
|
|
async def test_themes_api(hass, hass_ws_client):
|
2017-07-13 01:08:13 +00:00
|
|
|
"""Test that /api/themes returns correct data."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, "frontend", CONFIG_THEMES)
|
2018-06-06 08:12:43 +00:00
|
|
|
client = await hass_ws_client(hass)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await client.send_json({"id": 5, "type": "frontend/get_themes"})
|
2018-06-06 08:12:43 +00:00
|
|
|
msg = await client.receive_json()
|
2017-07-13 01:08:13 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["result"]["default_theme"] == "default"
|
2020-08-05 15:42:23 +00:00
|
|
|
assert msg["result"]["default_dark_theme"] is None
|
|
|
|
assert msg["result"]["themes"] == {
|
|
|
|
"happy": {"primary-color": "red"},
|
|
|
|
"dark": {"primary-color": "black"},
|
|
|
|
}
|
2017-07-13 01:08:13 +00:00
|
|
|
|
2020-02-18 19:52:38 +00:00
|
|
|
# safe mode
|
|
|
|
hass.config.safe_mode = True
|
|
|
|
await client.send_json({"id": 6, "type": "frontend/get_themes"})
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
|
|
|
assert msg["result"]["default_theme"] == "safe_mode"
|
|
|
|
assert msg["result"]["themes"] == {
|
2020-09-23 16:57:35 +00:00
|
|
|
"safe_mode": {"primary-color": "#db4437", "accent-color": "#ffca28"}
|
2020-02-18 19:52:38 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 08:12:43 +00:00
|
|
|
|
2020-08-05 15:42:23 +00:00
|
|
|
async def test_themes_persist(hass, hass_ws_client, hass_storage):
|
|
|
|
"""Test that theme settings are restores after restart."""
|
|
|
|
|
|
|
|
hass_storage[THEMES_STORAGE_KEY] = {
|
|
|
|
"key": THEMES_STORAGE_KEY,
|
|
|
|
"version": 1,
|
|
|
|
"data": {
|
|
|
|
"frontend_default_theme": "happy",
|
|
|
|
"frontend_default_dark_theme": "dark",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert await async_setup_component(hass, "frontend", CONFIG_THEMES)
|
|
|
|
client = await hass_ws_client(hass)
|
|
|
|
|
|
|
|
await client.send_json({"id": 5, "type": "frontend/get_themes"})
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
|
|
|
assert msg["result"]["default_theme"] == "happy"
|
|
|
|
assert msg["result"]["default_dark_theme"] == "dark"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_themes_save_storage(hass, hass_storage):
|
|
|
|
"""Test that theme settings are restores after restart."""
|
|
|
|
|
|
|
|
hass_storage[THEMES_STORAGE_KEY] = {
|
|
|
|
"key": THEMES_STORAGE_KEY,
|
|
|
|
"version": 1,
|
|
|
|
"data": {},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert await async_setup_component(hass, "frontend", CONFIG_THEMES)
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, "set_theme", {"name": "happy"}, blocking=True
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, "set_theme", {"name": "dark", "mode": "dark"}, blocking=True
|
|
|
|
)
|
|
|
|
|
|
|
|
# To trigger the call_later
|
|
|
|
async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=60))
|
|
|
|
# To execute the save
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass_storage[THEMES_STORAGE_KEY]["data"] == {
|
|
|
|
"frontend_default_theme": "happy",
|
|
|
|
"frontend_default_dark_theme": "dark",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-06 08:12:43 +00:00
|
|
|
async def test_themes_set_theme(hass, hass_ws_client):
|
2017-07-13 01:08:13 +00:00
|
|
|
"""Test frontend.set_theme service."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, "frontend", CONFIG_THEMES)
|
2018-06-06 08:12:43 +00:00
|
|
|
client = await hass_ws_client(hass)
|
2017-07-13 01:08:13 +00:00
|
|
|
|
2018-06-06 08:12:43 +00:00
|
|
|
await hass.services.async_call(
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN, "set_theme", {"name": "happy"}, blocking=True
|
|
|
|
)
|
2017-07-13 01:08:13 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await client.send_json({"id": 5, "type": "frontend/get_themes"})
|
2018-06-06 08:12:43 +00:00
|
|
|
msg = await client.receive_json()
|
2017-07-13 01:08:13 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["result"]["default_theme"] == "happy"
|
2018-06-06 08:12:43 +00:00
|
|
|
|
|
|
|
await hass.services.async_call(
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN, "set_theme", {"name": "default"}, blocking=True
|
|
|
|
)
|
2018-06-06 08:12:43 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await client.send_json({"id": 6, "type": "frontend/get_themes"})
|
2018-06-06 08:12:43 +00:00
|
|
|
msg = await client.receive_json()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["result"]["default_theme"] == "default"
|
2018-06-06 08:12:43 +00:00
|
|
|
|
2020-08-05 15:42:23 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, "set_theme", {"name": "happy"}, blocking=True
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.services.async_call(DOMAIN, "set_theme", {"name": "none"}, blocking=True)
|
|
|
|
|
|
|
|
await client.send_json({"id": 7, "type": "frontend/get_themes"})
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
|
|
|
assert msg["result"]["default_theme"] == "default"
|
|
|
|
|
2018-06-06 08:12:43 +00:00
|
|
|
|
|
|
|
async def test_themes_set_theme_wrong_name(hass, hass_ws_client):
|
2017-07-13 01:08:13 +00:00
|
|
|
"""Test frontend.set_theme service called with wrong name."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, "frontend", CONFIG_THEMES)
|
2018-06-06 08:12:43 +00:00
|
|
|
client = await hass_ws_client(hass)
|
2017-07-13 01:08:13 +00:00
|
|
|
|
2018-06-06 08:12:43 +00:00
|
|
|
await hass.services.async_call(
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN, "set_theme", {"name": "wrong"}, blocking=True
|
|
|
|
)
|
2017-07-13 01:08:13 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await client.send_json({"id": 5, "type": "frontend/get_themes"})
|
2018-06-06 08:12:43 +00:00
|
|
|
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["result"]["default_theme"] == "default"
|
2018-06-06 08:12:43 +00:00
|
|
|
|
|
|
|
|
2020-08-05 15:42:23 +00:00
|
|
|
async def test_themes_set_dark_theme(hass, hass_ws_client):
|
|
|
|
"""Test frontend.set_theme service called with dark mode."""
|
|
|
|
assert await async_setup_component(hass, "frontend", CONFIG_THEMES)
|
|
|
|
client = await hass_ws_client(hass)
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, "set_theme", {"name": "dark", "mode": "dark"}, blocking=True
|
|
|
|
)
|
|
|
|
|
|
|
|
await client.send_json({"id": 5, "type": "frontend/get_themes"})
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
|
|
|
assert msg["result"]["default_dark_theme"] == "dark"
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, "set_theme", {"name": "default", "mode": "dark"}, blocking=True
|
|
|
|
)
|
|
|
|
|
|
|
|
await client.send_json({"id": 6, "type": "frontend/get_themes"})
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
|
|
|
assert msg["result"]["default_dark_theme"] == "default"
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, "set_theme", {"name": "none", "mode": "dark"}, blocking=True
|
|
|
|
)
|
|
|
|
|
|
|
|
await client.send_json({"id": 7, "type": "frontend/get_themes"})
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
|
|
|
assert msg["result"]["default_dark_theme"] is None
|
|
|
|
|
|
|
|
|
|
|
|
async def test_themes_set_dark_theme_wrong_name(hass, hass_ws_client):
|
|
|
|
"""Test frontend.set_theme service called with mode dark and wrong name."""
|
|
|
|
assert await async_setup_component(hass, "frontend", CONFIG_THEMES)
|
|
|
|
client = await hass_ws_client(hass)
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, "set_theme", {"name": "wrong", "mode": "dark"}, blocking=True
|
|
|
|
)
|
|
|
|
|
|
|
|
await client.send_json({"id": 5, "type": "frontend/get_themes"})
|
|
|
|
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
|
|
|
assert msg["result"]["default_dark_theme"] is None
|
|
|
|
|
|
|
|
|
2018-06-06 08:12:43 +00:00
|
|
|
async def test_themes_reload_themes(hass, hass_ws_client):
|
2017-07-13 01:08:13 +00:00
|
|
|
"""Test frontend.reload_themes service."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, "frontend", CONFIG_THEMES)
|
2018-06-06 08:12:43 +00:00
|
|
|
client = await hass_ws_client(hass)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2020-01-14 21:03:02 +00:00
|
|
|
"homeassistant.components.frontend.async_hass_config_yaml",
|
2019-07-31 19:25:30 +00:00
|
|
|
return_value={DOMAIN: {CONF_THEMES: {"sad": {"primary-color": "blue"}}}},
|
|
|
|
):
|
2018-06-06 08:12:43 +00:00
|
|
|
await hass.services.async_call(
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN, "set_theme", {"name": "happy"}, blocking=True
|
|
|
|
)
|
|
|
|
await hass.services.async_call(DOMAIN, "reload_themes", blocking=True)
|
2017-07-14 18:26:26 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await client.send_json({"id": 5, "type": "frontend/get_themes"})
|
2017-07-14 18:26:26 +00:00
|
|
|
|
2018-06-06 08:12:43 +00:00
|
|
|
msg = await client.receive_json()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["result"]["themes"] == {"sad": {"primary-color": "blue"}}
|
|
|
|
assert msg["result"]["default_theme"] == "default"
|
2018-06-06 08:12:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_missing_themes(hass, hass_ws_client):
|
2017-07-14 18:26:26 +00:00
|
|
|
"""Test that themes API works when themes are not defined."""
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_setup_component(hass, "frontend", {})
|
2018-06-06 08:12:43 +00:00
|
|
|
|
|
|
|
client = await hass_ws_client(hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
await client.send_json({"id": 5, "type": "frontend/get_themes"})
|
2018-06-06 08:12:43 +00:00
|
|
|
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["id"] == 5
|
|
|
|
assert msg["type"] == TYPE_RESULT
|
|
|
|
assert msg["success"]
|
|
|
|
assert msg["result"]["default_theme"] == "default"
|
|
|
|
assert msg["result"]["themes"] == {}
|
2017-08-27 16:07:58 +00:00
|
|
|
|
|
|
|
|
2019-05-30 11:37:01 +00:00
|
|
|
async def test_get_panels(hass, hass_ws_client, mock_http_client):
|
2018-05-01 17:35:23 +00:00
|
|
|
"""Test get_panels command."""
|
2019-05-30 11:37:01 +00:00
|
|
|
events = async_capture_events(hass, EVENT_PANELS_UPDATED)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = await mock_http_client.get("/map")
|
2020-04-08 22:57:47 +00:00
|
|
|
assert resp.status == HTTP_NOT_FOUND
|
2019-05-30 11:37:01 +00:00
|
|
|
|
|
|
|
hass.components.frontend.async_register_built_in_panel(
|
2019-07-31 19:25:30 +00:00
|
|
|
"map", "Map", "mdi:tooltip-account", require_admin=True
|
|
|
|
)
|
2018-05-01 17:35:23 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = await mock_http_client.get("/map")
|
2019-05-30 11:37:01 +00:00
|
|
|
assert resp.status == 200
|
|
|
|
|
|
|
|
assert len(events) == 1
|
|
|
|
|
2018-05-01 17:35:23 +00:00
|
|
|
client = await hass_ws_client(hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
await client.send_json({"id": 5, "type": "get_panels"})
|
2018-05-01 17:35:23 +00:00
|
|
|
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["id"] == 5
|
|
|
|
assert msg["type"] == TYPE_RESULT
|
|
|
|
assert msg["success"]
|
|
|
|
assert msg["result"]["map"]["component_name"] == "map"
|
|
|
|
assert msg["result"]["map"]["url_path"] == "map"
|
|
|
|
assert msg["result"]["map"]["icon"] == "mdi:tooltip-account"
|
|
|
|
assert msg["result"]["map"]["title"] == "Map"
|
|
|
|
assert msg["result"]["map"]["require_admin"] is True
|
2019-03-25 17:04:35 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.components.frontend.async_remove_panel("map")
|
2019-05-30 11:37:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = await mock_http_client.get("/map")
|
2020-04-08 22:57:47 +00:00
|
|
|
assert resp.status == HTTP_NOT_FOUND
|
2019-05-30 11:37:01 +00:00
|
|
|
|
|
|
|
assert len(events) == 2
|
|
|
|
|
2019-03-25 17:04:35 +00:00
|
|
|
|
|
|
|
async def test_get_panels_non_admin(hass, hass_ws_client, hass_admin_user):
|
|
|
|
"""Test get_panels command."""
|
|
|
|
hass_admin_user.groups = []
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_setup_component(hass, "frontend", {})
|
2019-05-30 11:37:01 +00:00
|
|
|
hass.components.frontend.async_register_built_in_panel(
|
2019-07-31 19:25:30 +00:00
|
|
|
"map", "Map", "mdi:tooltip-account", require_admin=True
|
|
|
|
)
|
2019-05-30 11:37:01 +00:00
|
|
|
hass.components.frontend.async_register_built_in_panel(
|
2019-07-31 19:25:30 +00:00
|
|
|
"history", "History", "mdi:history"
|
|
|
|
)
|
2019-03-25 17:04:35 +00:00
|
|
|
|
|
|
|
client = await hass_ws_client(hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
await client.send_json({"id": 5, "type": "get_panels"})
|
2019-03-25 17:04:35 +00:00
|
|
|
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["id"] == 5
|
|
|
|
assert msg["type"] == TYPE_RESULT
|
|
|
|
assert msg["success"]
|
|
|
|
assert "history" in msg["result"]
|
|
|
|
assert "map" not in msg["result"]
|
2018-06-06 08:12:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_get_translations(hass, hass_ws_client):
|
|
|
|
"""Test get_translations command."""
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_setup_component(hass, "frontend", {})
|
2018-06-06 08:12:43 +00:00
|
|
|
client = await hass_ws_client(hass)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.frontend.async_get_translations",
|
2020-04-19 00:13:13 +00:00
|
|
|
side_effect=lambda hass, lang, category, integration, config_flow: {
|
|
|
|
"lang": lang
|
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
|
|
|
await client.send_json(
|
2020-04-19 00:13:13 +00:00
|
|
|
{
|
|
|
|
"id": 5,
|
|
|
|
"type": "frontend/get_translations",
|
|
|
|
"language": "nl",
|
|
|
|
"category": "lang",
|
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-06-06 08:12:43 +00:00
|
|
|
msg = await client.receive_json()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["id"] == 5
|
|
|
|
assert msg["type"] == TYPE_RESULT
|
|
|
|
assert msg["success"]
|
|
|
|
assert msg["result"] == {"resources": {"lang": "nl"}}
|
2018-06-16 21:12:03 +00:00
|
|
|
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def test_auth_load(mock_http_client, mock_onboarded):
|
2018-07-22 07:49:58 +00:00
|
|
|
"""Test auth component loaded by default."""
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = await mock_http_client.get("/auth/providers")
|
2018-07-22 07:49:58 +00:00
|
|
|
assert resp.status == 200
|
|
|
|
|
|
|
|
|
|
|
|
async def test_onboarding_load(mock_http_client):
|
|
|
|
"""Test onboarding component loaded by default."""
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = await mock_http_client.get("/api/onboarding")
|
2018-07-22 07:49:58 +00:00
|
|
|
assert resp.status == 200
|
2018-08-09 07:27:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_auth_authorize(mock_http_client):
|
|
|
|
"""Test the authorize endpoint works."""
|
2018-09-27 16:02:50 +00:00
|
|
|
resp = await mock_http_client.get(
|
2019-07-31 19:25:30 +00:00
|
|
|
"/auth/authorize?response_type=code&client_id=https://localhost/&"
|
|
|
|
"redirect_uri=https://localhost/&state=123%23456"
|
|
|
|
)
|
2019-05-02 20:59:24 +00:00
|
|
|
assert resp.status == 200
|
|
|
|
# No caching of auth page.
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "cache-control" not in resp.headers
|
2018-08-09 07:27:54 +00:00
|
|
|
|
2019-05-02 20:59:24 +00:00
|
|
|
text = await resp.text()
|
2018-09-27 16:02:50 +00:00
|
|
|
|
2019-05-02 20:59:24 +00:00
|
|
|
# Test we can retrieve authorize.js
|
|
|
|
authorizejs = re.search(
|
2019-07-31 19:25:30 +00:00
|
|
|
r"(?P<app>\/frontend_latest\/authorize.[A-Za-z0-9]{8}.js)", text
|
|
|
|
)
|
2018-09-27 16:02:50 +00:00
|
|
|
|
2019-05-02 20:59:24 +00:00
|
|
|
assert authorizejs is not None, text
|
|
|
|
resp = await mock_http_client.get(authorizejs.groups(0)[0])
|
|
|
|
assert resp.status == 200
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "public" in resp.headers.get("cache-control")
|
2020-04-26 00:30:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_get_version(hass, hass_ws_client):
|
|
|
|
"""Test get_version command."""
|
|
|
|
frontend = await async_get_integration(hass, "frontend")
|
|
|
|
cur_version = next(
|
|
|
|
req.split("==", 1)[1]
|
|
|
|
for req in frontend.requirements
|
|
|
|
if req.startswith("home-assistant-frontend==")
|
|
|
|
)
|
|
|
|
|
|
|
|
await async_setup_component(hass, "frontend", {})
|
|
|
|
client = await hass_ws_client(hass)
|
|
|
|
|
|
|
|
await client.send_json({"id": 5, "type": "frontend/get_version"})
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
|
|
|
assert msg["id"] == 5
|
|
|
|
assert msg["type"] == TYPE_RESULT
|
|
|
|
assert msg["success"]
|
|
|
|
assert msg["result"] == {"version": cur_version}
|
2020-09-03 16:13:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_static_paths(hass, mock_http_client):
|
|
|
|
"""Test static paths."""
|
|
|
|
resp = await mock_http_client.get(
|
|
|
|
"/.well-known/change-password", allow_redirects=False
|
|
|
|
)
|
|
|
|
assert resp.status == 302
|
|
|
|
assert resp.headers["location"] == "/profile"
|