Create decorator to check service permissions (#22667)
* Create decorator to check service permissions * Typing * Linting * Member comments * Linting * Member comments * Updated import * Owner comments * Linting * Linting * More work * Fixed tests * Removed service helper tests in RainMachine * Linting * Owner comments * Linting * Owner comments Co-Authored-By: bachya <bachya1208@gmail.com>
This commit is contained in:
1 parent
7a6950fd72
commit
fc481133e7
5 files changed
+177
-118
No files matched your search
@@ -1,23 +0,0 @@
|
||||
"""Configuration for Rainmachine tests."""
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.rainmachine.const import DOMAIN
|
||||
from homeassistant.const import (
|
||||
CONF_IP_ADDRESS, CONF_PASSWORD, CONF_PORT, CONF_SCAN_INTERVAL, CONF_SSL)
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.fixture(name="config_entry")
|
||||
def config_entry_fixture():
|
||||
"""Create a mock RainMachine config entry."""
|
||||
return MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title='192.168.1.101',
|
||||
data={
|
||||
CONF_IP_ADDRESS: '192.168.1.101',
|
||||
CONF_PASSWORD: '12345',
|
||||
CONF_PORT: 8080,
|
||||
CONF_SSL: True,
|
||||
CONF_SCAN_INTERVAL: 60,
|
||||
})
|
||||
@@ -1,41 +0,0 @@
|
||||
"""Define tests for permissions on RainMachine service calls."""
|
||||
import asynctest
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.rainmachine.const import DOMAIN
|
||||
from homeassistant.core import Context
|
||||
from homeassistant.exceptions import Unauthorized, UnknownUser
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
|
||||
async def setup_platform(hass, config_entry):
|
||||
"""Set up the media player platform for testing."""
|
||||
with asynctest.mock.patch('regenmaschine.login') as mock_login:
|
||||
mock_client = mock_login.return_value
|
||||
mock_client.restrictions.current.return_value = mock_coro()
|
||||
mock_client.restrictions.universal.return_value = mock_coro()
|
||||
config_entry.add_to_hass(hass)
|
||||
assert await async_setup_component(hass, DOMAIN)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_services_authorization(
|
||||
hass, config_entry, hass_read_only_user):
|
||||
"""Test that a RainMachine service is halted on incorrect permissions."""
|
||||
await setup_platform(hass, config_entry)
|
||||
|
||||
with pytest.raises(UnknownUser):
|
||||
await hass.services.async_call(
|
||||
'rainmachine',
|
||||
'unpause_watering', {},
|
||||
blocking=True,
|
||||
context=Context(user_id='fake_user_id'))
|
||||
|
||||
with pytest.raises(Unauthorized):
|
||||
await hass.services.async_call(
|
||||
'rainmachine',
|
||||
'unpause_watering', {},
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_read_only_user.id))
|
||||
Reference in new issue
Block a user