Skip to content

Commit

Permalink
fix: make is_active_for_user consider when everyone is False
Browse files Browse the repository at this point in the history
fixes #401
  • Loading branch information
chess-octane committed Feb 20, 2024
1 parent 63444d6 commit df88581
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
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:
return True

if self.everyone is False:
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:
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)

0 comments on commit df88581

Please sign in to comment.