Add a service require_admin wrapper (#21953)
* Add a service require_admin wrapper * Allow it to be used as a decorator * Lint * Add comment * Add docstring * Update syntax
This commit is contained in:
1 parent
bf839687ad
commit
c15f433c3e
2 files changed
+61
-3
No files matched your search
@@ -5,18 +5,18 @@ from copy import deepcopy
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import voluptuous as vol
|
||||
import pytest
|
||||
|
||||
# To prevent circular import when running just this file
|
||||
import homeassistant.components # noqa
|
||||
from homeassistant import core as ha, loader, exceptions
|
||||
from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ENTITY_ID
|
||||
from homeassistant.helpers import service, template
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.auth.permissions import PolicyPermissions
|
||||
from homeassistant.helpers import (
|
||||
device_registry as dev_reg, entity_registry as ent_reg)
|
||||
service, template, device_registry as dev_reg, entity_registry as ent_reg)
|
||||
from tests.common import (
|
||||
get_test_home_assistant, mock_service, mock_coro, mock_registry,
|
||||
mock_device_registry)
|
||||
@@ -395,3 +395,37 @@ async def test_call_with_omit_entity_id(hass, mock_service_platform_call,
|
||||
mock_entities['light.kitchen'], mock_entities['light.living_room']]
|
||||
assert ('Not passing an entity ID to a service to target '
|
||||
'all entities is deprecated') in caplog.text
|
||||
|
||||
|
||||
async def test_register_admin_service(hass, hass_read_only_user,
|
||||
hass_admin_user):
|
||||
"""Test the register admin service."""
|
||||
calls = []
|
||||
|
||||
async def mock_service(call):
|
||||
calls.append(call)
|
||||
|
||||
hass.helpers.service.async_register_admin_service(
|
||||
'test', 'test', mock_service, vol.Schema({})
|
||||
)
|
||||
|
||||
with pytest.raises(exceptions.UnknownUser):
|
||||
await hass.services.async_call(
|
||||
'test', 'test', {}, blocking=True, context=ha.Context(
|
||||
user_id='non-existing'
|
||||
))
|
||||
assert len(calls) == 0
|
||||
|
||||
with pytest.raises(exceptions.Unauthorized):
|
||||
await hass.services.async_call(
|
||||
'test', 'test', {}, blocking=True, context=ha.Context(
|
||||
user_id=hass_read_only_user.id
|
||||
))
|
||||
assert len(calls) == 0
|
||||
|
||||
await hass.services.async_call(
|
||||
'test', 'test', {}, blocking=True, context=ha.Context(
|
||||
user_id=hass_admin_user.id
|
||||
))
|
||||
assert len(calls) == 1
|
||||
assert calls[0].context.user_id == hass_admin_user.id
|
||||
Reference in new issue
Block a user