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

Make is_active_for_user consider when everyone is False #498

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion waffle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ def is_active_for_user(self, user: AbstractBaseUser) -> bool | None:
if self.everyone:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe want to make this if self.everyone is True?

return True

if self.everyone is False:
chess-octane marked this conversation as resolved.
Show resolved Hide resolved
return False

if self.authenticated and user.is_authenticated:
return True

Expand Down Expand Up @@ -396,7 +399,8 @@ def _get_group_ids(self) -> set[Any]:

def is_active_for_user(self, user: AbstractBaseUser) -> bool | None:
is_active = super().is_active_for_user(user)
if is_active:

if is_active is True or is_active is False:
chess-octane marked this conversation as resolved.
Show resolved Hide resolved
return is_active

user_ids = self._get_user_ids()
Expand Down
12 changes: 9 additions & 3 deletions waffle/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from django.contrib.auth import get_user_model
from django.test import TestCase

from waffle import (
get_waffle_flag_model,
get_waffle_sample_model,
get_waffle_switch_model,
)
from django.contrib.auth.models import User


class ModelsTests(TestCase):
def test_natural_keys(self):
Expand All @@ -33,6 +32,13 @@ def test_flag_is_not_active_for_none_requests(self):
self.assertEqual(flag.is_active(None), False)

def test_is_active_for_user_when_everyone_is_active(self):
user = get_user_model()(username='john.doe')
flag = get_waffle_flag_model().objects.create(name='test-flag')
flag.everyone = True
self.assertEqual(flag.is_active_for_user(User()), True)
self.assertEqual(flag.is_active_for_user(user), True)

def test_is_active_for_user_when_everyone_is_disabled(self):
user = get_user_model()(username='john.doe')
flag = get_waffle_flag_model().objects.create(name='test-flag')
flag.everyone = False
self.assertEqual(flag.is_active_for_user(user), False)
Loading