Dynamic panels (#24184)

* Don't require all panel urls to be registered

* Allow removing panels, fire event when panels updated
This commit is contained in:
Paulus Schoutsen 2019-05-30 04:37:01 -07:00 committed by Pascal Vizeli
parent 59ce31f44f
commit 1a3a38d370
14 changed files with 133 additions and 58 deletions

View file

@ -8,10 +8,11 @@ import pytest
from homeassistant.setup import async_setup_component
from homeassistant.components.frontend import (
DOMAIN, CONF_JS_VERSION, CONF_THEMES, CONF_EXTRA_HTML_URL,
CONF_EXTRA_HTML_URL_ES5)
CONF_EXTRA_HTML_URL_ES5, generate_negative_index_regex,
EVENT_PANELS_UPDATED)
from homeassistant.components.websocket_api.const import TYPE_RESULT
from tests.common import mock_coro
from tests.common import mock_coro, async_capture_events
CONFIG_THEMES = {
@ -232,12 +233,21 @@ def test_extra_urls(mock_http_client_with_urls, mock_onboarded):
assert text.find('href="https://domain.com/my_extra_url.html"') >= 0
async def test_get_panels(hass, hass_ws_client):
async def test_get_panels(hass, hass_ws_client, mock_http_client):
"""Test get_panels command."""
await async_setup_component(hass, 'frontend', {})
await hass.components.frontend.async_register_built_in_panel(
events = async_capture_events(hass, EVENT_PANELS_UPDATED)
resp = await mock_http_client.get('/map')
assert resp.status == 404
hass.components.frontend.async_register_built_in_panel(
'map', 'Map', 'mdi:tooltip-account', require_admin=True)
resp = await mock_http_client.get('/map')
assert resp.status == 200
assert len(events) == 1
client = await hass_ws_client(hass)
await client.send_json({
'id': 5,
@ -255,14 +265,21 @@ async def test_get_panels(hass, hass_ws_client):
assert msg['result']['map']['title'] == 'Map'
assert msg['result']['map']['require_admin'] is True
hass.components.frontend.async_remove_panel('map')
resp = await mock_http_client.get('/map')
assert resp.status == 404
assert len(events) == 2
async def test_get_panels_non_admin(hass, hass_ws_client, hass_admin_user):
"""Test get_panels command."""
hass_admin_user.groups = []
await async_setup_component(hass, 'frontend', {})
await hass.components.frontend.async_register_built_in_panel(
hass.components.frontend.async_register_built_in_panel(
'map', 'Map', 'mdi:tooltip-account', require_admin=True)
await hass.components.frontend.async_register_built_in_panel(
hass.components.frontend.async_register_built_in_panel(
'history', 'History', 'mdi:history')
client = await hass_ws_client(hass)
@ -331,3 +348,43 @@ async def test_auth_authorize(mock_http_client):
resp = await mock_http_client.get(authorizejs.groups(0)[0])
assert resp.status == 200
assert 'public' in resp.headers.get('cache-control')
def test_index_regex():
"""Test the index regex."""
pattern = re.compile('/' + generate_negative_index_regex())
for should_match in (
'/',
'/lovelace',
'/lovelace/default_view',
'/map',
'/config',
):
assert pattern.match(should_match), should_match
for should_not_match in (
'/service_worker.js',
'/manifest.json',
'/onboarding.html',
'/manifest.json',
'static',
'static/',
'static/index.html',
'frontend_latest',
'frontend_latest/',
'frontend_latest/index.html',
'frontend_es5',
'frontend_es5/',
'frontend_es5/index.html',
'local',
'local/',
'local/index.html',
'auth',
'auth/',
'auth/index.html',
'/api',
'/api/',
'/api/logbook',
):
assert not pattern.match(should_not_match), should_not_match