Add permissions check in service helper (#18596)
* Add permissions check in service helper * Lint * Fix tests * Lint * Typing * Fix unused impoert
This commit is contained in:
1 parent
8aa2cefd75
commit
36c31a6293
14 files changed
+268
-46
No files matched your search
@@ -3,6 +3,7 @@ from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.auth.const import GROUP_ID_ADMIN, GROUP_ID_READ_ONLY
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components.websocket_api.http import URL
|
||||
from homeassistant.components.websocket_api.auth import (
|
||||
@@ -77,3 +78,19 @@ def hass_access_token(hass):
|
||||
refresh_token = hass.loop.run_until_complete(
|
||||
hass.auth.async_create_refresh_token(user, CLIENT_ID))
|
||||
yield hass.auth.async_create_access_token(refresh_token)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def hass_admin_user(hass):
|
||||
"""Return a Home Assistant admin user."""
|
||||
admin_group = hass.loop.run_until_complete(hass.auth.async_get_group(
|
||||
GROUP_ID_ADMIN))
|
||||
return MockUser(groups=[admin_group]).add_to_hass(hass)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def hass_read_only_user(hass):
|
||||
"""Return a Home Assistant read only user."""
|
||||
read_only_group = hass.loop.run_until_complete(hass.auth.async_get_group(
|
||||
GROUP_ID_READ_ONLY))
|
||||
return MockUser(groups=[read_only_group]).add_to_hass(hass)
|
||||
@@ -234,7 +234,7 @@ def test_no_initial_state_and_no_restore_state(hass):
|
||||
assert int(state.state) == 0
|
||||
|
||||
|
||||
async def test_counter_context(hass):
|
||||
async def test_counter_context(hass, hass_admin_user):
|
||||
"""Test that counter context works."""
|
||||
assert await async_setup_component(hass, 'counter', {
|
||||
'counter': {
|
||||
@@ -247,9 +247,9 @@ async def test_counter_context(hass):
|
||||
|
||||
await hass.services.async_call('counter', 'increment', {
|
||||
'entity_id': state.entity_id,
|
||||
}, True, Context(user_id='abcd'))
|
||||
}, True, Context(user_id=hass_admin_user.id))
|
||||
|
||||
state2 = hass.states.get('counter.test')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
assert state2.context.user_id == hass_admin_user.id
|
||||
@@ -476,7 +476,7 @@ async def test_intent_set_color_and_brightness(hass):
|
||||
assert call.data.get(light.ATTR_BRIGHTNESS_PCT) == 20
|
||||
|
||||
|
||||
async def test_light_context(hass):
|
||||
async def test_light_context(hass, hass_admin_user):
|
||||
"""Test that light context works."""
|
||||
assert await async_setup_component(hass, 'light', {
|
||||
'light': {
|
||||
@@ -489,9 +489,9 @@ async def test_light_context(hass):
|
||||
|
||||
await hass.services.async_call('light', 'toggle', {
|
||||
'entity_id': state.entity_id,
|
||||
}, True, core.Context(user_id='abcd'))
|
||||
}, True, core.Context(user_id=hass_admin_user.id))
|
||||
|
||||
state2 = hass.states.get('light.ceiling')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
assert state2.context.user_id == hass_admin_user.id
|
||||
@@ -91,7 +91,7 @@ class TestSwitch(unittest.TestCase):
|
||||
)
|
||||
|
||||
|
||||
async def test_switch_context(hass):
|
||||
async def test_switch_context(hass, hass_admin_user):
|
||||
"""Test that switch context works."""
|
||||
assert await async_setup_component(hass, 'switch', {
|
||||
'switch': {
|
||||
@@ -104,9 +104,9 @@ async def test_switch_context(hass):
|
||||
|
||||
await hass.services.async_call('switch', 'toggle', {
|
||||
'entity_id': state.entity_id,
|
||||
}, True, core.Context(user_id='abcd'))
|
||||
}, True, core.Context(user_id=hass_admin_user.id))
|
||||
|
||||
state2 = hass.states.get('switch.ac')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
assert state2.context.user_id == hass_admin_user.id
|
||||
@@ -147,7 +147,7 @@ def test_initial_state_overrules_restore_state(hass):
|
||||
assert state.state == 'on'
|
||||
|
||||
|
||||
async def test_input_boolean_context(hass):
|
||||
async def test_input_boolean_context(hass, hass_admin_user):
|
||||
"""Test that input_boolean context works."""
|
||||
assert await async_setup_component(hass, 'input_boolean', {
|
||||
'input_boolean': {
|
||||
@@ -160,9 +160,9 @@ async def test_input_boolean_context(hass):
|
||||
|
||||
await hass.services.async_call('input_boolean', 'turn_off', {
|
||||
'entity_id': state.entity_id,
|
||||
}, True, Context(user_id='abcd'))
|
||||
}, True, Context(user_id=hass_admin_user.id))
|
||||
|
||||
state2 = hass.states.get('input_boolean.ac')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
assert state2.context.user_id == hass_admin_user.id
|
||||
@@ -195,7 +195,7 @@ def test_restore_state(hass):
|
||||
assert state_bogus.state == str(initial)
|
||||
|
||||
|
||||
async def test_input_datetime_context(hass):
|
||||
async def test_input_datetime_context(hass, hass_admin_user):
|
||||
"""Test that input_datetime context works."""
|
||||
assert await async_setup_component(hass, 'input_datetime', {
|
||||
'input_datetime': {
|
||||
@@ -211,9 +211,9 @@ async def test_input_datetime_context(hass):
|
||||
await hass.services.async_call('input_datetime', 'set_datetime', {
|
||||
'entity_id': state.entity_id,
|
||||
'date': '2018-01-02'
|
||||
}, True, Context(user_id='abcd'))
|
||||
}, True, Context(user_id=hass_admin_user.id))
|
||||
|
||||
state2 = hass.states.get('input_datetime.only_date')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
assert state2.context.user_id == hass_admin_user.id
|
||||
@@ -266,7 +266,7 @@ def test_no_initial_state_and_no_restore_state(hass):
|
||||
assert float(state.state) == 0
|
||||
|
||||
|
||||
async def test_input_number_context(hass):
|
||||
async def test_input_number_context(hass, hass_admin_user):
|
||||
"""Test that input_number context works."""
|
||||
assert await async_setup_component(hass, 'input_number', {
|
||||
'input_number': {
|
||||
@@ -282,9 +282,9 @@ async def test_input_number_context(hass):
|
||||
|
||||
await hass.services.async_call('input_number', 'increment', {
|
||||
'entity_id': state.entity_id,
|
||||
}, True, Context(user_id='abcd'))
|
||||
}, True, Context(user_id=hass_admin_user.id))
|
||||
|
||||
state2 = hass.states.get('input_number.b1')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
assert state2.context.user_id == hass_admin_user.id
|
||||
@@ -302,7 +302,7 @@ def test_initial_state_overrules_restore_state(hass):
|
||||
assert state.state == 'middle option'
|
||||
|
||||
|
||||
async def test_input_select_context(hass):
|
||||
async def test_input_select_context(hass, hass_admin_user):
|
||||
"""Test that input_select context works."""
|
||||
assert await async_setup_component(hass, 'input_select', {
|
||||
'input_select': {
|
||||
@@ -321,9 +321,9 @@ async def test_input_select_context(hass):
|
||||
|
||||
await hass.services.async_call('input_select', 'select_next', {
|
||||
'entity_id': state.entity_id,
|
||||
}, True, Context(user_id='abcd'))
|
||||
}, True, Context(user_id=hass_admin_user.id))
|
||||
|
||||
state2 = hass.states.get('input_select.s1')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
assert state2.context.user_id == hass_admin_user.id
|
||||
@@ -184,7 +184,7 @@ def test_no_initial_state_and_no_restore_state(hass):
|
||||
assert str(state.state) == 'unknown'
|
||||
|
||||
|
||||
async def test_input_text_context(hass):
|
||||
async def test_input_text_context(hass, hass_admin_user):
|
||||
"""Test that input_text context works."""
|
||||
assert await async_setup_component(hass, 'input_text', {
|
||||
'input_text': {
|
||||
@@ -200,9 +200,9 @@ async def test_input_text_context(hass):
|
||||
await hass.services.async_call('input_text', 'set_value', {
|
||||
'entity_id': state.entity_id,
|
||||
'value': 'new_value',
|
||||
}, True, Context(user_id='abcd'))
|
||||
}, True, Context(user_id=hass_admin_user.id))
|
||||
|
||||
state2 = hass.states.get('input_text.t1')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
assert state2.context.user_id == hass_admin_user.id
|
||||
Reference in new issue
Block a user