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 non standard implementation of __getattr__ #675

Open
wants to merge 2 commits 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
4 changes: 3 additions & 1 deletion rest_framework_simplejwt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,6 @@ def get_username(self):

def __getattr__(self, attr):
"""This acts as a backup attribute getter for custom claims defined in Token serializers."""
return self.token.get(attr, None)
if attr in self.token:
return self.token[attr]
raise AttributeError(f"'TokenUser' does not have an attribute {attr}")
11 changes: 11 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from django.test import TestCase

from rest_framework_simplejwt.models import TokenUser
Expand Down Expand Up @@ -105,3 +106,13 @@ def test_is_authenticated(self):

def test_get_username(self):
self.assertEqual(self.user.get_username(), "deep-thought")

def test_getattr_returns_token_data(self):
self.assertEqual(self.user.__getattr__("some_other_stuff"), "arstarst")

def test_getattr_raises_attribute_error(self):
with pytest.raises(AttributeError):
self.user.__getattr__("_not_an_attribute")

def test_hasattr_for_missing_attribute(self):
self.assertFalse(hasattr(self.user, "_not_an_attribute"))