Permissions improv (#17811)
* Break up permissions file. * Granular entity permissions * Add "all" entity permission * Lint * Fix types
This commit is contained in:
1 parent
d1ef875132
commit
f4ac317d64
11 files changed
+625
-453
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Tests for permissions."""
|
||||
@@ -0,0 +1,187 @@
|
||||
"""Tests for entity permissions."""
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.auth.permissions.entities import (
|
||||
compile_entities, ENTITY_POLICY_SCHEMA)
|
||||
|
||||
|
||||
def test_entities_none():
|
||||
"""Test entity ID policy."""
|
||||
policy = None
|
||||
compiled = compile_entities(policy)
|
||||
assert compiled('light.kitchen', ('read',)) is False
|
||||
|
||||
|
||||
def test_entities_empty():
|
||||
"""Test entity ID policy."""
|
||||
policy = {}
|
||||
ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = compile_entities(policy)
|
||||
assert compiled('light.kitchen', ('read',)) is False
|
||||
|
||||
|
||||
def test_entities_false():
|
||||
"""Test entity ID policy."""
|
||||
policy = False
|
||||
with pytest.raises(vol.Invalid):
|
||||
ENTITY_POLICY_SCHEMA(policy)
|
||||
|
||||
|
||||
def test_entities_true():
|
||||
"""Test entity ID policy."""
|
||||
policy = True
|
||||
ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = compile_entities(policy)
|
||||
assert compiled('light.kitchen', ('read',)) is True
|
||||
|
||||
|
||||
def test_entities_domains_true():
|
||||
"""Test entity ID policy."""
|
||||
policy = {
|
||||
'domains': True
|
||||
}
|
||||
ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = compile_entities(policy)
|
||||
assert compiled('light.kitchen', ('read',)) is True
|
||||
|
||||
|
||||
def test_entities_domains_domain_true():
|
||||
"""Test entity ID policy."""
|
||||
policy = {
|
||||
'domains': {
|
||||
'light': True
|
||||
}
|
||||
}
|
||||
ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = compile_entities(policy)
|
||||
assert compiled('light.kitchen', ('read',)) is True
|
||||
assert compiled('switch.kitchen', ('read',)) is False
|
||||
|
||||
|
||||
def test_entities_domains_domain_false():
|
||||
"""Test entity ID policy."""
|
||||
policy = {
|
||||
'domains': {
|
||||
'light': False
|
||||
}
|
||||
}
|
||||
with pytest.raises(vol.Invalid):
|
||||
ENTITY_POLICY_SCHEMA(policy)
|
||||
|
||||
|
||||
def test_entities_entity_ids_true():
|
||||
"""Test entity ID policy."""
|
||||
policy = {
|
||||
'entity_ids': True
|
||||
}
|
||||
ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = compile_entities(policy)
|
||||
assert compiled('light.kitchen', ('read',)) is True
|
||||
|
||||
|
||||
def test_entities_entity_ids_false():
|
||||
"""Test entity ID policy."""
|
||||
policy = {
|
||||
'entity_ids': False
|
||||
}
|
||||
with pytest.raises(vol.Invalid):
|
||||
ENTITY_POLICY_SCHEMA(policy)
|
||||
|
||||
|
||||
def test_entities_entity_ids_entity_id_true():
|
||||
"""Test entity ID policy."""
|
||||
policy = {
|
||||
'entity_ids': {
|
||||
'light.kitchen': True
|
||||
}
|
||||
}
|
||||
ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = compile_entities(policy)
|
||||
assert compiled('light.kitchen', ('read',)) is True
|
||||
assert compiled('switch.kitchen', ('read',)) is False
|
||||
|
||||
|
||||
def test_entities_entity_ids_entity_id_false():
|
||||
"""Test entity ID policy."""
|
||||
policy = {
|
||||
'entity_ids': {
|
||||
'light.kitchen': False
|
||||
}
|
||||
}
|
||||
with pytest.raises(vol.Invalid):
|
||||
ENTITY_POLICY_SCHEMA(policy)
|
||||
|
||||
|
||||
def test_entities_control_only():
|
||||
"""Test policy granting control only."""
|
||||
policy = {
|
||||
'entity_ids': {
|
||||
'light.kitchen': {
|
||||
'read': True,
|
||||
}
|
||||
}
|
||||
}
|
||||
ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = compile_entities(policy)
|
||||
assert compiled('light.kitchen', ('read',)) is True
|
||||
assert compiled('light.kitchen', ('control',)) is False
|
||||
assert compiled('light.kitchen', ('edit',)) is False
|
||||
|
||||
|
||||
def test_entities_read_control():
|
||||
"""Test policy granting control only."""
|
||||
policy = {
|
||||
'domains': {
|
||||
'light': {
|
||||
'read': True,
|
||||
'control': True,
|
||||
}
|
||||
}
|
||||
}
|
||||
ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = compile_entities(policy)
|
||||
assert compiled('light.kitchen', ('read',)) is True
|
||||
assert compiled('light.kitchen', ('control',)) is True
|
||||
assert compiled('light.kitchen', ('edit',)) is False
|
||||
|
||||
|
||||
def test_entities_all_allow():
|
||||
"""Test policy allowing all entities."""
|
||||
policy = {
|
||||
'all': True
|
||||
}
|
||||
ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = compile_entities(policy)
|
||||
assert compiled('light.kitchen', ('read',)) is True
|
||||
assert compiled('light.kitchen', ('control',)) is True
|
||||
assert compiled('switch.kitchen', ('read',)) is True
|
||||
|
||||
|
||||
def test_entities_all_read():
|
||||
"""Test policy applying read to all entities."""
|
||||
policy = {
|
||||
'all': {
|
||||
'read': True
|
||||
}
|
||||
}
|
||||
ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = compile_entities(policy)
|
||||
assert compiled('light.kitchen', ('read',)) is True
|
||||
assert compiled('light.kitchen', ('control',)) is False
|
||||
assert compiled('switch.kitchen', ('read',)) is True
|
||||
|
||||
|
||||
def test_entities_all_control():
|
||||
"""Test entity ID policy applying control to all."""
|
||||
policy = {
|
||||
'all': {
|
||||
'control': True
|
||||
}
|
||||
}
|
||||
ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = compile_entities(policy)
|
||||
assert compiled('light.kitchen', ('read',)) is False
|
||||
assert compiled('light.kitchen', ('control',)) is True
|
||||
assert compiled('switch.kitchen', ('read',)) is False
|
||||
assert compiled('switch.kitchen', ('control',)) is True
|
||||
@@ -0,0 +1,46 @@
|
||||
"""Tests for the auth permission system."""
|
||||
from homeassistant.core import State
|
||||
from homeassistant.auth import permissions
|
||||
|
||||
|
||||
def test_policy_perm_filter_states():
|
||||
"""Test filtering entitites."""
|
||||
states = [
|
||||
State('light.kitchen', 'on'),
|
||||
State('light.living_room', 'off'),
|
||||
State('light.balcony', 'on'),
|
||||
]
|
||||
perm = permissions.PolicyPermissions({
|
||||
'entities': {
|
||||
'entity_ids': {
|
||||
'light.kitchen': True,
|
||||
'light.balcony': True,
|
||||
}
|
||||
}
|
||||
})
|
||||
filtered = perm.filter_states(states)
|
||||
assert len(filtered) == 2
|
||||
assert filtered == [states[0], states[2]]
|
||||
|
||||
|
||||
def test_owner_permissions():
|
||||
"""Test owner permissions access all."""
|
||||
assert permissions.OwnerPermissions.check_entity('light.kitchen', 'write')
|
||||
states = [
|
||||
State('light.kitchen', 'on'),
|
||||
State('light.living_room', 'off'),
|
||||
State('light.balcony', 'on'),
|
||||
]
|
||||
assert permissions.OwnerPermissions.filter_states(states) == states
|
||||
|
||||
|
||||
def test_default_policy_allow_all():
|
||||
"""Test that the default policy is to allow all entity actions."""
|
||||
perm = permissions.PolicyPermissions(permissions.DEFAULT_POLICY)
|
||||
assert perm.check_entity('light.kitchen', 'read')
|
||||
states = [
|
||||
State('light.kitchen', 'on'),
|
||||
State('light.living_room', 'off'),
|
||||
State('light.balcony', 'on'),
|
||||
]
|
||||
assert perm.filter_states(states) == states
|
||||
@@ -0,0 +1,44 @@
|
||||
"""Tests for permissions merging."""
|
||||
from homeassistant.auth.permissions.merge import merge_policies
|
||||
|
||||
|
||||
def test_merging_permissions_true_rules_dict():
|
||||
"""Test merging policy with two entities."""
|
||||
policy1 = {
|
||||
'something_else': True,
|
||||
'entities': {
|
||||
'entity_ids': {
|
||||
'light.kitchen': True,
|
||||
}
|
||||
}
|
||||
}
|
||||
policy2 = {
|
||||
'entities': {
|
||||
'entity_ids': True
|
||||
}
|
||||
}
|
||||
assert merge_policies([policy1, policy2]) == {
|
||||
'something_else': True,
|
||||
'entities': {
|
||||
'entity_ids': True
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def test_merging_permissions_multiple_subcategories():
|
||||
"""Test merging policy with two entities."""
|
||||
policy1 = {
|
||||
'entities': None
|
||||
}
|
||||
policy2 = {
|
||||
'entities': {
|
||||
'entity_ids': True,
|
||||
}
|
||||
}
|
||||
policy3 = {
|
||||
'entities': True
|
||||
}
|
||||
assert merge_policies([policy1, policy2]) == policy2
|
||||
assert merge_policies([policy1, policy3]) == policy3
|
||||
|
||||
assert merge_policies([policy2, policy3]) == policy3
|
||||
@@ -29,6 +29,6 @@ def test_permissions_merged():
|
||||
# Make sure we cache instance
|
||||
assert user.permissions is user.permissions
|
||||
|
||||
assert user.permissions.check_entity('switch.bla') is True
|
||||
assert user.permissions.check_entity('light.kitchen') is True
|
||||
assert user.permissions.check_entity('light.not_kitchen') is False
|
||||
assert user.permissions.check_entity('switch.bla', 'read') is True
|
||||
assert user.permissions.check_entity('light.kitchen', 'read') is True
|
||||
assert user.permissions.check_entity('light.not_kitchen', 'read') is False
|
||||
@@ -1,198 +0,0 @@
|
||||
"""Tests for the auth permission system."""
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.core import State
|
||||
from homeassistant.auth import permissions
|
||||
|
||||
|
||||
def test_entities_none():
|
||||
"""Test entity ID policy."""
|
||||
policy = None
|
||||
compiled = permissions._compile_entities(policy)
|
||||
assert compiled('light.kitchen', []) is False
|
||||
|
||||
|
||||
def test_entities_empty():
|
||||
"""Test entity ID policy."""
|
||||
policy = {}
|
||||
permissions.ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = permissions._compile_entities(policy)
|
||||
assert compiled('light.kitchen', []) is False
|
||||
|
||||
|
||||
def test_entities_false():
|
||||
"""Test entity ID policy."""
|
||||
policy = False
|
||||
with pytest.raises(vol.Invalid):
|
||||
permissions.ENTITY_POLICY_SCHEMA(policy)
|
||||
|
||||
|
||||
def test_entities_true():
|
||||
"""Test entity ID policy."""
|
||||
policy = True
|
||||
permissions.ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = permissions._compile_entities(policy)
|
||||
assert compiled('light.kitchen', []) is True
|
||||
|
||||
|
||||
def test_entities_domains_true():
|
||||
"""Test entity ID policy."""
|
||||
policy = {
|
||||
'domains': True
|
||||
}
|
||||
permissions.ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = permissions._compile_entities(policy)
|
||||
assert compiled('light.kitchen', []) is True
|
||||
|
||||
|
||||
def test_entities_domains_domain_true():
|
||||
"""Test entity ID policy."""
|
||||
policy = {
|
||||
'domains': {
|
||||
'light': True
|
||||
}
|
||||
}
|
||||
permissions.ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = permissions._compile_entities(policy)
|
||||
assert compiled('light.kitchen', []) is True
|
||||
assert compiled('switch.kitchen', []) is False
|
||||
|
||||
|
||||
def test_entities_domains_domain_false():
|
||||
"""Test entity ID policy."""
|
||||
policy = {
|
||||
'domains': {
|
||||
'light': False
|
||||
}
|
||||
}
|
||||
with pytest.raises(vol.Invalid):
|
||||
permissions.ENTITY_POLICY_SCHEMA(policy)
|
||||
|
||||
|
||||
def test_entities_entity_ids_true():
|
||||
"""Test entity ID policy."""
|
||||
policy = {
|
||||
'entity_ids': True
|
||||
}
|
||||
permissions.ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = permissions._compile_entities(policy)
|
||||
assert compiled('light.kitchen', []) is True
|
||||
|
||||
|
||||
def test_entities_entity_ids_false():
|
||||
"""Test entity ID policy."""
|
||||
policy = {
|
||||
'entity_ids': False
|
||||
}
|
||||
with pytest.raises(vol.Invalid):
|
||||
permissions.ENTITY_POLICY_SCHEMA(policy)
|
||||
|
||||
|
||||
def test_entities_entity_ids_entity_id_true():
|
||||
"""Test entity ID policy."""
|
||||
policy = {
|
||||
'entity_ids': {
|
||||
'light.kitchen': True
|
||||
}
|
||||
}
|
||||
permissions.ENTITY_POLICY_SCHEMA(policy)
|
||||
compiled = permissions._compile_entities(policy)
|
||||
assert compiled('light.kitchen', []) is True
|
||||
assert compiled('switch.kitchen', []) is False
|
||||
|
||||
|
||||
def test_entities_entity_ids_entity_id_false():
|
||||
"""Test entity ID policy."""
|
||||
policy = {
|
||||
'entity_ids': {
|
||||
'light.kitchen': False
|
||||
}
|
||||
}
|
||||
with pytest.raises(vol.Invalid):
|
||||
permissions.ENTITY_POLICY_SCHEMA(policy)
|
||||
|
||||
|
||||
def test_policy_perm_filter_states():
|
||||
"""Test filtering entitites."""
|
||||
states = [
|
||||
State('light.kitchen', 'on'),
|
||||
State('light.living_room', 'off'),
|
||||
State('light.balcony', 'on'),
|
||||
]
|
||||
perm = permissions.PolicyPermissions({
|
||||
'entities': {
|
||||
'entity_ids': {
|
||||
'light.kitchen': True,
|
||||
'light.balcony': True,
|
||||
}
|
||||
}
|
||||
})
|
||||
filtered = perm.filter_states(states)
|
||||
assert len(filtered) == 2
|
||||
assert filtered == [states[0], states[2]]
|
||||
|
||||
|
||||
def test_owner_permissions():
|
||||
"""Test owner permissions access all."""
|
||||
assert permissions.OwnerPermissions.check_entity('light.kitchen')
|
||||
states = [
|
||||
State('light.kitchen', 'on'),
|
||||
State('light.living_room', 'off'),
|
||||
State('light.balcony', 'on'),
|
||||
]
|
||||
assert permissions.OwnerPermissions.filter_states(states) == states
|
||||
|
||||
|
||||
def test_default_policy_allow_all():
|
||||
"""Test that the default policy is to allow all entity actions."""
|
||||
perm = permissions.PolicyPermissions(permissions.DEFAULT_POLICY)
|
||||
assert perm.check_entity('light.kitchen')
|
||||
states = [
|
||||
State('light.kitchen', 'on'),
|
||||
State('light.living_room', 'off'),
|
||||
State('light.balcony', 'on'),
|
||||
]
|
||||
assert perm.filter_states(states) == states
|
||||
|
||||
|
||||
def test_merging_permissions_true_rules_dict():
|
||||
"""Test merging policy with two entities."""
|
||||
policy1 = {
|
||||
'something_else': True,
|
||||
'entities': {
|
||||
'entity_ids': {
|
||||
'light.kitchen': True,
|
||||
}
|
||||
}
|
||||
}
|
||||
policy2 = {
|
||||
'entities': {
|
||||
'entity_ids': True
|
||||
}
|
||||
}
|
||||
assert permissions.merge_policies([policy1, policy2]) == {
|
||||
'something_else': True,
|
||||
'entities': {
|
||||
'entity_ids': True
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def test_merging_permissions_multiple_subcategories():
|
||||
"""Test merging policy with two entities."""
|
||||
policy1 = {
|
||||
'entities': None
|
||||
}
|
||||
policy2 = {
|
||||
'entities': {
|
||||
'entity_ids': True,
|
||||
}
|
||||
}
|
||||
policy3 = {
|
||||
'entities': True
|
||||
}
|
||||
assert permissions.merge_policies([policy1, policy2]) == policy2
|
||||
assert permissions.merge_policies([policy1, policy3]) == policy3
|
||||
|
||||
assert permissions.merge_policies([policy2, policy3]) == policy3
|
||||
Reference in new issue
Block a user