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

AttributeError: type object 'OutstandingToken' has no attribute 'objects' #875

Open
amirhakimnejad opened this issue Feb 27, 2025 · 3 comments

Comments

@amirhakimnejad
Copy link

I have updated my simplejwt to 5.5.0
My refresh token test refused to pass with the following info:

def test_refresh_token(self):
    from rest_framework_simplejwt.views import TokenRefreshView
    client = APIRequestFactory()
    access_token, refresh_token = self.test_new_user_creation()
    response = client.post('/api/users/refreshtoken/', {'refresh': refresh_token})
    response = TokenRefreshView.as_view()(response) # Raises exception
    self.assertEqual(response.status_code, 200)
    self.assertIsNotNone(response.data.get('access'))
    self.assertIsNotNone(response.data.get('refresh'))

Error:

Traceback (most recent call last):
File "/Project/common/tests.py", line 259, in test_refresh_token
response = TokenRefreshView.as_view()(response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Project/.venv/lib/python3.11/site-packages/django/views/decorators/csrf.py", line 65, in _view_wrapper
return view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Project/.venv/lib/python3.11/site-packages/django/views/generic/base.py", line 104, in view
return self.dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Project/.venv/lib/python3.11/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Project/.venv/lib/python3.11/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/Project/.venv/lib/python3.11/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/Project/.venv/lib/python3.11/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Project/.venv/lib/python3.11/site-packages/rest_framework_simplejwt/views.py", line 44, in post
serializer.is_valid(raise_exception=True)
File "/Project/.venv/lib/python3.11/site-packages/rest_framework/serializers.py", line 223, in is_valid
self._validated_data = self.run_validation(self.initial_data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Project/.venv/lib/python3.11/site-packages/rest_framework/serializers.py", line 445, in run_validation
value = self.validate(value)
^^^^^^^^^^^^^^^^^^^^
File "/Project/.venv/lib/python3.11/site-packages/rest_framework_simplejwt/serializers.py", line 141, in validate
refresh.outstand()
File "/Project/.venv/lib/python3.11/site-packages/rest_framework_simplejwt/tokens.py", line 222, in outstand
return OutstandingToken.objects.get_or_create(
    ^^^^^^^^^^^^^^^^^^^^^^^^
    AttributeError: type object 'OutstandingToken' has no attribute 'objects'
@fabianfalon
Copy link
Contributor

fabianfalon commented Feb 27, 2025

@amirhakimnejad I had the same problem, I solved it by adding in the test settings_test.py in the installed apps section

"rest_framework_simplejwt.token_blacklist",

THIRD_PARTY_APPS = [
    "rest_framework",
    "rest_framework_simplejwt",
    "rest_framework_simplejwt.token_blacklist",
]

@adelkhayata76
Copy link

As per the documentation for the BLACKLIST_AFTER_ROTATION setting

When set to True, causes refresh tokens submitted to the TokenRefreshView to be added to the blacklist if the blacklist app is in use and the ROTATE_REFRESH_TOKENS setting is set to True. You need to add 'rest_framework_simplejwt.token_blacklist', to your INSTALLED_APPS in the settings file to use this setting.

I have BLACKLIST_AFTER_ROTATION set to False and I didn't include "rest_framework_simplejwt.token_blacklist" in my INSTALLED_APPS, but I am still getting this error.

I checked serializers.py file and I believe that this commit caused it because the code is calling refresh.outstand() regardless of the BLACKLIST_AFTER_ROTATION value.

        if api_settings.ROTATE_REFRESH_TOKENS:
            if api_settings.BLACKLIST_AFTER_ROTATION:
                try:
                    # Attempt to blacklist the given refresh token
                    refresh.blacklist()
                except AttributeError:
                    # If blacklist app not installed, `blacklist` method will
                    # not be present
                    pass

            refresh.set_jti()
            refresh.set_exp()
            refresh.set_iat()
            refresh.outstand()

            data["refresh"] = str(refresh)

@jardila
Copy link

jardila commented Feb 27, 2025

My solution today was downgrading the version to 5.4.0.
That solved my problem because I'm not implementing blacklist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants