[FIX] Fix cached groups (#647)

This commit is contained in:
Julien Constant 2023-05-12 13:27:51 +02:00 committed by GitHub
parent 8852ef990e
commit 84768eb74e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View File

@ -180,14 +180,15 @@ def get_group(*, pk: int = None, name: str = None) -> Optional[Group]:
:param pk: The primary key of the group :param pk: The primary key of the group
:param name: The name of the group :param name: The name of the group
:return: The group if it exists, else None :return: The group if it exists, else None
:raises ValueError: If no group matches the criteria :raise ValueError: If no group matches the criteria
""" """
if pk is None and name is None: if pk is None and name is None:
raise ValueError("Either pk or name must be set") raise ValueError("Either pk or name must be set")
if name is not None:
name = name.replace(" ", "_") # avoid errors with memcached backend # replace space characters to hide warnings with memcached backend
pk_or_name: Union[str, int] = pk if pk is not None else name pk_or_name: Union[str, int] = pk if pk is not None else name.replace(" ", "_")
group = cache.get(f"sith_group_{pk_or_name}") group = cache.get(f"sith_group_{pk_or_name}")
if group == "not_found": if group == "not_found":
# Using None as a cache value is a little bit tricky, # Using None as a cache value is a little bit tricky,
# so we use a special string to represent None # so we use a special string to represent None

View File

@ -597,12 +597,20 @@ class UserIsInGroupTest(TestCase):
Test that when a user is removed from a group, Test that when a user is removed from a group,
the is_in_group_method return False when calling it again the is_in_group_method return False when calling it again
""" """
# testing with pk
self.toto.groups.add(self.com_admin.pk) self.toto.groups.add(self.com_admin.pk)
self.assertTrue(self.toto.is_in_group(pk=self.com_admin.pk)) self.assertTrue(self.toto.is_in_group(pk=self.com_admin.pk))
self.toto.groups.remove(self.com_admin.pk) self.toto.groups.remove(self.com_admin.pk)
self.assertFalse(self.toto.is_in_group(pk=self.com_admin.pk)) self.assertFalse(self.toto.is_in_group(pk=self.com_admin.pk))
# testing with name
self.toto.groups.add(self.sas_admin.pk)
self.assertTrue(self.toto.is_in_group(name="SAS admin"))
self.toto.groups.remove(self.sas_admin.pk)
self.assertFalse(self.toto.is_in_group(name="SAS admin"))
def test_not_existing_group(self): def test_not_existing_group(self):
""" """
Test that searching for a not existing group Test that searching for a not existing group