-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Add permission checking to all RainMachine services #22399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
balloob
merged 7 commits into
home-assistant:dev
from
bachya:rainmachine-service-permissions
Apr 1, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1a9d5d8
Add permission checking to all RainMachine services
bachya 899990c
Linting
bachya dc06ff7
Some initial work
bachya b570bf2
Owner comments
bachya e8c0fe2
Test in place (I think)
bachya fcb12d7
Linting
bachya 1bd8013
Update conftest.py
balloob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| """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, | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| """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)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we make this a helper decorator? There's nothing here that is specific to rainmachine. It could be used on any domain that has entity registry support that needs to do the same check, if we pass in domain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, love that idea. I'll submit a PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bachya and then maybe also update your implementation here so that you do something like this:
That way the decorator is re-used instead of re-generated each time.