System groups (#18303)
* Add read only and admin policies * Migrate to 2 system groups * Add system groups * Add system groups admin & read only * Dont' mutate parameters * Fix types
This commit is contained in:
1 parent
54b0cde52a
commit
f7f0a4e811
14 files changed
+309
-68
No files matched your search
@@ -32,15 +32,3 @@ def test_owner_permissions():
|
||||
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,25 @@
|
||||
"""Test system policies."""
|
||||
from homeassistant.auth.permissions import (
|
||||
PolicyPermissions, system_policies, POLICY_SCHEMA)
|
||||
|
||||
|
||||
def test_admin_policy():
|
||||
"""Test admin policy works."""
|
||||
# Make sure it's valid
|
||||
POLICY_SCHEMA(system_policies.ADMIN_POLICY)
|
||||
|
||||
perms = PolicyPermissions(system_policies.ADMIN_POLICY)
|
||||
assert perms.check_entity('light.kitchen', 'read')
|
||||
assert perms.check_entity('light.kitchen', 'control')
|
||||
assert perms.check_entity('light.kitchen', 'edit')
|
||||
|
||||
|
||||
def test_read_only_policy():
|
||||
"""Test read only policy works."""
|
||||
# Make sure it's valid
|
||||
POLICY_SCHEMA(system_policies.READ_ONLY_POLICY)
|
||||
|
||||
perms = PolicyPermissions(system_policies.READ_ONLY_POLICY)
|
||||
assert perms.check_entity('light.kitchen', 'read')
|
||||
assert not perms.check_entity('light.kitchen', 'control')
|
||||
assert not perms.check_entity('light.kitchen', 'edit')
|
||||
@@ -2,8 +2,8 @@
|
||||
from homeassistant.auth import auth_store
|
||||
|
||||
|
||||
async def test_loading_old_data_format(hass, hass_storage):
|
||||
"""Test we correctly load an old data format."""
|
||||
async def test_loading_no_group_data_format(hass, hass_storage):
|
||||
"""Test we correctly load old data without any groups."""
|
||||
hass_storage[auth_store.STORAGE_KEY] = {
|
||||
'version': 1,
|
||||
'data': {
|
||||
@@ -60,9 +60,15 @@ async def test_loading_old_data_format(hass, hass_storage):
|
||||
|
||||
store = auth_store.AuthStore(hass)
|
||||
groups = await store.async_get_groups()
|
||||
assert len(groups) == 1
|
||||
group = groups[0]
|
||||
assert group.name == "All Access"
|
||||
assert len(groups) == 2
|
||||
admin_group = groups[0]
|
||||
assert admin_group.name == auth_store.GROUP_NAME_ADMIN
|
||||
assert admin_group.system_generated
|
||||
assert admin_group.id == auth_store.GROUP_ID_ADMIN
|
||||
read_group = groups[1]
|
||||
assert read_group.name == auth_store.GROUP_NAME_READ_ONLY
|
||||
assert read_group.system_generated
|
||||
assert read_group.id == auth_store.GROUP_ID_READ_ONLY
|
||||
|
||||
users = await store.async_get_users()
|
||||
assert len(users) == 2
|
||||
@@ -70,7 +76,7 @@ async def test_loading_old_data_format(hass, hass_storage):
|
||||
owner, system = users
|
||||
|
||||
assert owner.system_generated is False
|
||||
assert owner.groups == [group]
|
||||
assert owner.groups == [admin_group]
|
||||
assert len(owner.refresh_tokens) == 1
|
||||
owner_token = list(owner.refresh_tokens.values())[0]
|
||||
assert owner_token.id == 'user-token-id'
|
||||
@@ -80,3 +86,126 @@ async def test_loading_old_data_format(hass, hass_storage):
|
||||
assert len(system.refresh_tokens) == 1
|
||||
system_token = list(system.refresh_tokens.values())[0]
|
||||
assert system_token.id == 'system-token-id'
|
||||
|
||||
|
||||
async def test_loading_all_access_group_data_format(hass, hass_storage):
|
||||
"""Test we correctly load old data with single group."""
|
||||
hass_storage[auth_store.STORAGE_KEY] = {
|
||||
'version': 1,
|
||||
'data': {
|
||||
'credentials': [],
|
||||
'users': [
|
||||
{
|
||||
"id": "user-id",
|
||||
"is_active": True,
|
||||
"is_owner": True,
|
||||
"name": "Paulus",
|
||||
"system_generated": False,
|
||||
'group_ids': ['abcd-all-access']
|
||||
},
|
||||
{
|
||||
"id": "system-id",
|
||||
"is_active": True,
|
||||
"is_owner": True,
|
||||
"name": "Hass.io",
|
||||
"system_generated": True,
|
||||
}
|
||||
],
|
||||
"groups": [
|
||||
{
|
||||
"id": "abcd-all-access",
|
||||
"name": "All Access",
|
||||
}
|
||||
],
|
||||
"refresh_tokens": [
|
||||
{
|
||||
"access_token_expiration": 1800.0,
|
||||
"client_id": "http://localhost:8123/",
|
||||
"created_at": "2018-10-03T13:43:19.774637+00:00",
|
||||
"id": "user-token-id",
|
||||
"jwt_key": "some-key",
|
||||
"last_used_at": "2018-10-03T13:43:19.774712+00:00",
|
||||
"token": "some-token",
|
||||
"user_id": "user-id"
|
||||
},
|
||||
{
|
||||
"access_token_expiration": 1800.0,
|
||||
"client_id": None,
|
||||
"created_at": "2018-10-03T13:43:19.774637+00:00",
|
||||
"id": "system-token-id",
|
||||
"jwt_key": "some-key",
|
||||
"last_used_at": "2018-10-03T13:43:19.774712+00:00",
|
||||
"token": "some-token",
|
||||
"user_id": "system-id"
|
||||
},
|
||||
{
|
||||
"access_token_expiration": 1800.0,
|
||||
"client_id": "http://localhost:8123/",
|
||||
"created_at": "2018-10-03T13:43:19.774637+00:00",
|
||||
"id": "hidden-because-no-jwt-id",
|
||||
"last_used_at": "2018-10-03T13:43:19.774712+00:00",
|
||||
"token": "some-token",
|
||||
"user_id": "user-id"
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
store = auth_store.AuthStore(hass)
|
||||
groups = await store.async_get_groups()
|
||||
assert len(groups) == 2
|
||||
admin_group = groups[0]
|
||||
assert admin_group.name == auth_store.GROUP_NAME_ADMIN
|
||||
assert admin_group.system_generated
|
||||
assert admin_group.id == auth_store.GROUP_ID_ADMIN
|
||||
read_group = groups[1]
|
||||
assert read_group.name == auth_store.GROUP_NAME_READ_ONLY
|
||||
assert read_group.system_generated
|
||||
assert read_group.id == auth_store.GROUP_ID_READ_ONLY
|
||||
|
||||
users = await store.async_get_users()
|
||||
assert len(users) == 2
|
||||
|
||||
owner, system = users
|
||||
|
||||
assert owner.system_generated is False
|
||||
assert owner.groups == [admin_group]
|
||||
assert len(owner.refresh_tokens) == 1
|
||||
owner_token = list(owner.refresh_tokens.values())[0]
|
||||
assert owner_token.id == 'user-token-id'
|
||||
|
||||
assert system.system_generated is True
|
||||
assert system.groups == []
|
||||
assert len(system.refresh_tokens) == 1
|
||||
system_token = list(system.refresh_tokens.values())[0]
|
||||
assert system_token.id == 'system-token-id'
|
||||
|
||||
|
||||
async def test_loading_empty_data(hass, hass_storage):
|
||||
"""Test we correctly load with no existing data."""
|
||||
store = auth_store.AuthStore(hass)
|
||||
groups = await store.async_get_groups()
|
||||
assert len(groups) == 2
|
||||
admin_group = groups[0]
|
||||
assert admin_group.name == auth_store.GROUP_NAME_ADMIN
|
||||
assert admin_group.system_generated
|
||||
assert admin_group.id == auth_store.GROUP_ID_ADMIN
|
||||
read_group = groups[1]
|
||||
assert read_group.name == auth_store.GROUP_NAME_READ_ONLY
|
||||
assert read_group.system_generated
|
||||
assert read_group.id == auth_store.GROUP_ID_READ_ONLY
|
||||
|
||||
users = await store.async_get_users()
|
||||
assert len(users) == 0
|
||||
|
||||
|
||||
async def test_system_groups_only_store_id(hass, hass_storage):
|
||||
"""Test that for system groups we only store the ID."""
|
||||
store = auth_store.AuthStore(hass)
|
||||
await store._async_load()
|
||||
data = store._data_to_save()
|
||||
assert len(data['users']) == 0
|
||||
assert data['groups'] == [
|
||||
{'id': auth_store.GROUP_ID_ADMIN},
|
||||
{'id': auth_store.GROUP_ID_READ_ONLY},
|
||||
]
|
||||
Reference in new issue
Block a user