2018-10-11 17:24:25 +00:00
|
|
|
"""Tests for the auth models."""
|
2024-03-08 15:36:11 +00:00
|
|
|
|
2018-10-11 17:24:25 +00:00
|
|
|
from homeassistant.auth import models, permissions
|
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_owner_fetching_owner_permissions() -> None:
|
2018-10-11 17:24:25 +00:00
|
|
|
"""Test we fetch the owner permissions for an owner user."""
|
|
|
|
group = models.Group(name="Test Group", policy={})
|
2018-12-05 10:41:00 +00:00
|
|
|
owner = models.User(
|
2019-07-31 19:25:30 +00:00
|
|
|
name="Test User", perm_lookup=None, groups=[group], is_owner=True
|
2018-12-05 10:41:00 +00:00
|
|
|
)
|
2018-10-11 17:24:25 +00:00
|
|
|
assert owner.permissions is permissions.OwnerPermissions
|
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_permissions_merged() -> None:
|
2018-10-11 17:24:25 +00:00
|
|
|
"""Test we merge the groups permissions."""
|
2019-07-31 19:25:30 +00:00
|
|
|
group = models.Group(
|
|
|
|
name="Test Group", policy={"entities": {"domains": {"switch": True}}}
|
2018-12-05 10:41:00 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
group2 = models.Group(
|
|
|
|
name="Test Group", policy={"entities": {"entity_ids": {"light.kitchen": True}}}
|
|
|
|
)
|
|
|
|
user = models.User(name="Test User", perm_lookup=None, groups=[group, group2])
|
2018-10-11 17:24:25 +00:00
|
|
|
# Make sure we cache instance
|
|
|
|
assert user.permissions is user.permissions
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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
|
2024-01-13 20:10:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_cache_cleared_on_group_change() -> None:
|
|
|
|
"""Test we clear the cache when a group changes."""
|
|
|
|
group = models.Group(
|
|
|
|
name="Test Group", policy={"entities": {"domains": {"switch": True}}}
|
|
|
|
)
|
|
|
|
admin_group = models.Group(
|
|
|
|
name="Admin group", id=models.GROUP_ID_ADMIN, policy={"entities": {}}
|
|
|
|
)
|
|
|
|
user = models.User(
|
|
|
|
name="Test User", perm_lookup=None, groups=[group], is_active=True
|
|
|
|
)
|
|
|
|
# Make sure we cache instance
|
|
|
|
assert user.permissions is user.permissions
|
|
|
|
|
|
|
|
# Make sure we cache is_admin
|
|
|
|
assert user.is_admin is user.is_admin
|
|
|
|
assert user.is_active is True
|
|
|
|
|
|
|
|
user.groups = []
|
|
|
|
assert user.groups == []
|
|
|
|
assert user.is_admin is False
|
|
|
|
|
|
|
|
user.is_owner = True
|
|
|
|
assert user.is_admin is True
|
|
|
|
user.is_owner = False
|
|
|
|
|
|
|
|
assert user.is_admin is False
|
|
|
|
user.groups = [admin_group]
|
|
|
|
assert user.is_admin is True
|
|
|
|
|
|
|
|
user.is_active = False
|
|
|
|
assert user.is_admin is False
|