Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Fix cached groups #647

Merged
merged 4 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,15 @@ def get_group(*, pk: int = None, name: str = None) -> Optional[Group]:
:param pk: The primary key of the group
:param name: The name of the group
: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:
raise ValueError("Either pk or name must be set")
if name is not None:
name = name.replace(" ", "_") # avoid errors with memcached backend
pk_or_name: Union[str, int] = pk if pk is not None else name

# replace space characters to hide warnings with memcached backend
pk_or_name: Union[str, int] = pk if pk is not None else name.replace(" ", "_")
Juknum marked this conversation as resolved.
Show resolved Hide resolved
group = cache.get(f"sith_group_{pk_or_name}")

if group == "not_found":
# Using None as a cache value is a little bit tricky,
# so we use a special string to represent None
Expand Down
8 changes: 8 additions & 0 deletions core/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,12 +597,20 @@ def test_cache_properly_cleared_group(self):
Test that when a user is removed from a group,
the is_in_group_method return False when calling it again
"""
# testing with pk
self.toto.groups.add(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.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):
"""
Test that searching for a not existing group
Expand Down