jazzband / django-waffle

A feature flipper for Django
https://waffle.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.12k stars 258 forks source link

flag status on waffle_status endpoint is not properly shown #496

Open SoundWaveX81 opened 8 months ago

SoundWaveX81 commented 8 months ago

I have created this flag:

waffle admin config

I'm trying to get the flag status using the waffle_status endpoint, but no matter if the user is logged or not I get the same response:

{
    "flags": {
        "test_flag": {
            "is_active": false,
            "last_modified": "2023-12-21T16:03:38.922Z"
        }
    },
    "switches": {},
    "samples": {}
}

It is supposed that I have to get this flag as True for the user farid, the one with id 1, isn't?

I made this exercise using django 4, django-waffle 4.1.0, and I have made this configs on settings.py

.
.
.
INSTALLED_APPS = [
    'django.contrib.admin',
    'rest_framework',
    'rest_framework.authtoken',
    'django.contrib.auth',
    'waffle',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'exammple'
]
.
.
.

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'waffle.middleware.WaffleMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
.
.
.
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ]
}
.
.
.

and in project's url.py file

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/v1/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/v1/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
    path(
        'api/v1/', include(
            [
                path('', include(example_urls))
            ]
        )
    ),
    path('waffle/', include('waffle.urls')),
]

Is this a bug/missing feature on waffle? or I'm missing some config in somewhere?

P.S. The purpose of this is that I have to give this flag status to a FrontEnd app to show or hide some visual components depending on the flag's status.

I've tried mixing configs on django admin, and settings.py, and the expected result is that the waffle_status endpoint returns True if the user who made the request is in the added ones on the flag config, otherwise has to return False

UPDATE1: I did clone the repo of the django-waffle project and I wrote this test:

 def test_waffle_json_user_flag(self):
        user = get_user_model().objects.create(username='someone_else')
        flag = waffle.get_waffle_flag_model().objects.create(name='myflag')
        flag.users.add(user)
        self.client.force_login(user=user)
        response = self.client.get(reverse('waffle_status'))
        content = response.json()
        self.assertTrue(content['flags']['myflag']['is_active'])

        another_user = get_user_model().objects.create(username='another_one')
        self.client.force_login(user=another_user)
        another_response = self.client.get(reverse('waffle_status'))
        another_content = another_response.json()
        self.assertFalse(another_content['flags']['myflag']['is_active'])

It passed successfully!

Then I ran the django server on the project, I did create a flag and bind a user to it. I did tests using postman and I get the same issue as described above.

Doing some debug on the waffle_status view on waffle-django package, I saw that the request returned has no user, I meant if I dig into request.user i saw AnonymousUser so, what I'm missing here?