Refactor User attribute caching to be safer and more efficient (#96723)
* Cache construction of is_admin This has to be checked for a lot of api calls and the websocket every time the call is made * Cache construction of is_admin This has to be checked for a lot of api calls and the websocket every time the call is made * Cache construction of is_admin This has to be checked for a lot of api calls and the websocket every time the call is made * modernize * coverage * coverage * verify caching * verify caching * fix type * fix mocking
This commit is contained in:
parent
d7910841ef
commit
b1d0c6a4f1
5 changed files with 72 additions and 30 deletions
|
@ -26,3 +26,37 @@ def test_permissions_merged() -> None:
|
|||
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
|
||||
|
||||
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue